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] fix: use getRowId in filtering #9564

Merged
merged 6 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -390,6 +390,7 @@ export const useGridFilter = (

const dataRowIdToIdLookup = apiRef.current.state.rows.dataRowIdToModelLookup;
const rows = React.useMemo(() => Object.values(dataRowIdToIdLookup), [dataRowIdToIdLookup]);
const { getRowId } = props;

const flatFilteringMethod = React.useCallback<GridStrategyProcessor<'filtering'>>(
(params) => {
Expand All @@ -412,6 +413,7 @@ export const useGridFilter = (

for (let i = 0; i < rows.length; i += 1) {
const row = rows[i];
const id = getRowId ? getRowId(row) : row.id;

isRowMatchingFilters(row, undefined, result);

Expand All @@ -423,7 +425,7 @@ export const useGridFilter = (
filterCache,
);

filteredRowsLookup[row.id] = isRowPassing;
filteredRowsLookup[id] = isRowPassing;
}

const footerId = 'auto-generated-group-footer-root';
Expand All @@ -437,7 +439,7 @@ export const useGridFilter = (
filteredDescendantCountLookup: {},
};
},
[apiRef, props.filterMode, rows],
[apiRef, rows, props.filterMode, getRowId],
);

useGridRegisterPipeProcessor(apiRef, 'columnMenu', addColumnMenuItem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ export function useGridParamsApi(

const getRowValue = React.useCallback<GridParamsApi['getRowValue']>(
(row, colDef) => {
const id = (getRowId ? getRowId(row) : row.id) ?? row[GRID_ID_AUTOGENERATED];
const id =
GRID_ID_AUTOGENERATED in row ? row[GRID_ID_AUTOGENERATED] : getRowId?.(row) ?? row.id;
const field = colDef.field;

if (!colDef || !colDef.valueGetter) {
Expand Down
27 changes: 27 additions & 0 deletions packages/grid/x-data-grid/src/tests/filtering.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,33 @@ describe('<DataGrid /> - Filter', () => {
});
});

describe('prop: getRowId', () => {
it('works with filter', () => {
render(
<TestCase
getRowId={(row) => row.brand}
filterModel={{
items: [{ id: 0, field: 'brand', operator: 'contains', value: 'Nike' }],
}}
/>,
);
expect(getColumnValues(0)).to.deep.equal(['Nike']);
});

it('works with quick filter', () => {
render(
<TestCase
getRowId={(row) => row.brand}
filterModel={{
items: [],
quickFilterValues: ['Nike'],
}}
/>,
);
expect(getColumnValues(0)).to.deep.equal(['Nike']);
});
});

describe('column type: string', () => {
const getRows = (item: Omit<GridFilterItem, 'field'>) => {
const { unmount } = render(
Expand Down