@cognitap/radiant 0.0.38 → 0.0.40
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/CHANGELOG.md +12 -4
- package/dist/library/Combobox.d.ts +31 -7
- package/dist/library/Combobox.d.ts.map +1 -1
- package/dist/library/Combobox.js +162 -42
- package/dist/library/Combobox.js.map +1 -1
- package/dist/library/Drawer.js +3 -3
- package/dist/library/Drawer.js.map +1 -1
- package/dist/library/Input.d.ts +6 -1
- package/dist/library/Input.d.ts.map +1 -1
- package/dist/library/Input.js +6 -2
- package/dist/library/Input.js.map +1 -1
- package/dist/library/MultiSelect.d.ts +30 -0
- package/dist/library/MultiSelect.d.ts.map +1 -0
- package/dist/library/MultiSelect.js +49 -0
- package/dist/library/MultiSelect.js.map +1 -0
- package/dist/library/Tag.d.ts +17 -0
- package/dist/library/Tag.d.ts.map +1 -0
- package/dist/library/Tag.js +13 -0
- package/dist/library/Tag.js.map +1 -0
- package/dist/library/TagsInput.d.ts +21 -0
- package/dist/library/TagsInput.d.ts.map +1 -0
- package/dist/library/TagsInput.js +183 -0
- package/dist/library/TagsInput.js.map +1 -0
- package/dist/library/cn.d.ts.map +1 -1
- package/dist/library/cn.js +4 -1
- package/dist/library/cn.js.map +1 -1
- package/dist/library/index.d.ts +8 -0
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +4 -0
- package/dist/library/index.js.map +1 -1
- package/dist/library/internal/tags.d.ts +7 -0
- package/dist/library/internal/tags.d.ts.map +1 -0
- package/dist/library/internal/tags.js +30 -0
- package/dist/library/internal/tags.js.map +1 -0
- package/dist/library/react/useArrayFormBridge.d.ts +13 -0
- package/dist/library/react/useArrayFormBridge.d.ts.map +1 -0
- package/dist/library/react/useArrayFormBridge.js +52 -0
- package/dist/library/react/useArrayFormBridge.js.map +1 -0
- package/dist/library/react/useComboboxPopup.d.ts.map +1 -1
- package/dist/library/react/useComboboxPopup.js +9 -2
- package/dist/library/react/useComboboxPopup.js.map +1 -1
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/components.d.ts +9 -0
- package/dist/library/themes/components.d.ts.map +1 -1
- package/dist/library/themes/components.js +3 -0
- package/dist/library/themes/components.js.map +1 -1
- package/docs/architecture.md +11 -4
- package/package.json +1 -1
- package/src/Combobox.tsx +302 -91
- package/src/Drawer.tsx +3 -3
- package/src/Input.tsx +17 -3
- package/src/MultiSelect.tsx +93 -0
- package/src/Tag.tsx +59 -0
- package/src/TagsInput.tsx +302 -0
- package/src/cn.ts +4 -1
- package/src/index.ts +33 -0
- package/src/internal/tags.ts +31 -0
- package/src/react/useArrayFormBridge.ts +60 -0
- package/src/react/useComboboxPopup.ts +9 -2
- package/src/themes/components.ts +3 -0
package/src/Combobox.tsx
CHANGED
|
@@ -14,6 +14,10 @@ import {
|
|
|
14
14
|
import type { ComponentProps, ReactNode, RefObject } from 'react'
|
|
15
15
|
import { createPortal } from 'react-dom'
|
|
16
16
|
import { Button, type ButtonProps } from './Button.js'
|
|
17
|
+
import { Tag } from './Tag.js'
|
|
18
|
+
import { sameSelection, nextSelection, isItemOpen, type Selection } from './internal/selection.js'
|
|
19
|
+
import { validateTags } from './internal/tags.js'
|
|
20
|
+
import { useArrayFormBridge } from './react/useArrayFormBridge.js'
|
|
17
21
|
import { useField, useFieldControl } from './Field.js'
|
|
18
22
|
import type { DistributiveOmit } from './internal/types.js'
|
|
19
23
|
import { cn } from './cn.js'
|
|
@@ -36,11 +40,8 @@ export type ComboboxOption = { value: string; disabled?: boolean } & (
|
|
|
36
40
|
| { label: string; textValue?: string }
|
|
37
41
|
| { label: ReactNode; textValue: string }
|
|
38
42
|
)
|
|
39
|
-
|
|
43
|
+
type ComboboxCommonProps = {
|
|
40
44
|
items: readonly ComboboxOption[]
|
|
41
|
-
onValueChange?: (value: string | null) => void
|
|
42
|
-
/** Application-owned selected option when it is absent from asynchronous results. */
|
|
43
|
-
selectedItem?: ComboboxOption
|
|
44
45
|
/** Local text matching by default. Pass null for application-filtered results. */
|
|
45
46
|
filter?: ((item: ComboboxOption, query: string) => boolean) | null
|
|
46
47
|
onQueryChange?: (query: string) => void
|
|
@@ -51,6 +52,7 @@ export type ComboboxProps = ValueOwner<string | null> & {
|
|
|
51
52
|
name?: string
|
|
52
53
|
form?: string
|
|
53
54
|
disabled?: boolean
|
|
55
|
+
readOnly?: boolean
|
|
54
56
|
required?: boolean
|
|
55
57
|
controlSize?: ControlSize
|
|
56
58
|
children?: ReactNode
|
|
@@ -58,6 +60,22 @@ export type ComboboxProps = ValueOwner<string | null> & {
|
|
|
58
60
|
({ open: boolean; defaultOpen?: never } | { open?: undefined; defaultOpen?: boolean }) & {
|
|
59
61
|
onOpenChange?: (open: boolean) => void
|
|
60
62
|
}
|
|
63
|
+
export type ComboboxMultipleProps = ComboboxCommonProps &
|
|
64
|
+
ValueOwner<string[]> & {
|
|
65
|
+
multiple: true
|
|
66
|
+
onValueChange?: (value: string[]) => void
|
|
67
|
+
selectedItems?: readonly ComboboxOption[]
|
|
68
|
+
selectedItem?: never
|
|
69
|
+
}
|
|
70
|
+
export type ComboboxProps =
|
|
71
|
+
| ComboboxMultipleProps
|
|
72
|
+
| (ComboboxCommonProps &
|
|
73
|
+
ValueOwner<string | null> & {
|
|
74
|
+
multiple?: false
|
|
75
|
+
onValueChange?: (value: string | null) => void
|
|
76
|
+
selectedItem?: ComboboxOption
|
|
77
|
+
selectedItems?: never
|
|
78
|
+
})
|
|
61
79
|
export type ComboboxState = {
|
|
62
80
|
value: string | null
|
|
63
81
|
query: string
|
|
@@ -67,7 +85,17 @@ export type ComboboxState = {
|
|
|
67
85
|
setQuery(query: string): void
|
|
68
86
|
setOpen(open: boolean): void
|
|
69
87
|
}
|
|
70
|
-
type
|
|
88
|
+
export type ComboboxMultipleState = Omit<ComboboxState, 'value' | 'setValue'> & {
|
|
89
|
+
value: string[]
|
|
90
|
+
setValue(value: string[]): void
|
|
91
|
+
}
|
|
92
|
+
type Context = Omit<ComboboxState, 'value' | 'setValue'> & {
|
|
93
|
+
value: Selection
|
|
94
|
+
multiple: boolean
|
|
95
|
+
readOnly: boolean
|
|
96
|
+
setValue(value: Selection): void
|
|
97
|
+
selected: readonly ComboboxOption[]
|
|
98
|
+
remove(value: string): void
|
|
71
99
|
items: readonly ComboboxOption[]
|
|
72
100
|
visible: readonly ComboboxOption[]
|
|
73
101
|
active: string | null
|
|
@@ -88,8 +116,16 @@ type Context = ComboboxState & {
|
|
|
88
116
|
commit(value: string | null): void
|
|
89
117
|
}
|
|
90
118
|
const ComboboxContext = createContext<Context | null>(null)
|
|
91
|
-
export function useCombobox():
|
|
92
|
-
|
|
119
|
+
export function useCombobox(options: { multiple: true }): ComboboxMultipleState | null
|
|
120
|
+
export function useCombobox(options?: { multiple?: false }): ComboboxState | null
|
|
121
|
+
export function useCombobox(options?: {
|
|
122
|
+
multiple?: boolean
|
|
123
|
+
}): ComboboxState | ComboboxMultipleState | null {
|
|
124
|
+
const box = useContext(ComboboxContext)
|
|
125
|
+
if (!box || box.multiple !== !!options?.multiple) return null
|
|
126
|
+
if (Array.isArray(box.value))
|
|
127
|
+
return { ...box, value: box.value, setValue: (value: string[]) => box.setValue(value) }
|
|
128
|
+
return { ...box, value: box.value, setValue: (value: string | null) => box.setValue(value) }
|
|
93
129
|
}
|
|
94
130
|
function useComboboxContext() {
|
|
95
131
|
const context = useContext(ComboboxContext)
|
|
@@ -101,31 +137,38 @@ const textOf = (item?: ComboboxOption) =>
|
|
|
101
137
|
const localFilter = (item: ComboboxOption, query: string) =>
|
|
102
138
|
textOf(item).toLocaleLowerCase().includes(query.trim().toLocaleLowerCase())
|
|
103
139
|
|
|
104
|
-
/** Editable search
|
|
105
|
-
export function Combobox({
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
140
|
+
/** Editable search and committed selection have independent owners; results never select themselves. */
|
|
141
|
+
export function Combobox(props: ComboboxProps) {
|
|
142
|
+
const {
|
|
143
|
+
items,
|
|
144
|
+
value,
|
|
145
|
+
defaultValue,
|
|
146
|
+
selectedItem,
|
|
147
|
+
selectedItems,
|
|
148
|
+
multiple = false,
|
|
149
|
+
filter = localFilter,
|
|
150
|
+
query,
|
|
151
|
+
defaultQuery,
|
|
152
|
+
onQueryChange,
|
|
153
|
+
open,
|
|
154
|
+
defaultOpen,
|
|
155
|
+
onOpenChange,
|
|
156
|
+
loading = false,
|
|
157
|
+
error,
|
|
158
|
+
loadingText = 'Loading results…',
|
|
159
|
+
emptyText = 'No results found.',
|
|
160
|
+
name,
|
|
161
|
+
form,
|
|
162
|
+
disabled: disabledProp,
|
|
163
|
+
readOnly = false,
|
|
164
|
+
required: requiredProp,
|
|
165
|
+
controlSize = 'medium',
|
|
166
|
+
children
|
|
167
|
+
} = props
|
|
168
|
+
const [initialMultiple] = useState(multiple)
|
|
169
|
+
if (multiple !== initialMultiple)
|
|
170
|
+
throw new TypeError('Combobox cannot change multiple mode. Remount with a new key.')
|
|
171
|
+
const selectedOptions = multiple ? (selectedItems ?? []) : selectedItem ? [selectedItem] : []
|
|
129
172
|
const ids = useId()
|
|
130
173
|
const field = useField()
|
|
131
174
|
const disabled = !!(disabledProp || field?.disabled)
|
|
@@ -133,18 +176,36 @@ export function Combobox({
|
|
|
133
176
|
if (new Set(items.map(item => item.value)).size !== items.length)
|
|
134
177
|
throw new TypeError('Combobox item values must be distinct.')
|
|
135
178
|
if (
|
|
136
|
-
[...items, ...
|
|
179
|
+
[...items, ...selectedOptions].some(
|
|
137
180
|
item => typeof item.value !== 'string' || !textOf(item).trim()
|
|
138
181
|
)
|
|
139
182
|
)
|
|
140
183
|
throw new TypeError('Combobox items need a string value and readable textValue.')
|
|
141
|
-
const selection = useControllableState({
|
|
184
|
+
const selection = useControllableState<Selection>({
|
|
142
185
|
name: 'Combobox',
|
|
143
186
|
value,
|
|
144
|
-
defaultValue,
|
|
145
|
-
initialValue: null,
|
|
146
|
-
|
|
187
|
+
defaultValue: Array.isArray(defaultValue) ? [...defaultValue] : defaultValue,
|
|
188
|
+
initialValue: multiple ? [] : null,
|
|
189
|
+
equal: sameSelection,
|
|
190
|
+
onChange(next) {
|
|
191
|
+
if (props.multiple) {
|
|
192
|
+
if (!Array.isArray(next)) throw new TypeError('Multiple Combobox value must be an array.')
|
|
193
|
+
props.onValueChange?.([...next])
|
|
194
|
+
} else {
|
|
195
|
+
if (Array.isArray(next))
|
|
196
|
+
throw new TypeError('Single Combobox value must be a string or null.')
|
|
197
|
+
props.onValueChange?.(next)
|
|
198
|
+
}
|
|
199
|
+
}
|
|
147
200
|
})
|
|
201
|
+
if (multiple !== Array.isArray(selection.value))
|
|
202
|
+
throw new TypeError('Combobox value must match its multiple mode.')
|
|
203
|
+
if (Array.isArray(selection.value)) validateTags(selection.value)
|
|
204
|
+
const values = Array.isArray(selection.value)
|
|
205
|
+
? selection.value
|
|
206
|
+
: selection.value === null
|
|
207
|
+
? []
|
|
208
|
+
: [selection.value]
|
|
148
209
|
const search = useControllableState({
|
|
149
210
|
name: 'Combobox query',
|
|
150
211
|
value: query,
|
|
@@ -165,26 +226,36 @@ export function Combobox({
|
|
|
165
226
|
const [active, highlight] = useState<string | null>(null)
|
|
166
227
|
const [searching, setSearching] = useState(!!search.value)
|
|
167
228
|
const [retained, setRetained] = useState(() => new Map<string, ComboboxOption>())
|
|
168
|
-
const
|
|
169
|
-
items.find(item => item.value ===
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
229
|
+
const selectedEntries = values.map(value => {
|
|
230
|
+
const item = items.find(item => item.value === value) ??
|
|
231
|
+
selectedOptions.find(item => item.value === value) ??
|
|
232
|
+
retained.get(value) ?? { value, label: multiple ? value || 'Selected option' : value }
|
|
233
|
+
return item
|
|
234
|
+
})
|
|
235
|
+
if (selectedEntries.some(item => retained.get(item.value) !== item))
|
|
236
|
+
setRetained(
|
|
237
|
+
new Map<string, ComboboxOption>([
|
|
238
|
+
...retained,
|
|
239
|
+
...selectedEntries.map((item): [string, ComboboxOption] => [item.value, item])
|
|
240
|
+
])
|
|
241
|
+
)
|
|
242
|
+
const selected = selectedEntries[0]
|
|
243
|
+
const unavailable = () => disabled || readOnly || !!input?.matches(':disabled')
|
|
176
244
|
const isOpen = expansion.value && !disabled
|
|
177
245
|
const visible =
|
|
178
246
|
loading || error ? [] : filter ? items.filter(item => filter(item, search.value)) : items
|
|
179
247
|
const enabled = visible.filter(item => !item.disabled)
|
|
180
248
|
const activeValue = enabled.some(item => item.value === active) ? active : null
|
|
181
249
|
if (active !== null && activeValue === null) highlight(null)
|
|
182
|
-
const text =
|
|
183
|
-
|
|
250
|
+
const text = multiple
|
|
251
|
+
? search.value
|
|
252
|
+
: isOpen && (searching || search.value !== '')
|
|
184
253
|
? search.value
|
|
185
254
|
: selected
|
|
186
255
|
? textOf(selected)
|
|
187
|
-
:
|
|
256
|
+
: typeof selection.value === 'string'
|
|
257
|
+
? selection.value
|
|
258
|
+
: ''
|
|
188
259
|
const setOpen = (next: boolean) => {
|
|
189
260
|
if (next && unavailable()) return
|
|
190
261
|
expansion.request(next)
|
|
@@ -204,28 +275,48 @@ export function Combobox({
|
|
|
204
275
|
setOpen(true)
|
|
205
276
|
}
|
|
206
277
|
const pending = useRef<{ value: string | null } | null>(null)
|
|
207
|
-
const
|
|
278
|
+
const arrayPending = useRef<string[] | null>(null)
|
|
279
|
+
const requestValue = (update: Selection | ((current: Selection) => Selection)) => {
|
|
208
280
|
if (unavailable()) return
|
|
209
|
-
|
|
210
|
-
|
|
281
|
+
selection.request(current => {
|
|
282
|
+
const next = typeof update === 'function' ? update(current) : update
|
|
283
|
+
if (multiple !== Array.isArray(next))
|
|
284
|
+
throw new TypeError('Combobox value must match its multiple mode.')
|
|
285
|
+
if (Array.isArray(next)) validateTags(next)
|
|
286
|
+
if (!sameSelection(current, next)) {
|
|
287
|
+
if (Array.isArray(next)) arrayPending.current = next
|
|
288
|
+
else pending.current = { value: next }
|
|
289
|
+
}
|
|
290
|
+
return next
|
|
291
|
+
})
|
|
211
292
|
queueMicrotask(() => {
|
|
212
293
|
pending.current = null
|
|
294
|
+
arrayPending.current = null
|
|
213
295
|
})
|
|
214
296
|
}
|
|
215
297
|
const commit = (next: string | null) => {
|
|
216
298
|
if (unavailable() || (next !== null && !enabled.some(item => item.value === next))) return
|
|
217
299
|
const item = enabled.find(item => item.value === next)
|
|
218
300
|
if (item) setRetained(previous => new Map(previous).set(item.value, item))
|
|
219
|
-
|
|
220
|
-
|
|
301
|
+
if (multiple) {
|
|
302
|
+
requestValue(current =>
|
|
303
|
+
next === null ? [] : nextSelection(current, next, !isItemOpen(current, next))
|
|
304
|
+
)
|
|
305
|
+
search.request('')
|
|
306
|
+
setSearching(true)
|
|
307
|
+
} else {
|
|
308
|
+
requestValue(next)
|
|
309
|
+
dismiss()
|
|
310
|
+
}
|
|
221
311
|
input?.focus({ preventScroll: true })
|
|
222
312
|
}
|
|
223
|
-
const nativeValue = selection.value
|
|
313
|
+
const nativeValue = typeof selection.value === 'string' ? selection.value : ''
|
|
224
314
|
const latest = useRef({ selection, search, nativeValue, expansion })
|
|
225
315
|
useLayoutEffect(() => {
|
|
226
316
|
latest.current = { selection, search, nativeValue, expansion }
|
|
227
317
|
})
|
|
228
318
|
useLayoutEffect(() => {
|
|
319
|
+
if (multiple) return
|
|
229
320
|
const node = bridge.current
|
|
230
321
|
if (!node) return
|
|
231
322
|
const changedByUser = pending.current && Object.is(pending.current.value, selection.value)
|
|
@@ -247,6 +338,7 @@ export function Combobox({
|
|
|
247
338
|
}
|
|
248
339
|
})
|
|
249
340
|
useEffect(() => {
|
|
341
|
+
if (multiple) return
|
|
250
342
|
const node = bridge.current
|
|
251
343
|
const owner = node?.form
|
|
252
344
|
if (!node || !owner) return
|
|
@@ -269,7 +361,7 @@ export function Combobox({
|
|
|
269
361
|
owner.removeEventListener('reset', reset)
|
|
270
362
|
win.clearTimeout(timer)
|
|
271
363
|
}
|
|
272
|
-
}, [input, form])
|
|
364
|
+
}, [input, form, multiple])
|
|
273
365
|
useLayoutEffect(() => {
|
|
274
366
|
if (!isOpen || activeValue === null) return
|
|
275
367
|
const option = content.current?.ownerDocument.getElementById(
|
|
@@ -277,14 +369,50 @@ export function Combobox({
|
|
|
277
369
|
)
|
|
278
370
|
option?.scrollIntoView({ block: 'nearest' })
|
|
279
371
|
}, [activeValue, isOpen, ids])
|
|
372
|
+
const arrayBridgeRef = useArrayFormBridge({
|
|
373
|
+
values: Array.isArray(selection.value) ? selection.value : [],
|
|
374
|
+
controlled: selection.controlled,
|
|
375
|
+
pending: arrayPending,
|
|
376
|
+
form,
|
|
377
|
+
validity: required && values.length === 0 ? 'Please select at least one option.' : '',
|
|
378
|
+
reset() {
|
|
379
|
+
selection.reset()
|
|
380
|
+
search.reset()
|
|
381
|
+
setSearching(false)
|
|
382
|
+
highlight(null)
|
|
383
|
+
expansion.request(false)
|
|
384
|
+
}
|
|
385
|
+
})
|
|
280
386
|
const context: Context = {
|
|
387
|
+
multiple,
|
|
388
|
+
readOnly,
|
|
389
|
+
selected: selectedEntries,
|
|
390
|
+
remove(value) {
|
|
391
|
+
requestValue(current =>
|
|
392
|
+
Array.isArray(current) ? current.filter(item => item !== value) : current
|
|
393
|
+
)
|
|
394
|
+
input?.focus({ preventScroll: true })
|
|
395
|
+
},
|
|
281
396
|
value: selection.value,
|
|
282
397
|
query: search.value,
|
|
283
398
|
open: isOpen,
|
|
284
399
|
disabled,
|
|
285
400
|
setValue: next => {
|
|
286
|
-
if (
|
|
287
|
-
|
|
401
|
+
if (Array.isArray(next)) {
|
|
402
|
+
if (!multiple) throw new TypeError('Single Combobox value must be a string or null.')
|
|
403
|
+
validateTags(next)
|
|
404
|
+
if (
|
|
405
|
+
next.every(
|
|
406
|
+
value =>
|
|
407
|
+
values.includes(value) || items.some(item => item.value === value && !item.disabled)
|
|
408
|
+
)
|
|
409
|
+
)
|
|
410
|
+
requestValue([...next])
|
|
411
|
+
} else {
|
|
412
|
+
if (multiple) throw new TypeError('Multiple Combobox value must be an array.')
|
|
413
|
+
if (next === null || items.some(item => item.value === next && !item.disabled))
|
|
414
|
+
requestValue(next)
|
|
415
|
+
}
|
|
288
416
|
},
|
|
289
417
|
setQuery,
|
|
290
418
|
setOpen,
|
|
@@ -311,9 +439,10 @@ export function Combobox({
|
|
|
311
439
|
<ComboboxContext.Provider value={context}>
|
|
312
440
|
{children ?? (
|
|
313
441
|
<>
|
|
442
|
+
{multiple && <ComboboxTags />}
|
|
314
443
|
<div
|
|
315
444
|
className="relative w-full min-w-0"
|
|
316
|
-
data-slot=
|
|
445
|
+
data-slot={multiple ? 'combobox-control multi-select-control' : 'combobox-control'}
|
|
317
446
|
>
|
|
318
447
|
<ComboboxInput />
|
|
319
448
|
<ComboboxTrigger className="absolute inset-y-0 end-0 w-12" />
|
|
@@ -323,24 +452,59 @@ export function Combobox({
|
|
|
323
452
|
</ComboboxPortal>
|
|
324
453
|
</>
|
|
325
454
|
)}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
455
|
+
{multiple ? (
|
|
456
|
+
<>
|
|
457
|
+
<input
|
|
458
|
+
ref={arrayBridgeRef}
|
|
459
|
+
type="text"
|
|
460
|
+
className="sr-only"
|
|
461
|
+
data-slot="combobox-native multi-select-native"
|
|
462
|
+
form={form}
|
|
463
|
+
disabled={disabled}
|
|
464
|
+
readOnly={readOnly}
|
|
465
|
+
aria-hidden="true"
|
|
466
|
+
tabIndex={-1}
|
|
467
|
+
autoComplete="off"
|
|
468
|
+
defaultValue={JSON.stringify(selection.value)}
|
|
469
|
+
onFocus={() => input?.focus()}
|
|
470
|
+
onInvalid={event => {
|
|
471
|
+
event.preventDefault()
|
|
472
|
+
input?.focus()
|
|
473
|
+
}}
|
|
474
|
+
/>
|
|
475
|
+
{values.map(value => (
|
|
476
|
+
<input
|
|
477
|
+
key={value}
|
|
478
|
+
type="hidden"
|
|
479
|
+
data-slot="multi-select-value"
|
|
480
|
+
name={name}
|
|
481
|
+
form={form}
|
|
482
|
+
disabled={disabled}
|
|
483
|
+
value={value}
|
|
484
|
+
/>
|
|
485
|
+
))}
|
|
486
|
+
</>
|
|
487
|
+
) : (
|
|
488
|
+
<input
|
|
489
|
+
ref={bridge}
|
|
490
|
+
readOnly={readOnly}
|
|
491
|
+
className="sr-only"
|
|
492
|
+
data-slot="combobox-native"
|
|
493
|
+
type="text"
|
|
494
|
+
name={name}
|
|
495
|
+
form={form}
|
|
496
|
+
disabled={disabled}
|
|
497
|
+
aria-hidden="true"
|
|
498
|
+
tabIndex={-1}
|
|
499
|
+
autoComplete="off"
|
|
500
|
+
defaultValue={nativeValue}
|
|
501
|
+
onFocus={() => input?.focus()}
|
|
502
|
+
onInvalid={event => {
|
|
503
|
+
event.preventDefault()
|
|
504
|
+
input?.focus()
|
|
505
|
+
}}
|
|
506
|
+
/>
|
|
507
|
+
)}
|
|
344
508
|
</ComboboxContext.Provider>
|
|
345
509
|
)
|
|
346
510
|
}
|
|
@@ -387,6 +551,7 @@ export function ComboboxInput({
|
|
|
387
551
|
type="text"
|
|
388
552
|
role="combobox"
|
|
389
553
|
required={false}
|
|
554
|
+
readOnly={box.readOnly || props.readOnly}
|
|
390
555
|
autoComplete={props.autoComplete ?? 'off'}
|
|
391
556
|
value={box.text}
|
|
392
557
|
className={cn(
|
|
@@ -394,7 +559,7 @@ export function ComboboxInput({
|
|
|
394
559
|
controlMetrics({ controlSize: box.controlSize }),
|
|
395
560
|
className
|
|
396
561
|
)}
|
|
397
|
-
data-slot=
|
|
562
|
+
data-slot={box.multiple ? 'combobox-input multi-select-input' : 'combobox-input'}
|
|
398
563
|
data-control-size={box.controlSize}
|
|
399
564
|
aria-autocomplete="list"
|
|
400
565
|
aria-expanded={box.open}
|
|
@@ -426,7 +591,18 @@ export function ComboboxInput({
|
|
|
426
591
|
event.metaKey
|
|
427
592
|
)
|
|
428
593
|
return
|
|
429
|
-
if (
|
|
594
|
+
if (
|
|
595
|
+
box.multiple &&
|
|
596
|
+
event.key === 'Backspace' &&
|
|
597
|
+
box.query === '' &&
|
|
598
|
+
Array.isArray(box.value) &&
|
|
599
|
+
box.value.length
|
|
600
|
+
) {
|
|
601
|
+
event.preventDefault()
|
|
602
|
+
box.remove(box.value.at(-1)!)
|
|
603
|
+
return
|
|
604
|
+
}
|
|
605
|
+
if (!box.multiple && event.key === 'Escape' && !box.open && box.value !== null) {
|
|
430
606
|
event.preventDefault()
|
|
431
607
|
box.commit(null)
|
|
432
608
|
return
|
|
@@ -507,7 +683,7 @@ export function ComboboxTrigger({
|
|
|
507
683
|
tone="neutral"
|
|
508
684
|
{...props}
|
|
509
685
|
type="button"
|
|
510
|
-
disabled={box.disabled}
|
|
686
|
+
disabled={box.disabled || box.readOnly}
|
|
511
687
|
pending={false}
|
|
512
688
|
motion="off"
|
|
513
689
|
controlSize={box.controlSize}
|
|
@@ -519,7 +695,9 @@ export function ComboboxTrigger({
|
|
|
519
695
|
aria-label={props['aria-label'] ?? 'Show options'}
|
|
520
696
|
aria-controls={box.open ? box.listId : undefined}
|
|
521
697
|
aria-expanded={box.open}
|
|
522
|
-
data-slot=
|
|
698
|
+
data-slot={
|
|
699
|
+
box.multiple ? 'button combobox-trigger multi-select-trigger' : 'button combobox-trigger'
|
|
700
|
+
}
|
|
523
701
|
onPointerDown={event => {
|
|
524
702
|
onPointerDown?.(event)
|
|
525
703
|
if (!event.defaultPrevented) event.preventDefault()
|
|
@@ -561,14 +739,14 @@ export function ComboboxClear({
|
|
|
561
739
|
...props
|
|
562
740
|
}: ComboboxClearProps) {
|
|
563
741
|
const box = useComboboxContext()
|
|
564
|
-
if (box.value === null) return null
|
|
742
|
+
if (box.value === null || (Array.isArray(box.value) && !box.value.length)) return null
|
|
565
743
|
return (
|
|
566
744
|
<Button
|
|
567
745
|
variant="ghost"
|
|
568
746
|
tone="neutral"
|
|
569
747
|
{...props}
|
|
570
748
|
type="button"
|
|
571
|
-
disabled={box.disabled}
|
|
749
|
+
disabled={box.disabled || box.readOnly}
|
|
572
750
|
pending={false}
|
|
573
751
|
motion="off"
|
|
574
752
|
controlSize={box.controlSize}
|
|
@@ -576,9 +754,11 @@ export function ComboboxClear({
|
|
|
576
754
|
const native = { ...resolved, className: cn(comboboxActionClass, className) }
|
|
577
755
|
return render ? render(native) : <button {...native} />
|
|
578
756
|
}}
|
|
579
|
-
tabIndex={-1}
|
|
757
|
+
tabIndex={box.multiple ? 0 : -1}
|
|
580
758
|
aria-label={props['aria-label'] ?? 'Clear selection'}
|
|
581
|
-
data-slot=
|
|
759
|
+
data-slot={
|
|
760
|
+
box.multiple ? 'button combobox-clear multi-select-clear' : 'button combobox-clear'
|
|
761
|
+
}
|
|
582
762
|
onPointerDown={event => {
|
|
583
763
|
onPointerDown?.(event)
|
|
584
764
|
if (!event.defaultPrevented) event.preventDefault()
|
|
@@ -641,7 +821,7 @@ export function ComboboxContent({
|
|
|
641
821
|
{...props}
|
|
642
822
|
ref={transitionRef}
|
|
643
823
|
popover="manual"
|
|
644
|
-
data-slot=
|
|
824
|
+
data-slot={box.multiple ? 'combobox-content multi-select-content' : 'combobox-content'}
|
|
645
825
|
data-motion={motion}
|
|
646
826
|
data-state={box.open ? 'open' : 'closed'}
|
|
647
827
|
className={cn(
|
|
@@ -656,10 +836,11 @@ export function ComboboxContent({
|
|
|
656
836
|
<div
|
|
657
837
|
id={box.listId}
|
|
658
838
|
role="listbox"
|
|
839
|
+
aria-multiselectable={box.multiple || undefined}
|
|
659
840
|
aria-label={props['aria-label'] ?? box.input?.getAttribute('aria-label') ?? 'Suggestions'}
|
|
660
841
|
aria-labelledby={props['aria-label'] ? undefined : box.input?.labels?.[0]?.id}
|
|
661
842
|
aria-busy={box.loading || undefined}
|
|
662
|
-
data-slot=
|
|
843
|
+
data-slot={box.multiple ? 'combobox-listbox multi-select-listbox' : 'combobox-listbox'}
|
|
663
844
|
className="max-h-[calc(var(--_rad-combobox-max-height,100dvh)-0.5rem)] overflow-y-auto overscroll-contain"
|
|
664
845
|
>
|
|
665
846
|
{children ??
|
|
@@ -697,12 +878,12 @@ export function ComboboxItem({
|
|
|
697
878
|
role="option"
|
|
698
879
|
tabIndex={-1}
|
|
699
880
|
onKeyDown={props.onKeyDown}
|
|
700
|
-
aria-selected={box.active === value}
|
|
881
|
+
aria-selected={box.multiple ? isItemOpen(box.value, value) : box.active === value}
|
|
701
882
|
aria-disabled={item.disabled || undefined}
|
|
702
|
-
data-slot=
|
|
883
|
+
data-slot={box.multiple ? 'combobox-item multi-select-item' : 'combobox-item'}
|
|
703
884
|
data-value={value}
|
|
704
885
|
data-highlighted={box.active === value ? '' : undefined}
|
|
705
|
-
data-selected={box.value
|
|
886
|
+
data-selected={isItemOpen(box.value, value) ? '' : undefined}
|
|
706
887
|
data-disabled={item.disabled ? '' : undefined}
|
|
707
888
|
className={cn(comboboxItemClass, className)}
|
|
708
889
|
onPointerDown={event => {
|
|
@@ -724,7 +905,7 @@ export function ComboboxItem({
|
|
|
724
905
|
<span className="min-w-0 flex-1">{item.label}</span>
|
|
725
906
|
<span
|
|
726
907
|
aria-hidden="true"
|
|
727
|
-
className={cn(box.value
|
|
908
|
+
className={cn(!isItemOpen(box.value, value) && 'invisible')}
|
|
728
909
|
>
|
|
729
910
|
✓
|
|
730
911
|
</span>
|
|
@@ -744,10 +925,40 @@ export function ComboboxStatus({ children, className, ...props }: ComboboxStatus
|
|
|
744
925
|
role="status"
|
|
745
926
|
aria-live="polite"
|
|
746
927
|
aria-atomic="true"
|
|
747
|
-
data-slot=
|
|
928
|
+
data-slot={box.multiple ? 'combobox-status multi-select-status' : 'combobox-status'}
|
|
748
929
|
className={cn('px-3 py-2 empty:hidden', className)}
|
|
749
930
|
>
|
|
750
931
|
{box.open ? (children ?? box.status) : null}
|
|
751
932
|
</div>
|
|
752
933
|
)
|
|
753
934
|
}
|
|
935
|
+
|
|
936
|
+
export type ComboboxTagsProps = Omit<ComponentProps<'div'>, 'children' | 'style'> & {
|
|
937
|
+
style?: ThemeStyle
|
|
938
|
+
}
|
|
939
|
+
export function ComboboxTags({ className, ...props }: ComboboxTagsProps) {
|
|
940
|
+
const box = useComboboxContext()
|
|
941
|
+
if (!box.multiple) throw new Error('ComboboxTags requires a multiple Combobox.')
|
|
942
|
+
return (
|
|
943
|
+
<div
|
|
944
|
+
{...props}
|
|
945
|
+
data-slot="combobox-tags multi-select-tags"
|
|
946
|
+
className={cn(
|
|
947
|
+
'flex flex-wrap items-center gap-[var(--rad-multi-select-gap,0.5rem)] mb-[var(--rad-multi-select-gap,0.5rem)] empty:hidden',
|
|
948
|
+
className
|
|
949
|
+
)}
|
|
950
|
+
>
|
|
951
|
+
{box.selected.map(item => (
|
|
952
|
+
<Tag
|
|
953
|
+
key={item.value}
|
|
954
|
+
data-slot="multi-select-tag"
|
|
955
|
+
removeLabel={`Remove ${textOf(item)}`}
|
|
956
|
+
disabled={box.disabled || box.readOnly}
|
|
957
|
+
onRemove={() => box.remove(item.value)}
|
|
958
|
+
>
|
|
959
|
+
{item.label}
|
|
960
|
+
</Tag>
|
|
961
|
+
))}
|
|
962
|
+
</div>
|
|
963
|
+
)
|
|
964
|
+
}
|
package/src/Drawer.tsx
CHANGED
|
@@ -150,9 +150,9 @@ const surface = [
|
|
|
150
150
|
const viewport = [
|
|
151
151
|
'absolute inset-y-0 left-0 grid content-start box-border min-w-0 min-h-0 overflow-auto overscroll-contain max-w-full max-h-full',
|
|
152
152
|
'w-[var(--_rad-drawer-visible,100%)]',
|
|
153
|
-
'
|
|
154
|
-
'
|
|
155
|
-
'
|
|
153
|
+
'[[data-physical-side=left]>&]:left-auto [[data-physical-side=left]>&]:right-0',
|
|
154
|
+
'[[data-physical-side=top]>&]:inset-x-0 [[data-physical-side=top]>&]:top-auto [[data-physical-side=top]>&]:bottom-0 [[data-physical-side=top]>&]:w-full [[data-physical-side=top]>&]:h-[var(--_rad-drawer-visible,100%)]',
|
|
155
|
+
'[[data-physical-side=bottom]>&]:inset-x-0 [[data-physical-side=bottom]>&]:bottom-auto [[data-physical-side=bottom]>&]:top-0 [[data-physical-side=bottom]>&]:w-full [[data-physical-side=bottom]>&]:h-[var(--_rad-drawer-visible,100%)]',
|
|
156
156
|
'gap-[var(--rad-dialog-gap,var(--rad-surface-gap,1rem))] p-[var(--rad-dialog-padding,var(--rad-surface-padding,1.5rem))]'
|
|
157
157
|
]
|
|
158
158
|
export function DrawerContent({
|