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

Conversation

cherniavskii
Copy link
Member

@cherniavskii cherniavskii commented Oct 3, 2023

@cherniavskii cherniavskii added component: data grid This is the name of the generic UI component, not the React module! feature: Filtering Related to the data grid Filtering feature customization: logic Logic customizability labels Oct 3, 2023
@mui-bot
Copy link

mui-bot commented Oct 3, 2023

@cherniavskii cherniavskii marked this pull request as ready for review October 9, 2023 18:27
Copy link
Member

@MBilalShafi MBilalShafi left a comment

Choose a reason for hiding this comment

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

Nice enhancement, thanks for working on this. 👍

docs/data/data-grid/filtering/quick-filter.md Show resolved Hide resolved
{{"demo": "QuickFilteringDiacritics.js", "bg": "inline", "defaultCodeOpen": false}}

:::warning
Note that `ignoreDiacritics` affects [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/) simultaneously.
Copy link
Member

Choose a reason for hiding this comment

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

Probably this warning won't be necessary if we move this section to the customization page

Suggested change
Note that `ignoreDiacritics` affects [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/) simultaneously.
Note that `ignoreDiacritics` affects [normal 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/) simultaneously.

Copy link
Member Author

Choose a reason for hiding this comment

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

I wanted to make it explicit. Not everything on Customization page applies to all filter types

fn: getApplyQuickFilterFnV7(value, column, apiRef),
})),
appliers: quickFilterValues.map((quickFilterValue) => {
const value = column.ignoreDiacritics
Copy link
Member

Choose a reason for hiding this comment

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

Minor: Does it make sense to make this a grid root prop? I guess not as it's only supported with string types.
I am considering a less usual scenario with hundreds of columns with accented data where such a search is needed but users have to add ignoreDiacritics to all of the colDefs, a root-level prop may save some time/hassle.
But I'm not sure about making this a root prop, since it's string only. 🤷‍♂️

Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't necessarily just apply to string columns. Not sure what's the default date format but e.g. "February" is "février" in French so we can get diacritics elsewhere, and those are supposed to be searchable through the quickfilter. Any column can return a formatted localized (aka with diacritics) value with the .valueFormatter also.

I think it might make sense to make it a root-level option.

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, for quick filters, it could apply to any column type.
For regular filters and header filters, it will only apply to the string column type.

I was thinking about where to put this option and decided to put it in GridColDef to allow per-column configuration.
But it's a bit of a pain if you have a lot of columns and want to ignore diacritics in all of them.
This seems to emphasize a larger problem - we don't provide a way to pass something to all columns at once (or all columns of a certain type at once).

Regarding ignoreDiacritics specifically, enabling it on the root level should cover most, if not all, use cases.
So this makes sense to me, I will update the demo and the docs 👍🏻

@resatakks
Copy link

@cherniavskii hi,gm.Any update?maybe u can help,i watching this issue for few weeks

@cherniavskii
Copy link
Member Author

@resatakks Thanks for the reminder, I'll update the PR to address the feedback from the team 👍🏻

@@ -175,6 +182,13 @@ const getFilterCallbackFromItem = (
} else {
parsedValue = filterItem.value;
}

const { ignoreDiacriticsInFiltering } = apiRef.current.getRootProps();
Copy link
Member Author

Choose a reason for hiding this comment

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

I have added the getRootProps method to be able to access ignoreDiacriticsInFiltering here, otherwise, I had to pass the prop through in ~20 places 😅
What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense. Could have even been just a field like apiRef.current.rootProps, this is private anyway.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed to apiRef.current.rootProps instead 👍🏻

Copy link
Member

Choose a reason for hiding this comment

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

What do you think?

Nice one, it could also be useful in some other places in the future too.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Nov 1, 2023

This comment was marked as resolved.

Comment on lines 464 to 469
/**
* 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?

Comment on lines 21 to 25
params.value.toLocaleDateString('fr-FR', {
day: 'numeric',
month: 'long',
year: 'numeric',
}),
Copy link
Contributor

Choose a reason for hiding this comment

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

Sidenote but this is slow for formatters, not sure if we should show the more performant way in this example though. (using Intl.DateTimeFormat directly)

Copy link
Member Author

Choose a reason for hiding this comment

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

Interesting, I didn't think about performance in this case.
However, the difference seems to be minimal (toLocaleDateString being a bit faster): https://jsperf.app/lohope

Am I missing something?

Copy link
Contributor

Choose a reason for hiding this comment

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

.toLocaleDateString builds a new Intl.DateTimeFormat instance on each call. It's best to build an instance outside the loop, and re-use it for each call.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

Copy link
Contributor

@romgrk romgrk Nov 2, 2023

Choose a reason for hiding this comment

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

Another sidenote but I have also noticed in previous performance benchmarking that using the native Intl formatter vs using a JS based one is much slower. Crossing the JS/C++ boundary is usually quite bad for performance.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, gotcha!
Here's a benchmark for the singleton formatter: https://jsperf.app/hutiwe

I'll update the demo.

using the native Intl formatter vs using a JS based one is much slower

I didn't know that, thanks for the heads-up 👍🏻
I think for demo purposes Intl.DateTimeFormat is fine.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Nov 1, 2023
@cherniavskii cherniavskii merged commit b0cb729 into mui:master Nov 2, 2023
5 checks passed
@cherniavskii cherniavskii deleted the ignore-diacritics branch November 2, 2023 13:56
alexfauquette pushed a commit to alexfauquette/mui-x that referenced this pull request Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: data grid This is the name of the generic UI component, not the React module! customization: logic Logic customizability feature: Filtering Related to the data grid Filtering feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to Use Local Characters (e.g., "İ", "Ş") in Material-UI DataGrid Filter
5 participants