@cognitap/radiant 0.0.81 → 0.0.83
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 +23 -0
- 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/Combobox/Combobox.d.ts.map +1 -1
- package/dist/library/components/Combobox/Combobox.js +10 -4
- package/dist/library/components/Combobox/Combobox.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 +12 -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 +26 -0
- package/dist/library/components/DateRangePicker/DateRangePicker.d.ts.map +1 -0
- package/dist/library/components/DateRangePicker/DateRangePicker.js +72 -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/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 +21 -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 +6 -0
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +6 -0
- 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 +8 -4
- 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/package.json +1 -1
- package/src/components/Autocomplete/Autocomplete.tsx +1 -13
- package/src/components/Calendar/Calendar.tsx +310 -0
- package/src/components/Combobox/Combobox.tsx +19 -6
- package/src/components/DatePicker/DateCalendar.tsx +83 -0
- package/src/components/DatePicker/DateEditor.tsx +189 -0
- package/src/components/DatePicker/DatePicker.tsx +121 -0
- package/src/components/DatePicker/dateSegments.ts +46 -0
- package/src/components/DateRangePicker/DateRangePicker.tsx +209 -0
- package/src/components/LocaleProvider/LocaleProvider.tsx +26 -0
- package/src/components/Menu/Menu.tsx +8 -2
- 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 +274 -0
- package/src/components/TimeInput/timeSegments.ts +102 -0
- package/src/components/Tooltip/Tooltip.tsx +3 -1
- package/src/index.ts +16 -0
- 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 +9 -4
- 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
|
@@ -20,8 +20,10 @@ import { cn } from 'cn'
|
|
|
20
20
|
import { joinIds } from '../../internal/ids.js'
|
|
21
21
|
import { controlMetrics, itemGroupLabelClass } from '../shared/controlStyles.js'
|
|
22
22
|
import { matchSelectText, moveSelectFocus, positionSelect } from '../../internal/select.js'
|
|
23
|
+
import { observeAnchorLocale, syncPopupLocale } from '../../internal/popupLocale.js'
|
|
23
24
|
import { useComposedRefs } from '../../react/useComposedRefs.js'
|
|
24
25
|
import { useControllableState } from '../../react/useControllableState.js'
|
|
26
|
+
import { useLocaleMatcher } from '../../react/useLocale.js'
|
|
25
27
|
import { useTransitionHost } from '../../motion/host.js'
|
|
26
28
|
import type { StateOwner, ValueOwner } from '../../react/useNativeOwner.js'
|
|
27
29
|
import type { ControlSize } from '../../presentation.js'
|
|
@@ -75,6 +77,7 @@ type Context = SelectState & {
|
|
|
75
77
|
commit(value: string): void
|
|
76
78
|
close(restore?: boolean): void
|
|
77
79
|
search(key: string, current: string | null): string | undefined
|
|
80
|
+
isSearchKey(key: string): boolean
|
|
78
81
|
restoreFocus: RefObject<boolean>
|
|
79
82
|
}
|
|
80
83
|
const SelectContext = createContext<Context | null>(null)
|
|
@@ -108,6 +111,7 @@ export function Select({
|
|
|
108
111
|
children
|
|
109
112
|
}: SelectProps) {
|
|
110
113
|
const ids = useId()
|
|
114
|
+
const matcher = useLocaleMatcher()
|
|
111
115
|
const field = useField()
|
|
112
116
|
const disabled = !!(disabledProp || field?.disabled)
|
|
113
117
|
const required = !!(requiredProp || field?.required)
|
|
@@ -140,7 +144,7 @@ export function Select({
|
|
|
140
144
|
const restoreFocus = useRef(true)
|
|
141
145
|
const pendingUser = useRef<string | null>(null)
|
|
142
146
|
const dispatching = useRef(false)
|
|
143
|
-
const searchBuffer = useRef({ text: '', at: 0 })
|
|
147
|
+
const searchBuffer = useRef({ text: '', at: 0, matcher })
|
|
144
148
|
const selected = items.find(item => item.value === selection.value)
|
|
145
149
|
const nativeValue = selected?.value ?? ''
|
|
146
150
|
const [initialNativeValue] = useState(nativeValue)
|
|
@@ -148,7 +152,7 @@ export function Select({
|
|
|
148
152
|
const unavailable = () => disabled || readOnly || !!trigger?.matches(':disabled')
|
|
149
153
|
const setOpen = (next: boolean) => {
|
|
150
154
|
if (next && unavailable()) return
|
|
151
|
-
searchBuffer.current = { text: '', at: 0 }
|
|
155
|
+
searchBuffer.current = { text: '', at: 0, matcher }
|
|
152
156
|
restoreFocus.current = true
|
|
153
157
|
expansion.request(next)
|
|
154
158
|
}
|
|
@@ -237,11 +241,14 @@ export function Select({
|
|
|
237
241
|
commit,
|
|
238
242
|
close,
|
|
239
243
|
restoreFocus,
|
|
244
|
+
isSearchKey: matcher.isSingleKey,
|
|
240
245
|
search(key, current) {
|
|
241
246
|
const now = Date.now()
|
|
242
|
-
const
|
|
243
|
-
|
|
244
|
-
|
|
247
|
+
const previous = searchBuffer.current
|
|
248
|
+
const text =
|
|
249
|
+
(previous.matcher === matcher && now - previous.at < 1000 ? previous.text : '') + key
|
|
250
|
+
searchBuffer.current = { text, at: now, matcher }
|
|
251
|
+
return matchSelectText(entries, text, current, matcher)
|
|
245
252
|
}
|
|
246
253
|
}
|
|
247
254
|
return (
|
|
@@ -376,7 +383,7 @@ export function SelectTrigger({
|
|
|
376
383
|
if (['Enter', ' ', 'ArrowDown', 'ArrowUp'].includes(event.key)) {
|
|
377
384
|
event.preventDefault()
|
|
378
385
|
select.setOpen(true)
|
|
379
|
-
} else if (event.key
|
|
386
|
+
} else if (select.isSearchKey(event.key)) {
|
|
380
387
|
const next = select.search(event.key, select.value)
|
|
381
388
|
if (next !== undefined) {
|
|
382
389
|
event.preventDefault()
|
|
@@ -473,6 +480,7 @@ export function SelectContent({
|
|
|
473
480
|
...props
|
|
474
481
|
}: SelectContentProps) {
|
|
475
482
|
const select = useSelectContext()
|
|
483
|
+
const { lang, dir } = props
|
|
476
484
|
const mergedRef = useComposedRefs(select.content, ref)
|
|
477
485
|
const {
|
|
478
486
|
ref: transitionRef,
|
|
@@ -511,9 +519,11 @@ export function SelectContent({
|
|
|
511
519
|
}
|
|
512
520
|
// Top-layer auto layout is viewport-sized. Establish the anchor width before it can paint.
|
|
513
521
|
seedWidth()
|
|
522
|
+
syncPopupLocale(trigger, node, { lang, dir })
|
|
514
523
|
node.showPopover()
|
|
515
524
|
const place = () => {
|
|
516
525
|
if (!node.isConnected || !node.matches(':popover-open')) return
|
|
526
|
+
const rtl = syncPopupLocale(trigger, node, { lang, dir })
|
|
517
527
|
seedWidth()
|
|
518
528
|
const viewport = win.visualViewport
|
|
519
529
|
const selected = Array.from(
|
|
@@ -544,7 +554,7 @@ export function SelectContent({
|
|
|
544
554
|
padding: Math.max(0, collisionPadding),
|
|
545
555
|
offset: sideOffset,
|
|
546
556
|
aligned: position === 'item-aligned',
|
|
547
|
-
rtl
|
|
557
|
+
rtl
|
|
548
558
|
})
|
|
549
559
|
for (const [key, value] of Object.entries({
|
|
550
560
|
top: next.top,
|
|
@@ -594,6 +604,7 @@ export function SelectContent({
|
|
|
594
604
|
const observer = new win.ResizeObserver(schedule)
|
|
595
605
|
const mutations = new win.MutationObserver(schedule)
|
|
596
606
|
mutations.observe(node, { childList: true, subtree: true, characterData: true })
|
|
607
|
+
observeAnchorLocale(mutations, trigger)
|
|
597
608
|
observer.observe(trigger)
|
|
598
609
|
observer.observe(node)
|
|
599
610
|
doc.addEventListener('pointerdown', outside, true)
|
|
@@ -613,7 +624,7 @@ export function SelectContent({
|
|
|
613
624
|
win.visualViewport?.removeEventListener('resize', place)
|
|
614
625
|
win.visualViewport?.removeEventListener('scroll', place)
|
|
615
626
|
}
|
|
616
|
-
}, [select.open, select.trigger, select.items, position, sideOffset, collisionPadding])
|
|
627
|
+
}, [select.open, select.trigger, select.items, position, sideOffset, collisionPadding, lang, dir])
|
|
617
628
|
useLayoutEffect(() => {
|
|
618
629
|
const node = select.content.current
|
|
619
630
|
const trigger = select.trigger
|
|
@@ -665,7 +676,7 @@ export function SelectContent({
|
|
|
665
676
|
if (['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) {
|
|
666
677
|
event.preventDefault()
|
|
667
678
|
select.focus(moveSelectFocus(select.entries, current, event.key))
|
|
668
|
-
} else if (event.key
|
|
679
|
+
} else if (select.isSearchKey(event.key) && event.key !== ' ') {
|
|
669
680
|
event.preventDefault()
|
|
670
681
|
select.focus(select.search(event.key, current))
|
|
671
682
|
}
|
|
@@ -0,0 +1,274 @@
|
|
|
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 type { ThemeStyle } from '../../themes/defineTheme.js'
|
|
25
|
+
import {
|
|
26
|
+
isoTime,
|
|
27
|
+
secondsOf,
|
|
28
|
+
timeRange,
|
|
29
|
+
timeSegments,
|
|
30
|
+
type ClockPart,
|
|
31
|
+
type HourCycle,
|
|
32
|
+
type TimeParts
|
|
33
|
+
} from './timeSegments.js'
|
|
34
|
+
|
|
35
|
+
export type TimeInputProps = ValueOwner<string | null> &
|
|
36
|
+
Pick<
|
|
37
|
+
ComponentProps<'input'>,
|
|
38
|
+
| 'ref'
|
|
39
|
+
| 'id'
|
|
40
|
+
| 'name'
|
|
41
|
+
| 'form'
|
|
42
|
+
| 'className'
|
|
43
|
+
| 'readOnly'
|
|
44
|
+
| 'disabled'
|
|
45
|
+
| 'required'
|
|
46
|
+
| 'aria-label'
|
|
47
|
+
| 'aria-labelledby'
|
|
48
|
+
| 'aria-describedby'
|
|
49
|
+
| 'aria-invalid'
|
|
50
|
+
> & {
|
|
51
|
+
onValueChange?: (time: string | null) => void
|
|
52
|
+
locale?: string
|
|
53
|
+
hourCycle?: HourCycle
|
|
54
|
+
/** Show seconds. Second-precision values, bounds and steps also enable this segment. */
|
|
55
|
+
showSeconds?: boolean
|
|
56
|
+
min?: string
|
|
57
|
+
max?: string
|
|
58
|
+
/** Whole seconds, counted from min or midnight. Defaults to one minute, or one second with seconds shown. */
|
|
59
|
+
step?: number
|
|
60
|
+
controlSize?: ControlSize
|
|
61
|
+
/** Styles and theme variables for the shared field wrapper. */
|
|
62
|
+
style?: ThemeStyle
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const units = { hour: 3600, minute: 60, second: 1 }
|
|
66
|
+
|
|
67
|
+
export function TimeInput({
|
|
68
|
+
value,
|
|
69
|
+
defaultValue,
|
|
70
|
+
onValueChange,
|
|
71
|
+
locale,
|
|
72
|
+
hourCycle,
|
|
73
|
+
showSeconds = false,
|
|
74
|
+
min,
|
|
75
|
+
max,
|
|
76
|
+
step,
|
|
77
|
+
controlSize = 'medium',
|
|
78
|
+
ref,
|
|
79
|
+
name,
|
|
80
|
+
form,
|
|
81
|
+
className,
|
|
82
|
+
style,
|
|
83
|
+
readOnly,
|
|
84
|
+
...props
|
|
85
|
+
}: TimeInputProps) {
|
|
86
|
+
const field = useField()
|
|
87
|
+
const control = useFieldControl(props)
|
|
88
|
+
const labelledBy = props['aria-labelledby'] ?? field?.labelId
|
|
89
|
+
const label = props['aria-label']
|
|
90
|
+
requireAccessibleName('TimeInput', { 'aria-label': label, 'aria-labelledby': labelledBy })
|
|
91
|
+
const owner = useControllableState({
|
|
92
|
+
name: 'TimeInput value',
|
|
93
|
+
value,
|
|
94
|
+
defaultValue,
|
|
95
|
+
initialValue: null,
|
|
96
|
+
onChange: onValueChange
|
|
97
|
+
})
|
|
98
|
+
const seconds = owner.value === null ? null : secondsOf(owner.value)
|
|
99
|
+
const withSeconds =
|
|
100
|
+
showSeconds ||
|
|
101
|
+
[owner.value, defaultValue, min, max].some(t => t?.length === 8) ||
|
|
102
|
+
(step !== undefined && step % 60 !== 0)
|
|
103
|
+
const range = timeRange(min, max, step ?? (withSeconds ? 1 : 60))
|
|
104
|
+
const language = useLocale(locale)
|
|
105
|
+
const format = useMemo(
|
|
106
|
+
() => timeSegments(language, hourCycle, withSeconds),
|
|
107
|
+
[language, hourCycle, withSeconds]
|
|
108
|
+
)
|
|
109
|
+
const [draft, setDraft] = useState<{
|
|
110
|
+
owner: string | null
|
|
111
|
+
format: typeof format
|
|
112
|
+
parts: TimeParts
|
|
113
|
+
} | null>(null)
|
|
114
|
+
const editing = draft?.format === format && draft.owner === owner.value
|
|
115
|
+
if (draft && !editing) setDraft(null)
|
|
116
|
+
const parts = editing ? draft.parts : format.display(seconds)
|
|
117
|
+
const parsed = format.parse(parts)
|
|
118
|
+
const invalid = parsed === undefined || (parsed !== null && !range.allows(parsed))
|
|
119
|
+
if (editing && !invalid && parsed !== seconds) setDraft(null)
|
|
120
|
+
|
|
121
|
+
const first = useRef<HTMLInputElement>(null)
|
|
122
|
+
const editorRef = useComposedRefs(first, ref)
|
|
123
|
+
useLayoutEffect(() => {
|
|
124
|
+
const input = first.current
|
|
125
|
+
input?.setCustomValidity(invalid ? 'Enter a valid time within the allowed range and step.' : '')
|
|
126
|
+
return () => input?.setCustomValidity('')
|
|
127
|
+
}, [format, invalid])
|
|
128
|
+
useFormReset(first, () => {
|
|
129
|
+
owner.reset()
|
|
130
|
+
setDraft(null)
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
function edit(updated: TimeParts) {
|
|
134
|
+
const next = format.parse(updated)
|
|
135
|
+
if (next === undefined || (next !== null && !range.allows(next))) {
|
|
136
|
+
setDraft({ owner: owner.value, format, parts: updated })
|
|
137
|
+
return
|
|
138
|
+
}
|
|
139
|
+
const iso = next === null ? null : isoTime(next, withSeconds)
|
|
140
|
+
setDraft({ owner: iso, format, parts: updated })
|
|
141
|
+
owner.request(iso)
|
|
142
|
+
}
|
|
143
|
+
function togglePeriod() {
|
|
144
|
+
edit({ ...parts, dayPeriod: parts.dayPeriod === 'am' ? 'pm' : 'am' })
|
|
145
|
+
}
|
|
146
|
+
function editNumber(part: ClockPart, input: HTMLInputElement) {
|
|
147
|
+
const text = format.ascii(input.value)
|
|
148
|
+
if (!/^\d{0,2}$/.test(text)) return
|
|
149
|
+
edit({ ...parts, [part]: text })
|
|
150
|
+
if (text.length === 2) {
|
|
151
|
+
const segments = Array.from(
|
|
152
|
+
input.parentElement?.querySelectorAll<HTMLElement>('[data-segment]') ?? []
|
|
153
|
+
)
|
|
154
|
+
segments[segments.indexOf(input) + 1]?.focus()
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
function advance(event: KeyboardEvent, part: ClockPart) {
|
|
158
|
+
if (readOnly || control.disabled || event.altKey || event.metaKey || event.ctrlKey) return
|
|
159
|
+
if (event.key !== 'ArrowUp' && event.key !== 'ArrowDown') return
|
|
160
|
+
event.preventDefault()
|
|
161
|
+
const direction = event.key === 'ArrowUp' ? 1 : -1
|
|
162
|
+
let from = parsed
|
|
163
|
+
if (from === undefined) {
|
|
164
|
+
const seed = format.display(min === undefined ? 0 : secondsOf(min))
|
|
165
|
+
from =
|
|
166
|
+
format.parse({
|
|
167
|
+
hour: parts.hour || seed.hour,
|
|
168
|
+
minute: parts.minute || seed.minute,
|
|
169
|
+
second: parts.second || seed.second,
|
|
170
|
+
dayPeriod: parts.dayPeriod
|
|
171
|
+
}) ?? seconds
|
|
172
|
+
}
|
|
173
|
+
const next = range.move(from, direction * units[part])
|
|
174
|
+
setDraft(null)
|
|
175
|
+
owner.request(isoTime(next, withSeconds))
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return (
|
|
179
|
+
<InputGroup
|
|
180
|
+
data-slot="time-input"
|
|
181
|
+
controlSize={controlSize}
|
|
182
|
+
// oxlint-disable-next-line jsx-a11y/prefer-tag-over-role -- The grouped native controls share one time label.
|
|
183
|
+
role="group"
|
|
184
|
+
aria-labelledby={labelledBy}
|
|
185
|
+
aria-label={label}
|
|
186
|
+
className={cn('w-fit max-w-full gap-0', className)}
|
|
187
|
+
style={style}
|
|
188
|
+
>
|
|
189
|
+
<WithoutField>
|
|
190
|
+
{format.parts.map(({ type: part, value: literal }, index) => {
|
|
191
|
+
if (part === 'dayPeriod')
|
|
192
|
+
return (
|
|
193
|
+
<Button
|
|
194
|
+
key={part}
|
|
195
|
+
type="button"
|
|
196
|
+
variant="ghost"
|
|
197
|
+
tone="neutral"
|
|
198
|
+
motion="off"
|
|
199
|
+
data-slot="time-input-period"
|
|
200
|
+
data-segment={part}
|
|
201
|
+
aria-label={[format.names.of(part), format[parts.dayPeriod]]
|
|
202
|
+
.filter(Boolean)
|
|
203
|
+
.join(' ')}
|
|
204
|
+
aria-describedby={control['aria-describedby']}
|
|
205
|
+
controlSize={controlSize}
|
|
206
|
+
disabled={control.disabled || readOnly}
|
|
207
|
+
onClick={togglePeriod}
|
|
208
|
+
onKeyDown={event => {
|
|
209
|
+
if (event.altKey || event.metaKey || event.ctrlKey || event.defaultPrevented)
|
|
210
|
+
return
|
|
211
|
+
if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
|
|
212
|
+
event.preventDefault()
|
|
213
|
+
togglePeriod()
|
|
214
|
+
}
|
|
215
|
+
}}
|
|
216
|
+
className={cn(
|
|
217
|
+
inlineActionClass,
|
|
218
|
+
focusRingInsetClass,
|
|
219
|
+
'min-h-[max(1.5rem,calc(var(--_rad-control-height)-0.75rem))] min-w-6 [--rad-button-border-width:0px] px-1 leading-none',
|
|
220
|
+
'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'
|
|
221
|
+
)}
|
|
222
|
+
>
|
|
223
|
+
{format[parts.dayPeriod]}
|
|
224
|
+
</Button>
|
|
225
|
+
)
|
|
226
|
+
if (part !== 'hour' && part !== 'minute' && part !== 'second')
|
|
227
|
+
return (
|
|
228
|
+
<span
|
|
229
|
+
key={index}
|
|
230
|
+
aria-hidden="true"
|
|
231
|
+
>
|
|
232
|
+
{literal}
|
|
233
|
+
</span>
|
|
234
|
+
)
|
|
235
|
+
return (
|
|
236
|
+
<InputGroupInput
|
|
237
|
+
key={part}
|
|
238
|
+
data-segment={part}
|
|
239
|
+
// oxlint-disable-next-line jsx-a11y/prefer-tag-over-role -- Localized digits need text entry with per-segment spinbutton semantics.
|
|
240
|
+
role="spinbutton"
|
|
241
|
+
aria-valuemin={part === 'hour' ? format.hourMin : 0}
|
|
242
|
+
aria-valuemax={part === 'hour' ? format.hourMax : 59}
|
|
243
|
+
aria-valuenow={parts[part] ? Number(parts[part]) : undefined}
|
|
244
|
+
ref={part === 'hour' ? editorRef : undefined}
|
|
245
|
+
id={part === 'hour' ? control.id : undefined}
|
|
246
|
+
form={form}
|
|
247
|
+
disabled={control.disabled}
|
|
248
|
+
readOnly={readOnly}
|
|
249
|
+
required={control.required}
|
|
250
|
+
aria-label={format.names.of(part)}
|
|
251
|
+
aria-describedby={control['aria-describedby']}
|
|
252
|
+
aria-invalid={invalid || control['aria-invalid'] || undefined}
|
|
253
|
+
placeholder="––"
|
|
254
|
+
autoComplete="off"
|
|
255
|
+
inputMode="numeric"
|
|
256
|
+
className="w-[2.7ch] flex-none text-center tabular-nums focus-visible:underline"
|
|
257
|
+
value={format.localize(parts[part])}
|
|
258
|
+
onFocus={event => event.currentTarget.select()}
|
|
259
|
+
onChange={event => editNumber(part, event.currentTarget)}
|
|
260
|
+
onKeyDown={event => advance(event, part)}
|
|
261
|
+
/>
|
|
262
|
+
)
|
|
263
|
+
})}
|
|
264
|
+
</WithoutField>
|
|
265
|
+
<input
|
|
266
|
+
type="hidden"
|
|
267
|
+
name={name}
|
|
268
|
+
form={form}
|
|
269
|
+
disabled={control.disabled}
|
|
270
|
+
value={invalid || seconds === null ? '' : isoTime(seconds, withSeconds)}
|
|
271
|
+
/>
|
|
272
|
+
</InputGroup>
|
|
273
|
+
)
|
|
274
|
+
}
|
|
@@ -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'
|
|
@@ -817,3 +822,14 @@ export {
|
|
|
817
822
|
type CarouselAutoPlayProps,
|
|
818
823
|
type CarouselIndicatorsProps
|
|
819
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
|
+
}
|