Skip to content

Commit

Permalink
fix logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sai6855 committed Apr 15, 2024
1 parent 928a846 commit 1f6d3ae
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe('<DataGridPro /> - Cell editing', () => {
});
});

it('should not call onCellEditStop if preProcessEditCellProps returns error', async () => {
it('should not publish onCellEditStop if field has error', async () => {
columnProps.preProcessEditCellProps = spy(({ props }: GridPreProcessEditCellProps) => ({
...props,
error: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,37 +234,14 @@ export const useGridCellEditing = (
[apiRef],
);

const runIfNoPreProcessError =
const runIfNoFieldErrors =
<Args extends any[]>(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 };

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

if (!newProps.error) {
const { id,field } = args[0];
const editRowsState = apiRef.current.state.editRows;
const hasFieldErrors = editRowsState[id][field]?.error;
if (!hasFieldErrors) {
callback(...args);
}
}
Expand All @@ -278,7 +255,7 @@ export const useGridCellEditing = (
useGridApiEventHandler(apiRef, 'cellEditStop', runIfEditModeIsCell(handleCellEditStop));

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

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

0 comments on commit 1f6d3ae

Please sign in to comment.