@cognitap/radiant 0.0.80 → 0.0.82
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 +25 -0
- package/dist/library/charts/Charts.d.ts +1 -2
- package/dist/library/charts/Charts.d.ts.map +1 -1
- package/dist/library/charts/Charts.js +4 -4
- package/dist/library/charts/Charts.js.map +1 -1
- package/dist/library/components/AppShell/AppShell.d.ts +1 -6
- package/dist/library/components/AppShell/AppShell.d.ts.map +1 -1
- package/dist/library/components/AppShell/AppShell.js +4 -10
- package/dist/library/components/AppShell/AppShell.js.map +1 -1
- package/dist/library/components/Autocomplete/Autocomplete.d.ts +1 -1
- package/dist/library/components/Autocomplete/Autocomplete.d.ts.map +1 -1
- package/dist/library/components/Autocomplete/Autocomplete.js +2 -6
- package/dist/library/components/Autocomplete/Autocomplete.js.map +1 -1
- package/dist/library/components/Calendar/Calendar.d.ts +39 -0
- package/dist/library/components/Calendar/Calendar.d.ts.map +1 -0
- package/dist/library/components/Calendar/Calendar.js +143 -0
- package/dist/library/components/Calendar/Calendar.js.map +1 -0
- package/dist/library/components/Carousel/Carousel.d.ts +9 -5
- package/dist/library/components/Carousel/Carousel.d.ts.map +1 -1
- package/dist/library/components/Carousel/Carousel.js +14 -6
- package/dist/library/components/Carousel/Carousel.js.map +1 -1
- package/dist/library/components/Combobox/Combobox.d.ts +6 -0
- package/dist/library/components/Combobox/Combobox.d.ts.map +1 -1
- package/dist/library/components/Combobox/Combobox.js +51 -10
- package/dist/library/components/Combobox/Combobox.js.map +1 -1
- package/dist/library/components/Combobox/comboboxStyles.d.ts +3 -0
- package/dist/library/components/Combobox/comboboxStyles.d.ts.map +1 -1
- package/dist/library/components/Combobox/comboboxStyles.js +20 -1
- package/dist/library/components/Combobox/comboboxStyles.js.map +1 -1
- package/dist/library/components/DatePicker/DateCalendar.d.ts +13 -0
- package/dist/library/components/DatePicker/DateCalendar.d.ts.map +1 -0
- package/dist/library/components/DatePicker/DateCalendar.js +25 -0
- package/dist/library/components/DatePicker/DateCalendar.js.map +1 -0
- package/dist/library/components/DatePicker/DateEditor.d.ts +13 -0
- package/dist/library/components/DatePicker/DateEditor.d.ts.map +1 -0
- package/dist/library/components/DatePicker/DateEditor.js +84 -0
- package/dist/library/components/DatePicker/DateEditor.js.map +1 -0
- package/dist/library/components/DatePicker/DatePicker.d.ts +10 -0
- package/dist/library/components/DatePicker/DatePicker.d.ts.map +1 -0
- package/dist/library/components/DatePicker/DatePicker.js +34 -0
- package/dist/library/components/DatePicker/DatePicker.js.map +1 -0
- package/dist/library/components/DatePicker/dateSegments.d.ts +13 -0
- package/dist/library/components/DatePicker/dateSegments.d.ts.map +1 -0
- package/dist/library/components/DatePicker/dateSegments.js +43 -0
- package/dist/library/components/DatePicker/dateSegments.js.map +1 -0
- package/dist/library/components/DateRangePicker/DateRangePicker.d.ts +23 -0
- package/dist/library/components/DateRangePicker/DateRangePicker.d.ts.map +1 -0
- package/dist/library/components/DateRangePicker/DateRangePicker.js +69 -0
- package/dist/library/components/DateRangePicker/DateRangePicker.js.map +1 -0
- package/dist/library/components/LocaleProvider/LocaleProvider.d.ts +9 -0
- package/dist/library/components/LocaleProvider/LocaleProvider.d.ts.map +1 -0
- package/dist/library/components/LocaleProvider/LocaleProvider.js +11 -0
- package/dist/library/components/LocaleProvider/LocaleProvider.js.map +1 -0
- package/dist/library/components/Menu/Menu.d.ts.map +1 -1
- package/dist/library/components/Menu/Menu.js +7 -2
- package/dist/library/components/Menu/Menu.js.map +1 -1
- package/dist/library/components/MultiSelect/MultiSelect.d.ts +4 -1
- package/dist/library/components/MultiSelect/MultiSelect.d.ts.map +1 -1
- package/dist/library/components/MultiSelect/MultiSelect.js +6 -1
- package/dist/library/components/MultiSelect/MultiSelect.js.map +1 -1
- package/dist/library/components/NumberField/NumberField.d.ts +1 -1
- package/dist/library/components/NumberField/NumberField.d.ts.map +1 -1
- package/dist/library/components/NumberField/NumberField.js +3 -1
- package/dist/library/components/NumberField/NumberField.js.map +1 -1
- package/dist/library/components/Popover/Popover.d.ts +2 -1
- package/dist/library/components/Popover/Popover.d.ts.map +1 -1
- package/dist/library/components/Popover/Popover.js +4 -1
- package/dist/library/components/Popover/Popover.js.map +1 -1
- package/dist/library/components/Select/Select.d.ts.map +1 -1
- package/dist/library/components/Select/Select.js +18 -9
- package/dist/library/components/Select/Select.js.map +1 -1
- package/dist/library/components/TimeInput/TimeInput.d.ts +18 -0
- package/dist/library/components/TimeInput/TimeInput.d.ts.map +1 -0
- package/dist/library/components/TimeInput/TimeInput.js +123 -0
- package/dist/library/components/TimeInput/TimeInput.js.map +1 -0
- package/dist/library/components/TimeInput/timeSegments.d.ts +24 -0
- package/dist/library/components/TimeInput/timeSegments.d.ts.map +1 -0
- package/dist/library/components/TimeInput/timeSegments.js +94 -0
- package/dist/library/components/TimeInput/timeSegments.js.map +1 -0
- package/dist/library/components/Tooltip/Tooltip.d.ts.map +1 -1
- package/dist/library/components/Tooltip/Tooltip.js +3 -1
- package/dist/library/components/Tooltip/Tooltip.js.map +1 -1
- package/dist/library/index.d.ts +12 -6
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +9 -3
- package/dist/library/index.js.map +1 -1
- package/dist/library/internal/calendar.d.ts +18 -0
- package/dist/library/internal/calendar.d.ts.map +1 -0
- package/dist/library/internal/calendar.js +78 -0
- package/dist/library/internal/calendar.js.map +1 -0
- package/dist/library/internal/locale.d.ts +9 -0
- package/dist/library/internal/locale.d.ts.map +1 -0
- package/dist/library/internal/locale.js +57 -0
- package/dist/library/internal/locale.js.map +1 -0
- package/dist/library/internal/localeMatcher.d.ts +6 -0
- package/dist/library/internal/localeMatcher.d.ts.map +1 -0
- package/dist/library/internal/localeMatcher.js +52 -0
- package/dist/library/internal/localeMatcher.js.map +1 -0
- package/dist/library/internal/localizedDigits.d.ts +5 -0
- package/dist/library/internal/localizedDigits.d.ts.map +1 -0
- package/dist/library/internal/localizedDigits.js +13 -0
- package/dist/library/internal/localizedDigits.js.map +1 -0
- package/dist/library/internal/numberField.d.ts.map +1 -1
- package/dist/library/internal/numberField.js +14 -5
- package/dist/library/internal/numberField.js.map +1 -1
- package/dist/library/internal/popupLocale.d.ts +6 -0
- package/dist/library/internal/popupLocale.d.ts.map +1 -0
- package/dist/library/internal/popupLocale.js +22 -0
- package/dist/library/internal/popupLocale.js.map +1 -0
- package/dist/library/internal/select.d.ts +2 -1
- package/dist/library/internal/select.d.ts.map +1 -1
- package/dist/library/internal/select.js +5 -6
- package/dist/library/internal/select.js.map +1 -1
- package/dist/library/react/useComboboxPopup.d.ts +4 -1
- package/dist/library/react/useComboboxPopup.d.ts.map +1 -1
- package/dist/library/react/useComboboxPopup.js +13 -7
- package/dist/library/react/useComboboxPopup.js.map +1 -1
- package/dist/library/react/useFormReset.d.ts +4 -0
- package/dist/library/react/useFormReset.d.ts.map +1 -0
- package/dist/library/react/useFormReset.js +32 -0
- package/dist/library/react/useFormReset.js.map +1 -0
- package/dist/library/react/useLocale.d.ts +8 -0
- package/dist/library/react/useLocale.d.ts.map +1 -0
- package/dist/library/react/useLocale.js +14 -0
- package/dist/library/react/useLocale.js.map +1 -0
- package/dist/library/react/usePopoverSurface.d.ts +3 -0
- package/dist/library/react/usePopoverSurface.d.ts.map +1 -1
- package/dist/library/react/usePopoverSurface.js +18 -4
- package/dist/library/react/usePopoverSurface.js.map +1 -1
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/components.d.ts +4 -0
- package/dist/library/themes/components.d.ts.map +1 -1
- package/dist/library/themes/components.js +1 -0
- package/dist/library/themes/components.js.map +1 -1
- package/docs/architecture.md +8 -0
- package/docs/theming.md +10 -10
- package/package.json +1 -1
- package/src/charts/Charts.tsx +10 -7
- package/src/components/AppShell/AppShell.tsx +4 -14
- package/src/components/Autocomplete/Autocomplete.tsx +1 -13
- package/src/components/Calendar/Calendar.tsx +310 -0
- package/src/components/Carousel/Carousel.tsx +34 -6
- package/src/components/Combobox/Combobox.tsx +95 -19
- package/src/components/Combobox/comboboxStyles.ts +22 -1
- package/src/components/DatePicker/DateCalendar.tsx +83 -0
- package/src/components/DatePicker/DateEditor.tsx +189 -0
- package/src/components/DatePicker/DatePicker.tsx +117 -0
- package/src/components/DatePicker/dateSegments.ts +46 -0
- package/src/components/DateRangePicker/DateRangePicker.tsx +201 -0
- package/src/components/LocaleProvider/LocaleProvider.tsx +26 -0
- package/src/components/Menu/Menu.tsx +8 -2
- package/src/components/MultiSelect/MultiSelect.tsx +8 -0
- package/src/components/NumberField/NumberField.tsx +3 -1
- package/src/components/Popover/Popover.tsx +8 -1
- package/src/components/Select/Select.tsx +20 -9
- package/src/components/TimeInput/TimeInput.tsx +269 -0
- package/src/components/TimeInput/timeSegments.ts +102 -0
- package/src/components/Tooltip/Tooltip.tsx +3 -1
- package/src/index.ts +23 -4
- package/src/internal/calendar.ts +99 -0
- package/src/internal/locale.ts +60 -0
- package/src/internal/localeMatcher.ts +53 -0
- package/src/internal/localizedDigits.ts +14 -0
- package/src/internal/numberField.ts +14 -5
- package/src/internal/popupLocale.ts +28 -0
- package/src/internal/select.ts +8 -8
- package/src/react/useComboboxPopup.ts +14 -7
- package/src/react/useFormReset.ts +30 -0
- package/src/react/useLocale.ts +17 -0
- package/src/react/usePopoverSurface.ts +21 -4
- package/src/themes/components.ts +1 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
useLayoutEffect,
|
|
5
|
+
useMemo,
|
|
6
|
+
useRef,
|
|
7
|
+
useState,
|
|
8
|
+
type ComponentProps,
|
|
9
|
+
type KeyboardEvent
|
|
10
|
+
} from 'react'
|
|
11
|
+
import { cn } from 'cn'
|
|
12
|
+
import { useField, useFieldControl, WithoutField } from '../Field/Field.js'
|
|
13
|
+
import { InputGroup, InputGroupInput } from '../InputGroup/InputGroup.js'
|
|
14
|
+
import { inlineActionClass } from '../shared/actionStyles.js'
|
|
15
|
+
import { focusRingInsetClass } from '../shared/focusStyles.js'
|
|
16
|
+
import { Button } from '../Button/Button.js'
|
|
17
|
+
import { requireAccessibleName } from '../../internal/accessibleName.js'
|
|
18
|
+
import { useFormReset } from '../../react/useFormReset.js'
|
|
19
|
+
import { useLocale } from '../../react/useLocale.js'
|
|
20
|
+
import { useControllableState } from '../../react/useControllableState.js'
|
|
21
|
+
import { useComposedRefs } from '../../react/useComposedRefs.js'
|
|
22
|
+
import type { ValueOwner } from '../../react/useNativeOwner.js'
|
|
23
|
+
import type { ControlSize } from '../../presentation.js'
|
|
24
|
+
import {
|
|
25
|
+
isoTime,
|
|
26
|
+
secondsOf,
|
|
27
|
+
timeRange,
|
|
28
|
+
timeSegments,
|
|
29
|
+
type ClockPart,
|
|
30
|
+
type HourCycle,
|
|
31
|
+
type TimeParts
|
|
32
|
+
} from './timeSegments.js'
|
|
33
|
+
|
|
34
|
+
export type TimeInputProps = ValueOwner<string | null> &
|
|
35
|
+
Pick<
|
|
36
|
+
ComponentProps<'input'>,
|
|
37
|
+
| 'ref'
|
|
38
|
+
| 'id'
|
|
39
|
+
| 'name'
|
|
40
|
+
| 'form'
|
|
41
|
+
| 'className'
|
|
42
|
+
| 'readOnly'
|
|
43
|
+
| 'disabled'
|
|
44
|
+
| 'required'
|
|
45
|
+
| 'aria-label'
|
|
46
|
+
| 'aria-labelledby'
|
|
47
|
+
| 'aria-describedby'
|
|
48
|
+
| 'aria-invalid'
|
|
49
|
+
> & {
|
|
50
|
+
onValueChange?: (time: string | null) => void
|
|
51
|
+
locale?: string
|
|
52
|
+
hourCycle?: HourCycle
|
|
53
|
+
/** Show seconds. Second-precision values, bounds and steps also enable this segment. */
|
|
54
|
+
showSeconds?: boolean
|
|
55
|
+
min?: string
|
|
56
|
+
max?: string
|
|
57
|
+
/** Whole seconds, counted from min or midnight. Defaults to one minute, or one second with seconds shown. */
|
|
58
|
+
step?: number
|
|
59
|
+
controlSize?: ControlSize
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const units = { hour: 3600, minute: 60, second: 1 }
|
|
63
|
+
|
|
64
|
+
export function TimeInput({
|
|
65
|
+
value,
|
|
66
|
+
defaultValue,
|
|
67
|
+
onValueChange,
|
|
68
|
+
locale,
|
|
69
|
+
hourCycle,
|
|
70
|
+
showSeconds = false,
|
|
71
|
+
min,
|
|
72
|
+
max,
|
|
73
|
+
step,
|
|
74
|
+
controlSize = 'medium',
|
|
75
|
+
ref,
|
|
76
|
+
name,
|
|
77
|
+
form,
|
|
78
|
+
className,
|
|
79
|
+
readOnly,
|
|
80
|
+
...props
|
|
81
|
+
}: TimeInputProps) {
|
|
82
|
+
const field = useField()
|
|
83
|
+
const control = useFieldControl(props)
|
|
84
|
+
const labelledBy = props['aria-labelledby'] ?? field?.labelId
|
|
85
|
+
const label = props['aria-label']
|
|
86
|
+
requireAccessibleName('TimeInput', { 'aria-label': label, 'aria-labelledby': labelledBy })
|
|
87
|
+
const owner = useControllableState({
|
|
88
|
+
name: 'TimeInput value',
|
|
89
|
+
value,
|
|
90
|
+
defaultValue,
|
|
91
|
+
initialValue: null,
|
|
92
|
+
onChange: onValueChange
|
|
93
|
+
})
|
|
94
|
+
const seconds = owner.value === null ? null : secondsOf(owner.value)
|
|
95
|
+
const withSeconds =
|
|
96
|
+
showSeconds ||
|
|
97
|
+
[owner.value, defaultValue, min, max].some(t => t?.length === 8) ||
|
|
98
|
+
(step !== undefined && step % 60 !== 0)
|
|
99
|
+
const range = timeRange(min, max, step ?? (withSeconds ? 1 : 60))
|
|
100
|
+
const language = useLocale(locale)
|
|
101
|
+
const format = useMemo(
|
|
102
|
+
() => timeSegments(language, hourCycle, withSeconds),
|
|
103
|
+
[language, hourCycle, withSeconds]
|
|
104
|
+
)
|
|
105
|
+
const [draft, setDraft] = useState<{
|
|
106
|
+
owner: string | null
|
|
107
|
+
format: typeof format
|
|
108
|
+
parts: TimeParts
|
|
109
|
+
} | null>(null)
|
|
110
|
+
const editing = draft?.format === format && draft.owner === owner.value
|
|
111
|
+
if (draft && !editing) setDraft(null)
|
|
112
|
+
const parts = editing ? draft.parts : format.display(seconds)
|
|
113
|
+
const parsed = format.parse(parts)
|
|
114
|
+
const invalid = parsed === undefined || (parsed !== null && !range.allows(parsed))
|
|
115
|
+
if (editing && !invalid && parsed !== seconds) setDraft(null)
|
|
116
|
+
|
|
117
|
+
const first = useRef<HTMLInputElement>(null)
|
|
118
|
+
const editorRef = useComposedRefs(first, ref)
|
|
119
|
+
useLayoutEffect(() => {
|
|
120
|
+
const input = first.current
|
|
121
|
+
input?.setCustomValidity(invalid ? 'Enter a valid time within the allowed range and step.' : '')
|
|
122
|
+
return () => input?.setCustomValidity('')
|
|
123
|
+
}, [format, invalid])
|
|
124
|
+
useFormReset(first, () => {
|
|
125
|
+
owner.reset()
|
|
126
|
+
setDraft(null)
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
function edit(updated: TimeParts) {
|
|
130
|
+
const next = format.parse(updated)
|
|
131
|
+
if (next === undefined || (next !== null && !range.allows(next))) {
|
|
132
|
+
setDraft({ owner: owner.value, format, parts: updated })
|
|
133
|
+
return
|
|
134
|
+
}
|
|
135
|
+
const iso = next === null ? null : isoTime(next, withSeconds)
|
|
136
|
+
setDraft({ owner: iso, format, parts: updated })
|
|
137
|
+
owner.request(iso)
|
|
138
|
+
}
|
|
139
|
+
function togglePeriod() {
|
|
140
|
+
edit({ ...parts, dayPeriod: parts.dayPeriod === 'am' ? 'pm' : 'am' })
|
|
141
|
+
}
|
|
142
|
+
function editNumber(part: ClockPart, input: HTMLInputElement) {
|
|
143
|
+
const text = format.ascii(input.value)
|
|
144
|
+
if (!/^\d{0,2}$/.test(text)) return
|
|
145
|
+
edit({ ...parts, [part]: text })
|
|
146
|
+
if (text.length === 2) {
|
|
147
|
+
const segments = Array.from(
|
|
148
|
+
input.parentElement?.querySelectorAll<HTMLElement>('[data-segment]') ?? []
|
|
149
|
+
)
|
|
150
|
+
segments[segments.indexOf(input) + 1]?.focus()
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function advance(event: KeyboardEvent, part: ClockPart) {
|
|
154
|
+
if (readOnly || control.disabled || event.altKey || event.metaKey || event.ctrlKey) return
|
|
155
|
+
if (event.key !== 'ArrowUp' && event.key !== 'ArrowDown') return
|
|
156
|
+
event.preventDefault()
|
|
157
|
+
const direction = event.key === 'ArrowUp' ? 1 : -1
|
|
158
|
+
let from = parsed
|
|
159
|
+
if (from === undefined) {
|
|
160
|
+
const seed = format.display(min === undefined ? 0 : secondsOf(min))
|
|
161
|
+
from =
|
|
162
|
+
format.parse({
|
|
163
|
+
hour: parts.hour || seed.hour,
|
|
164
|
+
minute: parts.minute || seed.minute,
|
|
165
|
+
second: parts.second || seed.second,
|
|
166
|
+
dayPeriod: parts.dayPeriod
|
|
167
|
+
}) ?? seconds
|
|
168
|
+
}
|
|
169
|
+
const next = range.move(from, direction * units[part])
|
|
170
|
+
setDraft(null)
|
|
171
|
+
owner.request(isoTime(next, withSeconds))
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return (
|
|
175
|
+
<InputGroup
|
|
176
|
+
data-slot="time-input"
|
|
177
|
+
controlSize={controlSize}
|
|
178
|
+
// oxlint-disable-next-line jsx-a11y/prefer-tag-over-role -- The grouped native controls share one time label.
|
|
179
|
+
role="group"
|
|
180
|
+
aria-labelledby={labelledBy}
|
|
181
|
+
aria-label={label}
|
|
182
|
+
className={cn('w-fit max-w-full gap-0', className)}
|
|
183
|
+
>
|
|
184
|
+
<WithoutField>
|
|
185
|
+
{format.parts.map(({ type: part, value: literal }, index) => {
|
|
186
|
+
if (part === 'dayPeriod')
|
|
187
|
+
return (
|
|
188
|
+
<Button
|
|
189
|
+
key={part}
|
|
190
|
+
type="button"
|
|
191
|
+
variant="ghost"
|
|
192
|
+
tone="neutral"
|
|
193
|
+
motion="off"
|
|
194
|
+
data-slot="time-input-period"
|
|
195
|
+
data-segment={part}
|
|
196
|
+
aria-label={[format.names.of(part), format[parts.dayPeriod]]
|
|
197
|
+
.filter(Boolean)
|
|
198
|
+
.join(' ')}
|
|
199
|
+
aria-describedby={control['aria-describedby']}
|
|
200
|
+
controlSize={controlSize}
|
|
201
|
+
disabled={control.disabled || readOnly}
|
|
202
|
+
onClick={togglePeriod}
|
|
203
|
+
onKeyDown={event => {
|
|
204
|
+
if (event.altKey || event.metaKey || event.ctrlKey || event.defaultPrevented)
|
|
205
|
+
return
|
|
206
|
+
if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
|
|
207
|
+
event.preventDefault()
|
|
208
|
+
togglePeriod()
|
|
209
|
+
}
|
|
210
|
+
}}
|
|
211
|
+
className={cn(
|
|
212
|
+
inlineActionClass,
|
|
213
|
+
focusRingInsetClass,
|
|
214
|
+
'min-h-[max(1.5rem,calc(var(--_rad-control-height)-0.75rem))] min-w-6 [--rad-button-border-width:0px] px-1 leading-none',
|
|
215
|
+
'text-[color:var(--rad-time-input-period-color,var(--rad-color-foreground))] underline decoration-dotted decoration-[color:var(--rad-time-input-period-indicator-color,var(--rad-color-muted-foreground))] underline-offset-4 enabled:hover:decoration-solid disabled:no-underline'
|
|
216
|
+
)}
|
|
217
|
+
>
|
|
218
|
+
{format[parts.dayPeriod]}
|
|
219
|
+
</Button>
|
|
220
|
+
)
|
|
221
|
+
if (part !== 'hour' && part !== 'minute' && part !== 'second')
|
|
222
|
+
return (
|
|
223
|
+
<span
|
|
224
|
+
key={index}
|
|
225
|
+
aria-hidden="true"
|
|
226
|
+
>
|
|
227
|
+
{literal}
|
|
228
|
+
</span>
|
|
229
|
+
)
|
|
230
|
+
return (
|
|
231
|
+
<InputGroupInput
|
|
232
|
+
key={part}
|
|
233
|
+
data-segment={part}
|
|
234
|
+
// oxlint-disable-next-line jsx-a11y/prefer-tag-over-role -- Localized digits need text entry with per-segment spinbutton semantics.
|
|
235
|
+
role="spinbutton"
|
|
236
|
+
aria-valuemin={part === 'hour' ? format.hourMin : 0}
|
|
237
|
+
aria-valuemax={part === 'hour' ? format.hourMax : 59}
|
|
238
|
+
aria-valuenow={parts[part] ? Number(parts[part]) : undefined}
|
|
239
|
+
ref={part === 'hour' ? editorRef : undefined}
|
|
240
|
+
id={part === 'hour' ? control.id : undefined}
|
|
241
|
+
form={form}
|
|
242
|
+
disabled={control.disabled}
|
|
243
|
+
readOnly={readOnly}
|
|
244
|
+
required={control.required}
|
|
245
|
+
aria-label={format.names.of(part)}
|
|
246
|
+
aria-describedby={control['aria-describedby']}
|
|
247
|
+
aria-invalid={invalid || control['aria-invalid'] || undefined}
|
|
248
|
+
placeholder="––"
|
|
249
|
+
autoComplete="off"
|
|
250
|
+
inputMode="numeric"
|
|
251
|
+
className="w-[2.7ch] flex-none text-center tabular-nums focus-visible:underline"
|
|
252
|
+
value={format.localize(parts[part])}
|
|
253
|
+
onFocus={event => event.currentTarget.select()}
|
|
254
|
+
onChange={event => editNumber(part, event.currentTarget)}
|
|
255
|
+
onKeyDown={event => advance(event, part)}
|
|
256
|
+
/>
|
|
257
|
+
)
|
|
258
|
+
})}
|
|
259
|
+
</WithoutField>
|
|
260
|
+
<input
|
|
261
|
+
type="hidden"
|
|
262
|
+
name={name}
|
|
263
|
+
form={form}
|
|
264
|
+
disabled={control.disabled}
|
|
265
|
+
value={invalid || seconds === null ? '' : isoTime(seconds, withSeconds)}
|
|
266
|
+
/>
|
|
267
|
+
</InputGroup>
|
|
268
|
+
)
|
|
269
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { supportedLocale } from '../../internal/locale.js'
|
|
2
|
+
import { localizedDigits } from '../../internal/localizedDigits.js'
|
|
3
|
+
|
|
4
|
+
export type HourCycle = 'h11' | 'h12' | 'h23' | 'h24'
|
|
5
|
+
export type ClockPart = 'hour' | 'minute' | 'second'
|
|
6
|
+
export type TimeParts = Record<ClockPart, string> & { dayPeriod: 'am' | 'pm' }
|
|
7
|
+
|
|
8
|
+
const day = 86400
|
|
9
|
+
const pad = (number: number) => String(number).padStart(2, '0')
|
|
10
|
+
|
|
11
|
+
export function secondsOf(iso: string) {
|
|
12
|
+
if (!/^(?:[01][0-9]|2[0-3]):[0-5][0-9](?::[0-5][0-9])?$/.test(iso))
|
|
13
|
+
throw new RangeError('TimeInput expects an ISO wall-clock time in HH:mm or HH:mm:ss format.')
|
|
14
|
+
const [h, m, s = 0] = iso.split(':').map(Number)
|
|
15
|
+
return h * 3600 + m * 60 + s
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function isoTime(seconds: number, withSeconds: boolean) {
|
|
19
|
+
const hm = pad(Math.floor(seconds / 3600)) + ':' + pad(Math.floor(seconds / 60) % 60)
|
|
20
|
+
return withSeconds ? hm + ':' + pad(seconds % 60) : hm
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function timeRange(min: string | undefined, max: string | undefined, step: number) {
|
|
24
|
+
if (!Number.isSafeInteger(step) || step < 1)
|
|
25
|
+
throw new RangeError('TimeInput step must be a positive whole number of seconds.')
|
|
26
|
+
const lower = min === undefined ? 0 : secondsOf(min)
|
|
27
|
+
const end = max === undefined ? day - 1 : secondsOf(max)
|
|
28
|
+
const overnight = end < lower
|
|
29
|
+
const upper = overnight ? end + day : end
|
|
30
|
+
const last = Math.floor((upper - lower) / step)
|
|
31
|
+
const position = (n: number) => (overnight && n < lower ? n + day : n)
|
|
32
|
+
return {
|
|
33
|
+
allows(n: number) {
|
|
34
|
+
const t = position(n)
|
|
35
|
+
return t >= lower && t <= upper && (t - lower) % step === 0
|
|
36
|
+
},
|
|
37
|
+
move(n: number | null, delta: number) {
|
|
38
|
+
if (n === null && min !== undefined) return lower
|
|
39
|
+
const index = (position(n ?? 0) + delta - lower) / step
|
|
40
|
+
let next = delta > 0 ? Math.ceil(index) : Math.floor(index)
|
|
41
|
+
if (min === undefined && max === undefined) {
|
|
42
|
+
const count = last + 1
|
|
43
|
+
next = ((next % count) + count) % count
|
|
44
|
+
} else next = Math.max(0, Math.min(last, next))
|
|
45
|
+
return (lower + next * step) % day
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function timeSegments(
|
|
51
|
+
requested: string,
|
|
52
|
+
hourCycle: HourCycle | undefined,
|
|
53
|
+
withSeconds: boolean
|
|
54
|
+
) {
|
|
55
|
+
const locale = supportedLocale(Intl.DateTimeFormat, requested)
|
|
56
|
+
const format = new Intl.DateTimeFormat(locale, {
|
|
57
|
+
hour: '2-digit',
|
|
58
|
+
minute: '2-digit',
|
|
59
|
+
second: withSeconds ? '2-digit' : undefined,
|
|
60
|
+
hourCycle,
|
|
61
|
+
timeZone: 'UTC'
|
|
62
|
+
})
|
|
63
|
+
const resolved = format.resolvedOptions()
|
|
64
|
+
const cycle = resolved.hourCycle
|
|
65
|
+
const halfDay = cycle === 'h11' || cycle === 'h12'
|
|
66
|
+
const hourMin = cycle === 'h12' || cycle === 'h24' ? 1 : 0
|
|
67
|
+
const hourMax = hourMin + (halfDay ? 11 : 23)
|
|
68
|
+
const parts = format.formatToParts(0)
|
|
69
|
+
const dayPeriodAt = (h: number) =>
|
|
70
|
+
format.formatToParts(h * 3600000).find(part => part.type === 'dayPeriod')?.value
|
|
71
|
+
const names = new Intl.DisplayNames(resolved.locale, { type: 'dateTimeField' })
|
|
72
|
+
return {
|
|
73
|
+
parts,
|
|
74
|
+
names,
|
|
75
|
+
hourMin,
|
|
76
|
+
hourMax,
|
|
77
|
+
am: dayPeriodAt(0),
|
|
78
|
+
pm: dayPeriodAt(12),
|
|
79
|
+
...localizedDigits(resolved.locale, resolved.numberingSystem),
|
|
80
|
+
display(n: number | null): TimeParts {
|
|
81
|
+
if (n === null) return { hour: '', minute: '', second: '', dayPeriod: 'am' }
|
|
82
|
+
const h = Math.floor(n / 3600)
|
|
83
|
+
const hour = (halfDay ? h % 12 : h) || (hourMin === 1 ? hourMax : 0)
|
|
84
|
+
return {
|
|
85
|
+
hour: pad(hour),
|
|
86
|
+
minute: pad(Math.floor(n / 60) % 60),
|
|
87
|
+
second: pad(n % 60),
|
|
88
|
+
dayPeriod: h < 12 ? 'am' : 'pm'
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
parse(parts: TimeParts): number | null | undefined {
|
|
92
|
+
if (!parts.hour && !parts.minute && (!withSeconds || !parts.second)) return null
|
|
93
|
+
if (!parts.hour || !parts.minute || (withSeconds && !parts.second)) return undefined
|
|
94
|
+
const h = Number(parts.hour),
|
|
95
|
+
m = Number(parts.minute),
|
|
96
|
+
s = withSeconds ? Number(parts.second) : 0
|
|
97
|
+
if (h < hourMin || h > hourMax || m > 59 || s > 59) return undefined
|
|
98
|
+
const hour = halfDay ? (h % 12) + (parts.dayPeriod === 'pm' ? 12 : 0) : h % 24
|
|
99
|
+
return hour * 3600 + m * 60 + s
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -251,6 +251,11 @@ export type {
|
|
|
251
251
|
} from './themes/index.js'
|
|
252
252
|
export { ThemeScope } from './components/ThemeScope/ThemeScope.js'
|
|
253
253
|
export type { ThemeScopeProps } from './components/ThemeScope/ThemeScope.js'
|
|
254
|
+
export {
|
|
255
|
+
LocaleProvider,
|
|
256
|
+
type LocaleProviderProps
|
|
257
|
+
} from './components/LocaleProvider/LocaleProvider.js'
|
|
258
|
+
export { getLocaleAttributes } from './internal/locale.js'
|
|
254
259
|
export type { ButtonProps, ButtonRenderProps, ButtonVariant } from './components/Button/Button.js'
|
|
255
260
|
export { generateTypography, typographyPresets, typographyRoles } from './typography/index.js'
|
|
256
261
|
export type * from './typography/types.js'
|
|
@@ -573,11 +578,12 @@ export { Tag } from './components/Tag/Tag.js'
|
|
|
573
578
|
export type { TagProps } from './components/Tag/Tag.js'
|
|
574
579
|
export { TagsInput } from './components/TagsInput/TagsInput.js'
|
|
575
580
|
export type { TagsInputProps } from './components/TagsInput/TagsInput.js'
|
|
576
|
-
export { ComboboxTags } from './components/Combobox/Combobox.js'
|
|
581
|
+
export { ComboboxTags, ComboboxControl } from './components/Combobox/Combobox.js'
|
|
577
582
|
export type {
|
|
578
583
|
ComboboxMultipleProps,
|
|
579
584
|
ComboboxMultipleState,
|
|
580
|
-
ComboboxTagsProps
|
|
585
|
+
ComboboxTagsProps,
|
|
586
|
+
ComboboxControlProps
|
|
581
587
|
} from './components/Combobox/Combobox.js'
|
|
582
588
|
export { OTPField } from './components/OTPField/OTPField.js'
|
|
583
589
|
export { ButtonGroup } from './components/ButtonGroup/ButtonGroup.js'
|
|
@@ -611,6 +617,7 @@ export {
|
|
|
611
617
|
MultiSelectItem,
|
|
612
618
|
MultiSelectStatus,
|
|
613
619
|
MultiSelectTags,
|
|
620
|
+
MultiSelectControl,
|
|
614
621
|
useMultiSelect
|
|
615
622
|
} from './components/MultiSelect/MultiSelect.js'
|
|
616
623
|
export type {
|
|
@@ -625,6 +632,7 @@ export type {
|
|
|
625
632
|
MultiSelectItemProps,
|
|
626
633
|
MultiSelectStatusProps,
|
|
627
634
|
MultiSelectTagsProps,
|
|
635
|
+
MultiSelectControlProps,
|
|
628
636
|
MultiSelectRenderProps
|
|
629
637
|
} from './components/MultiSelect/MultiSelect.js'
|
|
630
638
|
|
|
@@ -769,8 +777,7 @@ export {
|
|
|
769
777
|
SidebarTrigger,
|
|
770
778
|
SidebarLabel,
|
|
771
779
|
SidebarContent,
|
|
772
|
-
useAppShell
|
|
773
|
-
useSidebar
|
|
780
|
+
useAppShell
|
|
774
781
|
} from './components/AppShell/AppShell.js'
|
|
775
782
|
export type {
|
|
776
783
|
AppShellProps,
|
|
@@ -812,5 +819,17 @@ export {
|
|
|
812
819
|
type CarouselState,
|
|
813
820
|
type CarouselViewportProps,
|
|
814
821
|
type CarouselActionProps,
|
|
822
|
+
type CarouselAutoPlayProps,
|
|
815
823
|
type CarouselIndicatorsProps
|
|
816
824
|
} from './components/Carousel/Carousel.js'
|
|
825
|
+
|
|
826
|
+
export { Calendar, type CalendarProps, type CalendarRange } from './components/Calendar/Calendar.js'
|
|
827
|
+
export { DatePicker, type DatePickerProps } from './components/DatePicker/DatePicker.js'
|
|
828
|
+
|
|
829
|
+
export {
|
|
830
|
+
DateRangePicker,
|
|
831
|
+
type DateRangePickerProps,
|
|
832
|
+
type DateRangeValue,
|
|
833
|
+
type DateRangePreset
|
|
834
|
+
} from './components/DateRangePicker/DateRangePicker.js'
|
|
835
|
+
export { TimeInput, type TimeInputProps } from './components/TimeInput/TimeInput.js'
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { supportedLocale } from './locale.js'
|
|
2
|
+
|
|
3
|
+
export type CalendarSystem = 'gregory' | 'buddhist'
|
|
4
|
+
export type Weekday = 1 | 2 | 3 | 4 | 5 | 6 | 7
|
|
5
|
+
|
|
6
|
+
// CLDR supplemental/weekData firstDay, shared by server and browser.
|
|
7
|
+
// https://github.com/unicode-org/cldr-json/blob/main/cldr-json/cldr-core/supplemental/weekData.json
|
|
8
|
+
const sunday = new Set(
|
|
9
|
+
'AG AS BD BR BS BT BW BZ CA CO DM DO ET GT GU HK HN ID IL IN IS JM JP KE KH KR LA MH MM MO MT MX MZ NI NP PA PE PH PK PR PT PY SA SG SV TH TT TW UM US VE VI WS YE ZA ZW'.split(
|
|
10
|
+
' '
|
|
11
|
+
)
|
|
12
|
+
)
|
|
13
|
+
const saturday = new Set('AF BH DJ DZ EG IQ IR JO KW LY OM QA SD SY'.split(' '))
|
|
14
|
+
const weekdays = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
|
|
15
|
+
|
|
16
|
+
function regionalWeekStart(locale: string) {
|
|
17
|
+
const tag = new Intl.Locale(locale)
|
|
18
|
+
const extension =
|
|
19
|
+
tag
|
|
20
|
+
.toString()
|
|
21
|
+
.split('-u-')[1]
|
|
22
|
+
?.split(/-[a-z0-9]-/)[0] ?? ''
|
|
23
|
+
const preferred = weekdays.indexOf(extension.match(/(?:^|-)fw-([a-z]{3})(?:-|$)/)?.[1] ?? '')
|
|
24
|
+
if (preferred >= 0) return preferred + 1
|
|
25
|
+
const region =
|
|
26
|
+
extension.match(/(?:^|-)rg-([a-z]{2}|\d{3})zzzz(?:-|$)/)?.[1]?.toUpperCase() ??
|
|
27
|
+
tag.maximize().region ??
|
|
28
|
+
'001'
|
|
29
|
+
if (sunday.has(region)) return 7
|
|
30
|
+
if (saturday.has(region)) return 6
|
|
31
|
+
return region === 'MV' ? 5 : 1
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function calendarFormat(requested: string, calendar?: CalendarSystem) {
|
|
35
|
+
const locale = supportedLocale(Intl.DateTimeFormat, requested)
|
|
36
|
+
const preferred = new Intl.DateTimeFormat(locale).resolvedOptions().calendar
|
|
37
|
+
const options = {
|
|
38
|
+
calendar: calendar ?? (preferred === 'buddhist' ? 'buddhist' : 'gregory'),
|
|
39
|
+
timeZone: 'UTC'
|
|
40
|
+
}
|
|
41
|
+
return (fields: Intl.DateTimeFormatOptions) =>
|
|
42
|
+
new Intl.DateTimeFormat(locale, { ...options, ...fields })
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function calendarLabels(requested: string, calendar?: CalendarSystem) {
|
|
46
|
+
const locale = supportedLocale(Intl.DateTimeFormat, requested)
|
|
47
|
+
const dateFormat = calendarFormat(locale, calendar)
|
|
48
|
+
return {
|
|
49
|
+
weekStart: regionalWeekStart(locale),
|
|
50
|
+
month: dateFormat({ month: 'long', year: 'numeric' }),
|
|
51
|
+
date: dateFormat({ dateStyle: 'full' }),
|
|
52
|
+
weekday: dateFormat({ weekday: 'short' }),
|
|
53
|
+
weekdayName: dateFormat({ weekday: 'long' }),
|
|
54
|
+
day: new Intl.NumberFormat(locale, { useGrouping: false })
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const dayMs = 86_400_000
|
|
59
|
+
export function isoDate(time: number) {
|
|
60
|
+
return new Date(time).toISOString().slice(0, 10)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function civilTime(iso: string) {
|
|
64
|
+
const time = Date.parse(iso + 'T00:00:00Z')
|
|
65
|
+
if (
|
|
66
|
+
!/^\d{4}-\d{2}-\d{2}$/.test(iso) ||
|
|
67
|
+
iso.startsWith('0000') ||
|
|
68
|
+
!Number.isFinite(time) ||
|
|
69
|
+
isoDate(time) !== iso
|
|
70
|
+
)
|
|
71
|
+
throw new RangeError(
|
|
72
|
+
'Calendar dates must be valid ISO dates from 0001-01-01 through 9999-12-31.'
|
|
73
|
+
)
|
|
74
|
+
return time
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function monthStart(time: number, offset = 0) {
|
|
78
|
+
const date = new Date(time)
|
|
79
|
+
return date.setUTCMonth(date.getUTCMonth() + offset, 1)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function shiftMonth(time: number, offset: number) {
|
|
83
|
+
const start = monthStart(time, offset)
|
|
84
|
+
const sameDay = start + (new Date(time).getUTCDate() - 1) * dayMs
|
|
85
|
+
return Math.min(sameDay, monthStart(start, 1) - dayMs)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function rangeHasDisabledDate(
|
|
89
|
+
start: string,
|
|
90
|
+
end: string,
|
|
91
|
+
isDateDisabled?: (iso: string) => boolean
|
|
92
|
+
) {
|
|
93
|
+
if (!isDateDisabled) return false
|
|
94
|
+
const last = civilTime(end)
|
|
95
|
+
for (let day = civilTime(start); day <= last; day += dayMs) {
|
|
96
|
+
if (isDateDisabled(isoDate(day))) return true
|
|
97
|
+
}
|
|
98
|
+
return false
|
|
99
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export const defaultLocale = 'en-US'
|
|
2
|
+
|
|
3
|
+
// Right-to-left scripts from Unicode CLDR script metadata, including script variants.
|
|
4
|
+
const rtlScripts = new Set([
|
|
5
|
+
'Adlm',
|
|
6
|
+
'Arab',
|
|
7
|
+
'Aran',
|
|
8
|
+
'Armi',
|
|
9
|
+
'Avst',
|
|
10
|
+
'Chrs',
|
|
11
|
+
'Elym',
|
|
12
|
+
'Gara',
|
|
13
|
+
'Hatr',
|
|
14
|
+
'Hebr',
|
|
15
|
+
'Hung',
|
|
16
|
+
'Khar',
|
|
17
|
+
'Lydi',
|
|
18
|
+
'Mand',
|
|
19
|
+
'Mani',
|
|
20
|
+
'Mend',
|
|
21
|
+
'Merc',
|
|
22
|
+
'Mero',
|
|
23
|
+
'Narb',
|
|
24
|
+
'Nbat',
|
|
25
|
+
'Nkoo',
|
|
26
|
+
'Orkh',
|
|
27
|
+
'Ougr',
|
|
28
|
+
'Palm',
|
|
29
|
+
'Phli',
|
|
30
|
+
'Phlp',
|
|
31
|
+
'Phnx',
|
|
32
|
+
'Prti',
|
|
33
|
+
'Rohg',
|
|
34
|
+
'Samr',
|
|
35
|
+
'Sarb',
|
|
36
|
+
'Sidt',
|
|
37
|
+
'Sogd',
|
|
38
|
+
'Sogo',
|
|
39
|
+
'Syrc',
|
|
40
|
+
'Syre',
|
|
41
|
+
'Syrj',
|
|
42
|
+
'Syrn',
|
|
43
|
+
'Thaa',
|
|
44
|
+
'Yezi'
|
|
45
|
+
])
|
|
46
|
+
|
|
47
|
+
export function getLocaleAttributes(locale?: string): { lang?: string; dir?: 'ltr' | 'rtl' } {
|
|
48
|
+
if (locale === undefined) return {}
|
|
49
|
+
const tag = new Intl.Locale(locale)
|
|
50
|
+
const script = tag.script ?? tag.maximize().script
|
|
51
|
+
return { lang: tag.baseName, dir: script && rtlScripts.has(script) ? 'rtl' : 'ltr' }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
type LocaleSupport = Pick<typeof Intl.NumberFormat, 'supportedLocalesOf'>
|
|
55
|
+
|
|
56
|
+
export function supportedLocale(service: LocaleSupport, locale: string): string {
|
|
57
|
+
return service.supportedLocalesOf(locale, { localeMatcher: 'lookup' }).length
|
|
58
|
+
? locale
|
|
59
|
+
: defaultLocale
|
|
60
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { supportedLocale } from './locale.js'
|
|
2
|
+
|
|
3
|
+
const asciiWords = /^[A-Za-z0-9 ]*$/
|
|
4
|
+
|
|
5
|
+
export function createLocaleMatcher(locale: string) {
|
|
6
|
+
const collator = new Intl.Collator(supportedLocale(Intl.Collator, locale), {
|
|
7
|
+
localeMatcher: 'lookup',
|
|
8
|
+
usage: 'search',
|
|
9
|
+
sensitivity: 'accent',
|
|
10
|
+
ignorePunctuation: false
|
|
11
|
+
})
|
|
12
|
+
const segmenter = new Intl.Segmenter(supportedLocale(Intl.Segmenter, locale), {
|
|
13
|
+
localeMatcher: 'lookup'
|
|
14
|
+
})
|
|
15
|
+
const matches = (left: string, right: string) => collator.compare(left, right) === 0
|
|
16
|
+
|
|
17
|
+
function match(query: string, mode: 'prefix' | 'substring') {
|
|
18
|
+
if (!query) return () => true
|
|
19
|
+
const matchingLocale = collator.resolvedOptions().locale
|
|
20
|
+
const foldedQuery = asciiWords.test(query) ? query.toLocaleLowerCase(matchingLocale) : undefined
|
|
21
|
+
return (text: string) => {
|
|
22
|
+
if (foldedQuery !== undefined && asciiWords.test(text)) {
|
|
23
|
+
const folded = text.toLocaleLowerCase(matchingLocale)
|
|
24
|
+
return mode === 'prefix' ? folded.startsWith(foldedQuery) : folded.includes(foldedQuery)
|
|
25
|
+
}
|
|
26
|
+
const offsets = [
|
|
27
|
+
0,
|
|
28
|
+
...Array.from(segmenter.segment(text), ({ index, segment }) => index + segment.length)
|
|
29
|
+
]
|
|
30
|
+
// A collation match can span a different number of graphemes than the query.
|
|
31
|
+
const starts = mode === 'prefix' ? 1 : offsets.length - 1
|
|
32
|
+
for (let start = 0; start < starts; start++) {
|
|
33
|
+
for (let end = start + 1; end < offsets.length; end++) {
|
|
34
|
+
if (matches(text.slice(offsets[start], offsets[end]), query)) return true
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return false
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
match,
|
|
43
|
+
typeaheadQuery(text: string) {
|
|
44
|
+
const keys = Array.from(segmenter.segment(text), ({ segment }) => segment)
|
|
45
|
+
const first = keys[0] ?? ''
|
|
46
|
+
return keys.every(key => matches(key, first)) ? first : text
|
|
47
|
+
},
|
|
48
|
+
isSingleKey(text: string) {
|
|
49
|
+
const first = segmenter.segment(text).containing(0)
|
|
50
|
+
return first?.segment === text
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function localizedDigits(locale: string, numberingSystem: string) {
|
|
2
|
+
const numbers = new Intl.NumberFormat(locale, {
|
|
3
|
+
numberingSystem,
|
|
4
|
+
useGrouping: false
|
|
5
|
+
})
|
|
6
|
+
const digits = Array.from({ length: 10 }, (_, i) => numbers.format(i))
|
|
7
|
+
function ascii(text: string) {
|
|
8
|
+
return Array.from(text, char =>
|
|
9
|
+
digits.includes(char) ? String(digits.indexOf(char)) : char
|
|
10
|
+
).join('')
|
|
11
|
+
}
|
|
12
|
+
const localize = (text: string) => text.replace(/[0-9]/g, digit => digits[Number(digit)])
|
|
13
|
+
return { ascii, localize }
|
|
14
|
+
}
|