@greenharness/greenrack-ui 7.1.2 → 7.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/Combobox/Combobox.d.ts +6 -2
- package/dist/components/Combobox/Combobox.d.ts.map +1 -1
- package/dist/components/Combobox/Combobox.stories.d.ts +23 -0
- package/dist/components/Combobox/Combobox.stories.d.ts.map +1 -1
- package/dist/components/Combobox/ComboboxContext.d.ts +4 -0
- package/dist/components/Combobox/ComboboxContext.d.ts.map +1 -1
- package/dist/components/Combobox/comboboxState.d.ts +4 -2
- package/dist/components/Combobox/comboboxState.d.ts.map +1 -1
- package/dist/components/Combobox/useComboboxRoot.d.ts.map +1 -1
- package/dist/index.js +3003 -2983
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/components/Combobox/Combobox.meta.ts +1 -1
- package/src/components/Combobox/Combobox.native.tsx +18 -4
- package/src/components/Combobox/Combobox.stories.tsx +355 -2
- package/src/components/Combobox/Combobox.tsx +54 -11
- package/src/components/Combobox/ComboboxContext.ts +4 -0
- package/src/components/Combobox/comboboxState.ts +19 -3
- package/src/components/Combobox/useComboboxRoot.ts +128 -47
|
@@ -9,20 +9,50 @@ import {matchesQuery, useComboboxState, type ComboboxStateOptions} from './combo
|
|
|
9
9
|
export function useComboboxRoot(options: ComboboxStateOptions) {
|
|
10
10
|
const {value, inputValue, setInputValue, open, setOpen, labels, registerLabel, chooseValue} =
|
|
11
11
|
useComboboxState(options)
|
|
12
|
-
const [highlightedId,
|
|
12
|
+
const [highlightedId, setHighlightedState] = useState<string | null>(null)
|
|
13
|
+
const highlightedRef = useRef<string | null>(null)
|
|
14
|
+
const setHighlightedId = useCallback((id: string | null) => {
|
|
15
|
+
highlightedRef.current = id
|
|
16
|
+
setHighlightedState(id)
|
|
17
|
+
}, [])
|
|
18
|
+
// The list opens with every option while the field shows the chosen
|
|
19
|
+
// option's label; the label only filters once it is edited.
|
|
20
|
+
const query = value && inputValue === labels[value] ? '' : inputValue
|
|
21
|
+
const blurTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
|
22
|
+
const refocusAfterChooseRef = useRef(false)
|
|
23
|
+
// The list's items mount after the open, so the highlight they should take
|
|
24
|
+
// is settled as they register.
|
|
25
|
+
const pendingHighlightRef = useRef(false)
|
|
26
|
+
// Synced after each commit: an option that mounts in the same commit as a
|
|
27
|
+
// value change would read the old one. Unreachable today (the options mount
|
|
28
|
+
// in the commit after the open); a controlled value changed while the list is
|
|
29
|
+
// open would need the highlight settled from an effect on `value`.
|
|
30
|
+
const valueRef = useRef(value)
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
valueRef.current = value
|
|
33
|
+
})
|
|
13
34
|
const listboxId = useId()
|
|
14
35
|
const rootRef = useRef<HTMLDivElement>(null)
|
|
15
36
|
const inputRef = useRef<HTMLInputElement | null>(null)
|
|
16
37
|
const itemsRef = useRef(new Map<string, {value: string; element: HTMLElement}>())
|
|
17
38
|
const orderRef = useRef<string[]>([])
|
|
18
39
|
|
|
40
|
+
const cancelBlurClose = useCallback(() => {
|
|
41
|
+
if (blurTimerRef.current !== null) clearTimeout(blurTimerRef.current)
|
|
42
|
+
blurTimerRef.current = null
|
|
43
|
+
}, [])
|
|
44
|
+
|
|
45
|
+
useEffect(() => cancelBlurClose, [cancelBlurClose])
|
|
46
|
+
|
|
19
47
|
const close = useCallback(() => {
|
|
20
48
|
setOpen(false)
|
|
49
|
+
pendingHighlightRef.current = false
|
|
21
50
|
setHighlightedId(null)
|
|
22
|
-
}, [setOpen])
|
|
51
|
+
}, [setOpen, setHighlightedId])
|
|
23
52
|
|
|
24
53
|
const openPopup = useCallback(() => {
|
|
25
54
|
setOpen(true)
|
|
55
|
+
pendingHighlightRef.current = true
|
|
26
56
|
// Highlight the selected item or first visible item
|
|
27
57
|
const items = itemsRef.current
|
|
28
58
|
const order = orderRef.current
|
|
@@ -35,43 +65,80 @@ export function useComboboxRoot(options: ComboboxStateOptions) {
|
|
|
35
65
|
}
|
|
36
66
|
}
|
|
37
67
|
setHighlightedId(found ?? (order.length > 0 ? order[0] : null))
|
|
38
|
-
|
|
68
|
+
if (found) pendingHighlightRef.current = false
|
|
69
|
+
}, [value, setOpen, setHighlightedId])
|
|
70
|
+
|
|
71
|
+
// Focus opens the list, except the refocus that follows choosing an option.
|
|
72
|
+
const onInputFocus = useCallback(() => {
|
|
73
|
+
cancelBlurClose()
|
|
74
|
+
if (refocusAfterChooseRef.current) return
|
|
75
|
+
openPopup()
|
|
76
|
+
}, [cancelBlurClose, openPopup])
|
|
77
|
+
|
|
78
|
+
// Delay close to allow an item click to register.
|
|
79
|
+
const onInputBlur = useCallback(() => {
|
|
80
|
+
cancelBlurClose()
|
|
81
|
+
blurTimerRef.current = setTimeout(() => {
|
|
82
|
+
blurTimerRef.current = null
|
|
83
|
+
close()
|
|
84
|
+
}, 150)
|
|
85
|
+
}, [cancelBlurClose, close])
|
|
39
86
|
|
|
40
87
|
// Choosing keeps focus in the input (the web's editable combobox).
|
|
41
88
|
const selectValue = useCallback(
|
|
42
89
|
(itemValue: string) => {
|
|
90
|
+
cancelBlurClose()
|
|
43
91
|
chooseValue(itemValue)
|
|
44
92
|
setHighlightedId(null)
|
|
45
|
-
|
|
93
|
+
refocusAfterChooseRef.current = true
|
|
94
|
+
try {
|
|
95
|
+
inputRef.current?.focus()
|
|
96
|
+
} finally {
|
|
97
|
+
refocusAfterChooseRef.current = false
|
|
98
|
+
}
|
|
46
99
|
},
|
|
47
|
-
[chooseValue],
|
|
100
|
+
[chooseValue, cancelBlurClose, setHighlightedId],
|
|
48
101
|
)
|
|
49
102
|
|
|
50
|
-
const registerItem = useCallback(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
103
|
+
const registerItem = useCallback(
|
|
104
|
+
(id: string, itemValue: string, element: HTMLElement) => {
|
|
105
|
+
itemsRef.current.set(id, {value: itemValue, element})
|
|
106
|
+
// holds-until: a few hundred options. Each (re-)registration sorts every
|
|
107
|
+
// registered id by document position, and a filter change re-registers
|
|
108
|
+
// each option it shows again; past that, sort once per pass instead.
|
|
109
|
+
const map = itemsRef.current
|
|
110
|
+
const ids = [...map.keys()]
|
|
111
|
+
ids.sort((a, b) => {
|
|
112
|
+
const elA = map.get(a)?.element
|
|
113
|
+
const elB = map.get(b)?.element
|
|
114
|
+
if (!elA || !elB) return 0
|
|
115
|
+
return elA.compareDocumentPosition(elB) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1
|
|
116
|
+
})
|
|
117
|
+
orderRef.current = ids
|
|
118
|
+
if (pendingHighlightRef.current) {
|
|
119
|
+
if (itemValue === valueRef.current) {
|
|
120
|
+
pendingHighlightRef.current = false
|
|
121
|
+
setHighlightedId(id)
|
|
122
|
+
} else if (highlightedRef.current === null) {
|
|
123
|
+
setHighlightedId(ids[0] ?? null)
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return () => {
|
|
127
|
+
itemsRef.current.delete(id)
|
|
128
|
+
orderRef.current = orderRef.current.filter((i) => i !== id)
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
[setHighlightedId],
|
|
132
|
+
)
|
|
66
133
|
|
|
67
134
|
// Get visible items (those matching the filter)
|
|
68
135
|
const getVisibleOrder = useCallback(() => {
|
|
69
|
-
if (!
|
|
136
|
+
if (!query) return orderRef.current
|
|
70
137
|
return orderRef.current.filter((id) => {
|
|
71
138
|
const item = itemsRef.current.get(id)
|
|
72
|
-
return item ? matchesQuery(labels[item.value] ?? item.value,
|
|
139
|
+
return item ? matchesQuery(labels[item.value] ?? item.value, query) : false
|
|
73
140
|
})
|
|
74
|
-
}, [
|
|
141
|
+
}, [query, labels])
|
|
75
142
|
|
|
76
143
|
// Keyboard navigation
|
|
77
144
|
useEffect(() => {
|
|
@@ -79,24 +146,23 @@ export function useComboboxRoot(options: ComboboxStateOptions) {
|
|
|
79
146
|
if (!root || !open) return
|
|
80
147
|
|
|
81
148
|
function handleKeyDown(e: KeyboardEvent) {
|
|
149
|
+
pendingHighlightRef.current = false
|
|
82
150
|
const visibleOrder = getVisibleOrder()
|
|
83
151
|
if (visibleOrder.length === 0 && e.key !== 'Escape' && e.key !== 'Tab') return
|
|
84
152
|
|
|
85
153
|
switch (e.key) {
|
|
86
154
|
case 'ArrowDown': {
|
|
87
155
|
e.preventDefault()
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
return visibleOrder[(idx + 1) % visibleOrder.length] ?? null
|
|
91
|
-
})
|
|
156
|
+
const idx = highlightedRef.current ? visibleOrder.indexOf(highlightedRef.current) : -1
|
|
157
|
+
setHighlightedId(visibleOrder[(idx + 1) % visibleOrder.length] ?? null)
|
|
92
158
|
break
|
|
93
159
|
}
|
|
94
160
|
case 'ArrowUp': {
|
|
95
161
|
e.preventDefault()
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
162
|
+
const idx = highlightedRef.current ? visibleOrder.indexOf(highlightedRef.current) : 0
|
|
163
|
+
setHighlightedId(
|
|
164
|
+
visibleOrder[(idx - 1 + visibleOrder.length) % visibleOrder.length] ?? null,
|
|
165
|
+
)
|
|
100
166
|
break
|
|
101
167
|
}
|
|
102
168
|
case 'Home': {
|
|
@@ -111,16 +177,17 @@ export function useComboboxRoot(options: ComboboxStateOptions) {
|
|
|
111
177
|
}
|
|
112
178
|
case 'Enter': {
|
|
113
179
|
e.preventDefault()
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
180
|
+
const highlighted = highlightedRef.current
|
|
181
|
+
if (highlighted) {
|
|
182
|
+
// A highlighted option the filter has hidden is never committed.
|
|
183
|
+
const highlightedOption = visibleOrder.includes(highlighted)
|
|
184
|
+
? itemsRef.current.get(highlighted)
|
|
185
|
+
: undefined
|
|
186
|
+
if (highlightedOption) selectValue(highlightedOption.value)
|
|
187
|
+
} else if (!inputValue.trim()) {
|
|
188
|
+
// Empty input + Enter = clear selection (reset to "All")
|
|
189
|
+
selectValue('')
|
|
190
|
+
}
|
|
124
191
|
break
|
|
125
192
|
}
|
|
126
193
|
case 'Escape': {
|
|
@@ -138,14 +205,22 @@ export function useComboboxRoot(options: ComboboxStateOptions) {
|
|
|
138
205
|
|
|
139
206
|
root.addEventListener('keydown', handleKeyDown)
|
|
140
207
|
return () => root.removeEventListener('keydown', handleKeyDown)
|
|
141
|
-
}, [open, close, selectValue, getVisibleOrder])
|
|
208
|
+
}, [open, close, selectValue, getVisibleOrder, inputValue, setHighlightedId])
|
|
142
209
|
|
|
143
|
-
//
|
|
210
|
+
// Keep the highlighted item in view, also when the filter widens the list
|
|
211
|
+
// above it (typing the chosen label back shows every option again). An
|
|
212
|
+
// option the filter hides has unregistered (its effects ran before this
|
|
213
|
+
// one): drop the highlight, so aria-activedescendant never names a missing
|
|
214
|
+
// element and Enter commits nothing hidden.
|
|
144
215
|
useEffect(() => {
|
|
145
216
|
if (!highlightedId) return
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
217
|
+
const highlightedOption = itemsRef.current.get(highlightedId)
|
|
218
|
+
if (!highlightedOption) {
|
|
219
|
+
setHighlightedId(null)
|
|
220
|
+
return
|
|
221
|
+
}
|
|
222
|
+
highlightedOption.element.scrollIntoView({block: 'nearest'})
|
|
223
|
+
}, [highlightedId, query, setHighlightedId])
|
|
149
224
|
|
|
150
225
|
const ctx: ComboboxContextValue = useMemo(
|
|
151
226
|
() => ({
|
|
@@ -155,7 +230,10 @@ export function useComboboxRoot(options: ComboboxStateOptions) {
|
|
|
155
230
|
value,
|
|
156
231
|
setValue: selectValue,
|
|
157
232
|
inputValue,
|
|
233
|
+
query,
|
|
158
234
|
setInputValue,
|
|
235
|
+
onInputFocus,
|
|
236
|
+
onInputBlur,
|
|
159
237
|
labels,
|
|
160
238
|
registerLabel,
|
|
161
239
|
listboxId,
|
|
@@ -172,7 +250,10 @@ export function useComboboxRoot(options: ComboboxStateOptions) {
|
|
|
172
250
|
value,
|
|
173
251
|
selectValue,
|
|
174
252
|
inputValue,
|
|
253
|
+
query,
|
|
175
254
|
setInputValue,
|
|
255
|
+
onInputFocus,
|
|
256
|
+
onInputBlur,
|
|
176
257
|
labels,
|
|
177
258
|
registerLabel,
|
|
178
259
|
listboxId,
|