Skip to content

Commit 2396c49

Browse files
committed
small cleanup
In the cleanup PR, we added the `Data` and `Actions` type, but we already had a `Actions` type so had to rename it to something. Chose `Command` but this is now inconsistent with the rest of the codebase. Instead, let's revert that change and use these shorthands: - `Data` -> `_Data` - `Actions` -> `_Actions` - `Commands` -> `Actions` - `CommandTypes` -> `ActionTypes` The `_` prefix is a little bit strange, but it is a private type and not exposed so fine for now.
1 parent dafcc2d commit 2396c49

File tree

1 file changed

+39
-35
lines changed
  • packages/@headlessui-react/src/components/combobox

1 file changed

+39
-35
lines changed

packages/@headlessui-react/src/components/combobox/combobox.tsx

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ type ComboboxOptionDataRef<T> = MutableRefObject<{
6464
}>
6565

6666
interface StateDefinition<T> {
67-
dataRef: MutableRefObject<Data>
67+
dataRef: MutableRefObject<_Data>
6868

6969
comboboxState: ComboboxState
7070

@@ -73,7 +73,7 @@ interface StateDefinition<T> {
7373
activationTrigger: ActivationTrigger
7474
}
7575

76-
enum Command {
76+
enum ActionTypes {
7777
OpenCombobox,
7878
CloseCombobox,
7979

@@ -112,26 +112,30 @@ function adjustOrderedState<T>(
112112
}
113113
}
114114

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 }
122126

123127
let reducers: {
124-
[P in Command]: <T>(
128+
[P in ActionTypes]: <T>(
125129
state: StateDefinition<T>,
126-
command: Extract<Commands<T>, { type: P }>
130+
action: Extract<Actions<T>, { type: P }>
127131
) => StateDefinition<T>
128132
} = {
129-
[Command.CloseCombobox](state) {
133+
[ActionTypes.CloseCombobox](state) {
130134
if (state.dataRef.current.disabled) return state
131135
if (state.comboboxState === ComboboxState.Closed) return state
132136
return { ...state, activeOptionIndex: null, comboboxState: ComboboxState.Closed }
133137
},
134-
[Command.OpenCombobox](state) {
138+
[ActionTypes.OpenCombobox](state) {
135139
if (state.dataRef.current.disabled) return state
136140
if (state.comboboxState === ComboboxState.Open) return state
137141

@@ -146,7 +150,7 @@ let reducers: {
146150

147151
return { ...state, comboboxState: ComboboxState.Open, activeOptionIndex }
148152
},
149-
[Command.GoToOption](state, action) {
153+
[ActionTypes.GoToOption](state, action) {
150154
if (state.dataRef.current.disabled) return state
151155
if (
152156
state.dataRef.current.optionsRef.current &&
@@ -185,7 +189,7 @@ let reducers: {
185189
activationTrigger: action.trigger ?? ActivationTrigger.Other,
186190
}
187191
},
188-
[Command.RegisterOption]: (state, action) => {
192+
[ActionTypes.RegisterOption]: (state, action) => {
189193
let option = { id: action.id, dataRef: action.dataRef }
190194
let adjustedState = adjustOrderedState(state, (options) => [...options, option])
191195

@@ -208,7 +212,7 @@ let reducers: {
208212

209213
return nextState
210214
},
211-
[Command.UnregisterOption]: (state, action) => {
215+
[ActionTypes.UnregisterOption]: (state, action) => {
212216
let adjustedState = adjustOrderedState(state, (options) => {
213217
let idx = options.findIndex((a) => a.id === action.id)
214218
if (idx !== -1) options.splice(idx, 1)
@@ -244,7 +248,7 @@ function useActions(component: string) {
244248
}
245249
return context
246250
}
247-
type Actions = ReturnType<typeof useActions>
251+
type _Actions = ReturnType<typeof useActions>
248252

249253
let ComboboxDataContext = createContext<
250254
| ({
@@ -283,9 +287,9 @@ function useData(component: string) {
283287
}
284288
return context
285289
}
286-
type Data = ReturnType<typeof useData>
290+
type _Data = ReturnType<typeof useData>
287291

288-
function stateReducer<T>(state: StateDefinition<T>, action: Commands<T>) {
292+
function stateReducer<T>(state: StateDefinition<T>, action: Actions<T>) {
289293
return match(action.type, reducers, state, action)
290294
}
291295

@@ -342,13 +346,13 @@ let ComboboxRoot = forwardRefWithAs(function Combobox<
342346

343347
let defaultToFirstOption = useRef(false)
344348

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 })
347351

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)
352356

353357
let compare = useEvent(
354358
typeof by === 'string'
@@ -369,7 +373,7 @@ let ComboboxRoot = forwardRefWithAs(function Combobox<
369373
[value]
370374
)
371375

372-
let data = useMemo<Data>(
376+
let data = useMemo<_Data>(
373377
() => ({
374378
...state,
375379
optionsPropsRef,
@@ -414,7 +418,7 @@ let ComboboxRoot = forwardRefWithAs(function Combobox<
414418
useOutsideClick([data.buttonRef, data.inputRef, data.optionsRef], () => {
415419
if (data.comboboxState !== ComboboxState.Open) return
416420

417-
dispatch({ type: Command.CloseCombobox })
421+
dispatch({ type: ActionTypes.CloseCombobox })
418422
})
419423

420424
let slot = useMemo<ComboboxRenderPropArg<TType>>(
@@ -459,33 +463,33 @@ let ComboboxRoot = forwardRefWithAs(function Combobox<
459463

460464
// It could happen that the `activeOptionIndex` stored in state is actually null,
461465
// 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 })
463467
}
464468
})
465469

466470
let openCombobox = useEvent(() => {
467-
dispatch({ type: Command.OpenCombobox })
471+
dispatch({ type: ActionTypes.OpenCombobox })
468472
defaultToFirstOption.current = true
469473
})
470474

471475
let closeCombobox = useEvent(() => {
472-
dispatch({ type: Command.CloseCombobox })
476+
dispatch({ type: ActionTypes.CloseCombobox })
473477
defaultToFirstOption.current = false
474478
})
475479

476480
let goToOption = useEvent((focus, id, trigger) => {
477481
defaultToFirstOption.current = false
478482

479483
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 })
481485
}
482486

483-
return dispatch({ type: Command.GoToOption, focus, trigger })
487+
return dispatch({ type: ActionTypes.GoToOption, focus, trigger })
484488
})
485489

486490
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 })
489493
})
490494

491495
let onChange = useEvent((value: unknown) => {
@@ -508,7 +512,7 @@ let ComboboxRoot = forwardRefWithAs(function Combobox<
508512
})
509513
})
510514

511-
let actions = useMemo<Actions>(
515+
let actions = useMemo<_Actions>(
512516
() => ({
513517
onChange,
514518
registerOption,

0 commit comments

Comments
 (0)