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] Allow to ignore diacritics when filtering #10569

Merged
merged 27 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ca452f7
add ignoreDiacritics to gridColDef
cherniavskii Oct 3, 2023
c14f23c
avoid calling both getRowValue and getRowFormattedValue
cherniavskii Oct 3, 2023
b5f54d6
support `ignoreDiacritics` during quick filtering
cherniavskii Oct 3, 2023
9e66af1
Merge branch 'master' into ignore-diacritics
cherniavskii Oct 5, 2023
06a5e3f
add unit tests
cherniavskii Oct 5, 2023
bfe5306
add unit tests for quick filtering
cherniavskii Oct 5, 2023
ae4e190
add a demo
cherniavskii Oct 5, 2023
92b2830
rename argument
cherniavskii Oct 6, 2023
a56bd73
do not wait for flag
cherniavskii Oct 6, 2023
17cd875
Merge branch 'master' into ignore-diacritics
cherniavskii Oct 13, 2023
1f2e3e8
Merge branch 'master' into ignore-diacritics
cherniavskii Oct 30, 2023
1d32a5a
change function signature to accept arguments in object
cherniavskii Oct 30, 2023
37f12fb
add root-level ignoreDiacriticsInFiltering prop
cherniavskii Oct 30, 2023
38463f5
yarn proptypes
cherniavskii Oct 30, 2023
ef0be36
yarn docs:api
cherniavskii Oct 30, 2023
767294a
add getRootProps method to apiRef
cherniavskii Oct 31, 2023
0ff8b5a
use getRootProps method instead of passing ignoreDiacriticsInFiltering
cherniavskii Oct 31, 2023
7a49da6
Merge branch 'master' into ignore-diacritics
cherniavskii Oct 31, 2023
6c02040
update demo
cherniavskii Oct 31, 2023
0f94386
update jsdoc
cherniavskii Oct 31, 2023
0092a91
update demo
cherniavskii Oct 31, 2023
6d4fd1b
update docs
cherniavskii Oct 31, 2023
d1ba202
Merge branch 'master' into ignore-diacritics
cherniavskii Nov 1, 2023
9453a3c
getRootProps => rootProps
cherniavskii Nov 1, 2023
28dc32d
Merge branch 'master' into ignore-diacritics
cherniavskii Nov 2, 2023
15b524f
rename the prop
cherniavskii Nov 2, 2023
4f11d1f
extract formatter
cherniavskii Nov 2, 2023
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
41 changes: 41 additions & 0 deletions docs/data/data-grid/filtering/QuickFilteringDiacritics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as React from 'react';
import { DataGrid, GridToolbar } from '@mui/x-data-grid';
import FormControlLabel from '@mui/material/FormControlLabel';
import Switch from '@mui/material/Switch';

const rows = [{ id: 0, value: 'Café' }];
const columns = [{ field: 'value', width: 150 }];

