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] Customize FilterPanel with props #3497

Merged
merged 40 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
9afd3f4
add props to filter panel
alexfauquette Dec 22, 2021
129e939
add doc
alexfauquette Dec 22, 2021
0af7213
set default values
alexfauquette Dec 22, 2021
93e7aa3
small PR feedbacks
alexfauquette Jan 7, 2022
bd3fcdd
use class override for style modification
alexfauquette Jan 11, 2022
7651d6b
Merge remote-tracking branch 'upstream/master' into customize-filters
alexfauquette Jan 11, 2022
3c9de83
Merge remote-tracking branch 'upstream/master' into customize-filters
alexfauquette Jan 11, 2022
d421ad9
scripts
alexfauquette Jan 11, 2022
01dbe3e
markdownlint
alexfauquette Jan 11, 2022
5b50bf0
Apply suggestions from code review
alexfauquette Jan 17, 2022
3560f2b
typing feedbacks and avoid breaking changes
alexfauquette Jan 18, 2022
2855ab6
update classes names
alexfauquette Jan 18, 2022
d8c41ac
pass props to filterForm FormeControl
alexfauquette Jan 18, 2022
7d39cd8
proptypes
alexfauquette Jan 18, 2022
15630d7
remove required
alexfauquette Jan 18, 2022
87e656a
update doc explainations
alexfauquette Jan 18, 2022
1b16222
Merge remote-tracking branch 'upstream/master' into customize-filters
alexfauquette Jan 26, 2022
3d7b052
update styling
alexfauquette Jan 26, 2022
3936174
update doc table
alexfauquette Jan 26, 2022
9d3e4fa
fix tests
alexfauquette Jan 26, 2022
8170224
add classes
alexfauquette Jan 26, 2022
7ff5e6c
match clesse properties with slot names
alexfauquette Jan 26, 2022
08225c9
update CSS doc
alexfauquette Jan 26, 2022
808261e
update doc example
alexfauquette Feb 1, 2022
00f7cd1
use styled without memo
alexfauquette Feb 1, 2022
9dcadb0
add label for outlined input
alexfauquette Feb 1, 2022
b2e6453
Merge remote-tracking branch 'upstream/master' into customize-filters
alexfauquette Feb 2, 2022
7e659ab
docs:typescript:formatted
alexfauquette Feb 2, 2022
6c6d6a9
Jose feedback
alexfauquette Feb 7, 2022
84f4e16
mat feedbacks on typing
alexfauquette Feb 7, 2022
29bd314
language
alexfauquette Feb 7, 2022
6d5c487
yarn proptypes
alexfauquette Feb 7, 2022
72d2886
fix lint
alexfauquette Feb 7, 2022
64e7767
Merge remote-tracking branch 'upstream/master' into customize-filters
alexfauquette Feb 8, 2022
04a5331
feedbacks
alexfauquette Feb 8, 2022
f10cb3d
add files removed during migration
alexfauquette Feb 8, 2022
7127c06
Merge remote-tracking branch 'upstream/master' into customize-filters
alexfauquette Feb 10, 2022
3c8dbf7
Fix demo migration
alexfauquette Feb 10, 2022
ec687de
Merge remote-tracking branch 'upstream/master' into customize-filters
alexfauquette Feb 10, 2022
cc8ef3e
Merge remote-tracking branch 'upstream/master' into customize-filters
alexfauquette Feb 10, 2022
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
5 changes: 5 additions & 0 deletions docs/pages/api-docs/data-grid/data-grid-pro.json
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@
"paper",
"editBooleanCell",
"filterForm",
"filterFormDeleteIcon",
"filterFormLinkOperatorInput",
"filterFormColumnInput",
"filterFormOperatorInput",
"filterFormValueInput",
"editInputCell",
"filterIcon",
"footerContainer",
Expand Down
5 changes: 5 additions & 0 deletions docs/pages/api-docs/data-grid/data-grid.json
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@
"paper",
"editBooleanCell",
"filterForm",
"filterFormDeleteIcon",
"filterFormLinkOperatorInput",
"filterFormColumnInput",
"filterFormOperatorInput",
"filterFormValueInput",
"editInputCell",
"filterIcon",
"footerContainer",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import * as React from 'react';
import { DataGridPro, GridLinkOperator, GridToolbar } from '@mui/x-data-grid-pro';
import { useDemoData } from '@mui/x-data-grid-generator';

