Skip to content

Commit ebd8fef

Browse files
author
Владимир Колесников
committed
fix filter value in filter button tooltip
1 parent ac685cd commit ebd8fef

File tree

6 files changed

+26
-2
lines changed

6 files changed

+26
-2
lines changed

docs/data/data-grid/filtering/CustomRatingOperator.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22
import PropTypes from 'prop-types';
33
import Box from '@mui/material/Box';
44
import Rating from '@mui/material/Rating';
5-
import { DataGrid } from '@mui/x-data-grid';
5+
import { DataGrid, GridToolbarFilterButton } from '@mui/x-data-grid';
66
import { useDemoData } from '@mui/x-data-grid-generator';
77

88
function RatingInputValue(props) {
@@ -88,6 +88,7 @@ const ratingOnlyOperators = [
8888
},
8989
InputComponent: RatingInputValue,
9090
InputComponentProps: { type: 'number' },
91+
getValueAsString: (value) => `${value} starts`,
9192
},
9293
];
9394

@@ -116,6 +117,9 @@ export default function CustomRatingOperator() {
116117
return (
117118
<div style={{ height: 400, width: '100%' }}>
118119
<DataGrid
120+
components={{
121+
Toolbar: GridToolbarFilterButton,
122+
}}
119123
{...data}
120124
columns={columns}
121125
initialState={{

docs/data/data-grid/filtering/CustomRatingOperator.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const ratingOnlyOperators: GridFilterOperator[] = [
6262
},
6363
InputComponent: RatingInputValue,
6464
InputComponentProps: { type: 'number' },
65+
getValueAsString: (value: number) => `${value} stars`,
6566
},
6667
];
6768

docs/data/data-grid/filtering/filtering.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ The [`valueFormatter`](/x/react-data-grid/column-definition/#value-formatter) is
203203
If the column has a [`valueGetter`](/x/react-data-grid/column-definition/#value-getter), then `params.value` will be the resolved value.
204204
:::
205205

206+
:::info
207+
The `getValueAsString` function allows you to convert the value of a filter item to a human-readable form before displayed in the UI of filter button tooltip.
208+
:::
209+
206210
In the demo below, you can see how to create a completely new operator for the Rating column.
207211

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

docs/pages/x/api/data-grid/grid-filter-operator.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { GridFilterOperator } from '@mui/x-data-grid';
1717
| Name | Type | Default | Description |
1818
| :---------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
1919
| <span class="prop-name">getApplyFilterFn</span> | <span class="prop-type">(filterItem: GridFilterItem, column: GridColDef&lt;R, V, F&gt;) =&gt; null \| ((params: GridCellParams&lt;V, R, F&gt;) =&gt; boolean)</span> | | The callback that generates a filtering function for a given filter item and column.<br />This function can return `null` to skip filtering for this item and column. |
20+
| <span class="prop-name optional">getValueAsString<sup><abbr title="optional">?</abbr></sup></span> | <span class="prop-type">(value: GridFilterItem['value']) =&gt; string</span> | | Converts the value of a filter item to a human-readable form. |
2021
| <span class="prop-name optional">InputComponent<sup><abbr title="optional">?</abbr></sup></span> | <span class="prop-type">React.JSXElementConstructor&lt;GridFilterInputValueProps&gt; \| React.JSXElementConstructor&lt;GridFilterInputMultipleValueProps&gt; \| React.JSXElementConstructor&lt;GridFilterInputMultipleSingleSelectProps&gt;</span> | | The input component to render in the filter panel for this filter operator. |
2122
| <span class="prop-name optional">InputComponentProps<sup><abbr title="optional">?</abbr></sup></span> | <span class="prop-type">Record&lt;string, any&gt;</span> | | The props to pass to the input component in the filter panel for this filter operator. |
2223
| <span class="prop-name optional">label<sup><abbr title="optional">?</abbr></sup></span> | <span class="prop-type">string</span> | | The label of the filter operator. |

packages/grid/x-data-grid/src/components/toolbar/GridToolbarFilterButton.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ const GridToolbarFilterButton = React.forwardRef<HTMLButtonElement, GridToolbarF
7777
.getLocaleText(`filterOperator${capitalize(item.operator!)}` as GridTranslationKeys)!
7878
.toString();
7979

80+
const getFilterItemValue = (item: GridFilterItem): string => {
81+
const { getValueAsString } = lookup[item.field!].filterOperators!.find(
82+
(operator) => operator.value === item.operator,
83+
)!;
84+
85+
return getValueAsString ? getValueAsString(item.value) : item.value;
86+
};
87+
8088
return (
8189
<div>
8290
{apiRef.current.getLocaleText('toolbarFiltersTooltipActive')(activeFilters.length)}
@@ -86,7 +94,7 @@ const GridToolbarFilterButton = React.forwardRef<HTMLButtonElement, GridToolbarF
8694
<li key={index}>
8795
{`${lookup[item.field!].headerName || item.field}
8896
${getOperatorLabel(item)}
89-
${item.value ?? ''}`}
97+
${item.value ? getFilterItemValue(item.value) : ''}`}
9098
</li>
9199
)),
92100
}))}

packages/grid/x-data-grid/src/models/gridFilterOperator.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ export interface GridFilterOperator<R extends GridValidRowModel = any, V = any,
4242
* The props to pass to the input component in the filter panel for this filter operator.
4343
*/
4444
InputComponentProps?: Record<string, any>;
45+
/**
46+
* Converts the value of a filter item to a human-readable form.
47+
* @param {GridFilterItem['value']} value The filter item value.
48+
* @returns {string} The value formatted to be displayed in the UI of filter button tooltip.
49+
*/
50+
getValueAsString?: (value: GridFilterItem['value']) => string;
4551
/**
4652
* If `false`, filter operator doesn't require user-entered value to work.
4753
* Usually should be set to `false` for filter operators that don't have `InputComponent` (for example `isEmpty`)

0 commit comments

Comments
 (0)