@@ -29,19 +29,40 @@ type Props = {
29
29
onClose ?: ( ) => void ;
30
30
} ;
31
31
32
+ const DateInput = ( { value, onChange, placeholder, max } : { value : string ; onChange : ( value : string ) => void ; placeholder : string ; max : string } ) => {
33
+ const [ tempValue , setTempValue ] = React . useState ( value ? dayjs ( value ) . format ( 'YYYY-MM-DD' ) : '' ) ;
34
+
35
+ const handleChange = React . useCallback ( ( event : ChangeEvent < HTMLInputElement > ) => {
36
+ setTempValue ( event . target . value ) ;
37
+ onChange ( event . target . value ) ;
38
+ } , [ onChange ] ) ;
39
+
40
+ return (
41
+ < Input
42
+ value = { tempValue }
43
+ onChange = { handleChange }
44
+ placeholder = { placeholder }
45
+ type = "date"
46
+ max = { max }
47
+ autoComplete = "off"
48
+ size = "sm"
49
+ />
50
+ ) ;
51
+ } ;
52
+
32
53
const AgeFilter = ( { value = defaultValue , handleFilterChange, onClose } : Props ) => {
33
54
const [ currentValue , setCurrentValue ] = React . useState < AgeFromToValue > ( {
34
- age : value . age ,
35
- from : value . age ? '' : value . from ,
36
- to : value . age ? '' : value . to ,
55
+ age : value . age || '' ,
56
+ from : value . age ? '' : ( value . from || '' ) ,
57
+ to : value . age ? '' : ( value . to || '' ) ,
37
58
} ) ;
38
59
39
- const handleFromChange = React . useCallback ( ( event : ChangeEvent < HTMLInputElement > ) => {
40
- setCurrentValue ( prev => ( { age : '' , to : prev . to , from : event . target . value } ) ) ;
60
+ const handleFromChange = React . useCallback ( ( newValue : string ) => {
61
+ setCurrentValue ( prev => ( { age : '' , to : prev . to , from : newValue } ) ) ;
41
62
} , [ ] ) ;
42
63
43
- const handleToChange = React . useCallback ( ( event : ChangeEvent < HTMLInputElement > ) => {
44
- setCurrentValue ( prev => ( { age : '' , from : prev . from , to : event . target . value } ) ) ;
64
+ const handleToChange = React . useCallback ( ( newValue : string ) => {
65
+ setCurrentValue ( prev => ( { age : '' , from : prev . from , to : newValue } ) ) ;
45
66
} , [ ] ) ;
46
67
47
68
const onPresetChange = React . useCallback ( ( age : AdvancedFilterAge ) => {
@@ -64,9 +85,9 @@ const AgeFilter = ({ value = defaultValue, handleFilterChange, onClose }: Props)
64
85
}
65
86
const from = currentValue . age ?
66
87
dayjs ( ( dayjs ( ) . valueOf ( ) - getDurationFromAge ( currentValue . age ) ) ) . toISOString ( ) :
67
- dayjs ( currentValue . from ) . startOf ( 'day' ) . toISOString ( ) ;
88
+ dayjs ( currentValue . from || undefined ) . startOf ( 'day' ) . toISOString ( ) ;
68
89
handleFilterChange ( FILTER_PARAM_FROM , from ) ;
69
- const to = currentValue . age ? dayjs ( ) . toISOString ( ) : dayjs ( currentValue . to ) . endOf ( 'day' ) . toISOString ( ) ;
90
+ const to = currentValue . age ? dayjs ( ) . toISOString ( ) : dayjs ( currentValue . to || undefined ) . endOf ( 'day' ) . toISOString ( ) ;
70
91
handleFilterChange ( FILTER_PARAM_TO , to ) ;
71
92
handleFilterChange ( FILTER_PARAM_AGE , currentValue . age ) ;
72
93
} , [ handleFilterChange , currentValue ] ) ;
@@ -75,7 +96,7 @@ const AgeFilter = ({ value = defaultValue, handleFilterChange, onClose }: Props)
75
96
< TableColumnFilter
76
97
title = "Set last duration"
77
98
isFilled = { Boolean ( currentValue . from || currentValue . to || currentValue . age ) }
78
- isTouched = { value . age ? value . age !== currentValue . age : ! isEqual ( currentValue , value ) }
99
+ isTouched = { currentValue . age ? value . age !== currentValue . age : Boolean ( currentValue . from && currentValue . to && ! isEqual ( currentValue , value ) ) }
79
100
onFilter = { onFilter }
80
101
onReset = { onReset }
81
102
hasReset
@@ -90,20 +111,18 @@ const AgeFilter = ({ value = defaultValue, handleFilterChange, onClose }: Props)
90
111
</ PopoverCloseTriggerWrapper >
91
112
</ Flex >
92
113
< Flex mt = { 3 } >
93
- < Input
94
- value = { currentValue . age ? '' : dayjs ( currentValue . from ) . format ( 'YYYY-MM-DD' ) }
114
+ < DateInput
115
+ value = { currentValue . age ? '' : currentValue . from }
95
116
onChange = { handleFromChange }
96
117
placeholder = "From"
97
- type = "date"
98
- size = "sm"
118
+ max = { dayjs ( ) . format ( 'YYYY-MM-DD' ) }
99
119
/>
100
120
< Text mx = { 3 } > { ndash } </ Text >
101
- < Input
102
- value = { currentValue . age ? '' : dayjs ( currentValue . to ) . format ( 'YYYY-MM-DD' ) }
121
+ < DateInput
122
+ value = { currentValue . age ? '' : currentValue . to }
103
123
onChange = { handleToChange }
104
124
placeholder = "To"
105
- type = "date"
106
- size = "sm"
125
+ max = { dayjs ( ) . format ( 'YYYY-MM-DD' ) }
107
126
/>
108
127
</ Flex >
109
128
</ TableColumnFilter >
0 commit comments