From c52cda7828ee2d0ee4bfb1089b7a354de9dd77e9 Mon Sep 17 00:00:00 2001 From: damien tassone Date: Fri, 25 Sep 2020 18:39:20 +0200 Subject: [PATCH] [DataGrid] Throw if rows id is missing (#349) * remove the need for a row id * fix prettier * Rename createRow to createRowModel * throw error when row has no id * fix error message and added test --- .../grid/_modules_/grid/hooks/root/useRows.ts | 16 +++++++++++++--- .../grid/models/__tests__/rows.tests.ts | 15 +++++++++++++++ packages/grid/_modules_/grid/models/rows.ts | 12 +++++++++++- .../src/stories/grid-sorting.stories.tsx | 1 + 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 packages/grid/_modules_/grid/models/__tests__/rows.tests.ts diff --git a/packages/grid/_modules_/grid/hooks/root/useRows.ts b/packages/grid/_modules_/grid/hooks/root/useRows.ts index 13cee8ceb8190..cd2eaeecbe94e 100644 --- a/packages/grid/_modules_/grid/hooks/root/useRows.ts +++ b/packages/grid/_modules_/grid/hooks/root/useRows.ts @@ -1,6 +1,6 @@ import * as React from 'react'; import { - createRow, + createRowModel, GridOptions, RowData, RowId, @@ -30,7 +30,7 @@ export const useRows = ( apiRef: ApiRef, ): RowModel[] => { const logger = useLogger('useRows'); - const rowModels = React.useMemo(() => rows.map((r) => createRow(r)), [rows]); + const rowModels = React.useMemo(() => rows.map((r) => createRowModel(r)), [rows]); const [rowModelsState, setRowModelsState] = React.useState(rowModels); const [, forceUpdate] = React.useState(); const [rafUpdate] = useRafUpdate(() => forceUpdate((p: any) => !p)); @@ -119,6 +119,16 @@ export const useRows = ( // we removes duplicate updates. A server can batch updates, and send several updates for the same row in one fn call. const uniqUpdates = updates.reduce((uniq, update) => { + if (update.id == null) { + throw new Error( + [ + 'Material-UI: The data grid component requires all rows to have a unique id property.', + 'A row was provided without when calling updateRowData():', + JSON.stringify(update), + ].join('\n'), + ); + } + uniq[update.id] = uniq[update.id] != null ? { ...uniq[update.id], ...update } : update; return uniq; }, {} as { [id: string]: any }); @@ -126,7 +136,7 @@ export const useRows = ( const rowModelUpdates = Object.values(uniqUpdates).map((partialRow) => { const oldRow = getRowFromId(partialRow.id!); if (!oldRow) { - return createRow(partialRow); + return createRowModel(partialRow); } return { ...oldRow, data: { ...oldRow.data, ...partialRow } }; }); diff --git a/packages/grid/_modules_/grid/models/__tests__/rows.tests.ts b/packages/grid/_modules_/grid/models/__tests__/rows.tests.ts new file mode 100644 index 0000000000000..c4ad5f78aea30 --- /dev/null +++ b/packages/grid/_modules_/grid/models/__tests__/rows.tests.ts @@ -0,0 +1,15 @@ +import { createRowModel, RowData } from '../rows'; + +describe('Row: createRowModel', () => { + it('should have an id', () => { + const row = { + name: 'damien', + }; + const expectedError = [ + 'Material-UI: The data grid component requires all rows to have a unique id property.', + 'A row was provided without in the rows prop:', + JSON.stringify(row), + ].join('\n'); + expect(() => createRowModel((row))).toThrow(expectedError); + }); +}); diff --git a/packages/grid/_modules_/grid/models/rows.ts b/packages/grid/_modules_/grid/models/rows.ts index 3bf16610891c0..854056476d398 100644 --- a/packages/grid/_modules_/grid/models/rows.ts +++ b/packages/grid/_modules_/grid/models/rows.ts @@ -44,7 +44,17 @@ export interface RowModel { * @param rowData Row as [[RowData]]. * @returns A row as [[RowModel]]. */ -export function createRow(rowData: RowData): RowModel { +export function createRowModel(rowData: RowData): RowModel { + if (rowData.id == null) { + throw new Error( + [ + 'Material-UI: The data grid component requires all rows to have a unique id property.', + 'A row was provided without in the rows prop:', + JSON.stringify(rowData), + ].join('\n'), + ); + } + const row: RowModel = { id: rowData.id, data: rowData, diff --git a/packages/storybook/src/stories/grid-sorting.stories.tsx b/packages/storybook/src/stories/grid-sorting.stories.tsx index 13208c4f6a879..8d9b4e489356d 100644 --- a/packages/storybook/src/stories/grid-sorting.stories.tsx +++ b/packages/storybook/src/stories/grid-sorting.stories.tsx @@ -248,6 +248,7 @@ export const SortingWithFormatter = () => { ); }; + export const SortModelOptionsMultiple = () => { const sortModel: SortModel = React.useMemo( () => [