Skip to content

Commit

Permalink
[DataGridPro] Fix header filtering with boolean column type (#15528)
Browse files Browse the repository at this point in the history
  • Loading branch information
k-rajat19 authored and arminmeh committed Nov 27, 2024
1 parent 72d000d commit fe3104e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ const GridHeaderFilterCell = React.forwardRef<HTMLDivElement, GridHeaderFilterCe

const applyFilterChanges = React.useCallback(
(updatedItem: GridFilterItem) => {
if (item.value && !updatedItem.value) {
if (item.value && updatedItem.value === undefined) {
apiRef.current.deleteFilterItem(updatedItem);
return;
}
Expand Down Expand Up @@ -314,7 +314,7 @@ const GridHeaderFilterCell = React.forwardRef<HTMLDivElement, GridHeaderFilterCe

const isNoInputOperator = currentOperator.requiresFilterValue === false;

const isApplied = Boolean(item?.value) || isNoInputOperator;
const isApplied = item?.value !== undefined || isNoInputOperator;

const label =
currentOperator.headerLabel ??
Expand Down
69 changes: 69 additions & 0 deletions packages/x-data-grid-pro/src/tests/filtering.DataGridPro.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
gridClasses,
GridColDef,
getGridStringOperators,
GridFilterItem,
} from '@mui/x-data-grid-pro';
import { createRenderer, fireEvent, screen, act, within } from '@mui/internal-test-utils';
import { expect } from 'chai';
Expand Down Expand Up @@ -1118,6 +1119,74 @@ describe('<DataGridPro /> - Filter', () => {
);
}).not.toErrorDev();
});

it('should work correctly with boolean column type', () => {
const getRows = (item: Omit<GridFilterItem, 'field'>) => {
const { unmount } = render(
<TestCase
filterModel={{
items: [{ field: 'isPublished', ...item }],
}}
rows={[
{
id: 0,
isPublished: undefined,
},
{
id: 1,
isPublished: null,
},
{
id: 2,
isPublished: true,
},
{
id: 3,
isPublished: false,
},
]}
columns={[
{
field: 'isPublished',
type: 'boolean',
// The boolean cell does not handle the formatted value, so we override it
renderCell: (params) => {
const value = params.value as boolean | null | undefined;

if (value === null) {
return 'null';
}

if (value === undefined) {
return 'undefined';
}

return value.toString();
},
},
]}
headerFilters
/>,
);
const values = getColumnValues(0);
unmount();
return values;
};
const ALL_ROWS = ['undefined', 'null', 'true', 'false'];
const TRUTHY_ROWS = ['true'];
const FALSY_ROWS = ['undefined', 'null', 'false'];

expect(getRows({ operator: 'is', value: 'true' })).to.deep.equal(TRUTHY_ROWS);
expect(getRows({ operator: 'is', value: true })).to.deep.equal(TRUTHY_ROWS);

expect(getRows({ operator: 'is', value: 'false' })).to.deep.equal(FALSY_ROWS);
expect(getRows({ operator: 'is', value: false })).to.deep.equal(FALSY_ROWS);

expect(getRows({ operator: 'is', value: '' })).to.deep.equal(ALL_ROWS);
expect(getRows({ operator: 'is', value: undefined })).to.deep.equal(ALL_ROWS);
expect(getRows({ operator: 'is', value: null })).to.deep.equal(ALL_ROWS);
expect(getRows({ operator: 'is', value: 'test' })).to.deep.equal(ALL_ROWS); // Ignores invalid values
});
});

describe('Read-only filters', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/x-data-grid/src/tests/filtering.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,7 @@ describe('<DataGrid /> - Filter', () => {

expect(getRows({ operator: 'is', value: '' })).to.deep.equal(ALL_ROWS);
expect(getRows({ operator: 'is', value: undefined })).to.deep.equal(ALL_ROWS);
expect(getRows({ operator: 'is', value: null })).to.deep.equal(ALL_ROWS);
expect(getRows({ operator: 'is', value: 'test' })).to.deep.equal(ALL_ROWS); // Ignores invalid values
});
});
Expand Down

0 comments on commit fe3104e

Please sign in to comment.