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 all commits
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 publish onCellEditStop if field has 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,21 @@ export const useGridCellEditing = (
[apiRef],
);

const runIfNoFieldErrors =
<Args extends Parameters<GridEventListener<'cellEditStop'>>>(
callback?: (...args: Args) => void,
) =>
async (...args: Args) => {
if (callback) {
const { id, field } = args[0];
const editRowsState = apiRef.current.state.editRows;
const hasFieldErrors = editRowsState[id][field]?.error;
if (!hasFieldErrors) {
callback(...args);
}
}
};

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

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

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