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 filter non-filterable columns programmatically #11538

Merged
merged 12 commits into from
Jan 13, 2024

Conversation

MBilalShafi
Copy link
Member

@MBilalShafi MBilalShafi commented Jan 1, 2024

ToDos:

  • Update tests
  • Update docs (added a section specific to filter, another generic covering the broader concept covered in the other PR, could possibly be added to the upcoming FAQ page too)
  • Update migration guide (this generic one added in the other PR should serve the broader concept)

Doc: https://deploy-preview-11538--material-ui-x.netlify.app/x/react-data-grid/filtering/#read-only-filters
Example usage: https://codesandbox.io/p/sandbox/bold-platform-9jg53c?file=%2Fsrc%2FDemo.tsx%3A41%2C9

Changelog

Breaking changes

  • Non-filterable columns could now be filtered programmatically by controlling or initialing filterModel, or by updating the filterModel by API method apiRef.current.setFilterModel.

@MBilalShafi MBilalShafi added breaking change 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 v7.x labels Jan 1, 2024
@mui-bot
Copy link

mui-bot commented Jan 1, 2024

Deploy preview: https://deploy-preview-11538--material-ui-x.netlify.app/

Updated pages:

Generated by 🚫 dangerJS against 81497f6

Copy link
Member Author

Choose a reason for hiding this comment

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

Since the MIT Grid allows one filter at a max, I have allowed clearing the read-only filter by adding a filter on another column. Users can prevent this by controlling the filter and not updating the filterModel on filterModelChange.

@@ -232,6 +238,7 @@ const GridFilterForm = React.forwardRef<HTMLDivElement, GridFilterFormProps>(
const classes = useUtilityClasses(rootProps);
const valueRef = React.useRef<any>(null);
const filterSelectorRef = React.useRef<HTMLInputElement>(null);
const multiFilterOperator = filterModel.logicOperator ?? GridLogicOperator.And;
Copy link
Member Author

Choose a reason for hiding this comment

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

Solves https://codesandbox.io/p/sandbox/zealous-shamir-jktxkz?file=%2Fsrc%2FDemo.tsx%3A20%2C11 i.e filterModel.logicOperator is undefined on initializing multiple filters. This issue is on next and not on master. Let me know if there's a better fix you are aware of.

Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't it be Or by default?

* @default `GridLogicOperator.Or`
*/
logicOperator?: GridLogicOperator;

Copy link
Member Author

@MBilalShafi MBilalShafi Jan 12, 2024

Choose a reason for hiding this comment

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

Interesting point. @cherniavskii
I feel this @default GridLogicOperator.Or statement is incorrect. Because if you check the current behavior on https://mui.com/x/react-data-grid/filtering/multi-filters/ it's GridLogicOperator.And (try adding 2-3 filters without touching logic operator)
Any guesses if I should update the default value on this prop or the default behavior on the attached demo is incorrect?

Copy link
Member

Choose a reason for hiding this comment

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

Indeed, here we set the default logic operator:

export const getDefaultGridFilterModel: () => GridFilterModel = () => ({
items: [],
logicOperator: GridLogicOperator.And,

Feel free to update the jsdoc to reflect the actual default value 👍🏻

@@ -103,6 +103,26 @@ You can use the `onFilterModelChange` prop to listen to changes to the filters a

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

### Read-only filters

You can initialize the `filterModel`, set the `filterModel` prop, or use `apiRef.current.setFilterModel` to define the filters for columns with `colDef.filterable` set to `false`. These filters will be applied but the user won't be able to change them.
Copy link
Member Author

@MBilalShafi MBilalShafi Jan 2, 2024

Choose a reason for hiding this comment

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

A small possible positive side effect would be this kind of filtering will now be possible in read-only mode without tweaking groupingColDef.

@MBilalShafi MBilalShafi marked this pull request as ready for review January 3, 2024 15:58
@@ -127,6 +127,26 @@ In the example below, the _rating_ column can not be filtered.

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

### Filter non-filterable columns programmatically
Copy link
Member Author

@MBilalShafi MBilalShafi Jan 10, 2024

Choose a reason for hiding this comment

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

Update in context of #11512 (comment)
Sidenote: I've kept internal variable naming to have the keyword readOnly for better understanding. The alternative disabled may also convey not visible at all, so to me readOnly is the more readable naming choice (on a dx perspective).
Let me know if there's a disagreement.

@MBilalShafi MBilalShafi changed the title [DataGrid] Introduce read-only filters [DataGrid] Allow to filter non-filterable columns programmatically Jan 10, 2024
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 10, 2024
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 11, 2024
docs/data/data-grid/filtering/index.md Outdated Show resolved Hide resolved
@@ -205,30 +205,6 @@
"default": "ArrowDropDown",
"class": null
},
{
Copy link
Member

Choose a reason for hiding this comment

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

@alexfauquette This seems to be a side-effect of #11303, some items are reordered for no reason after yarn docs:api. Any idea why this happens?

Copy link
Member

Choose a reason for hiding this comment

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

@michaldudak Should we sort them in a specific order?

Copy link
Member

Choose a reason for hiding this comment

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

IMO slots order should reflect the source. Usually we want to have the root slot at the top. Or we could sort them alphabetically but place 'root' at the top.

Copy link
Member

Choose a reason for hiding this comment

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

By the way it can be a configuration parameter: sortingStrategy: { porps, classes, slots } with either the sort callback or null if no sorting

Co-authored-by: Andrew Cherniavskii <[email protected]>
Signed-off-by: Bilal Shafi <[email protected]>
@MBilalShafi MBilalShafi merged commit 1a841bb into mui:next Jan 13, 2024
17 checks passed
@MBilalShafi MBilalShafi deleted the fix-10552/filterable branch January 13, 2024 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change 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 v7.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[data grid] Filtering on non filterable columns yield weird behavior
5 participants