Skip to content

Commit

Permalink
[DataGrid] Only update input with value prop if debounce is off
Browse files Browse the repository at this point in the history
  • Loading branch information
m4theushw committed Jul 28, 2022
1 parent e273835 commit a49fa27
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ describe('<DataGridPro /> - Cell Editing', () => {
value: 'USD GBP',
error: false,
isProcessingProps: true,
changeReason: 'setEditCellValue',
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ describe('<DataGridPro /> - Edit Components', () => {
field: 'brand',
value: 'Puma',
debounceMs: 200,
unstable_skipValueParser: true,
});
});

it('should the value prop to the input', () => {
it('should pass the value prop to the input', () => {
defaultData.columns[0].valueParser = (value) => (value as string).toUpperCase();
render(<TestCase />);

Expand All @@ -97,7 +98,7 @@ describe('<DataGridPro /> - Edit Components', () => {
expect(input.value).to.equal('Nike');

fireEvent.change(input, { target: { value: 'Puma' } });
expect(input.value).to.equal('Puma');
expect(input.value).to.equal('PUMA');

clock.tick(200);
expect(input.value).to.equal('PUMA');
Expand Down Expand Up @@ -166,8 +167,9 @@ describe('<DataGridPro /> - Edit Components', () => {
expect(spiedSetEditCellValue.lastCall.args[0]).to.deep.equal({
id: 0,
field: 'quantity',
value: '110',
value: 110,
debounceMs: 200,
unstable_skipValueParser: true,
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ describe('<DataGridPro /> - Row Editing', () => {
value: 'USD GBP',
error: false,
isProcessingProps: true,
changeReason: 'setEditCellValue',
});

const args2 = column2Props.preProcessEditCellProps.lastCall.args[0];
Expand Down
4 changes: 3 additions & 1 deletion packages/grid/x-data-grid/src/components/GridRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,12 @@ function GridRow(props: React.HTMLAttributes<HTMLDivElement> & GridRowProps) {
updatedRow = apiRef.current.unstable_getRowWithUpdatedValues(rowId, column.field);
}

const { changeReason, ...editCellStateRest } = editCellState;

const params: GridRenderEditCellParams = {
...cellParams,
row: updatedRow,
...editCellState,
...editCellStateRest,
api: apiRef.current,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function GridEditInputCell(props: GridEditInputCellProps) {

const apiRef = useGridApiContext();
const inputRef = React.useRef<HTMLInputElement>();
const [valueState, setValueState] = React.useState(value);
const [valueState, setValueState] = React.useState<any>(value);
const ownerState = { classes: rootProps.classes };
const classes = useUtilityClasses(ownerState);

Expand All @@ -91,15 +91,31 @@ function GridEditInputCell(props: GridEditInputCellProps) {
await onValueChange(event, newValue);
}

setValueState(newValue);
apiRef.current.setEditCellValue({ id, field, value: newValue, debounceMs }, event);
const column = apiRef.current.getColumn(field);

let parsedValue = newValue;
if (column.valueParser && rootProps.experimentalFeatures?.newEditingApi) {
parsedValue = column.valueParser(newValue, apiRef.current.getCellParams(id, field));
}

setValueState(parsedValue);
apiRef.current.setEditCellValue(
{ id, field, value: parsedValue, debounceMs, unstable_skipValueParser: true },
event,
);
},
[apiRef, debounceMs, field, id, onValueChange],
[apiRef, debounceMs, field, id, onValueChange, rootProps.experimentalFeatures?.newEditingApi],
);

const meta = apiRef.current.unstable_getEditCellMeta
? apiRef.current.unstable_getEditCellMeta(id, field)
: {};

React.useEffect(() => {
setValueState(value);
}, [value]);
if (meta.changeReason !== 'debouncedSetEditCellValue') {
setValueState(value);
}
}, [meta.changeReason, value]);

useEnhancedEffect(() => {
if (hasFocus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export const useGridCellEditing = (
GridNewCellEditingApi['unstable_setCellEditingEditCellValue']
>(
async (params) => {
const { id, field, value } = params;
const { id, field, value, debounceMs, unstable_skipValueParser: skipValueParser } = params;

throwIfNotEditable(id, field);
throwIfNotInMode(id, field, GridCellModes.Edit);
Expand All @@ -435,12 +435,16 @@ export const useGridCellEditing = (
const row = apiRef.current.getRow(id)!;

let parsedValue = value;
if (column.valueParser) {
if (column.valueParser && !skipValueParser) {
parsedValue = column.valueParser(value, apiRef.current.getCellParams(id, field));
}

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

if (column.preProcessEditCellProps) {
const hasChanged = value !== editingState[id][field].value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useGridCellEditing } from './useGridCellEditing.new';
import { GridCellModes, GridEditModes } from '../../../models/gridEditRowModel';
import { useGridRowEditing } from './useGridRowEditing.new';
import { GridStateInitializer } from '../../utils/useGridInitializeState';
import { gridEditRowsStateSelector } from './gridEditRowsSelector';

export const editingStateInitializer: GridStateInitializer = (state) => ({
...state,
Expand Down Expand Up @@ -156,11 +157,21 @@ export const useGridEditing = (
[apiRef, props.editMode],
);

const getEditCellMeta = React.useCallback<GridNewEditingSharedApi['unstable_getEditCellMeta']>(
(id, field) => {
const editingState = gridEditRowsStateSelector(apiRef.current.state);

return { changeReason: editingState[id][field].changeReason };
},
[apiRef],
);

const editingSharedApi: GridNewEditingSharedApi = {
isCellEditable,
setEditCellValue,
unstable_runPendingEditCellValueMutation: runPendingEditCellValueMutation,
unstable_getRowWithUpdatedValues: getRowWithUpdatedValues,
unstable_getEditCellMeta: getEditCellMeta,
};

useGridApiMethod(apiRef, editingSharedApi, 'EditingApi');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,20 +516,24 @@ export const useGridRowEditing = (
GridNewRowEditingApi['unstable_setRowEditingEditCellValue']
>(
(params) => {
const { id, field, value } = params;
const { id, field, value, debounceMs, unstable_skipValueParser: skipValueParser } = params;

throwIfNotEditable(id, field);

const column = apiRef.current.getColumn(field);
const row = apiRef.current.getRow(id)!;

let parsedValue = value;
if (column.valueParser) {
if (column.valueParser && !skipValueParser) {
parsedValue = column.valueParser(value, apiRef.current.getCellParams(id, field));
}

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

if (!column.preProcessEditCellProps) {
updateOrDeleteFieldState(id, field, newProps);
Expand Down
11 changes: 11 additions & 0 deletions packages/grid/x-data-grid/src/models/api/gridEditingApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export type GridRowModesModelProps =

export type GridRowModesModel = Record<GridRowId, GridRowModesModelProps>;

export interface GridEditCellMeta {
changeReason?: 'debouncedSetEditCellValue' | 'setEditCellValue';
}

export interface GridNewEditingSharedApi {
/**
* Controls if a cell is editable.
Expand Down Expand Up @@ -59,6 +63,13 @@ export interface GridNewEditingSharedApi {
* @ignore - do not document.
*/
unstable_getRowWithUpdatedValues: (id: GridRowId, field: string) => GridRowModel;
/**
* Gets the meta information for the edit cell.
* @param {GridRowId} id The row id being edited.
* @param {string} field The field being edited.
* @ignore - do not document.
*/
unstable_getEditCellMeta: (id: GridRowId, field: string) => GridEditCellMeta;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/grid/x-data-grid/src/models/gridEditRowModel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { GridEditCellMeta } from './api/gridEditingApi';

export interface GridEditCellProps<V = any> {
value?: V | undefined;
isValidating?: boolean;
isProcessingProps?: boolean;
changeReason?: GridEditCellMeta['changeReason'];
[prop: string]: any;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export interface GridEditCellValueParams {
* The debounce time in milliseconds.
*/
debounceMs?: number;
/**
* TBD
*/
unstable_skipValueParser?: boolean;
}

// TODO v6 - remove
Expand Down

0 comments on commit a49fa27

Please sign in to comment.