@@ -333,39 +333,47 @@ const Input = React.forwardRef<InputRef, InputProps>((props, ref) => {
333333 // ======================== Format ========================
334334 const rafRef = React . useRef < number > ( ) ;
335335
336- useLayoutEffect ( ( ) => {
337- if ( ! focused || ! format || mouseDownRef . current ) {
338- return ;
339- }
336+ useLayoutEffect (
337+ function ( ) {
338+ if ( ! focused || ! format ) {
339+ return ;
340+ }
340341
341- // Reset with format if not match
342- if ( ! maskFormat . match ( inputValue ) ) {
343- triggerInputChange ( format ) ;
344- return ;
345- }
342+ // Reset with format if not match (always apply when focused so mask works when focusing by mousedown)
343+ if ( ! maskFormat . match ( inputValue ) ) {
344+ triggerInputChange ( format ) ;
345+ return ;
346+ }
346347
347- // Match the selection range
348- inputRef . current . setSelectionRange ( selectionStart , selectionEnd ) ;
348+ // When mousedown get focus, defer selection to mouseUp so click position is used
349+ if ( mouseDownRef . current ) {
350+ return ;
351+ }
349352
350- // Chrome has the bug anchor position looks not correct but actually correct
351- rafRef . current = raf ( ( ) => {
353+ // Match the selection range
352354 inputRef . current . setSelectionRange ( selectionStart , selectionEnd ) ;
353- } ) ;
354355
355- return ( ) => {
356- raf . cancel ( rafRef . current ) ;
357- } ;
358- } , [
359- maskFormat ,
360- format ,
361- focused ,
362- inputValue ,
363- focusCellIndex ,
364- selectionStart ,
365- selectionEnd ,
366- forceSelectionSyncMark ,
367- triggerInputChange ,
368- ] ) ;
356+ // Chrome has the bug anchor position looks not correct but actually correct
357+ rafRef . current = raf ( ( ) => {
358+ inputRef . current . setSelectionRange ( selectionStart , selectionEnd ) ;
359+ } ) ;
360+
361+ return ( ) => {
362+ raf . cancel ( rafRef . current ) ;
363+ } ;
364+ } ,
365+ [
366+ maskFormat ,
367+ format ,
368+ focused ,
369+ inputValue ,
370+ focusCellIndex ,
371+ selectionStart ,
372+ selectionEnd ,
373+ forceSelectionSyncMark ,
374+ triggerInputChange ,
375+ ] ,
376+ ) ;
369377
370378 // ======================== Render ========================
371379 // Input props for format
0 commit comments