const VISIBLE_FIELDS = ['name', 'rating', 'country', 'dateCreated', 'isAdmin'];

const initialState = {
filter: {
filterModel: {
items: [
{
id: 1,
columnField: 'name',
operatorValue: 'contains',
value: 'D',
},
{
id: 2,
columnField: 'name',
operatorValue: 'contains',
value: 'D',
},
{
id: 3,
columnField: 'rating',
operatorValue: '>',
value: '0',
},
],
},
},
};

export default function CustomFilterPanelContent() {
const { data } = useDemoData({
dataSet: 'Employee',
visibleFields: VISIBLE_FIELDS,
rowLength: 100,
});

return (
<div style={{ height: 400, width: '100%' }}>
<DataGridPro
{...data}
components={{
Toolbar: GridToolbar,
// Use custom FilterPanel only for deep modification
// FilterPanel: MyCustomFilterPanel,
}}
componentsProps={{
filterPanel: {
// Force to use "And" operator
linkOperators: [GridLinkOperator.And],
// Display columns by ascending alphabetical order
columnsSort: 'asc',
filterFormProps: {
// Customize inputs by passing props
columnInputProps: {
variant: 'outlined',
size: 'small',
sx: { justifyContent: 'flex-end' },
},
operatorInputProps: {
variant: 'outlined',
size: 'small',
sx: { justifyContent: 'flex-end' },
},
valueInputProps: {
required: true,
},
},
sx: {
// Customize inputs using css selectors
'& .MuiDataGrid-filterForm': { p: 2 },
'& .MuiDataGrid-filterForm:nth-child(even)': {
backgroundColor: (theme) =>
theme.palette.mode === 'dark' ? '#444' : '#ddd',
},
'& .MuiDataGrid-filterFormDeleteIcon': { display: 'none' },
'& .MuiDataGrid-filterFormLinkOperatorInput': { mr: 2 },
'& .MuiDataGrid-filterFormColumnInput': { mr: 2, width: 200 },
'& .MuiDataGrid-filterFormOperatorInput': { mr: 5 },
'& .MuiDataGrid-filterFormValueInput': { width: 300 },
},
},
}}
initialState={initialState}
/>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import * as React from 'react';
import { DataGridPro, GridLinkOperator, GridToolbar } from '@mui/x-data-grid-pro';
import { useDemoData } from '@mui/x-data-grid-generator';

const VISIBLE_FIELDS = ['name', 'rating', 'country', 'dateCreated', 'isAdmin'];

const initialState = {
filter: {
filterModel: {
items: [
{
id: 1,
columnField: 'name',
operatorValue: 'contains',
value: 'D',
},
{
id: 2,
columnField: 'name',
operatorValue: 'contains',
value: 'D',
},
{
id: 3,
columnField: 'rating',
operatorValue: '>',
value: '0',
},
],
},
},
};

export default function CustomFilterPanelContent() {
const { data } = useDemoData({
dataSet: 'Employee',
visibleFields: VISIBLE_FIELDS,
rowLength: 100,
});

return (
<div style={{ height: 400, width: '100%' }}>
<DataGridPro
{...data}
components={{
Toolbar: GridToolbar,
// Use custom FilterPanel only for deep modification
// FilterPanel: MyCustomFilterPanel,
}}
componentsProps={{
filterPanel: {
// Force to use "And" operator
linkOperators: [GridLinkOperator.And],
// Display columns by ascending alphabetical order
columnsSort: 'asc',
filterFormProps: {
// Customize inputs by passing props
columnInputProps: {
variant: 'outlined',
Copy link
Member

Choose a reason for hiding this comment

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

There's something wrong using this variant. The input border should be passing behind the label. See https://mui.com/components/text-fields/#select

image

Copy link
Member Author

@alexfauquette alexfauquette Feb 1, 2022

Choose a reason for hiding this comment

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

That's because of the sx: { justifyContent: 'flex-end' } applied to the filter form which impact the select, but not the label
I also had to add the label prop to <Select /> otherwise the outline is not removed under the label

size: 'small',
sx: { justifyContent: 'flex-end' },
},
operatorInputProps: {
variant: 'outlined',
size: 'small',
sx: { justifyContent: 'flex-end' },
},
valueInputProps: {
required: true,
},
Copy link
Member

@joserodolfofreitas joserodolfofreitas Feb 4, 2022

Choose a reason for hiding this comment

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

Not sure this is the best way to customize the deleteIcon color.
But I think we could add a simple example for deleteIconProps too.

Suggested change
},
deleteIconProps: {
sx: {
"& .MuiSvgIcon-root": { color: "#f00" }
}
},

Copy link
Member Author

@alexfauquette alexfauquette Feb 7, 2022

Choose a reason for hiding this comment

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

Yes, it's better than my reorder which was a bad accessibility practice

},
sx: {
// Customize inputs using css selectors
'& .MuiDataGrid-filterForm': { p: 2 },
'& .MuiDataGrid-filterForm:nth-child(even)': {
backgroundColor: (theme) =>
theme.palette.mode === 'dark' ? '#444' : '#ddd',
Copy link
Member

Choose a reason for hiding this comment

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

The color for the dark theme is good, but the color for the light theme is too dark.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, I will pick the one used in <Table /> demo

},
'& .MuiDataGrid-filterFormDeleteIcon': { display: 'none' },
Copy link
Member

Choose a reason for hiding this comment

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

There's no way to delete a filter anymore.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, not the best demo example. I tried a new one with order: 1 to move the button to the end

'& .MuiDataGrid-filterFormLinkOperatorInput': { mr: 2 },
'& .MuiDataGrid-filterFormColumnInput': { mr: 2, width: 200 },
'& .MuiDataGrid-filterFormOperatorInput': { mr: 5 },
Copy link
Member

Choose a reason for hiding this comment

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

To keep the margin symmetrical.

Suggested change
'& .MuiDataGrid-filterFormOperatorInput': { mr: 5 },
'& .MuiDataGrid-filterFormOperatorInput': { mr: 2 },

'& .MuiDataGrid-filterFormValueInput': { width: 300 },
},
},
}}
initialState={initialState}
/>
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ CustomToolbar.propTypes = {
setFilterButtonEl: PropTypes.func.isRequired,
};

export default function CustomFilterPanel() {
export default function CustomFilterPanelPosition() {
const { data } = useDemoData({
dataSet: 'Employee',
visibleFields: VISIBLE_FIELDS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const CustomToolbar: React.FunctionComponent<{
</GridToolbarContainer>
);

export default function CustomFilterPanel() {
export default function CustomFilterPanelPosition() {
const { data } = useDemoData({
dataSet: 'Employee',
visibleFields: VISIBLE_FIELDS,
Expand Down
30 changes: 28 additions & 2 deletions docs/src/pages/components/data-grid/filtering/filtering.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ In the demo below, the `rating` column only has the `<` and `>` operators.
### Edit an operator

The value used by the operator to look for has to be entered by the user.
On most column types, a text field is used. However, a custom component can be rendered instead.
On most column types, a text field is used.
However, a custom component can be rendered instead.

In the demo below, the `rating` column reuses the numeric operators but the rating component is used to enter the value of the filter.

Expand All @@ -259,9 +260,34 @@ const ratingColumnType: GridColTypeDef = {

You can customize the rendering of the filter panel as shown in [the component section](/components/data-grid/components/#overriding-components) of the documentation.

### Customize the filter panel content

The customization of the filter panel content can be performed by passing props to the default `<GridFilterPanel />` component.
The props available allow to override:

- The available `linkOperators` (can contains `GridLinkOperator.And` and `GridLinkOperator.Or`)
- The order of the column selector (can be `"asc"` or `"desc"`)
- Any prop of the input components

Input components can be [customized](/customization/how-to-customize/) by using two approaches.
You can pass a `sx` prop to any input container or you can use css selectors on nested components of the filter panel.
More details are available in the demo.

| Props | Class |
| :----------------------- | :---------------------------------------- |
| `deleteIconProps` | `MuiDataGrid-filterFormDeleteIcon` |
| `linkOperatorInputProps` | `MuiDataGrid-filterFormLinkOperatorInput` |
| `columnInputProps` | `MuiDataGrid-filterFormColumnInput` |
| `operatorInputProps` | `MuiDataGrid-filterFormOperatorInput` |
| `valueInputProps` | `MuiDataGrid-filterFormValueInput` |

{{"demo": "pages/components/data-grid/filtering/CustomFilterPanelContent.js", "bg": "inline"}}

### Customize the filter panel position

The demo below shows how to anchor the filter panel to the toolbar button instead of the column header.

{{"demo": "pages/components/data-grid/filtering/CustomFilterPanel.js", "bg": "inline", "defaultCodeOpen": false}}
{{"demo": "pages/components/data-grid/filtering/CustomFilterPanelPosition.js", "bg": "inline", "defaultCodeOpen": false}}

## Server-side filter

Expand Down
20 changes: 20 additions & 0 deletions docs/translations/api-docs/data-grid/data-grid-pro-pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,26 @@
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the root of the filter form component"
},
"filterFormDeleteIcon": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the delete icon of the filter form component"
},
"filterFormLinkOperatorInput": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the link operator inout of the filter form component"
},
"filterFormColumnInput": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the column input of the filter form component"
},
"filterFormOperatorInput": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the operator input of the filter form component"
},
"filterFormValueInput": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the value input of the filter form component"
},
"editInputCell": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the root of the input component"
Expand Down
20 changes: 20 additions & 0 deletions docs/translations/api-docs/data-grid/data-grid-pro-zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,26 @@
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the root of the filter form component"
},
"filterFormDeleteIcon": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the delete icon of the filter form component"
},
"filterFormLinkOperatorInput": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the link operator inout of the filter form component"
},
"filterFormColumnInput": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the column input of the filter form component"
},
"filterFormOperatorInput": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the operator input of the filter form component"
},
"filterFormValueInput": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the value input of the filter form component"
},
"editInputCell": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the root of the input component"
Expand Down
20 changes: 20 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 @@ -303,6 +303,26 @@
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the root of the filter form component"
},
"filterFormDeleteIcon": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the delete icon of the filter form component"
},
"filterFormLinkOperatorInput": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the link operator inout of the filter form component"
},
"filterFormColumnInput": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the column input of the filter form component"
},
"filterFormOperatorInput": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the operator input of the filter form component"
},
"filterFormValueInput": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the value input of the filter form component"
},
"editInputCell": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the root of the input component"
Expand Down
20 changes: 20 additions & 0 deletions docs/translations/api-docs/data-grid/data-grid-pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,26 @@
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the root of the filter form component"
},
"filterFormDeleteIcon": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the delete icon of the filter form component"
},
"filterFormLinkOperatorInput": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the link operator inout of the filter form component"
},
"filterFormColumnInput": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the column input of the filter form component"
},
"filterFormOperatorInput": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the operator input of the filter form component"
},
"filterFormValueInput": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the value input of the filter form component"
},
"editInputCell": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the root of the input component"
Expand Down
Loading