@tangible/ui 0.0.21 → 0.2.0
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/components/Button/Button.js +6 -8
- package/components/Button/types.d.ts +5 -7
- package/components/Chip/Chip.d.ts +28 -0
- package/components/Chip/Chip.js +19 -3
- package/components/Combobox/Combobox.d.ts +1 -1
- package/components/Combobox/Combobox.js +2 -2
- package/components/Combobox/types.d.ts +13 -0
- package/components/DatePicker/Calendar.d.ts +12 -0
- package/components/DatePicker/Calendar.js +145 -0
- package/components/DatePicker/DatePicker.d.ts +23 -0
- package/components/DatePicker/DatePicker.js +123 -0
- package/components/DatePicker/DateRangePicker.d.ts +16 -0
- package/components/DatePicker/DateRangePicker.js +114 -0
- package/components/DatePicker/date-utils.d.ts +51 -0
- package/components/DatePicker/date-utils.js +228 -0
- package/components/DatePicker/index.d.ts +8 -0
- package/components/DatePicker/index.js +6 -0
- package/components/DatePicker/presets.d.ts +3 -0
- package/components/DatePicker/presets.js +37 -0
- package/components/DatePicker/types.d.ts +138 -0
- package/components/DatePicker/types.js +39 -0
- package/components/IconButton/types.d.ts +2 -2
- package/components/MultiCombobox/MultiCombobox.d.ts +33 -0
- package/components/MultiCombobox/MultiCombobox.js +595 -0
- package/components/MultiCombobox/MultiComboboxContext.d.ts +8 -0
- package/components/MultiCombobox/MultiComboboxContext.js +36 -0
- package/components/MultiCombobox/index.d.ts +3 -0
- package/components/MultiCombobox/index.js +2 -0
- package/components/MultiCombobox/types.d.ts +244 -0
- package/components/MultiCombobox/types.js +7 -0
- package/components/MultiSelect/MultiSelect.js +22 -2
- package/components/Popover/Popover.js +16 -2
- package/components/SegmentedControl/types.d.ts +2 -2
- package/components/Select/Select.js +14 -0
- package/components/Slider/Slider.d.ts +2 -0
- package/components/Slider/Slider.js +29 -0
- package/components/Slider/index.d.ts +2 -0
- package/components/Slider/index.js +1 -0
- package/components/Slider/types.d.ts +39 -0
- package/components/Slider/types.js +1 -0
- package/components/index.d.ts +4 -0
- package/components/index.js +3 -0
- package/package.json +13 -1
- package/styles/all.css +1 -1
- package/styles/all.expanded.css +1181 -187
- package/styles/all.expanded.unlayered.css +1181 -187
- package/styles/all.unlayered.css +1 -1
- package/styles/components/_bundle.scss +6 -0
- package/styles/components/button.scss +59 -43
- package/styles/components/card.scss +1 -1
- package/styles/components/chip.scss +84 -10
- package/styles/components/combobox.scss +16 -2
- package/styles/components/date-picker.scss +400 -0
- package/styles/components/icon-button.scss +36 -30
- package/styles/components/input/index.scss +1 -2
- package/styles/components/multi-combobox.scss +457 -0
- package/styles/components/multi-select.scss +34 -18
- package/styles/components/notice.scss +15 -6
- package/styles/components/pager.scss +4 -2
- package/styles/components/popover.scss +6 -0
- package/styles/components/progress.scss +23 -6
- package/styles/components/segmented-control.scss +53 -15
- package/styles/components/select.scss +19 -3
- package/styles/components/slider.scss +184 -0
- package/styles/components/switch.scss +1 -1
- package/styles/core.css +1 -1
- package/styles/core.unlayered.css +1 -1
- package/styles/system/_control.scss +9 -8
- package/styles/system/_tokens.scss +16 -0
- package/tui-manifest.json +311 -14
- package/utils/portal.d.ts +0 -4
- package/utils/portal.js +38 -5
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { cx } from '../../utils/cx.js';
|
|
4
|
+
import { useControllableState } from '../../utils/use-controllable-state.js';
|
|
5
|
+
import { Button } from '../Button/index.js';
|
|
6
|
+
import { Icon } from '../Icon/index.js';
|
|
7
|
+
import { Popover, PopoverContent, PopoverTrigger } from '../Popover/index.js';
|
|
8
|
+
import { TextInput } from '../TextInput/index.js';
|
|
9
|
+
import { Calendar } from './Calendar.js';
|
|
10
|
+
import { addMonths, formatDay, formatDayTime, formatNumeric, isBeforeDay, isSameMonth, parseNumeric, startOfMonth, resolveLocale, } from './date-utils.js';
|
|
11
|
+
import { defaultDateRangePresets } from './presets.js';
|
|
12
|
+
import { defaultDateRangePickerLabels } from './types.js';
|
|
13
|
+
// =============================================================================
|
|
14
|
+
// DateRangePicker
|
|
15
|
+
// =============================================================================
|
|
16
|
+
//
|
|
17
|
+
// Trigger opening a popover dialog with a quick-range rail, editable From/To
|
|
18
|
+
// fields, and two linked month calendars. Selection is staged: day clicks
|
|
19
|
+
// build a draft (first click sets From, second sets To, swapping if needed),
|
|
20
|
+
// and only Apply commits it — Cancel or dismissal discards the draft.
|
|
21
|
+
//
|
|
22
|
+
// =============================================================================
|
|
23
|
+
const EMPTY_RANGE = { from: null, to: null };
|
|
24
|
+
export const DateRangePicker = React.forwardRef(function DateRangePicker({ value: valueProp, defaultValue, onChange, withTime = false, presets = defaultDateRangePresets, min, max, isDayDisabled, weekStartsOn = 1, locale: localeProp, disabled, labels: labelsProp, calendarLabels, 'aria-label': ariaLabel, className, id, }, ref) {
|
|
25
|
+
const labels = { ...defaultDateRangePickerLabels, ...labelsProp };
|
|
26
|
+
const locale = resolveLocale(localeProp);
|
|
27
|
+
const [open, setOpen] = React.useState(false);
|
|
28
|
+
const [value, setValue] = useControllableState({
|
|
29
|
+
value: valueProp,
|
|
30
|
+
defaultValue: defaultValue ?? null,
|
|
31
|
+
onChange: onChange,
|
|
32
|
+
});
|
|
33
|
+
const [draft, setDraft] = React.useState(value ?? EMPTY_RANGE);
|
|
34
|
+
const [viewMonth, setViewMonth] = React.useState(() => value?.from ?? new Date());
|
|
35
|
+
// (Re)open seeds the draft and view from the committed value.
|
|
36
|
+
const handleOpenChange = (next) => {
|
|
37
|
+
if (next) {
|
|
38
|
+
setDraft(value ?? EMPTY_RANGE);
|
|
39
|
+
setViewMonth(startOfMonth(value?.from ?? new Date()));
|
|
40
|
+
}
|
|
41
|
+
setOpen(next);
|
|
42
|
+
};
|
|
43
|
+
const apply = () => {
|
|
44
|
+
if (!draft.from || !draft.to)
|
|
45
|
+
return;
|
|
46
|
+
setValue(draft);
|
|
47
|
+
setOpen(false);
|
|
48
|
+
};
|
|
49
|
+
const pickDay = (day) => {
|
|
50
|
+
setDraft((prev) => {
|
|
51
|
+
if (!prev.from || (prev.from && prev.to)) {
|
|
52
|
+
// Start a new range; keep the previous From time-of-day if any.
|
|
53
|
+
const from = new Date(day);
|
|
54
|
+
if (prev.from)
|
|
55
|
+
from.setHours(prev.from.getHours(), prev.from.getMinutes(), 0, 0);
|
|
56
|
+
else
|
|
57
|
+
from.setHours(0, 0, 0, 0);
|
|
58
|
+
return { from, to: null };
|
|
59
|
+
}
|
|
60
|
+
const to = new Date(day);
|
|
61
|
+
to.setHours(prev.to?.getHours() ?? 0, prev.to?.getMinutes() ?? 0, 0, 0);
|
|
62
|
+
if (isBeforeDay(to, prev.from)) {
|
|
63
|
+
return { from: to, to: prev.from };
|
|
64
|
+
}
|
|
65
|
+
return { from: prev.from, to };
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
const applyPreset = (preset) => {
|
|
69
|
+
const range = preset.getRange(new Date());
|
|
70
|
+
setDraft(range);
|
|
71
|
+
if (range.from)
|
|
72
|
+
setViewMonth(startOfMonth(range.from));
|
|
73
|
+
};
|
|
74
|
+
const format = (d) => withTime ? formatDayTime(d, locale) : formatDay(d, locale);
|
|
75
|
+
const formatted = value?.from && value?.to ? `${format(value.from)} - ${format(value.to)}` : null;
|
|
76
|
+
const fieldText = (d) => (d ? formatNumeric(d, locale, withTime) : '');
|
|
77
|
+
const rightMonth = addMonths(viewMonth, 1);
|
|
78
|
+
// Second calendar stays one month ahead; hide nav overlap by letting the
|
|
79
|
+
// left calendar own "previous" and the right own "next".
|
|
80
|
+
const showPresets = presets !== false && presets.length > 0;
|
|
81
|
+
return (_jsxs(Popover, { open: open, onOpenChange: handleOpenChange, children: [_jsx(PopoverTrigger, { asChild: true, children: _jsxs("button", { ref: ref, type: "button", id: id, disabled: disabled, className: cx('tui-input-group', 'tui-date-range-picker__trigger', disabled && 'is-disabled', className), "aria-haspopup": "dialog", "aria-expanded": open, "aria-label": ariaLabel ?? (formatted ? labels.trigger(formatted) : labels.triggerEmpty), children: [_jsx("span", { className: cx('tui-date-range-picker__trigger-value', !formatted && 'is-placeholder'), children: formatted ?? labels.placeholder }), _jsx("span", { className: "tui-input-group__suffix", children: _jsx(Icon, { name: "lms/calendar", size: "sm" }) })] }) }), _jsxs(PopoverContent, { modal: true, side: "bottom", align: "start", className: "tui-date-range-picker", "aria-label": labels.triggerEmpty, children: [_jsxs("div", { className: "tui-date-range-picker__body", children: [showPresets && (_jsx("div", { className: "tui-date-range-picker__presets", role: "group", "aria-label": labels.presets, children: presets.map((preset) => (_jsx(Button, { className: "tui-date-range-picker__preset", variant: "ghost", theme: "primary", size: "sm", label: preset.label, onClick: () => applyPreset(preset) }, preset.label))) })), _jsxs("div", { className: "tui-date-range-picker__main", children: [_jsxs("div", { className: "tui-date-range-picker__fields", children: [_jsx(RangeField, { label: labels.from, value: fieldText(draft.from), locale: locale, onCommit: (d) => {
|
|
82
|
+
setDraft((prev) => {
|
|
83
|
+
const next = { ...prev, from: d };
|
|
84
|
+
if (next.to && isBeforeDay(next.to, d))
|
|
85
|
+
next.to = null;
|
|
86
|
+
return next;
|
|
87
|
+
});
|
|
88
|
+
setViewMonth(startOfMonth(d));
|
|
89
|
+
} }), _jsx("span", { className: "tui-date-range-picker__fields-dash", "aria-hidden": "true", children: "-" }), _jsx(RangeField, { label: labels.to, value: fieldText(draft.to), locale: locale, onCommit: (d) => {
|
|
90
|
+
setDraft((prev) => prev.from && isBeforeDay(d, prev.from)
|
|
91
|
+
? { from: d, to: prev.from }
|
|
92
|
+
: { ...prev, to: d });
|
|
93
|
+
} })] }), _jsxs("div", { className: "tui-date-range-picker__calendars", children: [_jsx(Calendar, { className: "tui-date-range-picker__calendar", month: viewMonth, onMonthChange: (m) => {
|
|
94
|
+
// Keep the pair adjacent whichever side navigated.
|
|
95
|
+
setViewMonth(isSameMonth(m, rightMonth) ? addMonths(viewMonth, 1) : m);
|
|
96
|
+
}, onChange: pickDay, range: draft, min: min, max: max, isDayDisabled: isDayDisabled, weekStartsOn: weekStartsOn, locale: locale, labels: calendarLabels }), _jsx(Calendar, { className: "tui-date-range-picker__calendar", month: rightMonth, onMonthChange: (m) => {
|
|
97
|
+
setViewMonth(addMonths(m, -1));
|
|
98
|
+
}, onChange: pickDay, range: draft, min: min, max: max, isDayDisabled: isDayDisabled, weekStartsOn: weekStartsOn, locale: locale, labels: calendarLabels })] })] })] }), _jsxs("div", { className: "tui-date-range-picker__foot", children: [_jsx(Button, { className: "tui-date-range-picker__cancel", variant: "outline", theme: "primary", label: labels.cancel, onClick: () => setOpen(false) }), _jsx(Button, { className: "tui-date-range-picker__apply", theme: "primary", label: labels.apply, disabled: !draft.from || !draft.to, onClick: apply })] })] })] }));
|
|
99
|
+
});
|
|
100
|
+
DateRangePicker.displayName = 'DateRangePicker';
|
|
101
|
+
/** Editable From/To text field: shows the formatted value, parses on commit. */
|
|
102
|
+
function RangeField({ label, value, locale, onCommit, }) {
|
|
103
|
+
const [text, setText] = React.useState(null);
|
|
104
|
+
const commit = (raw) => {
|
|
105
|
+
setText(null);
|
|
106
|
+
const parsed = parseNumeric(raw, locale);
|
|
107
|
+
if (parsed)
|
|
108
|
+
onCommit(parsed);
|
|
109
|
+
};
|
|
110
|
+
return (_jsxs("label", { className: "tui-date-range-picker__field", children: [_jsx("span", { className: "tui-date-range-picker__field-label", children: label }), _jsx(TextInput, { value: text ?? value, onChange: (e) => setText(e.target.value), onBlur: (e) => commit(e.target.value), onKeyDown: (e) => {
|
|
111
|
+
if (e.key === 'Enter')
|
|
112
|
+
commit(e.currentTarget.value);
|
|
113
|
+
} })] }));
|
|
114
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export type DateRange = {
|
|
2
|
+
from: Date | null;
|
|
3
|
+
to: Date | null;
|
|
4
|
+
};
|
|
5
|
+
/** 0 = Sunday … 6 = Saturday, matching Date#getDay(). */
|
|
6
|
+
export type WeekStart = 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
7
|
+
export declare function isSameDay(a: Date | null | undefined, b: Date | null | undefined): boolean;
|
|
8
|
+
export declare function isSameMonth(a: Date, b: Date): boolean;
|
|
9
|
+
/** Strictly before, comparing calendar days (time-of-day ignored). */
|
|
10
|
+
export declare function isBeforeDay(a: Date, b: Date): boolean;
|
|
11
|
+
export declare function isAfterDay(a: Date, b: Date): boolean;
|
|
12
|
+
/** Day is within [from, to] inclusive, comparing calendar days. */
|
|
13
|
+
export declare function isWithinDays(day: Date, from: Date, to: Date): boolean;
|
|
14
|
+
/** New Date shifted by n days; preserves time-of-day. */
|
|
15
|
+
export declare function addDays(date: Date, n: number): Date;
|
|
16
|
+
/**
|
|
17
|
+
* New Date shifted by n months; clamps the day-of-month so Jan 31 + 1 month
|
|
18
|
+
* lands on Feb 28/29 rather than overflowing into March.
|
|
19
|
+
*/
|
|
20
|
+
export declare function addMonths(date: Date, n: number): Date;
|
|
21
|
+
export declare function addYears(date: Date, n: number): Date;
|
|
22
|
+
export declare function daysInMonth(date: Date): number;
|
|
23
|
+
export declare function startOfMonth(date: Date): Date;
|
|
24
|
+
export declare function startOfWeek(date: Date, weekStartsOn: WeekStart): Date;
|
|
25
|
+
/**
|
|
26
|
+
* The visible day grid for a month: always 6 rows of 7 (42 days), starting
|
|
27
|
+
* on the locale's week start, so the popover height never jumps between
|
|
28
|
+
* months. Time-of-day on the returned dates is noon — callers merge their
|
|
29
|
+
* own time when committing a selection.
|
|
30
|
+
*/
|
|
31
|
+
export declare function getMonthGrid(month: Date, weekStartsOn: WeekStart): Date[][];
|
|
32
|
+
/** New Date with the given calendar day but this date's time-of-day. */
|
|
33
|
+
export declare function withDay(time: Date | null | undefined, day: Date): Date;
|
|
34
|
+
export declare function clampDay(date: Date, min?: Date, max?: Date): Date;
|
|
35
|
+
export declare function getMonthNames(locale: string, width?: 'long' | 'short'): string[];
|
|
36
|
+
/** Weekday labels in grid order for the given week start. */
|
|
37
|
+
export declare function getWeekdayLabels(locale: string, weekStartsOn: WeekStart): {
|
|
38
|
+
narrow: string;
|
|
39
|
+
long: string;
|
|
40
|
+
}[];
|
|
41
|
+
export declare function formatDay(date: Date, locale: string): string;
|
|
42
|
+
export declare function formatDayTime(date: Date, locale: string): string;
|
|
43
|
+
/** Numeric form for text fields, e.g. "08/08/2024" or "08/08/2024, 00:00". */
|
|
44
|
+
export declare function formatNumeric(date: Date, locale: string, withTime: boolean): string;
|
|
45
|
+
/**
|
|
46
|
+
* Parse a numeric date string ("08/08/2024", "8.8.2024, 14:30") using the
|
|
47
|
+
* locale's part order. Returns null on anything that doesn't resolve to a
|
|
48
|
+
* real calendar day. Two-digit years are rejected rather than guessed.
|
|
49
|
+
*/
|
|
50
|
+
export declare function parseNumeric(text: string, locale: string): Date | null;
|
|
51
|
+
export declare function resolveLocale(locale?: string): string;
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// =============================================================================
|
|
2
|
+
// Date utilities — wall-clock, timezone-agnostic
|
|
3
|
+
// =============================================================================
|
|
4
|
+
//
|
|
5
|
+
// All math uses local (wall-clock) accessors on plain Date objects: the
|
|
6
|
+
// pickers edit "what the clock on the wall says", and the host owns
|
|
7
|
+
// interpretation (site timezone, serialisation). No UTC normalisation, no
|
|
8
|
+
// timezone conversion — ever. Day arithmetic goes through setDate/setMonth,
|
|
9
|
+
// which stays date-correct across DST transitions.
|
|
10
|
+
//
|
|
11
|
+
// Formatting and weekday/month names come from Intl.DateTimeFormat, so any
|
|
12
|
+
// locale tag works for display. Parsing accepts numeric dates in the
|
|
13
|
+
// locale's own part order (derived from formatToParts, not hardcoded).
|
|
14
|
+
//
|
|
15
|
+
// =============================================================================
|
|
16
|
+
export function isSameDay(a, b) {
|
|
17
|
+
if (!a || !b)
|
|
18
|
+
return false;
|
|
19
|
+
return (a.getFullYear() === b.getFullYear() &&
|
|
20
|
+
a.getMonth() === b.getMonth() &&
|
|
21
|
+
a.getDate() === b.getDate());
|
|
22
|
+
}
|
|
23
|
+
export function isSameMonth(a, b) {
|
|
24
|
+
return a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth();
|
|
25
|
+
}
|
|
26
|
+
/** Strictly before, comparing calendar days (time-of-day ignored). */
|
|
27
|
+
export function isBeforeDay(a, b) {
|
|
28
|
+
if (a.getFullYear() !== b.getFullYear())
|
|
29
|
+
return a.getFullYear() < b.getFullYear();
|
|
30
|
+
if (a.getMonth() !== b.getMonth())
|
|
31
|
+
return a.getMonth() < b.getMonth();
|
|
32
|
+
return a.getDate() < b.getDate();
|
|
33
|
+
}
|
|
34
|
+
export function isAfterDay(a, b) {
|
|
35
|
+
return isBeforeDay(b, a);
|
|
36
|
+
}
|
|
37
|
+
/** Day is within [from, to] inclusive, comparing calendar days. */
|
|
38
|
+
export function isWithinDays(day, from, to) {
|
|
39
|
+
return !isBeforeDay(day, from) && !isAfterDay(day, to);
|
|
40
|
+
}
|
|
41
|
+
/** New Date shifted by n days; preserves time-of-day. */
|
|
42
|
+
export function addDays(date, n) {
|
|
43
|
+
const next = new Date(date);
|
|
44
|
+
next.setDate(next.getDate() + n);
|
|
45
|
+
return next;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* New Date shifted by n months; clamps the day-of-month so Jan 31 + 1 month
|
|
49
|
+
* lands on Feb 28/29 rather than overflowing into March.
|
|
50
|
+
*/
|
|
51
|
+
export function addMonths(date, n) {
|
|
52
|
+
const next = new Date(date);
|
|
53
|
+
const day = next.getDate();
|
|
54
|
+
next.setDate(1);
|
|
55
|
+
next.setMonth(next.getMonth() + n);
|
|
56
|
+
next.setDate(Math.min(day, daysInMonth(next)));
|
|
57
|
+
return next;
|
|
58
|
+
}
|
|
59
|
+
export function addYears(date, n) {
|
|
60
|
+
return addMonths(date, n * 12);
|
|
61
|
+
}
|
|
62
|
+
export function daysInMonth(date) {
|
|
63
|
+
return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
|
|
64
|
+
}
|
|
65
|
+
export function startOfMonth(date) {
|
|
66
|
+
const next = new Date(date);
|
|
67
|
+
next.setDate(1);
|
|
68
|
+
return next;
|
|
69
|
+
}
|
|
70
|
+
export function startOfWeek(date, weekStartsOn) {
|
|
71
|
+
const diff = (date.getDay() - weekStartsOn + 7) % 7;
|
|
72
|
+
return addDays(date, -diff);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The visible day grid for a month: always 6 rows of 7 (42 days), starting
|
|
76
|
+
* on the locale's week start, so the popover height never jumps between
|
|
77
|
+
* months. Time-of-day on the returned dates is noon — callers merge their
|
|
78
|
+
* own time when committing a selection.
|
|
79
|
+
*/
|
|
80
|
+
export function getMonthGrid(month, weekStartsOn) {
|
|
81
|
+
const first = new Date(month.getFullYear(), month.getMonth(), 1, 12);
|
|
82
|
+
const gridStart = startOfWeek(first, weekStartsOn);
|
|
83
|
+
const rows = [];
|
|
84
|
+
for (let row = 0; row < 6; row++) {
|
|
85
|
+
const cells = [];
|
|
86
|
+
for (let col = 0; col < 7; col++) {
|
|
87
|
+
cells.push(addDays(gridStart, row * 7 + col));
|
|
88
|
+
}
|
|
89
|
+
rows.push(cells);
|
|
90
|
+
}
|
|
91
|
+
return rows;
|
|
92
|
+
}
|
|
93
|
+
/** New Date with the given calendar day but this date's time-of-day. */
|
|
94
|
+
export function withDay(time, day) {
|
|
95
|
+
const next = new Date(day);
|
|
96
|
+
if (time) {
|
|
97
|
+
next.setHours(time.getHours(), time.getMinutes(), 0, 0);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
next.setHours(0, 0, 0, 0);
|
|
101
|
+
}
|
|
102
|
+
return next;
|
|
103
|
+
}
|
|
104
|
+
export function clampDay(date, min, max) {
|
|
105
|
+
if (min && isBeforeDay(date, min))
|
|
106
|
+
return withDay(date, min);
|
|
107
|
+
if (max && isAfterDay(date, max))
|
|
108
|
+
return withDay(date, max);
|
|
109
|
+
return date;
|
|
110
|
+
}
|
|
111
|
+
// -----------------------------------------------------------------------------
|
|
112
|
+
// Locale-derived names (cached per locale)
|
|
113
|
+
// -----------------------------------------------------------------------------
|
|
114
|
+
const monthNameCache = new Map();
|
|
115
|
+
export function getMonthNames(locale, width = 'long') {
|
|
116
|
+
const key = `${locale}|${width}`;
|
|
117
|
+
let names = monthNameCache.get(key);
|
|
118
|
+
if (!names) {
|
|
119
|
+
const fmt = new Intl.DateTimeFormat(locale, { month: width });
|
|
120
|
+
names = Array.from({ length: 12 }, (_, m) => fmt.format(new Date(2024, m, 1, 12)));
|
|
121
|
+
monthNameCache.set(key, names);
|
|
122
|
+
}
|
|
123
|
+
return names;
|
|
124
|
+
}
|
|
125
|
+
const weekdayCache = new Map();
|
|
126
|
+
/** Weekday labels in grid order for the given week start. */
|
|
127
|
+
export function getWeekdayLabels(locale, weekStartsOn) {
|
|
128
|
+
const key = `${locale}|${weekStartsOn}`;
|
|
129
|
+
let labels = weekdayCache.get(key);
|
|
130
|
+
if (!labels) {
|
|
131
|
+
const narrow = new Intl.DateTimeFormat(locale, { weekday: 'narrow' });
|
|
132
|
+
const long = new Intl.DateTimeFormat(locale, { weekday: 'long' });
|
|
133
|
+
labels = Array.from({ length: 7 }, (_, i) => {
|
|
134
|
+
// 2024-09-01 is a Sunday; offset into the wanted weekday.
|
|
135
|
+
const day = new Date(2024, 8, 1 + ((weekStartsOn + i) % 7), 12);
|
|
136
|
+
return { narrow: narrow.format(day), long: long.format(day) };
|
|
137
|
+
});
|
|
138
|
+
weekdayCache.set(key, labels);
|
|
139
|
+
}
|
|
140
|
+
return labels;
|
|
141
|
+
}
|
|
142
|
+
// -----------------------------------------------------------------------------
|
|
143
|
+
// Formatting
|
|
144
|
+
// -----------------------------------------------------------------------------
|
|
145
|
+
export function formatDay(date, locale) {
|
|
146
|
+
return new Intl.DateTimeFormat(locale, { dateStyle: 'medium' }).format(date);
|
|
147
|
+
}
|
|
148
|
+
export function formatDayTime(date, locale) {
|
|
149
|
+
return new Intl.DateTimeFormat(locale, {
|
|
150
|
+
dateStyle: 'medium',
|
|
151
|
+
timeStyle: 'short',
|
|
152
|
+
hour12: false,
|
|
153
|
+
}).format(date);
|
|
154
|
+
}
|
|
155
|
+
/** Numeric form for text fields, e.g. "08/08/2024" or "08/08/2024, 00:00". */
|
|
156
|
+
export function formatNumeric(date, locale, withTime) {
|
|
157
|
+
const day = new Intl.DateTimeFormat(locale, {
|
|
158
|
+
year: 'numeric',
|
|
159
|
+
month: '2-digit',
|
|
160
|
+
day: '2-digit',
|
|
161
|
+
}).format(date);
|
|
162
|
+
if (!withTime)
|
|
163
|
+
return day;
|
|
164
|
+
const time = new Intl.DateTimeFormat(locale, {
|
|
165
|
+
hour: '2-digit',
|
|
166
|
+
minute: '2-digit',
|
|
167
|
+
hour12: false,
|
|
168
|
+
}).format(date);
|
|
169
|
+
return `${day}, ${time}`;
|
|
170
|
+
}
|
|
171
|
+
// -----------------------------------------------------------------------------
|
|
172
|
+
// Parsing — numeric dates in the locale's own part order
|
|
173
|
+
// -----------------------------------------------------------------------------
|
|
174
|
+
const partOrderCache = new Map();
|
|
175
|
+
function getPartOrder(locale) {
|
|
176
|
+
let order = partOrderCache.get(locale);
|
|
177
|
+
if (!order) {
|
|
178
|
+
const parts = new Intl.DateTimeFormat(locale, {
|
|
179
|
+
year: 'numeric',
|
|
180
|
+
month: '2-digit',
|
|
181
|
+
day: '2-digit',
|
|
182
|
+
}).formatToParts(new Date(2024, 0, 15, 12));
|
|
183
|
+
order = parts
|
|
184
|
+
.filter((p) => ['year', 'month', 'day'].includes(p.type))
|
|
185
|
+
.map((p) => p.type);
|
|
186
|
+
partOrderCache.set(locale, order);
|
|
187
|
+
}
|
|
188
|
+
return order;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Parse a numeric date string ("08/08/2024", "8.8.2024, 14:30") using the
|
|
192
|
+
* locale's part order. Returns null on anything that doesn't resolve to a
|
|
193
|
+
* real calendar day. Two-digit years are rejected rather than guessed.
|
|
194
|
+
*/
|
|
195
|
+
export function parseNumeric(text, locale) {
|
|
196
|
+
const groups = text.match(/\d+/g);
|
|
197
|
+
if (!groups || groups.length < 3 || groups.length > 5)
|
|
198
|
+
return null;
|
|
199
|
+
const order = getPartOrder(locale);
|
|
200
|
+
const dateParts = { year: 0, month: 0, day: 0 };
|
|
201
|
+
order.forEach((type, i) => {
|
|
202
|
+
dateParts[type] = Number(groups[i]);
|
|
203
|
+
});
|
|
204
|
+
const { year, month, day } = dateParts;
|
|
205
|
+
if (String(year).length < 4 || month < 1 || month > 12 || day < 1 || day > 31)
|
|
206
|
+
return null;
|
|
207
|
+
const hour = groups.length >= 4 ? Number(groups[3]) : 0;
|
|
208
|
+
const minute = groups.length >= 5 ? Number(groups[4]) : 0;
|
|
209
|
+
if (hour > 23 || minute > 59)
|
|
210
|
+
return null;
|
|
211
|
+
const parsed = new Date(year, month - 1, day, hour, minute, 0, 0);
|
|
212
|
+
// Reject overflowed days (e.g. Feb 31 rolling into March).
|
|
213
|
+
if (parsed.getMonth() !== month - 1 || parsed.getDate() !== day)
|
|
214
|
+
return null;
|
|
215
|
+
return parsed;
|
|
216
|
+
}
|
|
217
|
+
// -----------------------------------------------------------------------------
|
|
218
|
+
// Locale resolution + shared preset config (lives here so component files
|
|
219
|
+
// export only components — react-refresh constraint).
|
|
220
|
+
// -----------------------------------------------------------------------------
|
|
221
|
+
export function resolveLocale(locale) {
|
|
222
|
+
if (locale)
|
|
223
|
+
return locale;
|
|
224
|
+
if (typeof document !== 'undefined' && document.documentElement.lang) {
|
|
225
|
+
return document.documentElement.lang;
|
|
226
|
+
}
|
|
227
|
+
return 'en-US';
|
|
228
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { Calendar } from './Calendar';
|
|
2
|
+
export { DatePicker } from './DatePicker';
|
|
3
|
+
export { DateRangePicker } from './DateRangePicker';
|
|
4
|
+
export { defaultDateRangePresets } from './presets';
|
|
5
|
+
export { addDays, addMonths, addYears, isSameDay, withDay, } from './date-utils';
|
|
6
|
+
export type { DateRange, WeekStart } from './date-utils';
|
|
7
|
+
export type { CalendarProps, CalendarLabels, DatePickerProps, DatePickerLabels, DateRangePickerProps, DateRangePickerLabels, DateRangePreset, } from './types';
|
|
8
|
+
export { defaultCalendarLabels, defaultDatePickerLabels, defaultDateRangePickerLabels, } from './types';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { Calendar } from './Calendar.js';
|
|
2
|
+
export { DatePicker } from './DatePicker.js';
|
|
3
|
+
export { DateRangePicker } from './DateRangePicker.js';
|
|
4
|
+
export { defaultDateRangePresets } from './presets.js';
|
|
5
|
+
export { addDays, addMonths, addYears, isSameDay, withDay, } from './date-utils.js';
|
|
6
|
+
export { defaultCalendarLabels, defaultDatePickerLabels, defaultDateRangePickerLabels, } from './types.js';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { addDays, addMonths, startOfMonth, startOfWeek } from './date-utils.js';
|
|
2
|
+
/** The standard quick-range rail: Today … Last month. */
|
|
3
|
+
export const defaultDateRangePresets = [
|
|
4
|
+
{
|
|
5
|
+
label: 'Today',
|
|
6
|
+
getRange: (now) => ({ from: now, to: now }),
|
|
7
|
+
},
|
|
8
|
+
{
|
|
9
|
+
label: 'Yesterday',
|
|
10
|
+
getRange: (now) => ({ from: addDays(now, -1), to: addDays(now, -1) }),
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
label: 'Last week',
|
|
14
|
+
getRange: (now) => {
|
|
15
|
+
const thisWeek = startOfWeek(now, 1);
|
|
16
|
+
return { from: addDays(thisWeek, -7), to: addDays(thisWeek, -1) };
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
label: 'Last 2 weeks',
|
|
21
|
+
getRange: (now) => {
|
|
22
|
+
const thisWeek = startOfWeek(now, 1);
|
|
23
|
+
return { from: addDays(thisWeek, -14), to: addDays(thisWeek, -1) };
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
label: 'This month',
|
|
28
|
+
getRange: (now) => ({ from: startOfMonth(now), to: now }),
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
label: 'Last month',
|
|
32
|
+
getRange: (now) => {
|
|
33
|
+
const first = startOfMonth(addMonths(now, -1));
|
|
34
|
+
return { from: first, to: addDays(startOfMonth(now), -1) };
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
];
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { DateRange, WeekStart } from './date-utils';
|
|
3
|
+
export type { DateRange, WeekStart };
|
|
4
|
+
/**
|
|
5
|
+
* Props shared by everything that renders a day grid. All values are plain
|
|
6
|
+
* Date objects with wall-clock semantics — the components never do timezone
|
|
7
|
+
* math; the host owns interpretation.
|
|
8
|
+
*/
|
|
9
|
+
export type CalendarBaseProps = {
|
|
10
|
+
/** Earliest selectable day (inclusive, time-of-day ignored). */
|
|
11
|
+
min?: Date;
|
|
12
|
+
/** Latest selectable day (inclusive, time-of-day ignored). */
|
|
13
|
+
max?: Date;
|
|
14
|
+
/** Per-day disable hook, applied on top of min/max. */
|
|
15
|
+
isDayDisabled?: (day: Date) => boolean;
|
|
16
|
+
/** First day of the week. 0 = Sunday … 6 = Saturday. @default 1 (Monday) */
|
|
17
|
+
weekStartsOn?: WeekStart;
|
|
18
|
+
/**
|
|
19
|
+
* BCP 47 locale for month/weekday names and numeric formats.
|
|
20
|
+
* @default the document language, falling back to 'en-US'
|
|
21
|
+
*/
|
|
22
|
+
locale?: string;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Overridable strings for Calendar i18n. Month and weekday *names* come from
|
|
26
|
+
* Intl via `locale`; these cover the chrome.
|
|
27
|
+
*/
|
|
28
|
+
export type CalendarLabels = {
|
|
29
|
+
prevMonth?: string;
|
|
30
|
+
nextMonth?: string;
|
|
31
|
+
prevYear?: string;
|
|
32
|
+
nextYear?: string;
|
|
33
|
+
/** Accessible label for the caption button that opens the month picker. */
|
|
34
|
+
openMonthPicker?: (caption: string) => string;
|
|
35
|
+
/** Accessible label for a day cell. */
|
|
36
|
+
day?: (formatted: string) => string;
|
|
37
|
+
today?: string;
|
|
38
|
+
selected?: string;
|
|
39
|
+
};
|
|
40
|
+
export declare const defaultCalendarLabels: Required<CalendarLabels>;
|
|
41
|
+
export type CalendarProps = CalendarBaseProps & {
|
|
42
|
+
/** Selected day. */
|
|
43
|
+
value?: Date | null;
|
|
44
|
+
/**
|
|
45
|
+
* Day clicked/chosen. Receives the calendar day at noon; callers merge
|
|
46
|
+
* their own time-of-day (see `withDay`).
|
|
47
|
+
*/
|
|
48
|
+
onChange?: (day: Date) => void;
|
|
49
|
+
/** Controlled visible month (any day within it). */
|
|
50
|
+
month?: Date;
|
|
51
|
+
/** Initial visible month for uncontrolled usage. */
|
|
52
|
+
defaultMonth?: Date;
|
|
53
|
+
onMonthChange?: (month: Date) => void;
|
|
54
|
+
/**
|
|
55
|
+
* Range to paint (endpoints + in-between fill). Painting only — range
|
|
56
|
+
* *selection* logic lives in DateRangePicker.
|
|
57
|
+
*/
|
|
58
|
+
range?: DateRange | null;
|
|
59
|
+
/** Hide the caption/nav bar (for multi-month layouts that render their own). */
|
|
60
|
+
labels?: CalendarLabels;
|
|
61
|
+
'aria-label'?: string;
|
|
62
|
+
className?: string;
|
|
63
|
+
};
|
|
64
|
+
export type DatePickerLabels = {
|
|
65
|
+
/** Popover heading. */
|
|
66
|
+
title?: string;
|
|
67
|
+
titleWithTime?: string;
|
|
68
|
+
now?: string;
|
|
69
|
+
close?: string;
|
|
70
|
+
/** Placeholder on the trigger when empty. */
|
|
71
|
+
placeholder?: string;
|
|
72
|
+
timeSection?: string;
|
|
73
|
+
dateSection?: string;
|
|
74
|
+
month?: string;
|
|
75
|
+
dayOfMonth?: string;
|
|
76
|
+
year?: string;
|
|
77
|
+
hour?: string;
|
|
78
|
+
minute?: string;
|
|
79
|
+
am?: string;
|
|
80
|
+
pm?: string;
|
|
81
|
+
meridiem?: string;
|
|
82
|
+
/** Trigger aria-label when a value is set: `label(formattedValue)`. */
|
|
83
|
+
trigger?: (formatted: string) => string;
|
|
84
|
+
triggerEmpty?: string;
|
|
85
|
+
};
|
|
86
|
+
export declare const defaultDatePickerLabels: Required<DatePickerLabels>;
|
|
87
|
+
export type DatePickerProps = CalendarBaseProps & {
|
|
88
|
+
value?: Date | null;
|
|
89
|
+
defaultValue?: Date | null;
|
|
90
|
+
onChange?: (value: Date) => void;
|
|
91
|
+
/** Include the time field (and format the trigger with time). */
|
|
92
|
+
withTime?: boolean;
|
|
93
|
+
/** 12-hour time UI with an AM/PM toggle. @default true */
|
|
94
|
+
hour12?: boolean;
|
|
95
|
+
/** Display-only zone badge next to the time field, e.g. "UTC+0". */
|
|
96
|
+
zoneLabel?: ReactNode;
|
|
97
|
+
disabled?: boolean;
|
|
98
|
+
labels?: DatePickerLabels;
|
|
99
|
+
calendarLabels?: CalendarLabels;
|
|
100
|
+
'aria-label'?: string;
|
|
101
|
+
className?: string;
|
|
102
|
+
id?: string;
|
|
103
|
+
};
|
|
104
|
+
export type DateRangePreset = {
|
|
105
|
+
label: string;
|
|
106
|
+
/** Resolve the preset against "now" so stories/tests can pin the clock. */
|
|
107
|
+
getRange: (now: Date) => DateRange;
|
|
108
|
+
};
|
|
109
|
+
export type DateRangePickerLabels = {
|
|
110
|
+
from?: string;
|
|
111
|
+
to?: string;
|
|
112
|
+
apply?: string;
|
|
113
|
+
cancel?: string;
|
|
114
|
+
placeholder?: string;
|
|
115
|
+
presets?: string;
|
|
116
|
+
trigger?: (formatted: string) => string;
|
|
117
|
+
triggerEmpty?: string;
|
|
118
|
+
};
|
|
119
|
+
export declare const defaultDateRangePickerLabels: Required<DateRangePickerLabels>;
|
|
120
|
+
export type DateRangePickerProps = CalendarBaseProps & {
|
|
121
|
+
value?: DateRange | null;
|
|
122
|
+
defaultValue?: DateRange | null;
|
|
123
|
+
/** Fires on Apply with a complete range. */
|
|
124
|
+
onChange?: (range: DateRange) => void;
|
|
125
|
+
/** Show times in the trigger and From/To fields. */
|
|
126
|
+
withTime?: boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Quick-range presets for the side rail; `false` hides the rail.
|
|
129
|
+
* @default the standard six (Today … Last month)
|
|
130
|
+
*/
|
|
131
|
+
presets?: DateRangePreset[] | false;
|
|
132
|
+
disabled?: boolean;
|
|
133
|
+
labels?: DateRangePickerLabels;
|
|
134
|
+
calendarLabels?: CalendarLabels;
|
|
135
|
+
'aria-label'?: string;
|
|
136
|
+
className?: string;
|
|
137
|
+
id?: string;
|
|
138
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export const defaultCalendarLabels = {
|
|
2
|
+
prevMonth: 'Previous month',
|
|
3
|
+
nextMonth: 'Next month',
|
|
4
|
+
prevYear: 'Previous year',
|
|
5
|
+
nextYear: 'Next year',
|
|
6
|
+
openMonthPicker: (caption) => `${caption} — choose month and year`,
|
|
7
|
+
day: (formatted) => formatted,
|
|
8
|
+
today: 'today',
|
|
9
|
+
selected: 'selected',
|
|
10
|
+
};
|
|
11
|
+
export const defaultDatePickerLabels = {
|
|
12
|
+
title: 'Pick a Date',
|
|
13
|
+
titleWithTime: 'Pick a Date and Time',
|
|
14
|
+
now: 'Now',
|
|
15
|
+
close: 'Close',
|
|
16
|
+
placeholder: 'Select date',
|
|
17
|
+
timeSection: 'Time',
|
|
18
|
+
dateSection: 'Date',
|
|
19
|
+
month: 'Month',
|
|
20
|
+
dayOfMonth: 'Day',
|
|
21
|
+
year: 'Year',
|
|
22
|
+
hour: 'Hour',
|
|
23
|
+
minute: 'Minute',
|
|
24
|
+
am: 'AM',
|
|
25
|
+
pm: 'PM',
|
|
26
|
+
meridiem: 'AM or PM',
|
|
27
|
+
trigger: (formatted) => `Change date, ${formatted}`,
|
|
28
|
+
triggerEmpty: 'Select date',
|
|
29
|
+
};
|
|
30
|
+
export const defaultDateRangePickerLabels = {
|
|
31
|
+
from: 'From',
|
|
32
|
+
to: 'To',
|
|
33
|
+
apply: 'Apply',
|
|
34
|
+
cancel: 'Cancel',
|
|
35
|
+
placeholder: 'Select date range',
|
|
36
|
+
presets: 'Quick ranges',
|
|
37
|
+
trigger: (formatted) => `Change date range, ${formatted}`,
|
|
38
|
+
triggerEmpty: 'Select date range',
|
|
39
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { IconName } from '../../icons/registry';
|
|
2
|
-
import type { Size, ThemeIntent } from '../../types';
|
|
2
|
+
import type { Size, ThemeIntent, ThemeStatus } from '../../types';
|
|
3
3
|
export type { Size };
|
|
4
|
-
export type Theme = ThemeIntent;
|
|
4
|
+
export type Theme = ThemeIntent | ThemeStatus;
|
|
5
5
|
export type Variant = 'solid' | 'outline' | 'ghost';
|
|
6
6
|
export type Shape = 'square' | 'circle';
|
|
7
7
|
type IconButtonBaseProps = {
|