@@ -67,47 +67,49 @@ export default function useFocusManager(
6767 const onChange = useEventCallback ( opts . onChange )
6868 const isDisabled = useEventCallback ( opts . isDisabled )
6969
70- const handleFocusChange = useCallback (
70+ const handleChange = useCallback (
7171 ( focused : boolean , event : React . FocusEvent ) => {
72- if ( event && event . persist ) event . persist ( )
73-
74- if ( willHandle && willHandle ( focused , event ) === false ) return
72+ if ( focused !== lastFocused . current ) {
73+ didHandle ?.( focused , event )
7574
76- clearTimeout ( handle . current )
77- handle . current = window . setTimeout ( ( ) => {
78- if ( focused !== lastFocused . current ) {
79- if ( didHandle ) didHandle ( focused , event )
80-
81- // only fire a change when unmounted if its a blur
82- if ( isMounted ( ) || ! focused ) {
83- lastFocused . current = focused
84- onChange && onChange ( focused , event )
85- }
75+ // only fire a change when unmounted if its a blur
76+ if ( isMounted ( ) || ! focused ) {
77+ lastFocused . current = focused
78+ onChange ?.( focused , event )
8679 }
87- } )
80+ }
8881 } ,
89- [ isMounted , willHandle , didHandle , onChange , lastFocused ] ,
82+ [ isMounted , didHandle , onChange , lastFocused ] ,
9083 )
9184
92- const handleBlur = useCallback (
93- ( event ) => {
94- if ( ! isDisabled ( ) ) handleFocusChange ( false , event )
95- } ,
96- [ handleFocusChange , isDisabled ] ,
97- )
85+ const handleFocusChange = useCallback (
86+ ( focused : boolean , event : React . FocusEvent ) => {
87+ if ( isDisabled ( ) ) return
88+ if ( event && event . persist ) event . persist ( )
89+
90+ if ( willHandle ?.( focused , event ) === false ) {
91+ return
92+ }
93+ clearTimeout ( handle . current )
9894
99- const handleFocus = useCallback (
100- ( event ) => {
101- if ( ! isDisabled ( ) ) handleFocusChange ( true , event )
95+ if ( focused ) {
96+ handleChange ( focused , event )
97+ } else {
98+ handle . current = window . setTimeout ( ( ) => handleChange ( focused , event ) )
99+ }
102100 } ,
103- [ handleFocusChange , isDisabled ] ,
101+ [ willHandle , handleChange ] ,
104102 )
105103
106104 return useMemo (
107105 ( ) => ( {
108- onBlur : handleBlur ,
109- onFocus : handleFocus ,
106+ onBlur : ( event : React . FocusEvent ) => {
107+ handleFocusChange ( false , event )
108+ } ,
109+ onFocus : ( event : React . FocusEvent ) => {
110+ handleFocusChange ( true , event )
111+ } ,
110112 } ) ,
111- [ handleBlur , handleFocus ] ,
113+ [ handleFocusChange ] ,
112114 )
113115}
0 commit comments