-
-
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
[data grid] Add a default valueParser
and valueFormatter
to singleSelect columns
#5615
Comments
valueParser
valueFormatter
to singleSelect
columns to avoid duplicate code in cell editing, filtering and quick filteringvalueParser
and valueFormatter
to singleSelect
valueParser
and valueFormatter
to singleSelect
valueParser
and valueFormatter
to singleSelect columns
The following line could be moved to a mui-x/packages/grid/x-data-grid/src/components/cell/GridEditSingleSelectCell.tsx Line 108 in 9d6bd23
|
The idea of this breaking change came from https://github.com/mui/mui-x/pull/4317/files#r843910779. The problem that justifies adding the value formatter is the following. First we added the {
type: 'singleSelect',
valueOptions: ['Brazil', 'France', 'Spain']
} as well as a list of objects, e.g. {
type: 'singleSelect',
valueOptions: [
{ value: 'br', label: 'Brazil' },
{ value: 'fr', label: 'France' },
{ value: 'es', label: 'Spain' },
]
} When a list of object is provided as To make
This logic wouldn't be needed if we had added a default value formatter to diff --git a/packages/grid/x-data-grid/src/colDef/gridSingleSelectOperators.ts b/packages/grid/x-data-grid/src/colDef/gridSingleSelectOperators.ts
index c550564ea..88f815a85 100644
--- a/packages/grid/x-data-grid/src/colDef/gridSingleSelectOperators.ts
+++ b/packages/grid/x-data-grid/src/colDef/gridSingleSelectOperators.ts
@@ -13,50 +13,14 @@ const parseObjectValue = (value: GridFilterItem) => {
return value.value;
};
-export const getGridSingleSelectQuickFilterFn = (
- value: any,
- column: GridColDef,
- apiRef: React.MutableRefObject<GridApiCommunity>,
-) => {
- if (!value) {
+export const getGridSingleSelectQuickFilterFn = (quickFilterValue: any) => {
+ if (!quickFilterValue) {
return null;
}
- const { valueOptions, valueFormatter, field } = column;
- const potentialValues = [parseObjectValue(value).toString()];
-
- const iterableColumnValues =
- typeof valueOptions === 'function' ? valueOptions({ field }) : valueOptions || [];
-
- if (iterableColumnValues) {
- iterableColumnValues.forEach((option) => {
- // for each valueOption, check if the formatted value
- let optionValue;
- let optionLabel;
- if (typeof option === 'object') {
- optionValue = option.value;
- optionLabel = option.label;
- } else {
- optionValue = option;
- if (valueFormatter) {
- optionLabel = valueFormatter({ value: option, field, api: apiRef.current });
- } else {
- optionLabel = option;
- }
- }
-
- if (optionLabel.slice(0, value.length).toLowerCase() === value.toLowerCase()) {
- if (!potentialValues.includes(optionValue)) {
- potentialValues.push(optionValue.toString());
- }
- }
- });
- }
-
- return ({ value: columnValue }: GridCellParams): boolean => {
- return columnValue != null
- ? potentialValues.includes(parseObjectValue(columnValue).toString())
- : false;
+ return ({ value, formattedValue }: GridCellParams): boolean => {
+ const valueToTest = formattedValue == null ? value : formattedValue;
+ return valueToTest.toLowerCase() === quickFilterValue.toLowerCase();
};
}; At that time, adding a default value formatter was not allowed because it would be a breaking change. Maybe some user preferred to select About the value parser, I think it was a typo from @alexfauquette because he mentions "value parser" in https://github.com/mui/mui-x/pull/4317/files#r843616217 but later he gives an example with value formatter. However, we do can also add a value parser because the logic from #5615 (comment) is the same in mui-x/packages/grid/x-data-grid/src/components/panel/filterPanel/GridFilterInputSingleSelect.tsx Line 68 in a6c96b3
valueParser was designed to be called both for edit components and filter components. The value parser to be added only needs to act when the native select is used. Its job is to receive the value selected by the user (always as a string) and return the value in valueOptions with the correct type.
|
Summary
Add a default
valueParser
valueFormatter
tosingleSelect
columnsMotivation
DX: To avoid duplicate code in cell editing, filtering and quick filtering
The text was updated successfully, but these errors were encountered: