@@ -9,7 +9,6 @@ import React, {
99 useMemo ,
1010 useState ,
1111 useEffect ,
12- useCallback ,
1312 useContext ,
1413} from 'react' ;
1514import classnames from 'classnames' ;
@@ -21,7 +20,11 @@ import { Datepicker, DatepickerProps } from './Datepicker';
2120import { isElInChildren } from './util' ;
2221import { ComponentSettingsContext } from './ComponentSettings' ;
2322import mergeRefs from 'react-merge-refs' ;
24- import { useControlledValue , useFormElementId } from './hooks' ;
23+ import {
24+ useControlledValue ,
25+ useEventCallback ,
26+ useFormElementId ,
27+ } from './hooks' ;
2528import { AutoAlign , AutoAlignInjectedProps , AutoAlignProps } from './AutoAlign' ;
2629import { createFC } from './common' ;
2730
@@ -182,34 +185,31 @@ export const DateInput = createFC<DateInputProps, { isFormElement: boolean }>(
182185
183186 const { getActiveElement } = useContext ( ComponentSettingsContext ) ;
184187
185- const setValueFromInput = useCallback (
186- ( inputValue : string ) => {
187- let newValue = value ;
188- if ( ! inputValue ) {
189- newValue = '' ;
188+ const setValueFromInput = useEventCallback ( ( inputValue : string ) => {
189+ let newValue = value ;
190+ if ( ! inputValue ) {
191+ newValue = '' ;
192+ } else {
193+ const mvalue = moment ( inputValue , inputValueFormat ) ;
194+ if ( mvalue . isValid ( ) ) {
195+ newValue = mvalue . format ( valueFormat ) ;
190196 } else {
191- const mvalue = moment ( inputValue , inputValueFormat ) ;
192- if ( mvalue . isValid ( ) ) {
193- newValue = mvalue . format ( valueFormat ) ;
194- } else {
195- newValue = '' ;
196- }
197+ newValue = '' ;
197198 }
198- setValue ( newValue ) ;
199- setInputValue ( null ) ;
200- } ,
201- [ value , valueFormat , inputValueFormat , setValue , setInputValue ]
202- ) ;
199+ }
200+ setValue ( newValue ) ;
201+ setInputValue ( null ) ;
202+ } ) ;
203203
204- const isFocusedInComponent = useCallback ( ( ) => {
204+ const isFocusedInComponent = useEventCallback ( ( ) => {
205205 const targetEl = getActiveElement ( ) ;
206206 return (
207207 isElInChildren ( nodeRef . current , targetEl ) ||
208208 isElInChildren ( datepickerElRef . current , targetEl )
209209 ) ;
210- } , [ getActiveElement ] ) ;
210+ } ) ;
211211
212- const showDatepicker = useCallback ( ( ) => {
212+ const showDatepicker = useEventCallback ( ( ) => {
213213 let newValue = value ;
214214 if ( inputValue != null ) {
215215 const mvalue = moment ( inputValue , inputValueFormat ) ;
@@ -219,22 +219,22 @@ export const DateInput = createFC<DateInputProps, { isFormElement: boolean }>(
219219 }
220220 setOpened ( true ) ;
221221 setValue ( newValue ) ;
222- } , [ value , inputValue , inputValueFormat , valueFormat , setOpened , setValue ] ) ;
222+ } ) ;
223223
224224 const prevValueRef = useRef < typeof value > ( value ) ;
225225 useEffect ( ( ) => {
226226 onValueChange ?.( value , prevValueRef . current ) ;
227227 prevValueRef . current = value ;
228228 } , [ value , onValueChange ] ) ;
229229
230- const onDateIconClick = useCallback ( ( ) => {
230+ const onDateIconClick = useEventCallback ( ( ) => {
231231 inputElRef . current ?. focus ( ) ;
232232 setTimeout ( ( ) => {
233233 showDatepicker ( ) ;
234234 } , 10 ) ;
235- } , [ showDatepicker ] ) ;
235+ } ) ;
236236
237- const onInputKeyDown = useCallback (
237+ const onInputKeyDown = useEventCallback (
238238 ( e : KeyboardEvent < HTMLInputElement > ) => {
239239 if ( e . keyCode === 13 ) {
240240 // return key
@@ -255,20 +255,18 @@ export const DateInput = createFC<DateInputProps, { isFormElement: boolean }>(
255255 e . stopPropagation ( ) ;
256256 }
257257 onKeyDown ?.( e ) ;
258- } ,
259- [ setValueFromInput , showDatepicker , onComplete , onKeyDown ]
258+ }
260259 ) ;
261260
262- const onInputChange = useCallback (
261+ const onInputChange = useEventCallback (
263262 ( e : ChangeEvent < HTMLInputElement > ) => {
264263 const inputValue = e . target . value ;
265264 setInputValue ( inputValue ) ;
266265 onChange ?.( e ) ;
267- } ,
268- [ onChange ]
266+ }
269267 ) ;
270268
271- const onInputBlur = useCallback (
269+ const onInputBlur = useEventCallback (
272270 ( e : FocusEvent < HTMLInputElement | HTMLButtonElement > ) => {
273271 if ( e . target . tagName . toLowerCase ( ) === 'input' ) {
274272 setValueFromInput ( e . target . value ) ;
@@ -279,46 +277,42 @@ export const DateInput = createFC<DateInputProps, { isFormElement: boolean }>(
279277 onComplete ?.( ) ;
280278 }
281279 } , 10 ) ;
282- } ,
283- [ setValueFromInput , isFocusedInComponent , onBlur , onComplete ]
280+ }
284281 ) ;
285282
286- const onDatepickerSelect = useCallback (
287- ( dvalue : string ) => {
288- const value = moment ( dvalue ) . format ( valueFormat ) ;
289- setValue ( value ) ;
290- setInputValue ( null ) ;
291- setTimeout ( ( ) => {
292- setOpened ( false ) ;
293- const inputEl = inputElRef . current ;
294- if ( inputEl ) {
295- inputEl . focus ( ) ;
296- inputEl . select ( ) ;
297- }
298- onComplete ?.( ) ;
299- } , 200 ) ;
300- } ,
301- [ valueFormat , setValue , setOpened , onComplete ]
302- ) ;
283+ const onDatepickerSelect = useEventCallback ( ( dvalue : string ) => {
284+ const value = moment ( dvalue ) . format ( valueFormat ) ;
285+ setValue ( value ) ;
286+ setInputValue ( null ) ;
287+ setTimeout ( ( ) => {
288+ setOpened ( false ) ;
289+ const inputEl = inputElRef . current ;
290+ if ( inputEl ) {
291+ inputEl . focus ( ) ;
292+ inputEl . select ( ) ;
293+ }
294+ onComplete ?.( ) ;
295+ } , 200 ) ;
296+ } ) ;
303297
304- const onDatepickerBlur = useCallback ( ( ) => {
298+ const onDatepickerBlur = useEventCallback ( ( ) => {
305299 setOpened ( false ) ;
306300 setTimeout ( ( ) => {
307301 if ( ! isFocusedInComponent ( ) ) {
308302 onBlur ?.( ) ;
309303 onComplete ?.( ) ;
310304 }
311305 } , 500 ) ;
312- } , [ setOpened , isFocusedInComponent , onBlur , onComplete ] ) ;
306+ } ) ;
313307
314- const onDatepickerClose = useCallback ( ( ) => {
308+ const onDatepickerClose = useEventCallback ( ( ) => {
315309 setOpened ( false ) ;
316310 const inputEl = inputElRef . current ;
317311 if ( inputEl ) {
318312 inputEl . focus ( ) ;
319313 inputEl . select ( ) ;
320314 }
321- } , [ setOpened ] ) ;
315+ } ) ;
322316
323317 const formElemProps = { id, cols, label, required, error } ;
324318 return (
0 commit comments