export default function QuickFilteringDiacritics() {
const [ignoreDiacritics, setIgnoreDiacritics] = React.useState(true);

return (
<div style={{ width: '100%' }}>
<FormControlLabel
checked={ignoreDiacritics}
onChange={(event) => setIgnoreDiacritics(event.target.checked)}
control={<Switch color="primary" size="small" />}
label="Ignore diacritics"
/>
<div style={{ height: 200, width: '100%' }}>
<DataGrid
key={ignoreDiacritics.toString()}
rows={rows}
columns={columns}
initialState={{
filter: {
filterModel: { items: [], quickFilterValues: ['cafe'] },
},
}}
disableColumnFilter
disableColumnSelector
disableDensitySelector
hideFooter
slots={{ toolbar: GridToolbar }}
slotProps={{ toolbar: { showQuickFilter: true } }}
ignoreDiacriticsInFiltering={ignoreDiacritics}
/>
</div>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<DataGrid rows={[{ id: 0, value: 'Café' }]} columns={columns} />
43 changes: 43 additions & 0 deletions docs/data/data-grid/filtering/QuickFilteringDiacritics.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as React from 'react';
import { DataGrid, GridToolbar, GridColDef } from '@mui/x-data-grid';
import FormControlLabel from '@mui/material/FormControlLabel';
import Switch from '@mui/material/Switch';

const rows = [{ id: 0, value: 'Café' }];
const columns: GridColDef[] = [{ field: 'value', width: 150 }];

export default function QuickFilteringDiacritics() {
const [ignoreDiacritics, setIgnoreDiacritics] = React.useState(true);

return (
<div style={{ width: '100%' }}>
<FormControlLabel
checked={ignoreDiacritics}
onChange={(event) =>
setIgnoreDiacritics((event.target as HTMLInputElement).checked)
}
control={<Switch color="primary" size="small" />}
label="Ignore diacritics"
/>
<div style={{ height: 200, width: '100%' }}>
<DataGrid
key={ignoreDiacritics.toString()}
rows={rows}
columns={columns}
initialState={{
filter: {
filterModel: { items: [], quickFilterValues: ['cafe'] },
},
}}
disableColumnFilter
disableColumnSelector
disableDensitySelector
hideFooter
slots={{ toolbar: GridToolbar }}
slotProps={{ toolbar: { showQuickFilter: true } }}
ignoreDiacriticsInFiltering={ignoreDiacritics}
/>
</div>
</div>
);
}
4 changes: 4 additions & 0 deletions docs/data/data-grid/filtering/header-filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ Additionally, `slots.headerFilterMenu` could also be used to customize the menu

{{"demo": "CustomHeaderFilterDataGridPro.js", "bg": "inline", "defaultCodeOpen": false}}

## Ignore diacritics (accents)

You can ignore diacritics (accents) when filtering the rows. See [Quick filter - Ignore diacritics (accents)](/x/react-data-grid/filtering/quick-filter/#ignore-diacritics-accents).

## API

- [DataGrid](/x/api/data-grid/data-grid/)
Expand Down
4 changes: 4 additions & 0 deletions docs/data/data-grid/filtering/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ In the example below, the _rating_ column can not be filtered.

{{"demo": "DisableFilteringGridSomeColumns.js", "bg": "inline", "defaultCodeOpen": false}}

## Ignore diacritics (accents)

You can ignore diacritics (accents) when filtering the rows. See [Quick filter - Ignore diacritics (accents)](/x/react-data-grid/filtering/quick-filter/#ignore-diacritics-accents).

## apiRef

The grid exposes a set of methods that enables all of these features using the imperative `apiRef`. To know more about how to use it, check the [API Object](/x/react-data-grid/api-object/) section.
Expand Down
17 changes: 17 additions & 0 deletions docs/data/data-grid/filtering/quick-filter.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,23 @@ In the following demo, the quick filter value `"Saint Martin, Saint Lucia"` will

{{"demo": "QuickFilteringCustomizedGrid.js", "bg": "inline", "defaultCodeOpen": false}}

## Ignore diacritics (accents)

In some languages, the letters can have diacritics (accents) - for instance, the letter `é` in French.
By default, these letters are considered different from their non-accented versions when filtering.

To ignore diacritics, set the `ignoreDiacriticsInFiltering` prop to `true`:

```tsx
<DataGrid ignoreDiacriticsInFiltering />
```

{{"demo": "QuickFilteringDiacritics.js", "bg": "inline", "defaultCodeOpen": false}}

:::warning
Note that the `ignoreDiacriticsInFiltering` prop affects all columns and all types of filters: [regular filters](/x/react-data-grid/filtering/), [Quick filter](/x/react-data-grid/filtering/quick-filter/) and [Header filters](/x/react-data-grid/filtering/header-filters/).
:::

## API

- [GridToolbarQuickFilter](/x/api/data-grid/grid-toolbar-quick-filter/)
Expand Down
1 change: 1 addition & 0 deletions docs/pages/x/api/data-grid/data-grid-premium.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
"default": "false"
},
"hideFooterSelectedRowCount": { "type": { "name": "bool" } },
"ignoreDiacriticsInFiltering": { "type": { "name": "bool" } },
"initialState": { "type": { "name": "object" } },
"isCellEditable": {
"type": { "name": "func" },
Expand Down
1 change: 1 addition & 0 deletions docs/pages/x/api/data-grid/data-grid-pro.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
"default": "false"
},
"hideFooterSelectedRowCount": { "type": { "name": "bool" } },
"ignoreDiacriticsInFiltering": { "type": { "name": "bool" } },
"initialState": { "type": { "name": "object" } },
"isCellEditable": {
"type": { "name": "func" },
Expand Down
1 change: 1 addition & 0 deletions docs/pages/x/api/data-grid/data-grid.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"hideFooter": { "type": { "name": "bool" } },
"hideFooterPagination": { "type": { "name": "bool" } },
"hideFooterSelectedRowCount": { "type": { "name": "bool" } },
"ignoreDiacriticsInFiltering": { "type": { "name": "bool" } },
"initialState": { "type": { "name": "object" } },
"isCellEditable": {
"type": { "name": "func" },
Expand Down
5 changes: 5 additions & 0 deletions docs/translations/api-docs/data-grid/data-grid-premium.json
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,11 @@
"deprecated": "",
"typeDescriptions": {}
},
"ignoreDiacriticsInFiltering": {
"description": "If <code>true</code>, the diacritics (accents) are ignored when filtering or quick filtering. * E.g. when filter value is <code>cafe</code>, the rows with <code>café</code> will be visible.",
"deprecated": "",
"typeDescriptions": {}
},
"initialState": {
"description": "The initial state of the DataGridPremium. The data in it is set in the state on initialization but isn&#39;t controlled. If one of the data in <code>initialState</code> is also being controlled, then the control state wins.",
"deprecated": "",
Expand Down
5 changes: 5 additions & 0 deletions docs/translations/api-docs/data-grid/data-grid-pro.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@
"deprecated": "",
"typeDescriptions": {}
},
"ignoreDiacriticsInFiltering": {
"description": "If <code>true</code>, the diacritics (accents) are ignored when filtering or quick filtering. * E.g. when filter value is <code>cafe</code>, the rows with <code>café</code> will be visible.",
"deprecated": "",
"typeDescriptions": {}
},
"initialState": {
"description": "The initial state of the DataGridPro. The data in it will be set in the state on initialization but will not be controlled. If one of the data in <code>initialState</code> is also being controlled, then the control state wins.",
"deprecated": "",
Expand Down
5 changes: 5 additions & 0 deletions docs/translations/api-docs/data-grid/data-grid.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@
"deprecated": "",
"typeDescriptions": {}
},
"ignoreDiacriticsInFiltering": {
"description": "If <code>true</code>, the diacritics (accents) are ignored when filtering or quick filtering. * E.g. when filter value is <code>cafe</code>, the rows with <code>café</code> will be visible.",
"deprecated": "",
"typeDescriptions": {}
},
"initialState": {
"description": "The initial state of the DataGrid. The data in it will be set in the state on initialization but will not be controlled. If one of the data in <code>initialState</code> is also being controlled, then the control state wins.",
"deprecated": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,12 @@ DataGridPremiumRaw.propTypes = {
* @default false
*/
hideFooterSelectedRowCount: PropTypes.bool,
/**
* If `true`, the diacritics (accents) are ignored when filtering or quick filtering.
* * E.g. when filter value is `cafe`, the rows with `café` will be visible.
* @default false
*/
ignoreDiacriticsInFiltering: PropTypes.bool,
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure I like the naming, but I don't have anything better to propose atm.

Copy link
Member

Choose a reason for hiding this comment

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

Nit: To me personally, simply ignoreDiacritics also makes sense, as the JSDoc comment will be sufficient to explain the relation with filtering. Also, the user using it should already be aware of what it does so maybe a little bit less explanatory title would make the prop name a bit simple?

/**
* The initial state of the DataGridPremium.
* The data in it is set in the state on initialization but isn't controlled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
GridAggregatedFilterItemApplier,
GridAggregatedFilterItemApplierResult,
GridColumnRawLookup,
GridApiCommunity,
} from '@mui/x-data-grid-pro/internals';
import { DataGridPremiumProcessedProps } from '../../../models/dataGridPremiumProps';
import { GridGroupingValueGetterParams } from '../../../models/gridGroupingValueGetterParams';
Expand Down Expand Up @@ -58,7 +57,7 @@ interface FilterRowTreeFromTreeDataParams {
rowTree: GridRowTreeConfig;
isRowMatchingFilters: GridAggregatedFilterItemApplier | null;
filterModel: GridFilterModel;
apiRef: React.MutableRefObject<GridApiCommunity>;
apiRef: React.MutableRefObject<GridPrivateApiPremium>;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { GridInitialStatePremium, GridStatePremium } from './gridStatePremium';
import type { GridRowGroupingApi, GridExcelExportApi, GridAggregationApi } from '../hooks';
import { GridCellSelectionApi } from '../hooks/features/cellSelection/gridCellSelectionInterfaces';
import type { DataGridPremiumProcessedProps } from './dataGridPremiumProps';

/**
* The api of `DataGridPremium`.
Expand All @@ -35,5 +36,5 @@ export interface GridApiPremium

export interface GridPrivateApiPremium
extends GridApiPremium,
GridPrivateOnlyApiCommon<GridApiPremium, GridPrivateApiPremium>,
GridPrivateOnlyApiCommon<GridApiPremium, GridPrivateApiPremium, DataGridPremiumProcessedProps>,
GridDetailPanelPrivateApi {}
6 changes: 6 additions & 0 deletions packages/grid/x-data-grid-pro/src/DataGridPro/DataGridPro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ DataGridProRaw.propTypes = {
* @default false
*/
hideFooterSelectedRowCount: PropTypes.bool,
/**
* If `true`, the diacritics (accents) are ignored when filtering or quick filtering.
* * E.g. when filter value is `cafe`, the rows with `café` will be visible.
* @default false
*/
ignoreDiacriticsInFiltering: PropTypes.bool,
/**
* The initial state of the DataGridPro.
* The data in it will be set in the state on initialization but will not be controlled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import {
import {
GridAggregatedFilterItemApplier,
GridAggregatedFilterItemApplierResult,
GridApiCommunity,
passFilterLogic,
} from '@mui/x-data-grid/internals';
import type { GridPrivateApiPro } from '../../../models/gridApiPro';

interface FilterRowTreeFromTreeDataParams {
rowTree: GridRowTreeConfig;
disableChildrenFiltering: boolean;
isRowMatchingFilters: GridAggregatedFilterItemApplier | null;
filterModel: GridFilterModel;
apiRef: React.MutableRefObject<GridApiCommunity>;
apiRef: React.MutableRefObject<GridPrivateApiPro>;
}

export const TREE_DATA_STRATEGY = 'tree-data';
Expand Down
3 changes: 2 additions & 1 deletion packages/grid/x-data-grid-pro/src/models/gridApiPro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
GridRowPinningApi,
GridDetailPanelPrivateApi,
} from '../hooks';
import type { DataGridProProcessedProps } from './dataGridProProps';

/**
* The api of `DataGridPro`.
Expand All @@ -30,5 +31,5 @@ export interface GridApiPro

export interface GridPrivateApiPro
extends GridApiPro,
GridPrivateOnlyApiCommon<GridApiPro, GridPrivateApiPro>,
GridPrivateOnlyApiCommon<GridApiPro, GridPrivateApiPro, DataGridProProcessedProps>,
GridDetailPanelPrivateApi {}
6 changes: 6 additions & 0 deletions packages/grid/x-data-grid/src/DataGrid/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ DataGridRaw.propTypes = {
* @default false
*/
hideFooterSelectedRowCount: PropTypes.bool,
/**
* If `true`, the diacritics (accents) are ignored when filtering or quick filtering.
* * E.g. when filter value is `cafe`, the rows with `café` will be visible.
* @default false
*/
ignoreDiacriticsInFiltering: PropTypes.bool,
/**
* The initial state of the DataGrid.
* The data in it will be set in the state on initialization but will not be controlled.
Expand Down
1 change: 1 addition & 0 deletions packages/grid/x-data-grid/src/DataGrid/useDataGridProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const DATA_GRID_PROPS_DEFAULT_VALUES: DataGridPropsWithDefaultValues = {
hideFooterPagination: false,
hideFooterRowCount: false,
hideFooterSelectedRowCount: false,
ignoreDiacriticsInFiltering: false,
logger: console,
logLevel: process.env.NODE_ENV === 'production' ? ('error' as const) : ('warn' as const),
pagination: false,
Expand Down
3 changes: 1 addition & 2 deletions packages/grid/x-data-grid/src/colDef/gridStringOperators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ export const getGridStringQuickFilterFn = tagInternalFilter(
return null;
}
const filterRegex = new RegExp(escapeRegExp(value), 'i');
return (_, row, column, apiRef): boolean => {
const columnValue = apiRef.current.getRowFormattedValue(row, column);
return (columnValue): boolean => {
return columnValue != null ? filterRegex.test(columnValue.toString()) : false;
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const useGridInitialization = <
Api extends GridApiCommon,
>(
inputApiRef: React.MutableRefObject<Api> | undefined,
props: Pick<DataGridProcessedProps, 'signature' | 'logger' | 'logLevel' | 'localeText'>,
props: DataGridProcessedProps,
) => {
const privateApiRef = useGridApiInitialization<PrivateApi, Api>(inputApiRef, props);
useGridLoggerFactory(privateApiRef, props);
Expand All @@ -25,5 +25,7 @@ export const useGridInitialization = <
useGridStrategyProcessing(privateApiRef);
useGridLocaleText(privateApiRef, props);

privateApiRef.current.register('private', { getRootProps: () => props });

return privateApiRef;
};
Loading