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

[DataGridPro] Fix header filtering with boolean column type #15528

Merged
merged 3 commits into from
Nov 27, 2024
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 @@ -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) {
michelengelen marked this conversation as resolved.
Show resolved Hide resolved
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;
Copy link
Member

Choose a reason for hiding this comment

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

same here

Suggested change
const isApplied = item?.value !== undefined || 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