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] Only update input with value prop if debounce is off #5646

Merged
merged 5 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,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 @@ -81,10 +81,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 @@ -95,7 +96,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 @@ -164,8 +165,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 @@ -233,6 +233,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 @@ -300,10 +300,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 @@ -125,6 +125,7 @@ GridEditBooleanCell.propTypes = {
* The mode of the cell.
*/
cellMode: PropTypes.oneOf(['edit', 'view']).isRequired,
changeReason: PropTypes.oneOf(['debouncedSetEditCellValue', 'setEditCellValue']),
/**
* The column of the row that the current cell belongs to.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ GridEditDateCell.propTypes = {
* The mode of the cell.
*/
cellMode: PropTypes.oneOf(['edit', 'view']).isRequired,
changeReason: PropTypes.oneOf(['debouncedSetEditCellValue', 'setEditCellValue']),
/**
* The column of the row that the current cell belongs to.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,31 @@ const GridEditInputCell = React.forwardRef<HTMLInputElement, GridEditInputCellPr
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));
Copy link
Member Author

Choose a reason for hiding this comment

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

Since we don't sync the value between the global state and the local state, when the call is debounced, we need to manually call the value parser and skip the one in apiRef.current.setEditCellValue.

}

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 Expand Up @@ -138,6 +154,7 @@ GridEditInputCell.propTypes = {
* The mode of the cell.
*/
cellMode: PropTypes.oneOf(['edit', 'view']),
changeReason: PropTypes.oneOf(['debouncedSetEditCellValue', 'setEditCellValue']),
/**
* The column of the row that the current cell belongs to.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ GridEditSingleSelectCell.propTypes = {
* The mode of the cell.
*/
cellMode: PropTypes.oneOf(['edit', 'view']).isRequired,
changeReason: PropTypes.oneOf(['debouncedSetEditCellValue', 'setEditCellValue']),
/**
* The column of the row that the current cell belongs to.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,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 @@ -438,12 +438,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 @@ -519,20 +519,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