@@ -10,14 +10,15 @@ import {
1010 useIsomorphicLayoutEffect ,
1111 useOnChange ,
1212} from "../../../utils" ;
13+ import type { SelectContextValue } from "../../context" ;
1314
1415type Props < T extends HTMLElement > = {
1516 disabled : boolean ;
1617 readOnly : boolean ;
1718 autoFocus : boolean ;
1819 searchable : boolean ;
19- activeDescendant : HTMLElement | null ;
2020 listOpenState : boolean ;
21+ activeDescendant : SelectContextValue [ "activeDescendant" ] ;
2122 onClick ?: React . MouseEventHandler < T > ;
2223 onBlur ?: React . FocusEventHandler < T > ;
2324 onFocus ?: React . FocusEventHandler < T > ;
@@ -26,10 +27,15 @@ type Props<T extends HTMLElement> = {
2627 onEscapeKeyDown ?: React . KeyboardEventHandler < T > ;
2728 onBackspaceKeyDown ?: React . KeyboardEventHandler < T > ;
2829 onInputChange ?: React . ChangeEventHandler < HTMLInputElement > ;
29- getListItems : ( ) => HTMLElement [ ] ;
30- onFilteredEntities : ( entities : null | string [ ] ) => void ;
30+ getOptionElements : ( ) => HTMLElement [ ] ;
31+ getOptionsInfo : SelectContextValue [ "getOptions" ] ;
32+ onFilteredEntities : (
33+ entities : SelectContextValue [ "filteredEntities" ] ,
34+ ) => void ;
3135 onListOpenChange : ( nextListOpenState : boolean ) => void ;
32- onActiveDescendantChange : ( nextActiveDescendant : HTMLElement | null ) => void ;
36+ onActiveDescendantChange : (
37+ nextActiveDescendant : SelectContextValue [ "activeDescendant" ] ,
38+ ) => void ;
3339} ;
3440
3541export const useComboboxBase = < T extends HTMLElement > ( props : Props < T > ) => {
@@ -42,7 +48,8 @@ export const useComboboxBase = <T extends HTMLElement>(props: Props<T>) => {
4248 onPrintableKeyDown,
4349 onEscapeKeyDown,
4450 onBackspaceKeyDown,
45- getListItems,
51+ getOptionElements,
52+ getOptionsInfo,
4653 onActiveDescendantChange,
4754 onListOpenChange,
4855 onFilteredEntities,
@@ -65,7 +72,7 @@ export const useComboboxBase = <T extends HTMLElement>(props: Props<T>) => {
6572
6673 const jumpToChar = useJumpToChar ( {
6774 activeDescendantElement : activeDescendant ,
68- getListItems,
75+ getListItems : getOptionElements ,
6976 onActiveDescendantElementChange : onActiveDescendantChange ,
7077 } ) ;
7178
@@ -140,12 +147,14 @@ export const useComboboxBase = <T extends HTMLElement>(props: Props<T>) => {
140147 } ) ;
141148
142149 const handleKeyDown = useEventCallback < React . KeyboardEvent < T > > ( event => {
143- if ( disabled || readOnly || ! isMounted ( ) ) {
150+ if ( disabled || ! isMounted ( ) ) {
144151 event . preventDefault ( ) ;
145152
146153 return ;
147154 }
148155
156+ if ( readOnly ) return ;
157+
149158 const getAvailableItem = (
150159 items : ( HTMLElement | null ) [ ] ,
151160 idx : number ,
@@ -195,15 +204,15 @@ export const useComboboxBase = <T extends HTMLElement>(props: Props<T>) => {
195204 onListOpenChange ( true ) ;
196205 } ) ;
197206
198- const items = getListItems ( ) ;
207+ const items = getOptionElements ( ) ;
199208 const nextActive = getInitialAvailableItem ( items , true ) ;
200209
201210 onActiveDescendantChange ( nextActive ) ;
202211
203212 break ;
204213 }
205214
206- const items = getListItems ( ) ;
215+ const items = getOptionElements ( ) ;
207216 let nextActive : HTMLElement | null = null ;
208217
209218 if ( activeDescendant ) {
@@ -219,22 +228,24 @@ export const useComboboxBase = <T extends HTMLElement>(props: Props<T>) => {
219228 }
220229
221230 case SystemKeys . UP : {
231+ if ( readOnly ) return ;
232+
222233 event . preventDefault ( ) ;
223234
224235 if ( ! listOpenState ) {
225236 flushSync ( ( ) => {
226237 onListOpenChange ( true ) ;
227238 } ) ;
228239
229- const items = getListItems ( ) ;
240+ const items = getOptionElements ( ) ;
230241 const nextActive = getInitialAvailableItem ( items , true ) ;
231242
232243 onActiveDescendantChange ( nextActive ) ;
233244
234245 break ;
235246 }
236247
237- const items = getListItems ( ) ;
248+ const items = getOptionElements ( ) ;
238249 let nextActive : HTMLElement | null = null ;
239250
240251 if ( activeDescendant ) {
@@ -257,7 +268,7 @@ export const useComboboxBase = <T extends HTMLElement>(props: Props<T>) => {
257268
258269 if ( ! listOpenState ) break ;
259270
260- const items = getListItems ( ) ;
271+ const items = getOptionElements ( ) ;
261272 const nextActive = getAvailableItem ( items , 0 , true ) ;
262273
263274 onActiveDescendantChange ( nextActive ) ;
@@ -270,7 +281,7 @@ export const useComboboxBase = <T extends HTMLElement>(props: Props<T>) => {
270281
271282 if ( ! listOpenState ) break ;
272283
273- const items = getListItems ( ) ;
284+ const items = getOptionElements ( ) ;
274285 const nextActive = getAvailableItem ( items , items . length - 1 , false ) ;
275286
276287 onActiveDescendantChange ( nextActive ) ;
@@ -280,6 +291,7 @@ export const useComboboxBase = <T extends HTMLElement>(props: Props<T>) => {
280291
281292 case SystemKeys . ESCAPE : {
282293 event . preventDefault ( ) ;
294+
283295 onEscapeKeyDown ?.( event ) ;
284296
285297 break ;
@@ -353,15 +365,15 @@ export const useComboboxBase = <T extends HTMLElement>(props: Props<T>) => {
353365
354366 const query = target . value ;
355367
356- const items = getListItems ( ) ;
368+ const options = getOptionsInfo ( ) ;
357369
358- const entities = items
359- . filter ( item => {
360- const text = item . textContent ? .toLowerCase ( ) ?? "" ;
370+ const entities = options
371+ . filter ( option => {
372+ const text = option . valueLabel . toLowerCase ( ) ;
361373
362374 return text . includes ( query . toLowerCase ( ) ) ;
363375 } )
364- . map ( item => item . getAttribute ( "data-entity" ) ?? "" ) ;
376+ . map ( option => option . value ) ;
365377
366378 onFilteredEntities ( entities ) ;
367379 onInputChange ?.( event ) ;
0 commit comments