@@ -64,7 +64,7 @@ type ComboboxOptionDataRef<T> = MutableRefObject<{
64
64
} >
65
65
66
66
interface StateDefinition < T > {
67
- dataRef : MutableRefObject < Data >
67
+ dataRef : MutableRefObject < _Data >
68
68
69
69
comboboxState : ComboboxState
70
70
@@ -73,7 +73,7 @@ interface StateDefinition<T> {
73
73
activationTrigger : ActivationTrigger
74
74
}
75
75
76
- enum Command {
76
+ enum ActionTypes {
77
77
OpenCombobox ,
78
78
CloseCombobox ,
79
79
@@ -112,26 +112,30 @@ function adjustOrderedState<T>(
112
112
}
113
113
}
114
114
115
- type Commands < T > =
116
- | { type : Command . CloseCombobox }
117
- | { type : Command . OpenCombobox }
118
- | { type : Command . GoToOption ; focus : Focus . Specific ; id : string ; trigger ?: ActivationTrigger }
119
- | { type : Command . GoToOption ; focus : Exclude < Focus , Focus . Specific > ; trigger ?: ActivationTrigger }
120
- | { type : Command . RegisterOption ; id : string ; dataRef : ComboboxOptionDataRef < T > }
121
- | { type : Command . UnregisterOption ; id : string }
115
+ type Actions < T > =
116
+ | { type : ActionTypes . CloseCombobox }
117
+ | { type : ActionTypes . OpenCombobox }
118
+ | { type : ActionTypes . GoToOption ; focus : Focus . Specific ; id : string ; trigger ?: ActivationTrigger }
119
+ | {
120
+ type : ActionTypes . GoToOption
121
+ focus : Exclude < Focus , Focus . Specific >
122
+ trigger ?: ActivationTrigger
123
+ }
124
+ | { type : ActionTypes . RegisterOption ; id : string ; dataRef : ComboboxOptionDataRef < T > }
125
+ | { type : ActionTypes . UnregisterOption ; id : string }
122
126
123
127
let reducers : {
124
- [ P in Command ] : < T > (
128
+ [ P in ActionTypes ] : < T > (
125
129
state : StateDefinition < T > ,
126
- command : Extract < Commands < T > , { type : P } >
130
+ action : Extract < Actions < T > , { type : P } >
127
131
) => StateDefinition < T >
128
132
} = {
129
- [ Command . CloseCombobox ] ( state ) {
133
+ [ ActionTypes . CloseCombobox ] ( state ) {
130
134
if ( state . dataRef . current . disabled ) return state
131
135
if ( state . comboboxState === ComboboxState . Closed ) return state
132
136
return { ...state , activeOptionIndex : null , comboboxState : ComboboxState . Closed }
133
137
} ,
134
- [ Command . OpenCombobox ] ( state ) {
138
+ [ ActionTypes . OpenCombobox ] ( state ) {
135
139
if ( state . dataRef . current . disabled ) return state
136
140
if ( state . comboboxState === ComboboxState . Open ) return state
137
141
@@ -146,7 +150,7 @@ let reducers: {
146
150
147
151
return { ...state , comboboxState : ComboboxState . Open , activeOptionIndex }
148
152
} ,
149
- [ Command . GoToOption ] ( state , action ) {
153
+ [ ActionTypes . GoToOption ] ( state , action ) {
150
154
if ( state . dataRef . current . disabled ) return state
151
155
if (
152
156
state . dataRef . current . optionsRef . current &&
@@ -185,7 +189,7 @@ let reducers: {
185
189
activationTrigger : action . trigger ?? ActivationTrigger . Other ,
186
190
}
187
191
} ,
188
- [ Command . RegisterOption ] : ( state , action ) => {
192
+ [ ActionTypes . RegisterOption ] : ( state , action ) => {
189
193
let option = { id : action . id , dataRef : action . dataRef }
190
194
let adjustedState = adjustOrderedState ( state , ( options ) => [ ...options , option ] )
191
195
@@ -208,7 +212,7 @@ let reducers: {
208
212
209
213
return nextState
210
214
} ,
211
- [ Command . UnregisterOption ] : ( state , action ) => {
215
+ [ ActionTypes . UnregisterOption ] : ( state , action ) => {
212
216
let adjustedState = adjustOrderedState ( state , ( options ) => {
213
217
let idx = options . findIndex ( ( a ) => a . id === action . id )
214
218
if ( idx !== - 1 ) options . splice ( idx , 1 )
@@ -244,7 +248,7 @@ function useActions(component: string) {
244
248
}
245
249
return context
246
250
}
247
- type Actions = ReturnType < typeof useActions >
251
+ type _Actions = ReturnType < typeof useActions >
248
252
249
253
let ComboboxDataContext = createContext <
250
254
| ( {
@@ -283,9 +287,9 @@ function useData(component: string) {
283
287
}
284
288
return context
285
289
}
286
- type Data = ReturnType < typeof useData >
290
+ type _Data = ReturnType < typeof useData >
287
291
288
- function stateReducer < T > ( state : StateDefinition < T > , action : Commands < T > ) {
292
+ function stateReducer < T > ( state : StateDefinition < T > , action : Actions < T > ) {
289
293
return match ( action . type , reducers , state , action )
290
294
}
291
295
@@ -342,13 +346,13 @@ let ComboboxRoot = forwardRefWithAs(function Combobox<
342
346
343
347
let defaultToFirstOption = useRef ( false )
344
348
345
- let optionsPropsRef = useRef < Data [ 'optionsPropsRef' ] [ 'current' ] > ( { static : false , hold : false } )
346
- let inputPropsRef = useRef < Data [ 'inputPropsRef' ] [ 'current' ] > ( { displayValue : undefined } )
349
+ let optionsPropsRef = useRef < _Data [ 'optionsPropsRef' ] [ 'current' ] > ( { static : false , hold : false } )
350
+ let inputPropsRef = useRef < _Data [ 'inputPropsRef' ] [ 'current' ] > ( { displayValue : undefined } )
347
351
348
- let labelRef = useRef < Data [ 'labelRef' ] [ 'current' ] > ( null )
349
- let inputRef = useRef < Data [ 'inputRef' ] [ 'current' ] > ( null )
350
- let buttonRef = useRef < Data [ 'buttonRef' ] [ 'current' ] > ( null )
351
- let optionsRef = useRef < Data [ 'optionsRef' ] [ 'current' ] > ( null )
352
+ let labelRef = useRef < _Data [ 'labelRef' ] [ 'current' ] > ( null )
353
+ let inputRef = useRef < _Data [ 'inputRef' ] [ 'current' ] > ( null )
354
+ let buttonRef = useRef < _Data [ 'buttonRef' ] [ 'current' ] > ( null )
355
+ let optionsRef = useRef < _Data [ 'optionsRef' ] [ 'current' ] > ( null )
352
356
353
357
let compare = useEvent (
354
358
typeof by === 'string'
@@ -369,7 +373,7 @@ let ComboboxRoot = forwardRefWithAs(function Combobox<
369
373
[ value ]
370
374
)
371
375
372
- let data = useMemo < Data > (
376
+ let data = useMemo < _Data > (
373
377
( ) => ( {
374
378
...state ,
375
379
optionsPropsRef,
@@ -414,7 +418,7 @@ let ComboboxRoot = forwardRefWithAs(function Combobox<
414
418
useOutsideClick ( [ data . buttonRef , data . inputRef , data . optionsRef ] , ( ) => {
415
419
if ( data . comboboxState !== ComboboxState . Open ) return
416
420
417
- dispatch ( { type : Command . CloseCombobox } )
421
+ dispatch ( { type : ActionTypes . CloseCombobox } )
418
422
} )
419
423
420
424
let slot = useMemo < ComboboxRenderPropArg < TType > > (
@@ -459,33 +463,33 @@ let ComboboxRoot = forwardRefWithAs(function Combobox<
459
463
460
464
// It could happen that the `activeOptionIndex` stored in state is actually null,
461
465
// but we are getting the fallback active option back instead.
462
- dispatch ( { type : Command . GoToOption , focus : Focus . Specific , id } )
466
+ dispatch ( { type : ActionTypes . GoToOption , focus : Focus . Specific , id } )
463
467
}
464
468
} )
465
469
466
470
let openCombobox = useEvent ( ( ) => {
467
- dispatch ( { type : Command . OpenCombobox } )
471
+ dispatch ( { type : ActionTypes . OpenCombobox } )
468
472
defaultToFirstOption . current = true
469
473
} )
470
474
471
475
let closeCombobox = useEvent ( ( ) => {
472
- dispatch ( { type : Command . CloseCombobox } )
476
+ dispatch ( { type : ActionTypes . CloseCombobox } )
473
477
defaultToFirstOption . current = false
474
478
} )
475
479
476
480
let goToOption = useEvent ( ( focus , id , trigger ) => {
477
481
defaultToFirstOption . current = false
478
482
479
483
if ( focus === Focus . Specific ) {
480
- return dispatch ( { type : Command . GoToOption , focus : Focus . Specific , id : id ! , trigger } )
484
+ return dispatch ( { type : ActionTypes . GoToOption , focus : Focus . Specific , id : id ! , trigger } )
481
485
}
482
486
483
- return dispatch ( { type : Command . GoToOption , focus, trigger } )
487
+ return dispatch ( { type : ActionTypes . GoToOption , focus, trigger } )
484
488
} )
485
489
486
490
let registerOption = useEvent ( ( id , dataRef ) => {
487
- dispatch ( { type : Command . RegisterOption , id, dataRef } )
488
- return ( ) => dispatch ( { type : Command . UnregisterOption , id } )
491
+ dispatch ( { type : ActionTypes . RegisterOption , id, dataRef } )
492
+ return ( ) => dispatch ( { type : ActionTypes . UnregisterOption , id } )
489
493
} )
490
494
491
495
let onChange = useEvent ( ( value : unknown ) => {
@@ -508,7 +512,7 @@ let ComboboxRoot = forwardRefWithAs(function Combobox<
508
512
} )
509
513
} )
510
514
511
- let actions = useMemo < Actions > (
515
+ let actions = useMemo < _Actions > (
512
516
( ) => ( {
513
517
onChange,
514
518
registerOption,
0 commit comments