@cognitap/radiant 0.0.89 → 0.0.90
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 +10 -0
- package/dist/library/components/DateField/DateField.d.ts +7 -0
- package/dist/library/components/DateField/DateField.d.ts.map +1 -0
- package/dist/library/components/DateField/DateField.js +15 -0
- package/dist/library/components/DateField/DateField.js.map +1 -0
- package/dist/library/components/DatePicker/DateEditor.d.ts +4 -2
- package/dist/library/components/DatePicker/DateEditor.d.ts.map +1 -1
- package/dist/library/components/DatePicker/DateEditor.js +3 -3
- package/dist/library/components/DatePicker/DateEditor.js.map +1 -1
- package/dist/library/components/InlineEdit/InlineEdit.d.ts +19 -0
- package/dist/library/components/InlineEdit/InlineEdit.d.ts.map +1 -0
- package/dist/library/components/InlineEdit/InlineEdit.js +73 -0
- package/dist/library/components/InlineEdit/InlineEdit.js.map +1 -0
- package/dist/library/components/ListBox/ListBox.d.ts +27 -0
- package/dist/library/components/ListBox/ListBox.d.ts.map +1 -0
- package/dist/library/components/ListBox/ListBox.js +166 -0
- package/dist/library/components/ListBox/ListBox.js.map +1 -0
- package/dist/library/components/RangeSlider/RangeSlider.d.ts +24 -0
- package/dist/library/components/RangeSlider/RangeSlider.d.ts.map +1 -0
- package/dist/library/components/RangeSlider/RangeSlider.js +109 -0
- package/dist/library/components/RangeSlider/RangeSlider.js.map +1 -0
- package/dist/library/components/Slider/Slider.d.ts +6 -1
- package/dist/library/components/Slider/Slider.d.ts.map +1 -1
- package/dist/library/components/Slider/Slider.js +16 -10
- package/dist/library/components/Slider/Slider.js.map +1 -1
- package/dist/library/index.d.ts +4 -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/decimal.d.ts +2 -0
- package/dist/library/internal/decimal.d.ts.map +1 -1
- package/dist/library/internal/decimal.js +12 -7
- package/dist/library/internal/decimal.js.map +1 -1
- package/dist/library/styles.css +1 -1
- package/package.json +1 -1
- package/src/components/DateField/DateField.tsx +32 -0
- package/src/components/DatePicker/DateEditor.tsx +16 -11
- package/src/components/InlineEdit/InlineEdit.tsx +172 -0
- package/src/components/ListBox/ListBox.tsx +318 -0
- package/src/components/RangeSlider/RangeSlider.tsx +245 -0
- package/src/components/Slider/Slider.tsx +28 -11
- package/src/index.ts +8 -0
- package/src/internal/decimal.ts +14 -7
package/package.json
CHANGED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useControllableState } from '../../react/useControllableState.js'
|
|
4
|
+
import type { ValueOwner } from '../../react/useNativeOwner.js'
|
|
5
|
+
import { DateEditor, type DateEditorControlProps } from '../DatePicker/DateEditor.js'
|
|
6
|
+
|
|
7
|
+
export type DateFieldProps = DateEditorControlProps &
|
|
8
|
+
ValueOwner<string | null> & {
|
|
9
|
+
onValueChange?: (iso: string | null) => void
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function DateField({ value, defaultValue, onValueChange, ...control }: DateFieldProps) {
|
|
13
|
+
const owner = useControllableState({
|
|
14
|
+
name: 'DateField value',
|
|
15
|
+
value,
|
|
16
|
+
defaultValue,
|
|
17
|
+
initialValue: null,
|
|
18
|
+
onChange: onValueChange
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
return (
|
|
22
|
+
<DateEditor
|
|
23
|
+
{...control}
|
|
24
|
+
componentName="DateField"
|
|
25
|
+
slot="date-field"
|
|
26
|
+
revision={0}
|
|
27
|
+
value={owner.value}
|
|
28
|
+
onValueChange={owner.request}
|
|
29
|
+
onFormReset={owner.reset}
|
|
30
|
+
/>
|
|
31
|
+
)
|
|
32
|
+
}
|
|
@@ -18,7 +18,7 @@ import { useLocale } from '../../react/useLocale.js'
|
|
|
18
18
|
import { dateSegments, type Segment, type Segments } from './dateSegments.js'
|
|
19
19
|
import type { CalendarProps } from '../Calendar/Calendar.js'
|
|
20
20
|
|
|
21
|
-
type
|
|
21
|
+
export type DateEditorControlProps = Pick<
|
|
22
22
|
ComponentProps<'input'>,
|
|
23
23
|
| 'id'
|
|
24
24
|
| 'ref'
|
|
@@ -33,15 +33,19 @@ type EditorProps = Pick<
|
|
|
33
33
|
| 'aria-invalid'
|
|
34
34
|
| 'className'
|
|
35
35
|
> &
|
|
36
|
-
Pick<CalendarProps, 'controlSize' | 'locale' | 'calendar' | 'min' | 'max' | 'isDateDisabled'>
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
36
|
+
Pick<CalendarProps, 'controlSize' | 'locale' | 'calendar' | 'min' | 'max' | 'isDateDisabled'>
|
|
37
|
+
|
|
38
|
+
type EditorProps = DateEditorControlProps & {
|
|
39
|
+
componentName?: 'DatePicker' | 'DateField'
|
|
40
|
+
value: string | null
|
|
41
|
+
onValueChange: (date: string | null) => void
|
|
42
|
+
onFormReset?: () => void
|
|
43
|
+
revision: number
|
|
44
|
+
slot: string
|
|
45
|
+
children?: ReactNode
|
|
46
|
+
}
|
|
44
47
|
export function DateEditor({
|
|
48
|
+
componentName = 'DatePicker',
|
|
45
49
|
value,
|
|
46
50
|
onValueChange,
|
|
47
51
|
onFormReset,
|
|
@@ -65,10 +69,11 @@ export function DateEditor({
|
|
|
65
69
|
const control = useFieldControl(props)
|
|
66
70
|
const labelledBy = props['aria-labelledby'] ?? field?.labelId
|
|
67
71
|
const label = props['aria-label']
|
|
68
|
-
requireAccessibleName(
|
|
72
|
+
requireAccessibleName(componentName, { 'aria-label': label, 'aria-labelledby': labelledBy })
|
|
69
73
|
if (min !== undefined) civilTime(min)
|
|
70
74
|
if (max !== undefined) civilTime(max)
|
|
71
|
-
if (min && max && min > max)
|
|
75
|
+
if (min && max && min > max)
|
|
76
|
+
throw new RangeError(componentName + ' min must be on or before max.')
|
|
72
77
|
|
|
73
78
|
const language = useLocale(locale)
|
|
74
79
|
const format = useMemo(() => dateSegments(language, calendar), [language, calendar])
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useLayoutEffect, useRef, useState, type ComponentProps } from 'react'
|
|
4
|
+
import { cn } from 'cn'
|
|
5
|
+
import { requireAccessibleName } from '../../internal/accessibleName.js'
|
|
6
|
+
import { joinIds } from '../../internal/ids.js'
|
|
7
|
+
import type { ControlSize, PartStyle } from '../../presentation.js'
|
|
8
|
+
import { useControllableState } from '../../react/useControllableState.js'
|
|
9
|
+
import type { ValueOwner } from '../../react/useNativeOwner.js'
|
|
10
|
+
import { Button } from '../Button/Button.js'
|
|
11
|
+
import { Field, useField } from '../Field/Field.js'
|
|
12
|
+
import { Input } from '../Input/Input.js'
|
|
13
|
+
|
|
14
|
+
export type InlineEditProps = Omit<
|
|
15
|
+
ComponentProps<'div'>,
|
|
16
|
+
'children' | 'style' | 'value' | 'defaultValue' | 'onChange' | 'dangerouslySetInnerHTML'
|
|
17
|
+
> &
|
|
18
|
+
ValueOwner<string> & {
|
|
19
|
+
inputLabel: string
|
|
20
|
+
editLabel?: string
|
|
21
|
+
saveLabel?: string
|
|
22
|
+
cancelLabel?: string
|
|
23
|
+
placeholder?: string
|
|
24
|
+
disabled?: boolean
|
|
25
|
+
controlSize?: ControlSize
|
|
26
|
+
/** Return an error to keep the draft open and focus the input. */
|
|
27
|
+
validate?: (draft: string) => string | undefined
|
|
28
|
+
onValueChange?: (value: string) => void
|
|
29
|
+
style?: PartStyle['style']
|
|
30
|
+
'data-slot'?: string
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type Draft = { text: string; error?: string; requested?: string }
|
|
34
|
+
|
|
35
|
+
export function InlineEdit({
|
|
36
|
+
value,
|
|
37
|
+
defaultValue,
|
|
38
|
+
onValueChange,
|
|
39
|
+
inputLabel,
|
|
40
|
+
editLabel = 'Edit',
|
|
41
|
+
saveLabel = 'Save',
|
|
42
|
+
cancelLabel = 'Cancel',
|
|
43
|
+
placeholder,
|
|
44
|
+
disabled: disabledProp,
|
|
45
|
+
controlSize = 'medium',
|
|
46
|
+
validate,
|
|
47
|
+
className,
|
|
48
|
+
'data-slot': slot,
|
|
49
|
+
...props
|
|
50
|
+
}: InlineEditProps) {
|
|
51
|
+
requireAccessibleName('InlineEdit input', { 'aria-label': inputLabel })
|
|
52
|
+
requireAccessibleName('InlineEdit edit action', { 'aria-label': editLabel })
|
|
53
|
+
requireAccessibleName('InlineEdit save action', { 'aria-label': saveLabel })
|
|
54
|
+
requireAccessibleName('InlineEdit cancel action', { 'aria-label': cancelLabel })
|
|
55
|
+
const field = useField()
|
|
56
|
+
const disabled = disabledProp || field?.disabled
|
|
57
|
+
const owner = useControllableState({
|
|
58
|
+
name: 'InlineEdit value',
|
|
59
|
+
value,
|
|
60
|
+
defaultValue,
|
|
61
|
+
initialValue: '',
|
|
62
|
+
onChange: onValueChange
|
|
63
|
+
})
|
|
64
|
+
const [draft, setDraft] = useState<Draft | null>(null)
|
|
65
|
+
const input = useRef<HTMLInputElement>(null)
|
|
66
|
+
const trigger = useRef<HTMLButtonElement>(null)
|
|
67
|
+
const wasEditing = useRef(false)
|
|
68
|
+
const accepted = draft?.requested !== undefined && draft.requested === owner.value
|
|
69
|
+
if (draft && (accepted || disabled)) setDraft(null)
|
|
70
|
+
const editing = draft !== null && !accepted && !disabled
|
|
71
|
+
|
|
72
|
+
useLayoutEffect(() => {
|
|
73
|
+
if (editing) {
|
|
74
|
+
input.current?.focus()
|
|
75
|
+
input.current?.select()
|
|
76
|
+
} else if (wasEditing.current && !disabled) {
|
|
77
|
+
trigger.current?.focus()
|
|
78
|
+
}
|
|
79
|
+
wasEditing.current = editing
|
|
80
|
+
}, [editing, disabled])
|
|
81
|
+
|
|
82
|
+
function save() {
|
|
83
|
+
if (!draft) return
|
|
84
|
+
const error = validate?.(draft.text)
|
|
85
|
+
if (error) {
|
|
86
|
+
setDraft({ text: draft.text, error })
|
|
87
|
+
input.current?.focus()
|
|
88
|
+
return
|
|
89
|
+
}
|
|
90
|
+
if (owner.controlled && draft.text !== owner.value) {
|
|
91
|
+
setDraft({ text: draft.text, requested: draft.text })
|
|
92
|
+
} else {
|
|
93
|
+
setDraft(null)
|
|
94
|
+
}
|
|
95
|
+
owner.request(draft.text)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return (
|
|
99
|
+
<div
|
|
100
|
+
{...props}
|
|
101
|
+
className={cn('grid min-w-0 gap-2', className)}
|
|
102
|
+
data-slot={joinIds('inline-edit', slot)}
|
|
103
|
+
>
|
|
104
|
+
{editing ? (
|
|
105
|
+
<>
|
|
106
|
+
<Field
|
|
107
|
+
label={inputLabel}
|
|
108
|
+
error={draft.error}
|
|
109
|
+
parts={{ label: { className: 'sr-only' } }}
|
|
110
|
+
>
|
|
111
|
+
<Input
|
|
112
|
+
ref={input}
|
|
113
|
+
controlSize={controlSize}
|
|
114
|
+
data-slot="inline-edit-input"
|
|
115
|
+
placeholder={placeholder}
|
|
116
|
+
value={draft.text}
|
|
117
|
+
onChange={event => setDraft({ text: event.currentTarget.value })}
|
|
118
|
+
onKeyDown={event => {
|
|
119
|
+
if (event.nativeEvent.isComposing) return
|
|
120
|
+
if (event.key === 'Enter' || event.key === 'Escape') {
|
|
121
|
+
event.preventDefault()
|
|
122
|
+
event.stopPropagation()
|
|
123
|
+
if (event.key === 'Enter') save()
|
|
124
|
+
else setDraft(null)
|
|
125
|
+
}
|
|
126
|
+
}}
|
|
127
|
+
/>
|
|
128
|
+
</Field>
|
|
129
|
+
<div className="flex flex-wrap gap-2">
|
|
130
|
+
<Button
|
|
131
|
+
controlSize={controlSize}
|
|
132
|
+
type="button"
|
|
133
|
+
data-slot="inline-edit-save"
|
|
134
|
+
onClick={save}
|
|
135
|
+
>
|
|
136
|
+
{saveLabel}
|
|
137
|
+
</Button>
|
|
138
|
+
<Button
|
|
139
|
+
controlSize={controlSize}
|
|
140
|
+
type="button"
|
|
141
|
+
variant="outline"
|
|
142
|
+
data-slot="inline-edit-cancel"
|
|
143
|
+
onClick={() => setDraft(null)}
|
|
144
|
+
>
|
|
145
|
+
{cancelLabel}
|
|
146
|
+
</Button>
|
|
147
|
+
</div>
|
|
148
|
+
</>
|
|
149
|
+
) : (
|
|
150
|
+
<div className="flex min-w-0 flex-wrap items-center gap-2">
|
|
151
|
+
<span
|
|
152
|
+
data-slot="inline-edit-value"
|
|
153
|
+
className="min-w-0 break-words font-[family-name:var(--rad-typography-roles-ui-body-family,system-ui,sans-serif)]"
|
|
154
|
+
>
|
|
155
|
+
{owner.value || placeholder}
|
|
156
|
+
</span>
|
|
157
|
+
<Button
|
|
158
|
+
ref={trigger}
|
|
159
|
+
type="button"
|
|
160
|
+
variant="outline"
|
|
161
|
+
controlSize={controlSize}
|
|
162
|
+
disabled={disabled}
|
|
163
|
+
data-slot="inline-edit-trigger"
|
|
164
|
+
onClick={() => setDraft({ text: owner.value })}
|
|
165
|
+
>
|
|
166
|
+
{editLabel}
|
|
167
|
+
</Button>
|
|
168
|
+
</div>
|
|
169
|
+
)}
|
|
170
|
+
</div>
|
|
171
|
+
)
|
|
172
|
+
}
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useId, useRef, useState, type ComponentProps, type KeyboardEvent } from 'react'
|
|
4
|
+
import { cn } from 'cn'
|
|
5
|
+
import { requireAccessibleName } from '../../internal/accessibleName.js'
|
|
6
|
+
import { composeAction } from '../../internal/events.js'
|
|
7
|
+
import { joinIds } from '../../internal/ids.js'
|
|
8
|
+
import { matchSelectText, moveSelectFocus } from '../../internal/select.js'
|
|
9
|
+
import {
|
|
10
|
+
isItemOpen,
|
|
11
|
+
sameSelection,
|
|
12
|
+
nextSelection,
|
|
13
|
+
type Selection
|
|
14
|
+
} from '../../internal/selection.js'
|
|
15
|
+
import type { ControlSize, PartStyle } from '../../presentation.js'
|
|
16
|
+
import { useComposedRefs } from '../../react/useComposedRefs.js'
|
|
17
|
+
import { useControllableState } from '../../react/useControllableState.js'
|
|
18
|
+
import { useFormReset } from '../../react/useFormReset.js'
|
|
19
|
+
import { useLocaleMatcher } from '../../react/useLocale.js'
|
|
20
|
+
import type { ValueOwner } from '../../react/useNativeOwner.js'
|
|
21
|
+
import { useField, useFieldControl } from '../Field/Field.js'
|
|
22
|
+
import type { SelectOption } from '../Select/Select.js'
|
|
23
|
+
import { controlBorderClass, controlMetrics } from '../shared/controlStyles.js'
|
|
24
|
+
import { focusRingClass } from '../shared/focusStyles.js'
|
|
25
|
+
import { scrollbarClass } from '../shared/scrollbarStyles.js'
|
|
26
|
+
|
|
27
|
+
export type ListBoxOption = SelectOption
|
|
28
|
+
|
|
29
|
+
export type ListBoxProps = Omit<
|
|
30
|
+
ComponentProps<'div'>,
|
|
31
|
+
| 'children'
|
|
32
|
+
| 'style'
|
|
33
|
+
| 'role'
|
|
34
|
+
| 'tabIndex'
|
|
35
|
+
| 'defaultValue'
|
|
36
|
+
| 'value'
|
|
37
|
+
| 'onChange'
|
|
38
|
+
| 'aria-activedescendant'
|
|
39
|
+
| 'aria-multiselectable'
|
|
40
|
+
| 'dangerouslySetInnerHTML'
|
|
41
|
+
> & {
|
|
42
|
+
items: readonly ListBoxOption[]
|
|
43
|
+
name?: string
|
|
44
|
+
form?: string
|
|
45
|
+
required?: boolean
|
|
46
|
+
disabled?: boolean
|
|
47
|
+
controlSize?: ControlSize
|
|
48
|
+
style?: PartStyle['style']
|
|
49
|
+
parts?: { option?: PartStyle; indicator?: PartStyle }
|
|
50
|
+
'data-slot'?: string
|
|
51
|
+
} & (
|
|
52
|
+
| ({ type?: 'single'; onValueChange?: (value: string | null) => void } & ValueOwner<
|
|
53
|
+
string | null
|
|
54
|
+
>)
|
|
55
|
+
| ({ type: 'multiple'; onValueChange?: (value: string[]) => void } & ValueOwner<string[]>)
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
const listClass = [
|
|
59
|
+
'group/list-box grid min-w-0 gap-1 overflow-auto p-1',
|
|
60
|
+
controlBorderClass,
|
|
61
|
+
scrollbarClass,
|
|
62
|
+
'bg-[var(--rad-control-field-background,var(--rad-color-surface,white))] text-[var(--rad-color-foreground)]',
|
|
63
|
+
'font-[family-name:var(--rad-typography-roles-ui-body-family,system-ui,sans-serif)] text-[length:var(--_rad-control-font-size)]',
|
|
64
|
+
'data-invalid:[border-color:var(--rad-color-invalid)] data-disabled:opacity-[var(--rad-state-disabled-opacity,0.5)]',
|
|
65
|
+
'forced-colors:bg-[Canvas] forced-colors:text-[CanvasText] forced-colors:border-[CanvasText]',
|
|
66
|
+
focusRingClass
|
|
67
|
+
].join(' ')
|
|
68
|
+
|
|
69
|
+
const optionClass = [
|
|
70
|
+
'flex min-h-[var(--_rad-control-height)] min-w-0 items-center justify-between gap-2 px-[var(--_rad-control-padding)] py-2',
|
|
71
|
+
'rounded-[var(--rad-control-radius,0.5rem)] [corner-shape:var(--rad-control-corner-shape,round)]',
|
|
72
|
+
'cursor-default data-disabled:opacity-[var(--rad-state-disabled-opacity,0.5)]',
|
|
73
|
+
'data-selected:bg-[var(--rad-control-selected-background,transparent)] data-selected:text-[var(--rad-control-selected-foreground,currentColor)]',
|
|
74
|
+
'data-selected:[box-shadow:inset_0_0_0_var(--rad-control-border-width,1px)_var(--rad-control-selected-border-color,transparent)]',
|
|
75
|
+
'group-focus-visible/list-box:data-active:outline-2 group-focus-visible/list-box:data-active:-outline-offset-2 group-focus-visible/list-box:data-active:outline-[var(--rad-color-focus)]',
|
|
76
|
+
'forced-colors:data-selected:bg-[Highlight] forced-colors:data-selected:text-[HighlightText]',
|
|
77
|
+
'forced-colors:group-focus-visible/list-box:data-active:outline-[CanvasText] forced-colors:data-disabled:text-[GrayText]'
|
|
78
|
+
].join(' ')
|
|
79
|
+
|
|
80
|
+
export function ListBox(props: ListBoxProps) {
|
|
81
|
+
const {
|
|
82
|
+
items,
|
|
83
|
+
name,
|
|
84
|
+
form,
|
|
85
|
+
disabled,
|
|
86
|
+
required,
|
|
87
|
+
controlSize = 'medium',
|
|
88
|
+
id,
|
|
89
|
+
ref,
|
|
90
|
+
className,
|
|
91
|
+
parts,
|
|
92
|
+
type = 'single',
|
|
93
|
+
value,
|
|
94
|
+
defaultValue,
|
|
95
|
+
onValueChange: _onValueChange,
|
|
96
|
+
onKeyDown,
|
|
97
|
+
onClick,
|
|
98
|
+
'aria-label': label,
|
|
99
|
+
'aria-labelledby': labelledBy,
|
|
100
|
+
'aria-describedby': describedBy,
|
|
101
|
+
'aria-invalid': invalid,
|
|
102
|
+
'data-slot': slot,
|
|
103
|
+
...hostProps
|
|
104
|
+
} = props
|
|
105
|
+
const multiple = type === 'multiple'
|
|
106
|
+
const [initialType] = useState(type)
|
|
107
|
+
if (type !== initialType) throw new TypeError('ListBox needs a new key to change selection mode.')
|
|
108
|
+
|
|
109
|
+
const ids = useId()
|
|
110
|
+
const host = useRef<HTMLDivElement>(null)
|
|
111
|
+
const hostRef = useComposedRefs(host, ref)
|
|
112
|
+
const select = useRef<HTMLSelectElement>(null)
|
|
113
|
+
const matcher = useLocaleMatcher()
|
|
114
|
+
const search = useRef({ text: '', at: 0, matcher })
|
|
115
|
+
const field = useField()
|
|
116
|
+
const control = useFieldControl({
|
|
117
|
+
disabled,
|
|
118
|
+
required,
|
|
119
|
+
'aria-describedby': describedBy,
|
|
120
|
+
'aria-invalid': invalid
|
|
121
|
+
})
|
|
122
|
+
const listLabel = labelledBy ?? field?.labelId
|
|
123
|
+
requireAccessibleName('ListBox', { 'aria-label': label, 'aria-labelledby': listLabel })
|
|
124
|
+
|
|
125
|
+
const entries = items.map(item => ({
|
|
126
|
+
...item,
|
|
127
|
+
textValue: item.textValue ?? (typeof item.label === 'string' ? item.label : '')
|
|
128
|
+
}))
|
|
129
|
+
if (
|
|
130
|
+
new Set(entries.map(item => item.value)).size !== items.length ||
|
|
131
|
+
entries.some(item => typeof item.value !== 'string' || !item.value || !item.textValue?.trim())
|
|
132
|
+
) {
|
|
133
|
+
throw new TypeError('ListBox items need distinct, nonempty string values and readable labels.')
|
|
134
|
+
}
|
|
135
|
+
const owner = useControllableState<Selection>({
|
|
136
|
+
name: 'ListBox value',
|
|
137
|
+
value,
|
|
138
|
+
defaultValue,
|
|
139
|
+
initialValue: multiple ? [] : null,
|
|
140
|
+
equal: sameSelection,
|
|
141
|
+
onChange: next => {
|
|
142
|
+
if (Array.isArray(next)) {
|
|
143
|
+
if (props.type === 'multiple') props.onValueChange?.(next)
|
|
144
|
+
} else if (props.type !== 'multiple') {
|
|
145
|
+
props.onValueChange?.(next)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
})
|
|
149
|
+
const selected = (item: string) => isItemOpen(owner.value, item)
|
|
150
|
+
const [focus, setFocus] = useState<string>()
|
|
151
|
+
const activeIndex = entries.findIndex(item => !item.disabled && item.value === focus)
|
|
152
|
+
const selectedIndex = entries.findIndex(item => !item.disabled && selected(item.value))
|
|
153
|
+
const index =
|
|
154
|
+
activeIndex >= 0
|
|
155
|
+
? activeIndex
|
|
156
|
+
: selectedIndex >= 0
|
|
157
|
+
? selectedIndex
|
|
158
|
+
: entries.findIndex(item => !item.disabled)
|
|
159
|
+
const active = entries[index]?.value
|
|
160
|
+
const optionId = (idx: number) => ids + '-option-' + idx
|
|
161
|
+
const nativeValue = multiple
|
|
162
|
+
? entries.filter(item => selected(item.value)).map(item => item.value)
|
|
163
|
+
: (entries.find(item => selected(item.value))?.value ?? '')
|
|
164
|
+
|
|
165
|
+
const initialValue = useRef(owner.value)
|
|
166
|
+
useFormReset(select, () => {
|
|
167
|
+
owner.reset()
|
|
168
|
+
setFocus(undefined)
|
|
169
|
+
const resetValue = owner.controlled ? owner.value : initialValue.current
|
|
170
|
+
const hasSelection = entries.some(item => isItemOpen(resetValue, item.value))
|
|
171
|
+
for (const option of select.current?.options ?? []) {
|
|
172
|
+
option.selected =
|
|
173
|
+
isItemOpen(resetValue, option.value) || (option.value === '' && !hasSelection)
|
|
174
|
+
}
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
function move(next: string | undefined) {
|
|
178
|
+
if (next === undefined) return
|
|
179
|
+
setFocus(next)
|
|
180
|
+
const nextIndex = entries.findIndex(item => item.value === next)
|
|
181
|
+
host.current?.ownerDocument
|
|
182
|
+
.getElementById(optionId(nextIndex))
|
|
183
|
+
?.scrollIntoView({ block: 'nearest' })
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function choose(item: string) {
|
|
187
|
+
owner.request(nextSelection(owner.value, item, !multiple || !selected(item)))
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function navigate(event: KeyboardEvent<HTMLDivElement>) {
|
|
191
|
+
if (event.nativeEvent.isComposing || event.altKey || event.ctrlKey || event.metaKey) return
|
|
192
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
193
|
+
event.preventDefault()
|
|
194
|
+
if (active !== undefined) choose(active)
|
|
195
|
+
} else if (['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) {
|
|
196
|
+
event.preventDefault()
|
|
197
|
+
move(moveSelectFocus(entries, active ?? null, event.key))
|
|
198
|
+
} else if (matcher.isSingleKey(event.key)) {
|
|
199
|
+
event.preventDefault()
|
|
200
|
+
const now = event.timeStamp
|
|
201
|
+
const previous = search.current
|
|
202
|
+
const query =
|
|
203
|
+
(previous.matcher === matcher && now - previous.at < 1000 ? previous.text : '') + event.key
|
|
204
|
+
search.current = { text: query, at: now, matcher }
|
|
205
|
+
move(matchSelectText(entries, query, active ?? null, matcher))
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const unavailable = () => !!(control.disabled || select.current?.matches(':disabled'))
|
|
210
|
+
return (
|
|
211
|
+
<>
|
|
212
|
+
<div
|
|
213
|
+
{...hostProps}
|
|
214
|
+
ref={hostRef}
|
|
215
|
+
id={id}
|
|
216
|
+
// oxlint-disable-next-line jsx-a11y/prefer-tag-over-role -- The native bridge cannot present rich labels.
|
|
217
|
+
role="listbox"
|
|
218
|
+
tabIndex={control.disabled ? -1 : 0}
|
|
219
|
+
aria-label={label}
|
|
220
|
+
aria-labelledby={listLabel}
|
|
221
|
+
aria-describedby={control['aria-describedby']}
|
|
222
|
+
aria-invalid={control['aria-invalid']}
|
|
223
|
+
aria-required={control['aria-required']}
|
|
224
|
+
aria-multiselectable={multiple || undefined}
|
|
225
|
+
aria-activedescendant={index >= 0 ? optionId(index) : undefined}
|
|
226
|
+
aria-disabled={control.disabled || undefined}
|
|
227
|
+
data-invalid={control['data-invalid']}
|
|
228
|
+
data-disabled={control.disabled ? '' : undefined}
|
|
229
|
+
data-slot={joinIds('list-box', slot)}
|
|
230
|
+
className={cn(listClass, controlMetrics({ controlSize }), className)}
|
|
231
|
+
onKeyDown={event => composeAction(onKeyDown, navigate, unavailable)(event)}
|
|
232
|
+
onClick={event =>
|
|
233
|
+
composeAction(
|
|
234
|
+
onClick,
|
|
235
|
+
event => {
|
|
236
|
+
const option = (event.target as HTMLElement).closest<HTMLElement>(
|
|
237
|
+
'[data-list-box-option]'
|
|
238
|
+
)
|
|
239
|
+
const item = entries[Number(option?.dataset.listBoxOption)]
|
|
240
|
+
if (!item || item.disabled) return
|
|
241
|
+
host.current?.focus({ preventScroll: true })
|
|
242
|
+
setFocus(item.value)
|
|
243
|
+
choose(item.value)
|
|
244
|
+
},
|
|
245
|
+
unavailable
|
|
246
|
+
)(event)
|
|
247
|
+
}
|
|
248
|
+
>
|
|
249
|
+
{entries.map((item, idx) => (
|
|
250
|
+
<div
|
|
251
|
+
key={item.value}
|
|
252
|
+
id={optionId(idx)}
|
|
253
|
+
// oxlint-disable-next-line jsx-a11y/prefer-tag-over-role -- The corresponding native option lives in the form bridge.
|
|
254
|
+
role="option"
|
|
255
|
+
aria-selected={selected(item.value)}
|
|
256
|
+
aria-disabled={item.disabled || undefined}
|
|
257
|
+
data-list-box-option={idx}
|
|
258
|
+
data-slot="list-box-option"
|
|
259
|
+
data-active={index === idx ? '' : undefined}
|
|
260
|
+
data-selected={selected(item.value) ? '' : undefined}
|
|
261
|
+
data-disabled={item.disabled ? '' : undefined}
|
|
262
|
+
className={cn(optionClass, parts?.option?.className)}
|
|
263
|
+
style={parts?.option?.style}
|
|
264
|
+
>
|
|
265
|
+
<div className="min-w-0 break-words">{item.label}</div>
|
|
266
|
+
<span
|
|
267
|
+
aria-hidden="true"
|
|
268
|
+
data-slot="list-box-indicator"
|
|
269
|
+
className={cn('w-4 shrink-0 text-center', parts?.indicator?.className)}
|
|
270
|
+
style={parts?.indicator?.style}
|
|
271
|
+
>
|
|
272
|
+
{selected(item.value) ? '✓' : ''}
|
|
273
|
+
</span>
|
|
274
|
+
</div>
|
|
275
|
+
))}
|
|
276
|
+
</div>
|
|
277
|
+
<select
|
|
278
|
+
ref={select}
|
|
279
|
+
id={control.id}
|
|
280
|
+
name={name}
|
|
281
|
+
form={form}
|
|
282
|
+
multiple={multiple}
|
|
283
|
+
required={control.required}
|
|
284
|
+
disabled={control.disabled}
|
|
285
|
+
aria-hidden="true"
|
|
286
|
+
tabIndex={-1}
|
|
287
|
+
className="sr-only"
|
|
288
|
+
data-slot="list-box-native"
|
|
289
|
+
value={nativeValue}
|
|
290
|
+
onChange={event => {
|
|
291
|
+
if (unavailable()) return
|
|
292
|
+
const native = event.currentTarget
|
|
293
|
+
owner.request(
|
|
294
|
+
multiple
|
|
295
|
+
? Array.from(native.selectedOptions, option => option.value)
|
|
296
|
+
: native.value || null
|
|
297
|
+
)
|
|
298
|
+
}}
|
|
299
|
+
onFocus={() => host.current?.focus({ preventScroll: true })}
|
|
300
|
+
onInvalid={event => {
|
|
301
|
+
event.preventDefault()
|
|
302
|
+
host.current?.focus()
|
|
303
|
+
}}
|
|
304
|
+
>
|
|
305
|
+
{!multiple && <option value="">No selection</option>}
|
|
306
|
+
{entries.map(item => (
|
|
307
|
+
<option
|
|
308
|
+
key={item.value}
|
|
309
|
+
value={item.value}
|
|
310
|
+
disabled={item.disabled}
|
|
311
|
+
>
|
|
312
|
+
{item.textValue}
|
|
313
|
+
</option>
|
|
314
|
+
))}
|
|
315
|
+
</select>
|
|
316
|
+
</>
|
|
317
|
+
)
|
|
318
|
+
}
|