@cognitap/radiant 0.0.39 → 0.0.41
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 +11 -3
- 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/Input.d.ts +7 -1
- package/dist/library/Input.d.ts.map +1 -1
- package/dist/library/Input.js +7 -3
- 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/OTPField.d.ts +16 -0
- package/dist/library/OTPField.d.ts.map +1 -0
- package/dist/library/OTPField.js +149 -0
- package/dist/library/OTPField.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 +6 -1
- package/dist/library/cn.js.map +1 -1
- package/dist/library/index.d.ts +10 -0
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +5 -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 +13 -0
- package/dist/library/themes/components.d.ts.map +1 -1
- package/dist/library/themes/components.js +4 -0
- package/dist/library/themes/components.js.map +1 -1
- package/docs/architecture.md +12 -4
- package/package.json +1 -1
- package/src/Combobox.tsx +302 -91
- package/src/Input.tsx +18 -4
- package/src/MultiSelect.tsx +93 -0
- package/src/OTPField.tsx +252 -0
- package/src/Tag.tsx +59 -0
- package/src/TagsInput.tsx +302 -0
- package/src/cn.ts +6 -1
- package/src/index.ts +35 -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 +4 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import type { ComponentProps } from 'react'
|
|
4
|
+
import {
|
|
5
|
+
Combobox,
|
|
6
|
+
ComboboxInput,
|
|
7
|
+
ComboboxTrigger,
|
|
8
|
+
ComboboxClear,
|
|
9
|
+
ComboboxPortal,
|
|
10
|
+
ComboboxContent,
|
|
11
|
+
ComboboxItem,
|
|
12
|
+
ComboboxStatus,
|
|
13
|
+
ComboboxTags,
|
|
14
|
+
useCombobox,
|
|
15
|
+
type ComboboxMultipleProps,
|
|
16
|
+
type ComboboxMultipleState,
|
|
17
|
+
type ComboboxOption,
|
|
18
|
+
type ComboboxInputProps,
|
|
19
|
+
type ComboboxTriggerProps,
|
|
20
|
+
type ComboboxClearProps,
|
|
21
|
+
type ComboboxContentProps,
|
|
22
|
+
type ComboboxItemProps,
|
|
23
|
+
type ComboboxStatusProps,
|
|
24
|
+
type ComboboxTagsProps
|
|
25
|
+
} from './Combobox.js'
|
|
26
|
+
import type { ButtonRenderProps } from './Button.js'
|
|
27
|
+
import type { DistributiveOmit } from './internal/types.js'
|
|
28
|
+
|
|
29
|
+
export type MultiSelectProps = DistributiveOmit<ComboboxMultipleProps, 'multiple'> & {
|
|
30
|
+
multiple?: never
|
|
31
|
+
}
|
|
32
|
+
export type MultiSelectState = ComboboxMultipleState
|
|
33
|
+
export type MultiSelectOption = ComboboxOption
|
|
34
|
+
export type MultiSelectInputProps = ComboboxInputProps
|
|
35
|
+
export type MultiSelectTriggerProps = ComboboxTriggerProps
|
|
36
|
+
export type MultiSelectClearProps = ComboboxClearProps
|
|
37
|
+
export type MultiSelectPortalProps = ComponentProps<typeof ComboboxPortal>
|
|
38
|
+
export type MultiSelectContentProps = ComboboxContentProps
|
|
39
|
+
export type MultiSelectItemProps = ComboboxItemProps
|
|
40
|
+
export type MultiSelectStatusProps = ComboboxStatusProps
|
|
41
|
+
export type MultiSelectTagsProps = ComboboxTagsProps
|
|
42
|
+
export type MultiSelectRenderProps = ButtonRenderProps
|
|
43
|
+
|
|
44
|
+
export function useMultiSelect(): MultiSelectState | null {
|
|
45
|
+
return useCombobox({ multiple: true })
|
|
46
|
+
}
|
|
47
|
+
function useMultiSelectContext() {
|
|
48
|
+
const state = useMultiSelect()
|
|
49
|
+
if (!state)
|
|
50
|
+
throw new Error('MultiSelect parts must render inside MultiSelect or a multiple Combobox.')
|
|
51
|
+
return state
|
|
52
|
+
}
|
|
53
|
+
/** Array selection is the real Combobox multiple mode, including its native form bridge. */
|
|
54
|
+
export function MultiSelect(props: MultiSelectProps) {
|
|
55
|
+
return (
|
|
56
|
+
<Combobox
|
|
57
|
+
{...props}
|
|
58
|
+
multiple
|
|
59
|
+
/>
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
export function MultiSelectInput(props: MultiSelectInputProps) {
|
|
63
|
+
useMultiSelectContext()
|
|
64
|
+
return <ComboboxInput {...props} />
|
|
65
|
+
}
|
|
66
|
+
export function MultiSelectTrigger(props: MultiSelectTriggerProps) {
|
|
67
|
+
useMultiSelectContext()
|
|
68
|
+
return <ComboboxTrigger {...props} />
|
|
69
|
+
}
|
|
70
|
+
export function MultiSelectClear(props: MultiSelectClearProps) {
|
|
71
|
+
useMultiSelectContext()
|
|
72
|
+
return <ComboboxClear {...props} />
|
|
73
|
+
}
|
|
74
|
+
export function MultiSelectPortal(props: MultiSelectPortalProps) {
|
|
75
|
+
useMultiSelectContext()
|
|
76
|
+
return <ComboboxPortal {...props} />
|
|
77
|
+
}
|
|
78
|
+
export function MultiSelectContent(props: MultiSelectContentProps) {
|
|
79
|
+
useMultiSelectContext()
|
|
80
|
+
return <ComboboxContent {...props} />
|
|
81
|
+
}
|
|
82
|
+
export function MultiSelectItem(props: MultiSelectItemProps) {
|
|
83
|
+
useMultiSelectContext()
|
|
84
|
+
return <ComboboxItem {...props} />
|
|
85
|
+
}
|
|
86
|
+
export function MultiSelectStatus(props: MultiSelectStatusProps) {
|
|
87
|
+
useMultiSelectContext()
|
|
88
|
+
return <ComboboxStatus {...props} />
|
|
89
|
+
}
|
|
90
|
+
export function MultiSelectTags(props: MultiSelectTagsProps) {
|
|
91
|
+
useMultiSelectContext()
|
|
92
|
+
return <ComboboxTags {...props} />
|
|
93
|
+
}
|
package/src/OTPField.tsx
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useEffect, useRef, useState } from 'react'
|
|
4
|
+
import { InputRoot, inputVariants, type InputProps } from './Input.js'
|
|
5
|
+
import { useFieldControl } from './Field.js'
|
|
6
|
+
import { cn } from './cn.js'
|
|
7
|
+
import { controlMetrics } from './controlStyles.js'
|
|
8
|
+
import { useComposedRefs } from './react/useComposedRefs.js'
|
|
9
|
+
import { useNativeOwner, type ValueOwner } from './react/useNativeOwner.js'
|
|
10
|
+
import type { DistributiveOmit } from './internal/types.js'
|
|
11
|
+
import type { PartStyle } from './presentation.js'
|
|
12
|
+
|
|
13
|
+
export type OTPFieldMode = 'numeric' | 'alphanumeric'
|
|
14
|
+
export type OTPFieldProps = DistributiveOmit<
|
|
15
|
+
InputProps,
|
|
16
|
+
| 'value'
|
|
17
|
+
| 'defaultValue'
|
|
18
|
+
| 'type'
|
|
19
|
+
| 'children'
|
|
20
|
+
| 'pattern'
|
|
21
|
+
| 'maxLength'
|
|
22
|
+
| 'minLength'
|
|
23
|
+
| 'inputMode'
|
|
24
|
+
| 'autoComplete'
|
|
25
|
+
> &
|
|
26
|
+
ValueOwner<string> & {
|
|
27
|
+
length?: number
|
|
28
|
+
mode?: OTPFieldMode
|
|
29
|
+
parts?: { wrapper?: PartStyle; slot?: PartStyle }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** One native editing/form host; the character slots are purely visual. */
|
|
33
|
+
export function OTPField({
|
|
34
|
+
value,
|
|
35
|
+
defaultValue,
|
|
36
|
+
length = 6,
|
|
37
|
+
mode = 'numeric',
|
|
38
|
+
parts,
|
|
39
|
+
ref,
|
|
40
|
+
className,
|
|
41
|
+
controlSize = 'medium',
|
|
42
|
+
onChange,
|
|
43
|
+
onPaste,
|
|
44
|
+
onFocus,
|
|
45
|
+
onBlur,
|
|
46
|
+
onSelect,
|
|
47
|
+
onPointerDown,
|
|
48
|
+
onKeyDown,
|
|
49
|
+
onCompositionStart,
|
|
50
|
+
onCompositionEnd,
|
|
51
|
+
...props
|
|
52
|
+
}: OTPFieldProps) {
|
|
53
|
+
if (!Number.isInteger(length) || length < 1 || length > 32)
|
|
54
|
+
throw new TypeError('OTPField length must be an integer from 1 to 32.')
|
|
55
|
+
if (mode !== 'numeric' && mode !== 'alphanumeric')
|
|
56
|
+
throw new TypeError('OTPField mode must be numeric or alphanumeric.')
|
|
57
|
+
const [configuration] = useState({ length, mode })
|
|
58
|
+
if (configuration.length !== length || configuration.mode !== mode)
|
|
59
|
+
throw new TypeError('OTPField cannot change length or mode. Remount with a new key.')
|
|
60
|
+
const controlled = useNativeOwner('OTPField', value, defaultValue)
|
|
61
|
+
if (
|
|
62
|
+
(value !== undefined && typeof value !== 'string') ||
|
|
63
|
+
(defaultValue !== undefined && typeof defaultValue !== 'string')
|
|
64
|
+
)
|
|
65
|
+
throw new TypeError('OTPField values must be strings.')
|
|
66
|
+
if ((value ?? defaultValue ?? '').length > length)
|
|
67
|
+
throw new TypeError('OTPField values must fit the configured slots.')
|
|
68
|
+
const [local, setLocal] = useState(defaultValue ?? ''),
|
|
69
|
+
[position, setPosition] = useState(0),
|
|
70
|
+
[focused, setFocused] = useState(false)
|
|
71
|
+
const input = useRef<HTMLInputElement>(null),
|
|
72
|
+
composed = useComposedRefs(input, ref),
|
|
73
|
+
composing = useRef(false)
|
|
74
|
+
const control = useFieldControl(props)
|
|
75
|
+
const text = value ?? local
|
|
76
|
+
const normalize = (text: string) =>
|
|
77
|
+
text.replace(mode === 'numeric' ? /[^0-9]/g : /[^a-zA-Z0-9]/g, '').slice(0, length)
|
|
78
|
+
const unavailable = (node: HTMLInputElement) => node.readOnly || node.matches(':disabled')
|
|
79
|
+
const locate = (node: HTMLInputElement) => setPosition(node.selectionStart ?? 0)
|
|
80
|
+
const replace = (node: HTMLInputElement, next: string, caret: number) => {
|
|
81
|
+
const view = node.ownerDocument.defaultView
|
|
82
|
+
if (!view) return
|
|
83
|
+
Object.getOwnPropertyDescriptor(view.HTMLInputElement.prototype, 'value')!.set!.call(node, next)
|
|
84
|
+
node.dispatchEvent(new view.InputEvent('input', { bubbles: true, inputType: 'insertText' }))
|
|
85
|
+
node.setSelectionRange(Math.min(caret, node.value.length), Math.min(caret, node.value.length))
|
|
86
|
+
locate(node)
|
|
87
|
+
}
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
const node = input.current,
|
|
90
|
+
form = node?.form,
|
|
91
|
+
view = node?.ownerDocument.defaultView
|
|
92
|
+
if (!node || !form || !view || controlled) return
|
|
93
|
+
let timer: number | undefined
|
|
94
|
+
const reset = (event: Event) => {
|
|
95
|
+
view.clearTimeout(timer)
|
|
96
|
+
timer = view.setTimeout(() => {
|
|
97
|
+
if (event.defaultPrevented || !node.isConnected) return
|
|
98
|
+
setLocal(node.value)
|
|
99
|
+
locate(node)
|
|
100
|
+
}, 0)
|
|
101
|
+
}
|
|
102
|
+
form.addEventListener('reset', reset)
|
|
103
|
+
return () => {
|
|
104
|
+
form.removeEventListener('reset', reset)
|
|
105
|
+
view.clearTimeout(timer)
|
|
106
|
+
}
|
|
107
|
+
}, [controlled, props.form])
|
|
108
|
+
const active = Math.min(position, text.length, length - 1)
|
|
109
|
+
return (
|
|
110
|
+
<div
|
|
111
|
+
dir={props.dir}
|
|
112
|
+
data-slot="otp-field-wrapper"
|
|
113
|
+
className={cn(
|
|
114
|
+
'group/otp relative inline-grid max-w-full gap-[var(--rad-otp-field-gap,0.375rem)] has-[:disabled]:cursor-not-allowed',
|
|
115
|
+
controlMetrics({ controlSize }),
|
|
116
|
+
parts?.wrapper?.className
|
|
117
|
+
)}
|
|
118
|
+
style={{
|
|
119
|
+
gridTemplateColumns: `repeat(${length}, minmax(0, 1fr))`,
|
|
120
|
+
width: `calc(${length} * var(--rad-otp-field-slot-size,var(--_rad-control-height)) + ${length - 1} * var(--rad-otp-field-gap,0.375rem))`,
|
|
121
|
+
...parts?.wrapper?.style
|
|
122
|
+
}}
|
|
123
|
+
>
|
|
124
|
+
<InputRoot
|
|
125
|
+
{...props}
|
|
126
|
+
{...(value === undefined ? { defaultValue } : { value })}
|
|
127
|
+
ref={composed}
|
|
128
|
+
family="otp-field"
|
|
129
|
+
nativeRequired={undefined}
|
|
130
|
+
type="text"
|
|
131
|
+
controlSize={controlSize}
|
|
132
|
+
autoComplete="one-time-code"
|
|
133
|
+
inputMode={mode === 'numeric' ? 'numeric' : 'text'}
|
|
134
|
+
pattern={mode === 'numeric' ? `[0-9]{${length}}` : `[a-zA-Z0-9]{${length}}`}
|
|
135
|
+
maxLength={length}
|
|
136
|
+
spellCheck={props.spellCheck ?? false}
|
|
137
|
+
className={cn('absolute inset-0 z-10 h-full w-full opacity-0', className)}
|
|
138
|
+
onChange={event => {
|
|
139
|
+
const node = event.currentTarget
|
|
140
|
+
if (unavailable(node)) return
|
|
141
|
+
if (!composing.current) {
|
|
142
|
+
const before = node.value,
|
|
143
|
+
caret = node.selectionStart ?? before.length
|
|
144
|
+
node.value = normalize(before)
|
|
145
|
+
const nextCaret = normalize(before.slice(0, caret)).length
|
|
146
|
+
node.setSelectionRange(nextCaret, nextCaret)
|
|
147
|
+
}
|
|
148
|
+
if (!controlled) setLocal(node.value)
|
|
149
|
+
locate(node)
|
|
150
|
+
onChange?.(event)
|
|
151
|
+
}}
|
|
152
|
+
onPaste={event => {
|
|
153
|
+
const node = event.currentTarget
|
|
154
|
+
if (unavailable(node)) return
|
|
155
|
+
onPaste?.(event)
|
|
156
|
+
if (event.defaultPrevented || composing.current) return
|
|
157
|
+
event.preventDefault()
|
|
158
|
+
const start = node.selectionStart ?? node.value.length,
|
|
159
|
+
end = node.selectionEnd ?? start,
|
|
160
|
+
inserted = normalize(event.clipboardData.getData('text')),
|
|
161
|
+
next = normalize(node.value.slice(0, start) + inserted + node.value.slice(end))
|
|
162
|
+
if (next !== node.value) replace(node, next, start + inserted.length)
|
|
163
|
+
}}
|
|
164
|
+
onFocus={event => {
|
|
165
|
+
setFocused(true)
|
|
166
|
+
locate(event.currentTarget)
|
|
167
|
+
onFocus?.(event)
|
|
168
|
+
}}
|
|
169
|
+
onBlur={event => {
|
|
170
|
+
setFocused(false)
|
|
171
|
+
onBlur?.(event)
|
|
172
|
+
}}
|
|
173
|
+
onSelect={event => {
|
|
174
|
+
locate(event.currentTarget)
|
|
175
|
+
onSelect?.(event)
|
|
176
|
+
}}
|
|
177
|
+
onPointerDown={event => {
|
|
178
|
+
onPointerDown?.(event)
|
|
179
|
+
if (event.defaultPrevented || event.button !== 0) return
|
|
180
|
+
const node = event.currentTarget,
|
|
181
|
+
slots = node.parentElement?.querySelectorAll<HTMLElement>(
|
|
182
|
+
'[data-slot="otp-field-slot"]'
|
|
183
|
+
)
|
|
184
|
+
if (!slots?.length) return
|
|
185
|
+
const x = event.clientX
|
|
186
|
+
const index = [...slots].findIndex(slot => {
|
|
187
|
+
const box = slot.getBoundingClientRect()
|
|
188
|
+
return x >= box.left && x <= box.right
|
|
189
|
+
})
|
|
190
|
+
if (index < 0) return
|
|
191
|
+
event.preventDefault()
|
|
192
|
+
node.focus()
|
|
193
|
+
const caret = Math.min(index, node.value.length)
|
|
194
|
+
node.setSelectionRange(caret, caret)
|
|
195
|
+
locate(node)
|
|
196
|
+
}}
|
|
197
|
+
onKeyDown={event => {
|
|
198
|
+
onKeyDown?.(event)
|
|
199
|
+
if (
|
|
200
|
+
event.defaultPrevented ||
|
|
201
|
+
event.shiftKey ||
|
|
202
|
+
event.altKey ||
|
|
203
|
+
event.ctrlKey ||
|
|
204
|
+
event.metaKey ||
|
|
205
|
+
composing.current
|
|
206
|
+
)
|
|
207
|
+
return
|
|
208
|
+
if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') return
|
|
209
|
+
const node = event.currentTarget,
|
|
210
|
+
rtl = node.ownerDocument.defaultView?.getComputedStyle(node).direction === 'rtl',
|
|
211
|
+
forward = (event.key === 'ArrowRight') !== rtl,
|
|
212
|
+
caret = Math.max(
|
|
213
|
+
0,
|
|
214
|
+
Math.min(node.value.length, (node.selectionStart ?? 0) + (forward ? 1 : -1))
|
|
215
|
+
)
|
|
216
|
+
event.preventDefault()
|
|
217
|
+
node.setSelectionRange(caret, caret)
|
|
218
|
+
locate(node)
|
|
219
|
+
}}
|
|
220
|
+
onCompositionStart={event => {
|
|
221
|
+
composing.current = true
|
|
222
|
+
onCompositionStart?.(event)
|
|
223
|
+
}}
|
|
224
|
+
onCompositionEnd={event => {
|
|
225
|
+
composing.current = false
|
|
226
|
+
onCompositionEnd?.(event)
|
|
227
|
+
const node = event.currentTarget
|
|
228
|
+
if (unavailable(node)) return
|
|
229
|
+
const next = normalize(node.value)
|
|
230
|
+
if (next !== node.value) replace(node, next, next.length)
|
|
231
|
+
}}
|
|
232
|
+
/>
|
|
233
|
+
{Array.from({ length }, (_, index) => (
|
|
234
|
+
<span
|
|
235
|
+
key={index}
|
|
236
|
+
aria-hidden="true"
|
|
237
|
+
data-slot="otp-field-slot"
|
|
238
|
+
data-active={focused && active === index ? '' : undefined}
|
|
239
|
+
data-invalid={control['data-invalid']}
|
|
240
|
+
className={cn(
|
|
241
|
+
inputVariants(),
|
|
242
|
+
'pointer-events-none flex items-center justify-center px-0 group-has-[:disabled]/otp:opacity-60 data-active:outline-solid data-active:[outline-color:var(--rad-input-focus-color,var(--rad-color-focus,oklch(0.55_0.13_255)))] data-active:[outline-width:var(--rad-focus-width,2px)] data-active:[outline-offset:var(--rad-focus-offset,2px)]',
|
|
243
|
+
parts?.slot?.className
|
|
244
|
+
)}
|
|
245
|
+
style={parts?.slot?.style}
|
|
246
|
+
>
|
|
247
|
+
{text[index] ?? '\u00a0'}
|
|
248
|
+
</span>
|
|
249
|
+
))}
|
|
250
|
+
</div>
|
|
251
|
+
)
|
|
252
|
+
}
|
package/src/Tag.tsx
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import type { MouseEvent, ReactNode } from 'react'
|
|
4
|
+
import { Badge, type BadgeProps } from './Badge.js'
|
|
5
|
+
import { Button } from './Button.js'
|
|
6
|
+
import { cn } from './cn.js'
|
|
7
|
+
import { joinIds } from './internal/ids.js'
|
|
8
|
+
import { requireAccessibleName } from './internal/accessibleName.js'
|
|
9
|
+
import type { PartStyle } from './presentation.js'
|
|
10
|
+
|
|
11
|
+
export type TagProps = Omit<BadgeProps, 'decorative' | 'children'> & {
|
|
12
|
+
children: ReactNode
|
|
13
|
+
/** Name the actual removal Button, including when the content is rich. */
|
|
14
|
+
removeLabel: string
|
|
15
|
+
onRemove(event: MouseEvent<HTMLButtonElement>): void
|
|
16
|
+
disabled?: boolean
|
|
17
|
+
pending?: boolean
|
|
18
|
+
parts?: { remove?: PartStyle }
|
|
19
|
+
}
|
|
20
|
+
/** Removable content uses the real Badge span and a real, named Button. */
|
|
21
|
+
export function Tag({
|
|
22
|
+
removeLabel,
|
|
23
|
+
onRemove,
|
|
24
|
+
disabled = false,
|
|
25
|
+
pending = false,
|
|
26
|
+
parts,
|
|
27
|
+
className,
|
|
28
|
+
children,
|
|
29
|
+
...props
|
|
30
|
+
}: TagProps) {
|
|
31
|
+
requireAccessibleName('Tag removal', { 'aria-label': removeLabel })
|
|
32
|
+
return (
|
|
33
|
+
<Badge
|
|
34
|
+
{...props}
|
|
35
|
+
decorative={false}
|
|
36
|
+
data-slot={joinIds('tag', props['data-slot'])}
|
|
37
|
+
className={cn('gap-[var(--rad-tag-gap,0.25rem)]', className)}
|
|
38
|
+
>
|
|
39
|
+
<span className="min-w-0 break-words">{children}</span>
|
|
40
|
+
<Button
|
|
41
|
+
type="button"
|
|
42
|
+
variant="ghost"
|
|
43
|
+
tone={props.tone ?? 'neutral'}
|
|
44
|
+
controlSize="small"
|
|
45
|
+
motion="off"
|
|
46
|
+
iconOnly
|
|
47
|
+
aria-label={removeLabel}
|
|
48
|
+
disabled={disabled}
|
|
49
|
+
pending={pending}
|
|
50
|
+
data-slot="tag-remove"
|
|
51
|
+
className={cn('size-6 min-h-0 shrink-0 p-0', parts?.remove?.className)}
|
|
52
|
+
style={parts?.remove?.style}
|
|
53
|
+
onClick={onRemove}
|
|
54
|
+
>
|
|
55
|
+
<span aria-hidden="true">×</span>
|
|
56
|
+
</Button>
|
|
57
|
+
</Badge>
|
|
58
|
+
)
|
|
59
|
+
}
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useId, useLayoutEffect, useRef, useState } from 'react'
|
|
4
|
+
import { InputRoot, type InputProps } from './Input.js'
|
|
5
|
+
import { Tag } from './Tag.js'
|
|
6
|
+
import { useFieldControl } from './Field.js'
|
|
7
|
+
import { cn } from './cn.js'
|
|
8
|
+
import { joinIds } from './internal/ids.js'
|
|
9
|
+
import { sameSelection } from './internal/selection.js'
|
|
10
|
+
import { splitTags, tagSeparators, validateTags } from './internal/tags.js'
|
|
11
|
+
import type { DistributiveOmit } from './internal/types.js'
|
|
12
|
+
import { useControllableState } from './react/useControllableState.js'
|
|
13
|
+
import { useComposedRefs } from './react/useComposedRefs.js'
|
|
14
|
+
import { useArrayFormBridge } from './react/useArrayFormBridge.js'
|
|
15
|
+
import type { ValueOwner } from './react/useNativeOwner.js'
|
|
16
|
+
import type { PartStyle } from './presentation.js'
|
|
17
|
+
|
|
18
|
+
export type TagsInputProps = DistributiveOmit<
|
|
19
|
+
InputProps,
|
|
20
|
+
'value' | 'defaultValue' | 'type' | 'children'
|
|
21
|
+
> &
|
|
22
|
+
ValueOwner<string[]> & {
|
|
23
|
+
onValueChange?: (values: string[]) => void
|
|
24
|
+
/** Single-character separators; line breaks in pasted text also separate tags. */
|
|
25
|
+
separators?: readonly string[]
|
|
26
|
+
/** True accepts; false or an error message rejects. Values are trimmed before validation. */
|
|
27
|
+
validate?: (value: string) => boolean | string
|
|
28
|
+
maxCount?: number
|
|
29
|
+
parts?: { wrapper?: PartStyle; tag?: PartStyle; remove?: PartStyle; message?: PartStyle }
|
|
30
|
+
}
|
|
31
|
+
const defaultSeparators = [','] as const
|
|
32
|
+
|
|
33
|
+
/** Native text editing and Tag removal; only committed array values are submitted. */
|
|
34
|
+
export function TagsInput({
|
|
35
|
+
value,
|
|
36
|
+
defaultValue,
|
|
37
|
+
onValueChange,
|
|
38
|
+
separators = defaultSeparators,
|
|
39
|
+
validate,
|
|
40
|
+
maxCount,
|
|
41
|
+
parts,
|
|
42
|
+
name,
|
|
43
|
+
form,
|
|
44
|
+
disabled,
|
|
45
|
+
required,
|
|
46
|
+
readOnly,
|
|
47
|
+
className,
|
|
48
|
+
ref,
|
|
49
|
+
onChange,
|
|
50
|
+
onKeyDown,
|
|
51
|
+
onPaste,
|
|
52
|
+
onCompositionStart,
|
|
53
|
+
onCompositionEnd,
|
|
54
|
+
...props
|
|
55
|
+
}: TagsInputProps) {
|
|
56
|
+
const control = useFieldControl({
|
|
57
|
+
id: props.id,
|
|
58
|
+
disabled,
|
|
59
|
+
required,
|
|
60
|
+
'aria-describedby': props['aria-describedby'],
|
|
61
|
+
'aria-invalid': props['aria-invalid'],
|
|
62
|
+
'aria-required': props['aria-required']
|
|
63
|
+
})
|
|
64
|
+
const selection = useControllableState({
|
|
65
|
+
name: 'TagsInput',
|
|
66
|
+
value,
|
|
67
|
+
defaultValue: defaultValue ? [...defaultValue] : undefined,
|
|
68
|
+
initialValue: [] as string[],
|
|
69
|
+
equal: sameSelection,
|
|
70
|
+
onChange: (next: string[]) => onValueChange?.([...next])
|
|
71
|
+
})
|
|
72
|
+
validateTags(selection.value)
|
|
73
|
+
if (selection.value.some(value => !value.trim() || value !== value.trim()))
|
|
74
|
+
throw new TypeError('TagsInput values must be nonempty trimmed strings.')
|
|
75
|
+
if (maxCount !== undefined && (!Number.isInteger(maxCount) || maxCount < 0))
|
|
76
|
+
throw new TypeError('TagsInput maxCount must be a nonnegative integer.')
|
|
77
|
+
const delimiters = tagSeparators(separators)
|
|
78
|
+
const [draft, setDraft] = useState(''),
|
|
79
|
+
[error, setError] = useState('')
|
|
80
|
+
const input = useRef<HTMLInputElement>(null),
|
|
81
|
+
composed = useComposedRefs(input, ref),
|
|
82
|
+
composing = useRef(false)
|
|
83
|
+
const message = useId(),
|
|
84
|
+
pending = useRef<string[] | null>(null)
|
|
85
|
+
const draftRequest = useRef<{ values: string[]; remainder: string; error: string } | null>(null)
|
|
86
|
+
const maximum = maxCount === undefined ? Infinity : maxCount
|
|
87
|
+
const limitMessage = `Use at most ${maxCount} tags.`
|
|
88
|
+
const feedback = selection.value.length > maximum ? limitMessage : error
|
|
89
|
+
const unavailable = () => control.disabled || readOnly || !!input.current?.matches(':disabled')
|
|
90
|
+
const request = (next: string[]) => {
|
|
91
|
+
if (unavailable() || sameSelection(next, selection.value)) return
|
|
92
|
+
pending.current = next
|
|
93
|
+
selection.request(next)
|
|
94
|
+
queueMicrotask(() => {
|
|
95
|
+
pending.current = null
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
const remove = (value: string) => {
|
|
99
|
+
if (unavailable()) return
|
|
100
|
+
draftRequest.current = null
|
|
101
|
+
request(selection.value.filter(item => item !== value))
|
|
102
|
+
setError('')
|
|
103
|
+
input.current?.focus({ preventScroll: true })
|
|
104
|
+
}
|
|
105
|
+
function add(text: string, final: boolean) {
|
|
106
|
+
if (unavailable()) return
|
|
107
|
+
const { segments, remainder } = splitTags(text, delimiters, final)
|
|
108
|
+
if (!segments.length) return
|
|
109
|
+
const next = [...selection.value],
|
|
110
|
+
rejected: string[] = []
|
|
111
|
+
let reason = ''
|
|
112
|
+
for (const segment of segments) {
|
|
113
|
+
const candidate = segment.trim()
|
|
114
|
+
if (!candidate) continue
|
|
115
|
+
const verdict = validate?.(candidate) ?? true
|
|
116
|
+
const failure = next.includes(candidate)
|
|
117
|
+
? 'This tag is already added.'
|
|
118
|
+
: next.length >= maximum
|
|
119
|
+
? limitMessage
|
|
120
|
+
: verdict === true
|
|
121
|
+
? ''
|
|
122
|
+
: typeof verdict === 'string' && verdict
|
|
123
|
+
? verdict
|
|
124
|
+
: 'Enter a valid tag.'
|
|
125
|
+
if (failure) {
|
|
126
|
+
rejected.push(segment)
|
|
127
|
+
reason ||= failure
|
|
128
|
+
} else next.push(candidate)
|
|
129
|
+
}
|
|
130
|
+
const rest = [...rejected, ...(remainder ? [remainder] : [])].join(separators[0] ?? ',')
|
|
131
|
+
setError(reason)
|
|
132
|
+
if (sameSelection(next, selection.value)) {
|
|
133
|
+
draftRequest.current = null
|
|
134
|
+
setDraft(rest)
|
|
135
|
+
setError(reason)
|
|
136
|
+
return
|
|
137
|
+
}
|
|
138
|
+
draftRequest.current = { values: next, remainder: rest, error: reason }
|
|
139
|
+
request(next)
|
|
140
|
+
}
|
|
141
|
+
useLayoutEffect(() => {
|
|
142
|
+
const change = draftRequest.current
|
|
143
|
+
if (change && sameSelection(change.values, selection.value)) {
|
|
144
|
+
draftRequest.current = null
|
|
145
|
+
setDraft(change.remainder)
|
|
146
|
+
setError(change.error)
|
|
147
|
+
}
|
|
148
|
+
}, [selection.value])
|
|
149
|
+
const bridgeRef = useArrayFormBridge({
|
|
150
|
+
values: selection.value,
|
|
151
|
+
controlled: selection.controlled,
|
|
152
|
+
pending,
|
|
153
|
+
form,
|
|
154
|
+
validity:
|
|
155
|
+
feedback ||
|
|
156
|
+
(control.required && !selection.value.length ? 'Please enter at least one tag.' : ''),
|
|
157
|
+
reset() {
|
|
158
|
+
selection.reset()
|
|
159
|
+
draftRequest.current = null
|
|
160
|
+
setDraft('')
|
|
161
|
+
setError('')
|
|
162
|
+
}
|
|
163
|
+
})
|
|
164
|
+
const edit = (text: string) => {
|
|
165
|
+
draftRequest.current = null
|
|
166
|
+
setDraft(text)
|
|
167
|
+
setError('')
|
|
168
|
+
if (!composing.current) add(text, false)
|
|
169
|
+
}
|
|
170
|
+
return (
|
|
171
|
+
<div
|
|
172
|
+
data-slot="tags-input-control"
|
|
173
|
+
className={cn(
|
|
174
|
+
'flex min-w-0 flex-wrap items-center gap-[var(--rad-tags-input-gap,0.5rem)]',
|
|
175
|
+
parts?.wrapper?.className
|
|
176
|
+
)}
|
|
177
|
+
style={parts?.wrapper?.style}
|
|
178
|
+
>
|
|
179
|
+
{selection.value.map(value => (
|
|
180
|
+
<Tag
|
|
181
|
+
key={value}
|
|
182
|
+
removeLabel={`Remove ${value}`}
|
|
183
|
+
disabled={!!(control.disabled || readOnly)}
|
|
184
|
+
className={parts?.tag?.className}
|
|
185
|
+
style={parts?.tag?.style}
|
|
186
|
+
parts={{ remove: parts?.remove }}
|
|
187
|
+
onRemove={() => remove(value)}
|
|
188
|
+
>
|
|
189
|
+
{value}
|
|
190
|
+
</Tag>
|
|
191
|
+
))}
|
|
192
|
+
<InputRoot
|
|
193
|
+
{...props}
|
|
194
|
+
{...control}
|
|
195
|
+
family="tags-input"
|
|
196
|
+
nativeRequired={false}
|
|
197
|
+
type="text"
|
|
198
|
+
name={undefined}
|
|
199
|
+
form={form}
|
|
200
|
+
ref={composed}
|
|
201
|
+
readOnly={readOnly}
|
|
202
|
+
value={draft}
|
|
203
|
+
autoComplete={props.autoComplete ?? 'off'}
|
|
204
|
+
aria-invalid={feedback ? true : control['aria-invalid']}
|
|
205
|
+
aria-describedby={joinIds(control['aria-describedby'], message)}
|
|
206
|
+
className={cn('w-auto min-w-[8ch] flex-1', className)}
|
|
207
|
+
onChange={event => {
|
|
208
|
+
onChange?.(event)
|
|
209
|
+
edit(event.currentTarget.value)
|
|
210
|
+
}}
|
|
211
|
+
onCompositionStart={event => {
|
|
212
|
+
onCompositionStart?.(event)
|
|
213
|
+
composing.current = true
|
|
214
|
+
}}
|
|
215
|
+
onCompositionEnd={event => {
|
|
216
|
+
onCompositionEnd?.(event)
|
|
217
|
+
composing.current = false
|
|
218
|
+
edit(event.currentTarget.value)
|
|
219
|
+
}}
|
|
220
|
+
onPaste={event => {
|
|
221
|
+
onPaste?.(event)
|
|
222
|
+
if (event.defaultPrevented || unavailable() || composing.current) return
|
|
223
|
+
const text = event.clipboardData.getData('text')
|
|
224
|
+
if (![...text].some(character => delimiters.includes(character))) return
|
|
225
|
+
event.preventDefault()
|
|
226
|
+
const node = event.currentTarget
|
|
227
|
+
const next =
|
|
228
|
+
draft.slice(0, node.selectionStart ?? draft.length) +
|
|
229
|
+
text +
|
|
230
|
+
draft.slice(node.selectionEnd ?? draft.length)
|
|
231
|
+
draftRequest.current = null
|
|
232
|
+
setDraft(next)
|
|
233
|
+
setError('')
|
|
234
|
+
add(next, true)
|
|
235
|
+
}}
|
|
236
|
+
onKeyDown={event => {
|
|
237
|
+
onKeyDown?.(event)
|
|
238
|
+
if (
|
|
239
|
+
event.defaultPrevented ||
|
|
240
|
+
unavailable() ||
|
|
241
|
+
composing.current ||
|
|
242
|
+
event.nativeEvent.isComposing ||
|
|
243
|
+
event.keyCode === 229 ||
|
|
244
|
+
event.ctrlKey ||
|
|
245
|
+
event.metaKey ||
|
|
246
|
+
event.altKey
|
|
247
|
+
)
|
|
248
|
+
return
|
|
249
|
+
if (event.key === 'Enter' && draft.trim()) {
|
|
250
|
+
event.preventDefault()
|
|
251
|
+
add(draft, true)
|
|
252
|
+
} else if (event.key === 'Backspace' && !draft && selection.value.length) {
|
|
253
|
+
event.preventDefault()
|
|
254
|
+
remove(selection.value.at(-1)!)
|
|
255
|
+
}
|
|
256
|
+
}}
|
|
257
|
+
/>
|
|
258
|
+
<span
|
|
259
|
+
id={message}
|
|
260
|
+
data-slot="tags-input-message"
|
|
261
|
+
aria-live="polite"
|
|
262
|
+
aria-atomic="true"
|
|
263
|
+
className={cn(
|
|
264
|
+
'w-full empty:hidden text-[length:var(--rad-typography-roles-ui-supporting-size,0.8125rem)] [color:var(--rad-color-invalid,oklch(0.56_0.19_25))]',
|
|
265
|
+
parts?.message?.className
|
|
266
|
+
)}
|
|
267
|
+
style={parts?.message?.style}
|
|
268
|
+
>
|
|
269
|
+
{feedback}
|
|
270
|
+
</span>
|
|
271
|
+
<input
|
|
272
|
+
ref={bridgeRef}
|
|
273
|
+
type="text"
|
|
274
|
+
className="sr-only"
|
|
275
|
+
data-slot="tags-input-native"
|
|
276
|
+
form={form}
|
|
277
|
+
disabled={control.disabled}
|
|
278
|
+
readOnly={readOnly}
|
|
279
|
+
aria-hidden="true"
|
|
280
|
+
tabIndex={-1}
|
|
281
|
+
autoComplete="off"
|
|
282
|
+
defaultValue={JSON.stringify(selection.value)}
|
|
283
|
+
onFocus={() => input.current?.focus()}
|
|
284
|
+
onInvalid={event => {
|
|
285
|
+
event.preventDefault()
|
|
286
|
+
input.current?.focus()
|
|
287
|
+
}}
|
|
288
|
+
/>
|
|
289
|
+
{selection.value.map(value => (
|
|
290
|
+
<input
|
|
291
|
+
key={value}
|
|
292
|
+
data-slot="tags-input-value"
|
|
293
|
+
type="hidden"
|
|
294
|
+
name={name}
|
|
295
|
+
form={form}
|
|
296
|
+
disabled={control.disabled}
|
|
297
|
+
value={value}
|
|
298
|
+
/>
|
|
299
|
+
))}
|
|
300
|
+
</div>
|
|
301
|
+
)
|
|
302
|
+
}
|