@@ -35,22 +35,25 @@ function GridFilterInputValue(props: GridTypeFilterInputValueProps) {
35
35
variant = 'standard' ,
36
36
...others
37
37
} = props ;
38
+
38
39
const filterTimeout = useTimeout ( ) ;
39
- const [ filterValueState , setFilterValueState ] = React . useState < string > ( item . value ?? '' ) ;
40
+ const [ filterValueState , setFilterValueState ] = React . useState < string | number | undefined > (
41
+ sanitizeFilterItemValue ( item . value , type ) ,
42
+ ) ;
40
43
const [ applying , setIsApplying ] = React . useState ( false ) ;
41
44
const id = useId ( ) ;
42
45
const rootProps = useGridRootProps ( ) ;
43
46
44
47
const onFilterChange = React . useCallback (
45
48
( event : React . ChangeEvent < HTMLInputElement > ) => {
46
- const { value } = event . target ;
47
- setFilterValueState ( String ( value ) ) ;
49
+ const value = sanitizeFilterItemValue ( event . target . value , type ) ;
50
+ setFilterValueState ( value ) ;
48
51
49
52
setIsApplying ( true ) ;
50
53
filterTimeout . start ( rootProps . filterDebounceMs , ( ) => {
51
54
const newItem = {
52
55
...item ,
53
- value : type === 'number' ? Number ( value ) : value ,
56
+ value,
54
57
fromInput : id ! ,
55
58
} ;
56
59
applyValue ( newItem ) ;
@@ -62,17 +65,17 @@ function GridFilterInputValue(props: GridTypeFilterInputValueProps) {
62
65
63
66
React . useEffect ( ( ) => {
64
67
const itemPlusTag = item as ItemPlusTag ;
65
- if ( itemPlusTag . fromInput !== id || item . value === undefined ) {
66
- setFilterValueState ( String ( item . value ?? '' ) ) ;
68
+ if ( itemPlusTag . fromInput !== id || item . value == null ) {
69
+ setFilterValueState ( sanitizeFilterItemValue ( item . value , type ) ) ;
67
70
}
68
- } , [ id , item ] ) ;
71
+ } , [ id , item , type ] ) ;
69
72
70
73
return (
71
74
< rootProps . slots . baseTextField
72
75
id = { id }
73
76
label = { apiRef . current . getLocaleText ( 'filterPanelInputLabel' ) }
74
77
placeholder = { apiRef . current . getLocaleText ( 'filterPanelInputPlaceholder' ) }
75
- value = { filterValueState }
78
+ value = { filterValueState === undefined ? '' : String ( filterValueState ) }
76
79
onChange = { onFilterChange }
77
80
variant = { variant }
78
81
type = { type || 'text' }
@@ -103,6 +106,16 @@ function GridFilterInputValue(props: GridTypeFilterInputValueProps) {
103
106
) ;
104
107
}
105
108
109
+ function sanitizeFilterItemValue ( value : any , type : GridTypeFilterInputValueProps [ 'type' ] ) {
110
+ if ( value == null || value === '' ) {
111
+ return undefined ;
112
+ }
113
+ if ( type === 'number' ) {
114
+ return Number ( value ) ;
115
+ }
116
+ return String ( value ) ;
117
+ }
118
+
106
119
GridFilterInputValue . propTypes = {
107
120
// ----------------------------- Warning --------------------------------
108
121
// | These PropTypes are generated from the TypeScript type definitions |
0 commit comments