Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataGrid] Fix calling onCellEditStop on error #12747

Merged
merged 6 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,33 @@ describe('<DataGridPro /> - Cell editing', () => {
});
});

it('should not call onCellEditStop if preProcessEditCellProps returns error', async () => {
columnProps.preProcessEditCellProps = spy(({ props }: GridPreProcessEditCellProps) => ({
...props,
error: true,
}));

const handleEditCellStop = spy();

render(<TestCase onCellEditStop={handleEditCellStop} />);
act(() => apiRef.current.startCellEditMode({ id: 0, field: 'currencyPair' }));
await act(() =>
apiRef.current.setEditCellValue({
id: 0,
field: 'currencyPair',
value: 'USD GBP',
}),
);
const cell = getCell(0, 1);
cell.focus();

await act(() => {
fireEvent.keyDown(cell, { key: 'Enter' });
});

expect(handleEditCellStop.callCount).to.equal(0);
});

it('should pass to renderEditCell the props returned by preProcessEditCellProps', async () => {
columnProps.preProcessEditCellProps = ({ props }: GridPreProcessEditCellProps) => ({
...props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,42 @@ export const useGridCellEditing = (
[apiRef],
);

const runIfNoPreProcessError =
<Args extends any[]>(callback?: (...args: Args) => void) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: better type safety

Suggested change
<Args extends any[]>(callback?: (...args: Args) => void) =>
<Args extends Parameters<GridEventListener<'cellEditStop'>>>(callback?: (...args: Args) => void) =>

async (...args: Args) => {
if (callback) {
const { field, id, value, debounceMs, unstable_skipValueParser: skipValueParser } = args[0];
const column = apiRef.current.getColumn(field);
const row = apiRef.current.getRow(id)!;

let parsedValue = value;
if (column.valueParser && !skipValueParser) {
parsedValue = column.valueParser(value, row, column, apiRef);
}

const editingState = gridEditRowsStateSelector(apiRef.current.state);
let newProps: GridEditCellProps = {
...editingState[id][field],
value: parsedValue,
changeReason: debounceMs ? 'debouncedSetEditCellValue' : 'setEditCellValue',
};

if (column.preProcessEditCellProps) {
const hasChanged = value !== editingState[id][field].value;

newProps = { ...newProps, isProcessingProps: true };
Copy link
Contributor Author

@sai6855 sai6855 Apr 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire logic was copied from

const { id, field, value, debounceMs, unstable_skipValueParser: skipValueParser } = params;
throwIfNotEditable(id, field);
throwIfNotInMode(id, field, GridCellModes.Edit);
const column = apiRef.current.getColumn(field);
const row = apiRef.current.getRow(id)!;
let parsedValue = value;
if (column.valueParser && !skipValueParser) {
parsedValue = column.valueParser(value, row, column, apiRef);
}
let editingState = gridEditRowsStateSelector(apiRef.current.state);
let newProps: GridEditCellProps = {
...editingState[id][field],
value: parsedValue,
changeReason: debounceMs ? 'debouncedSetEditCellValue' : 'setEditCellValue',
};
if (column.preProcessEditCellProps) {
const hasChanged = value !== editingState[id][field].value;
newProps = { ...newProps, isProcessingProps: true };
updateOrDeleteFieldState(id, field, newProps);
newProps = await Promise.resolve(
column.preProcessEditCellProps({ id, row, props: newProps, hasChanged }),
);
}

Thought of storing { id, row, props: newProps, hasChanged } in ref, which are computed in below function, but wasn't sure if setCellEditingEditCellValue runs always and runs before runIfNoPreProcessError

const setCellEditingEditCellValue = React.useCallback<

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
Did you consider following the same approach as in #11383

Copy link
Contributor Author

@sai6855 sai6855 Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't aware of that PR. Now I've updated code to use similar approach as in #11383.


newProps = await Promise.resolve(
column.preProcessEditCellProps({ id, row, props: newProps, hasChanged }),
);
}

if (!newProps.error) {
callback(...args);
}
}
};

useGridApiEventHandler(apiRef, 'cellDoubleClick', runIfEditModeIsCell(handleCellDoubleClick));
useGridApiEventHandler(apiRef, 'cellFocusOut', runIfEditModeIsCell(handleCellFocusOut));
useGridApiEventHandler(apiRef, 'cellKeyDown', runIfEditModeIsCell(handleCellKeyDown));
Expand All @@ -242,7 +278,7 @@ export const useGridCellEditing = (
useGridApiEventHandler(apiRef, 'cellEditStop', runIfEditModeIsCell(handleCellEditStop));

useGridApiOptionHandler(apiRef, 'cellEditStart', props.onCellEditStart);
useGridApiOptionHandler(apiRef, 'cellEditStop', props.onCellEditStop);
useGridApiOptionHandler(apiRef, 'cellEditStop', runIfNoPreProcessError(props.onCellEditStop));

const getCellMode = React.useCallback<GridCellEditingApi['getCellMode']>(
(id, field) => {
Expand Down