-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changes from 23 commits
9afd3f4
129e939
0af7213
93e7aa3
bd3fcdd
7651d6b
3c9de83
d421ad9
01dbe3e
5b50bf0
3560f2b
2855ab6
d8c41ac
7d39cd8
15630d7
87e656a
1b16222
3d7b052
3936174
9d3e4fa
8170224
7ff5e6c
08225c9
808261e
00f7cd1
9dcadb0
b2e6453
7e659ab
6c6d6a9
84f4e16
29bd314
6d5c487
72d2886
64e7767
04a5331
f10cb3d
7127c06
3c8dbf7
ec687de
cc8ef3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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', | ||||||||||||||
size: 'small', | ||||||||||||||
sx: { justifyContent: 'flex-end' }, | ||||||||||||||
}, | ||||||||||||||
operatorInputProps: { | ||||||||||||||
variant: 'outlined', | ||||||||||||||
size: 'small', | ||||||||||||||
sx: { justifyContent: 'flex-end' }, | ||||||||||||||
}, | ||||||||||||||
valueInputProps: { | ||||||||||||||
required: true, | ||||||||||||||
}, | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure this is the best way to customize the deleteIcon color.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's better than my |
||||||||||||||
}, | ||||||||||||||
sx: { | ||||||||||||||
// Customize inputs using css selectors | ||||||||||||||
'& .MuiDataGrid-filterForm': { p: 2 }, | ||||||||||||||
'& .MuiDataGrid-filterForm:nth-child(even)': { | ||||||||||||||
backgroundColor: (theme) => | ||||||||||||||
theme.palette.mode === 'dark' ? '#444' : '#ddd', | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I will pick the one used in |
||||||||||||||
}, | ||||||||||||||
'& .MuiDataGrid-filterFormDeleteIcon': { display: 'none' }, | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no way to delete a filter anymore. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, not the best demo example. I tried a new one with |
||||||||||||||
'& .MuiDataGrid-filterFormLinkOperatorInput': { mr: 2 }, | ||||||||||||||
'& .MuiDataGrid-filterFormColumnInput': { mr: 2, width: 200 }, | ||||||||||||||
'& .MuiDataGrid-filterFormOperatorInput': { mr: 5 }, | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To keep the margin symmetrical.
Suggested change
|
||||||||||||||
'& .MuiDataGrid-filterFormValueInput': { width: 300 }, | ||||||||||||||
}, | ||||||||||||||
}, | ||||||||||||||
}} | ||||||||||||||
initialState={initialState} | ||||||||||||||
/> | ||||||||||||||
</div> | ||||||||||||||
); | ||||||||||||||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 labelI also had to add the
label
prop to<Select />
otherwise the outline is not removed under the label