forked from ultraworkers/claw-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-select-state.ts
More file actions
157 lines (130 loc) · 2.79 KB
/
Copy pathuse-select-state.ts
File metadata and controls
157 lines (130 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { useCallback, useState } from 'react'
import type { OptionWithDescription } from './select.js'
import { useSelectNavigation } from './use-select-navigation.js'
export type UseSelectStateProps<T> = {
/**
* Number of items to display.
*
* @default 5
*/
visibleOptionCount?: number
/**
* Options.
*/
options: OptionWithDescription<T>[]
/**
* Initially selected option's value.
*/
defaultValue?: T
/**
* Callback for selecting an option.
*/
onChange?: (value: T) => void
/**
* Callback for canceling the select.
*/
onCancel?: () => void
/**
* Callback for focusing an option.
*/
onFocus?: (value: T) => void
/**
* Value to focus
*/
focusValue?: T
}
export type SelectState<T> = {
/**
* Value of the currently focused option.
*/
focusedValue: T | undefined
/**
* 1-based index of the focused option in the full list.
* Returns 0 if no option is focused.
*/
focusedIndex: number
/**
* Index of the first visible option.
*/
visibleFromIndex: number
/**
* Index of the last visible option.
*/
visibleToIndex: number
/**
* Value of the selected option.
*/
value: T | undefined
/**
* All options.
*/
options: OptionWithDescription<T>[]
/**
* Visible options.
*/
visibleOptions: Array<OptionWithDescription<T> & { index: number }>
/**
* Whether the focused option is an input type.
*/
isInInput: boolean
/**
* Focus next option and scroll the list down, if needed.
*/
focusNextOption: () => void
/**
* Focus previous option and scroll the list up, if needed.
*/
focusPreviousOption: () => void
/**
* Focus next page and scroll the list down by a page.
*/
focusNextPage: () => void
/**
* Focus previous page and scroll the list up by a page.
*/
focusPreviousPage: () => void
/**
* Focus a specific option by value.
*/
focusOption: (value: T | undefined) => void
/**
* Select currently focused option.
*/
selectFocusedOption: () => void
/**
* Callback for selecting an option.
*/
onChange?: (value: T) => void
/**
* Callback for canceling the select.
*/
onCancel?: () => void
}
export function useSelectState<T>({
visibleOptionCount = 5,
options,
defaultValue,
onChange,
onCancel,
onFocus,
focusValue,
}: UseSelectStateProps<T>): SelectState<T> {
const [value, setValue] = useState<T | undefined>(defaultValue)
const navigation = useSelectNavigation<T>({
visibleOptionCount,
options,
initialFocusValue: undefined,
onFocus,
focusValue,
})
const selectFocusedOption = useCallback(() => {
setValue(navigation.focusedValue)
}, [navigation.focusedValue])
return {
...navigation,
value,
selectFocusedOption,
onChange,
onCancel,
}
}