@@ -9,8 +9,9 @@ import { useEvent, useEventCallback, useId, useRefExtra, useResize } from '@reac
99import { LoadingOutlined } from '@react-devui/icons' ;
1010import { findNested , getClassName , getOriginalSize , getVerticalSidePosition } from '@react-devui/utils' ;
1111
12- import { useMaxIndex , useDValue , useFocusVisible } from '../../hooks' ;
12+ import { useMaxIndex , useDValue } from '../../hooks' ;
1313import { cloneHTMLElement , registerComponentMate , TTANSITION_DURING_POPUP , WINDOW_SPACE } from '../../utils' ;
14+ import { DFocusVisible } from '../_focus-visible' ;
1415import { DComboboxKeyboard } from '../_keyboard' ;
1516import { DTransition } from '../_transition' ;
1617import { DInput } from '../input' ;
@@ -88,7 +89,7 @@ function AutoComplete<T extends DAutoCompleteItem>(
8889
8990 const [ visible , changeVisible ] = useDValue < boolean > ( false , dVisible , onVisibleChange ) ;
9091
91- const [ focusVisible , renderFocusVisible ] = useFocusVisible ( ) ;
92+ const [ focusVisible , setFocusVisible ] = useState ( false ) ;
9293
9394 const maxZIndex = useMaxIndex ( visible ) ;
9495
@@ -177,77 +178,81 @@ function AutoComplete<T extends DAutoCompleteItem>(
177178
178179 return (
179180 < >
180- < DComboboxKeyboard
181- dVisible = { visible }
182- dEditable
183- dHasSub = { false }
184- onVisibleChange = { changeVisible }
185- onFocusChange = { ( key ) => {
186- switch ( key ) {
187- case 'next' :
188- changeFocusItem ( vsRef . current ?. scrollByStep ( 1 ) ) ;
189- break ;
190-
191- case 'prev' :
192- changeFocusItem ( vsRef . current ?. scrollByStep ( - 1 ) ) ;
193- break ;
194-
195- case 'first' :
196- changeFocusItem ( vsRef . current ?. scrollToStart ( ) ) ;
197- break ;
198-
199- case 'last' :
200- changeFocusItem ( vsRef . current ?. scrollToEnd ( ) ) ;
201- break ;
202-
203- default :
204- break ;
205- }
206- } }
207- >
208- { ( { render : renderComboboxKeyboard } ) => {
209- const renderInput = ( el : React . ReactElement < React . HTMLAttributes < HTMLElement > > , props ?: React . HTMLAttributes < HTMLElement > ) =>
210- renderFocusVisible (
211- renderComboboxKeyboard (
212- cloneHTMLElement ( el , {
213- ...props ,
214- role : 'combobox' ,
215- 'aria-autocomplete' : 'list' ,
216- 'aria-expanded' : visible ,
217- 'aria-controls' : listId ,
218- onBlur : ( e ) => {
219- el . props . onBlur ?.( e ) ;
220-
221- changeVisible ( false ) ;
222- } ,
223- onClick : ( e ) => {
224- el . props . onClick ?.( e ) ;
225-
226- changeVisible ( true ) ;
227- } ,
228- onKeyDown : ( e ) => {
229- el . props . onKeyDown ?.( e ) ;
230-
231- if ( e . code === 'Enter' && visible && focusItem ) {
232- changeVisible ( false ) ;
233- onItemClick ?.( focusItem . value , focusItem ) ;
234- }
235- } ,
236- } )
237- )
238- ) ;
239-
240- const boxSelector = { [ 'data-auto-complete-box' as string ] : uniqueId } ;
241- if ( child . type === DInput ) {
242- return React . cloneElement < DInputProps > ( child , {
243- ...boxSelector ,
244- dInputRender : renderInput ,
245- } ) ;
246- } else {
247- return renderInput ( child , boxSelector ) ;
248- }
249- } }
250- </ DComboboxKeyboard >
181+ < DFocusVisible onFocusVisibleChange = { setFocusVisible } >
182+ { ( { render : renderFocusVisible } ) => (
183+ < DComboboxKeyboard
184+ dVisible = { visible }
185+ dEditable
186+ dHasSub = { false }
187+ onVisibleChange = { changeVisible }
188+ onFocusChange = { ( key ) => {
189+ switch ( key ) {
190+ case 'next' :
191+ changeFocusItem ( vsRef . current ?. scrollByStep ( 1 ) ) ;
192+ break ;
193+
194+ case 'prev' :
195+ changeFocusItem ( vsRef . current ?. scrollByStep ( - 1 ) ) ;
196+ break ;
197+
198+ case 'first' :
199+ changeFocusItem ( vsRef . current ?. scrollToStart ( ) ) ;
200+ break ;
201+
202+ case 'last' :
203+ changeFocusItem ( vsRef . current ?. scrollToEnd ( ) ) ;
204+ break ;
205+
206+ default :
207+ break ;
208+ }
209+ } }
210+ >
211+ { ( { render : renderComboboxKeyboard } ) => {
212+ const renderInput = ( el : React . ReactElement < React . HTMLAttributes < HTMLElement > > , props ?: React . HTMLAttributes < HTMLElement > ) =>
213+ renderFocusVisible (
214+ renderComboboxKeyboard (
215+ cloneHTMLElement ( el , {
216+ ...props ,
217+ role : 'combobox' ,
218+ 'aria-autocomplete' : 'list' ,
219+ 'aria-expanded' : visible ,
220+ 'aria-controls' : listId ,
221+ onBlur : ( e ) => {
222+ el . props . onBlur ?.( e ) ;
223+
224+ changeVisible ( false ) ;
225+ } ,
226+ onClick : ( e ) => {
227+ el . props . onClick ?.( e ) ;
228+
229+ changeVisible ( true ) ;
230+ } ,
231+ onKeyDown : ( e ) => {
232+ el . props . onKeyDown ?.( e ) ;
233+
234+ if ( e . code === 'Enter' && visible && focusItem ) {
235+ changeVisible ( false ) ;
236+ onItemClick ?.( focusItem . value , focusItem ) ;
237+ }
238+ } ,
239+ } )
240+ )
241+ ) ;
242+
243+ const boxSelector = { [ 'data-auto-complete-box' as string ] : uniqueId } ;
244+ if ( child . type === DInput ) {
245+ return React . cloneElement < DInputProps > ( child , {
246+ ...boxSelector ,
247+ dInputRender : renderInput ,
248+ } ) ;
249+ } else {
250+ return renderInput ( child , boxSelector ) ;
251+ }
252+ } }
253+ </ DComboboxKeyboard >
254+ ) }
255+ </ DFocusVisible >
251256 { containerRef . current &&
252257 ReactDOM . createPortal (
253258 < DTransition
0 commit comments