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] Throw if rows id is missing #349

Merged
merged 5 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 7 additions & 3 deletions packages/grid/_modules_/grid/hooks/root/useRows.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import {
createRow,
createRowModel,
GridOptions,
RowData,
RowId,
Expand Down Expand Up @@ -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, idx) => createRowModel(r, idx)), [rows]);
const [rowModelsState, setRowModelsState] = React.useState<RowModel[]>(rowModels);
const [, forceUpdate] = React.useState();
const [rafUpdate] = useRafUpdate(() => forceUpdate((p: any) => !p));
Expand Down Expand Up @@ -119,14 +119,18 @@ 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: Missing row Id in row data update.');
dtassone marked this conversation as resolved.
Show resolved Hide resolved
}

uniq[update.id] = uniq[update.id] != null ? { ...uniq[update.id], ...update } : update;
return uniq;
}, {} as { [id: string]: any });

const rowModelUpdates = Object.values<RowData>(uniqUpdates).map((partialRow) => {
const oldRow = getRowFromId(partialRow.id!);
if (!oldRow) {
return createRow(partialRow);
return createRowModel(partialRow, partialRow.id!);
}
return { ...oldRow, data: { ...oldRow.data, ...partialRow } };
});
Expand Down
4 changes: 2 additions & 2 deletions packages/grid/_modules_/grid/models/rows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ export interface RowModel {
* @param rowData Row as [[RowData]].
* @returns A row as [[RowModel]].
*/
export function createRow(rowData: RowData): RowModel {
export function createRowModel(rowData: RowData, index: RowId): RowModel {
const row: RowModel = {
id: rowData.id,
id: rowData.id == null ? index : rowData.id,
dtassone marked this conversation as resolved.
Show resolved Hide resolved
data: rowData,
selected: false,
};
Expand Down
12 changes: 12 additions & 0 deletions packages/storybook/src/stories/grid-sorting.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,18 @@ export const SortingWithFormatter = () => {
</div>
);
};
export const SortingNoId = () => {
const columns = [...getColumns()];
columns.shift();
columns[0] = { ...columns[0], sortDirection: 'asc' };

return (
<div className="grid-container">
<XGrid rows={getRows()} columns={columns} />
</div>
);
};

export const SortModelOptionsMultiple = () => {
const sortModel: SortModel = React.useMemo(
() => [
Expand Down