@jcsoftdev/ui 0.3.0 → 0.4.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/CHANGELOG.md +103 -1
- package/README.md +55 -2
- package/bin/ui-theme.mjs +13 -2
- package/dist/atoms/checkbox/checkbox.js +3 -1
- package/dist/atoms/chip/chip.d.ts +11 -3
- package/dist/atoms/chip/chip.js +8 -1
- package/dist/atoms/chip/index.d.ts +1 -1
- package/dist/atoms/chip/index.js +2 -0
- package/dist/atoms/due-chip/due-chip.d.ts +31 -0
- package/dist/atoms/due-chip/due-chip.js +41 -0
- package/dist/atoms/due-chip/index.d.ts +2 -0
- package/dist/atoms/due-chip/index.js +6 -0
- package/dist/atoms/due-chip/model.d.ts +25 -0
- package/dist/atoms/due-chip/model.js +44 -0
- package/dist/atoms/icon-button/icon-button.d.ts +22 -0
- package/dist/atoms/icon-button/icon-button.js +38 -0
- package/dist/atoms/icon-button/index.d.ts +1 -0
- package/dist/atoms/icon-button/index.js +8 -0
- package/dist/atoms/index.d.ts +5 -0
- package/dist/atoms/index.js +5 -0
- package/dist/atoms/internal/hit-area.d.ts +6 -0
- package/dist/atoms/internal/hit-area.js +4 -0
- package/dist/atoms/kbd/index.d.ts +1 -0
- package/dist/atoms/kbd/index.js +4 -0
- package/dist/atoms/kbd/kbd.d.ts +7 -0
- package/dist/atoms/kbd/kbd.js +17 -0
- package/dist/atoms/radio-group/radio-group.js +6 -2
- package/dist/atoms/seal/index.d.ts +1 -0
- package/dist/atoms/seal/index.js +4 -0
- package/dist/atoms/seal/seal.d.ts +30 -0
- package/dist/atoms/seal/seal.js +71 -0
- package/dist/atoms/spinner/index.d.ts +1 -0
- package/dist/atoms/spinner/index.js +5 -0
- package/dist/atoms/spinner/spinner.d.ts +19 -0
- package/dist/atoms/spinner/spinner.js +31 -0
- package/dist/atoms/switch/switch.js +3 -1
- package/dist/calendar/date-calendar/date-calendar.d.ts +32 -0
- package/dist/calendar/date-calendar/date-calendar.js +189 -0
- package/dist/calendar/date-calendar/index.d.ts +1 -0
- package/dist/calendar/date-calendar/index.js +6 -0
- package/dist/calendar/index.d.ts +1 -0
- package/dist/calendar/index.js +1 -0
- package/dist/calendar/internal/date-utils.d.ts +26 -0
- package/dist/calendar/internal/date-utils.js +95 -1
- package/dist/containers/aside/aside.d.ts +14 -0
- package/dist/containers/aside/aside.js +25 -0
- package/dist/containers/aside/index.d.ts +1 -0
- package/dist/containers/aside/index.js +5 -0
- package/dist/containers/banner/banner.d.ts +4 -1
- package/dist/containers/banner/banner.js +9 -1
- package/dist/containers/description-list/description-list.d.ts +29 -6
- package/dist/containers/description-list/description-list.js +47 -6
- package/dist/containers/feed/feed.d.ts +23 -0
- package/dist/containers/feed/feed.js +77 -0
- package/dist/containers/feed/index.d.ts +2 -0
- package/dist/containers/feed/index.js +10 -0
- package/dist/containers/feed/model.d.ts +19 -0
- package/dist/containers/feed/model.js +38 -0
- package/dist/containers/index.d.ts +6 -0
- package/dist/containers/index.js +6 -0
- package/dist/containers/kpi/kpi.d.ts +50 -8
- package/dist/containers/kpi/kpi.js +91 -39
- package/dist/containers/list-detail/index.d.ts +1 -0
- package/dist/containers/list-detail/index.js +20 -0
- package/dist/containers/list-detail/list-detail.d.ts +55 -0
- package/dist/containers/list-detail/list-detail.js +221 -0
- package/dist/containers/note/index.d.ts +1 -0
- package/dist/containers/note/index.js +4 -0
- package/dist/containers/note/note.d.ts +13 -0
- package/dist/containers/note/note.js +27 -0
- package/dist/containers/section/index.d.ts +1 -0
- package/dist/containers/section/index.js +6 -0
- package/dist/containers/section/section.d.ts +68 -0
- package/dist/containers/section/section.js +198 -0
- package/dist/containers/shortcuts/index.d.ts +1 -0
- package/dist/containers/shortcuts/index.js +8 -0
- package/dist/containers/shortcuts/shortcuts.d.ts +18 -0
- package/dist/containers/shortcuts/shortcuts.js +50 -0
- package/dist/containers/table/selection.d.ts +9 -0
- package/dist/containers/table/selection.js +20 -0
- package/dist/containers/table/table.d.ts +66 -4
- package/dist/containers/table/table.js +128 -7
- package/dist/containers/timeline/index.d.ts +1 -1
- package/dist/containers/timeline/index.js +4 -2
- package/dist/containers/timeline/timeline.d.ts +32 -5
- package/dist/containers/timeline/timeline.js +154 -90
- package/dist/forms/chip-group/chip-group.d.ts +54 -0
- package/dist/forms/chip-group/chip-group.js +114 -0
- package/dist/forms/chip-group/index.d.ts +1 -0
- package/dist/forms/chip-group/index.js +6 -0
- package/dist/forms/combobox/combobox.d.ts +60 -0
- package/dist/forms/combobox/combobox.js +281 -0
- package/dist/forms/combobox/index.d.ts +1 -0
- package/dist/forms/combobox/index.js +6 -0
- package/dist/forms/date-picker/date-picker.d.ts +40 -0
- package/dist/forms/date-picker/date-picker.js +183 -0
- package/dist/forms/date-picker/date-time-picker.d.ts +43 -0
- package/dist/forms/date-picker/date-time-picker.js +121 -0
- package/dist/forms/date-picker/index.d.ts +2 -0
- package/dist/forms/date-picker/index.js +10 -0
- package/dist/forms/date-picker/model.d.ts +35 -0
- package/dist/forms/date-picker/model.js +83 -0
- package/dist/forms/dropzone/dropzone.d.ts +30 -2
- package/dist/forms/dropzone/dropzone.js +153 -23
- package/dist/forms/field/field.d.ts +16 -10
- package/dist/forms/field/field.js +41 -5
- package/dist/forms/index.d.ts +4 -0
- package/dist/forms/index.js +4 -0
- package/dist/forms/input/index.d.ts +1 -1
- package/dist/forms/input/index.js +4 -1
- package/dist/forms/input/input.d.ts +21 -5
- package/dist/forms/input/input.js +74 -9
- package/dist/forms/input-icon/input-icon.d.ts +1 -1
- package/dist/forms/internal/listbox-items.d.ts +18 -0
- package/dist/forms/internal/listbox-items.js +88 -0
- package/dist/forms/internal/listbox.d.ts +44 -0
- package/dist/forms/internal/listbox.js +74 -0
- package/dist/forms/number-stepper/model.d.ts +16 -0
- package/dist/forms/number-stepper/model.js +23 -0
- package/dist/forms/number-stepper/number-stepper.d.ts +36 -6
- package/dist/forms/number-stepper/number-stepper.js +86 -37
- package/dist/forms/select-menu/index.d.ts +1 -0
- package/dist/forms/select-menu/index.js +6 -0
- package/dist/forms/select-menu/select-menu.d.ts +35 -0
- package/dist/forms/select-menu/select-menu.js +230 -0
- package/dist/i18n/strings.d.ts +89 -0
- package/dist/i18n/strings.js +54 -2
- package/dist/i18n/ui-strings-provider.d.ts +1 -1
- package/dist/i18n/ui-strings-provider.js +6 -2
- package/dist/navigation/index.d.ts +1 -0
- package/dist/navigation/index.js +1 -0
- package/dist/navigation/stepper/index.d.ts +1 -1
- package/dist/navigation/stepper/index.js +3 -1
- package/dist/navigation/stepper/stepper.d.ts +8 -0
- package/dist/navigation/stepper/stepper.js +68 -45
- package/dist/navigation/tabs/index.d.ts +1 -0
- package/dist/navigation/tabs/index.js +6 -0
- package/dist/navigation/tabs/tabs-nav.d.ts +37 -0
- package/dist/navigation/tabs/tabs-nav.js +57 -0
- package/dist/navigation/wizard/index.d.ts +1 -0
- package/dist/navigation/wizard/index.js +6 -0
- package/dist/navigation/wizard/model.d.ts +33 -0
- package/dist/navigation/wizard/model.js +34 -0
- package/dist/navigation/wizard/wizard.d.ts +45 -0
- package/dist/navigation/wizard/wizard.js +169 -0
- package/dist/overlays/alert-dialog/alert-dialog.d.ts +103 -0
- package/dist/overlays/alert-dialog/alert-dialog.js +342 -0
- package/dist/overlays/alert-dialog/index.d.ts +1 -0
- package/dist/overlays/alert-dialog/index.js +26 -0
- package/dist/overlays/dropdown-menu/dropdown-menu.d.ts +17 -1
- package/dist/overlays/dropdown-menu/dropdown-menu.js +12 -4
- package/dist/overlays/dropdown-menu/index.d.ts +1 -1
- package/dist/overlays/dropdown-menu/index.js +8 -2
- package/dist/overlays/index.d.ts +2 -0
- package/dist/overlays/index.js +2 -0
- package/dist/overlays/testing/index.d.ts +5 -0
- package/dist/overlays/testing/index.js +4 -0
- package/dist/overlays/toaster/index.d.ts +2 -1
- package/dist/overlays/toaster/index.js +4 -2
- package/dist/overlays/toaster/toast-store.d.ts +6 -2
- package/dist/overlays/toaster/toast.d.ts +3 -0
- package/dist/overlays/toaster/toast.js +3 -0
- package/dist/overlays/toaster/toaster.d.ts +3 -0
- package/dist/overlays/toaster/toaster.js +36 -3
- package/dist/overlays/tooltip/index.d.ts +1 -0
- package/dist/overlays/tooltip/index.js +4 -0
- package/dist/overlays/tooltip/model.d.ts +10 -0
- package/dist/overlays/tooltip/model.js +13 -0
- package/dist/overlays/tooltip/tooltip.d.ts +41 -0
- package/dist/overlays/tooltip/tooltip.js +116 -0
- package/dist/primitives/use-dismissable-layer.js +1 -1
- package/dist/states/error-state/error-state.d.ts +6 -1
- package/dist/states/error-state/error-state.js +13 -9
- package/dist/states/skeleton/skeleton.d.ts +8 -2
- package/dist/states/skeleton/skeleton.js +44 -2
- package/package.json +2 -2
- package/scripts/build-theme.mjs +12 -1
- package/src/atoms/checkbox/checkbox.tsx +3 -1
- package/src/atoms/chip/chip.tsx +18 -3
- package/src/atoms/chip/index.ts +2 -0
- package/src/atoms/due-chip/due-chip.tsx +75 -0
- package/src/atoms/due-chip/index.ts +2 -0
- package/src/atoms/due-chip/model.ts +67 -0
- package/src/atoms/icon-button/icon-button.tsx +58 -0
- package/src/atoms/icon-button/index.ts +6 -0
- package/src/atoms/index.ts +5 -0
- package/src/atoms/internal/hit-area.ts +7 -0
- package/src/atoms/kbd/index.ts +1 -0
- package/src/atoms/kbd/kbd.tsx +20 -0
- package/src/atoms/radio-group/radio-group.tsx +8 -2
- package/src/atoms/seal/index.ts +1 -0
- package/src/atoms/seal/seal.tsx +107 -0
- package/src/atoms/spinner/index.ts +1 -0
- package/src/atoms/spinner/spinner.tsx +49 -0
- package/src/atoms/switch/switch.tsx +3 -1
- package/src/calendar/date-calendar/date-calendar.tsx +230 -0
- package/src/calendar/date-calendar/index.ts +5 -0
- package/src/calendar/index.ts +1 -0
- package/src/calendar/internal/date-utils.ts +115 -0
- package/src/containers/aside/aside.tsx +40 -0
- package/src/containers/aside/index.ts +1 -0
- package/src/containers/banner/banner.tsx +18 -1
- package/src/containers/description-list/description-list.tsx +85 -9
- package/src/containers/feed/feed.tsx +99 -0
- package/src/containers/feed/index.ts +8 -0
- package/src/containers/feed/model.ts +54 -0
- package/src/containers/index.ts +6 -0
- package/src/containers/kpi/kpi.tsx +161 -62
- package/src/containers/list-detail/index.ts +15 -0
- package/src/containers/list-detail/list-detail.tsx +293 -0
- package/src/containers/note/index.ts +1 -0
- package/src/containers/note/note.tsx +38 -0
- package/src/containers/section/index.ts +11 -0
- package/src/containers/section/section.tsx +281 -0
- package/src/containers/shortcuts/index.ts +6 -0
- package/src/containers/shortcuts/shortcuts.tsx +92 -0
- package/src/containers/table/selection.ts +26 -0
- package/src/containers/table/table.tsx +215 -13
- package/src/containers/timeline/index.ts +3 -0
- package/src/containers/timeline/timeline.tsx +204 -93
- package/src/forms/chip-group/chip-group.tsx +184 -0
- package/src/forms/chip-group/index.ts +8 -0
- package/src/forms/combobox/combobox.tsx +359 -0
- package/src/forms/combobox/index.ts +7 -0
- package/src/forms/date-picker/date-picker.tsx +239 -0
- package/src/forms/date-picker/date-time-picker.tsx +178 -0
- package/src/forms/date-picker/index.ts +10 -0
- package/src/forms/date-picker/model.ts +108 -0
- package/src/forms/dropzone/dropzone.tsx +217 -35
- package/src/forms/field/field.tsx +72 -7
- package/src/forms/index.ts +4 -0
- package/src/forms/input/index.ts +8 -1
- package/src/forms/input/input.tsx +97 -12
- package/src/forms/input-icon/input-icon.tsx +1 -1
- package/src/forms/internal/listbox-items.tsx +103 -0
- package/src/forms/internal/listbox.ts +124 -0
- package/src/forms/number-stepper/model.ts +35 -0
- package/src/forms/number-stepper/number-stepper.tsx +132 -42
- package/src/forms/select-menu/index.ts +7 -0
- package/src/forms/select-menu/select-menu.tsx +280 -0
- package/src/i18n/strings.ts +89 -0
- package/src/i18n/ui-strings-provider.tsx +15 -4
- package/src/navigation/index.ts +1 -0
- package/src/navigation/stepper/index.ts +7 -1
- package/src/navigation/stepper/stepper.tsx +41 -5
- package/src/navigation/tabs/index.ts +7 -0
- package/src/navigation/tabs/tabs-nav.tsx +131 -0
- package/src/navigation/wizard/index.ts +7 -0
- package/src/navigation/wizard/model.ts +61 -0
- package/src/navigation/wizard/wizard.tsx +222 -0
- package/src/overlays/alert-dialog/alert-dialog.tsx +486 -0
- package/src/overlays/alert-dialog/index.ts +19 -0
- package/src/overlays/dropdown-menu/dropdown-menu.tsx +29 -4
- package/src/overlays/dropdown-menu/index.ts +3 -0
- package/src/overlays/index.ts +2 -0
- package/src/overlays/testing/index.ts +5 -0
- package/src/overlays/toaster/index.ts +2 -0
- package/src/overlays/toaster/toast-store.ts +6 -2
- package/src/overlays/toaster/toast.ts +6 -0
- package/src/overlays/toaster/toaster.tsx +44 -2
- package/src/overlays/tooltip/index.ts +1 -0
- package/src/overlays/tooltip/model.ts +18 -0
- package/src/overlays/tooltip/tooltip.tsx +166 -0
- package/src/primitives/use-dismissable-layer.ts +7 -2
- package/src/states/error-state/error-state.tsx +19 -8
- package/src/states/skeleton/skeleton.tsx +68 -2
- package/styles/tailwind.css +33 -30
- package/styles/theme.css +33 -30
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { type ComponentProps, type KeyboardEvent, useMemo, useRef, useState } from "react";
|
|
4
|
+
import { DateCalendar } from "../../calendar/date-calendar/date-calendar.js";
|
|
5
|
+
import { isIsoDate, type WeekStart } from "../../calendar/internal/date-utils.js";
|
|
6
|
+
import { useUiStrings } from "../../i18n/index.js";
|
|
7
|
+
import { Calendar as CalendarIcon, X } from "../../icons/index.js";
|
|
8
|
+
import * as PopoverPrimitive from "../../primitives/popover/popover.js";
|
|
9
|
+
import type { ClassNames } from "../../utils/class-names.js";
|
|
10
|
+
import { cn } from "../../utils/cn.js";
|
|
11
|
+
import { useComposedRefs } from "../../utils/compose.js";
|
|
12
|
+
import { type Material, materialClass } from "../../utils/material.js";
|
|
13
|
+
import { useControllableState } from "../../utils/use-controllable-state.js";
|
|
14
|
+
import {
|
|
15
|
+
dateFormatFor,
|
|
16
|
+
displayPlaceholder,
|
|
17
|
+
formatDisplay,
|
|
18
|
+
maskDisplay,
|
|
19
|
+
parseDisplay,
|
|
20
|
+
} from "./model.js";
|
|
21
|
+
|
|
22
|
+
export type DatePickerPart = "root" | "input" | "clear" | "trigger" | "content" | "calendar";
|
|
23
|
+
|
|
24
|
+
export interface DatePickerProps
|
|
25
|
+
extends Omit<
|
|
26
|
+
ComponentProps<"input">,
|
|
27
|
+
| "value"
|
|
28
|
+
| "defaultValue"
|
|
29
|
+
| "onChange"
|
|
30
|
+
| "type"
|
|
31
|
+
| "min"
|
|
32
|
+
| "max"
|
|
33
|
+
| "size"
|
|
34
|
+
| "inputMode"
|
|
35
|
+
| "maxLength"
|
|
36
|
+
> {
|
|
37
|
+
/** Selected date as "YYYY-MM-DD" (controlled); `""` means none. Never converted through a time zone. */
|
|
38
|
+
value?: string;
|
|
39
|
+
defaultValue?: string;
|
|
40
|
+
/** Called with a valid "YYYY-MM-DD" date, or `""` when the field is cleared. */
|
|
41
|
+
onValueChange?: (value: string) => void;
|
|
42
|
+
/** Earliest allowed date, "YYYY-MM-DD". */
|
|
43
|
+
min?: string;
|
|
44
|
+
/** Latest allowed date, "YYYY-MM-DD". */
|
|
45
|
+
max?: string;
|
|
46
|
+
/** Extra dates that cannot be picked or typed (holidays, taken days). */
|
|
47
|
+
isDateDisabled?: (date: string) => boolean;
|
|
48
|
+
/** BCP 47 locale for labels and the typed format. Defaults to the `locale` UI string (es-PE: dd/mm/aaaa). */
|
|
49
|
+
locale?: string;
|
|
50
|
+
/** First day of the week in the calendar, 0 (Sunday) to 6. Defaults from the locale (es-PE: Monday). */
|
|
51
|
+
weekStartsOn?: WeekStart;
|
|
52
|
+
/** Shows a button that clears the date. Default `true`. */
|
|
53
|
+
clearable?: boolean;
|
|
54
|
+
clearLabel?: string;
|
|
55
|
+
/** Submits the "YYYY-MM-DD" value with a surrounding `<form>` through a hidden input. */
|
|
56
|
+
name?: string;
|
|
57
|
+
invalid?: boolean;
|
|
58
|
+
/** Portal target for the calendar popover. Defaults to `document.body`. */
|
|
59
|
+
container?: HTMLElement | null;
|
|
60
|
+
/** Popover surface material; see `PopoverContent`. */
|
|
61
|
+
material?: Material;
|
|
62
|
+
/** Per-part classes merged with `cn`; consumer classes win. `className` still targets the input and is merged last. */
|
|
63
|
+
classNames?: ClassNames<DatePickerPart>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const ICON_BUTTON =
|
|
67
|
+
"flex w-8 items-center justify-center rounded-input text-ink-3 outline-none hover:text-ink focus-visible:focus-ring disabled:pointer-events-none disabled:opacity-50";
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Typed date field plus a calendar popover. The value is an ISO "YYYY-MM-DD" string end to end,
|
|
71
|
+
* so it never shifts with the time zone. The person types in the locale's order ("07/09/2026" in
|
|
72
|
+
* es-PE) or picks from the calendar; Alt+ArrowDown opens it from the keyboard.
|
|
73
|
+
*/
|
|
74
|
+
export function DatePicker({
|
|
75
|
+
value: valueProp,
|
|
76
|
+
defaultValue = "",
|
|
77
|
+
onValueChange,
|
|
78
|
+
min,
|
|
79
|
+
max,
|
|
80
|
+
isDateDisabled,
|
|
81
|
+
locale: localeProp,
|
|
82
|
+
weekStartsOn,
|
|
83
|
+
clearable = true,
|
|
84
|
+
clearLabel,
|
|
85
|
+
name,
|
|
86
|
+
invalid,
|
|
87
|
+
container,
|
|
88
|
+
material,
|
|
89
|
+
disabled,
|
|
90
|
+
placeholder,
|
|
91
|
+
className,
|
|
92
|
+
classNames,
|
|
93
|
+
ref,
|
|
94
|
+
onBlur,
|
|
95
|
+
onKeyDown,
|
|
96
|
+
"aria-invalid": ariaInvalid,
|
|
97
|
+
...props
|
|
98
|
+
}: DatePickerProps) {
|
|
99
|
+
const strings = useUiStrings();
|
|
100
|
+
const locale = localeProp || strings.locale;
|
|
101
|
+
const format = useMemo(() => dateFormatFor(locale), [locale]);
|
|
102
|
+
const inputRef = useRef<HTMLInputElement | null>(null);
|
|
103
|
+
const composedRef = useComposedRefs<HTMLInputElement>(ref, inputRef);
|
|
104
|
+
const [current, setCurrent] = useControllableState<string>({
|
|
105
|
+
prop: valueProp,
|
|
106
|
+
defaultProp: defaultValue,
|
|
107
|
+
onChange: onValueChange,
|
|
108
|
+
});
|
|
109
|
+
const date = isIsoDate(current) ? current : "";
|
|
110
|
+
const [draft, setDraft] = useState<string | null>(null);
|
|
111
|
+
const [open, setOpen] = useState(false);
|
|
112
|
+
|
|
113
|
+
const allowed = (iso: string) =>
|
|
114
|
+
!(min && iso < min) && !(max && iso > max) && !isDateDisabled?.(iso);
|
|
115
|
+
const typed = draft === null ? null : parseDisplay(draft, format);
|
|
116
|
+
const rejected = typed !== null && !allowed(typed);
|
|
117
|
+
|
|
118
|
+
const onChange = (raw: string) => {
|
|
119
|
+
const masked = maskDisplay(raw, format);
|
|
120
|
+
const iso = parseDisplay(masked, format);
|
|
121
|
+
if (iso && allowed(iso)) {
|
|
122
|
+
setCurrent(iso);
|
|
123
|
+
setDraft(null);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
setDraft(masked);
|
|
127
|
+
if (masked === "") setCurrent("");
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
|
|
131
|
+
onKeyDown?.(event);
|
|
132
|
+
if (event.defaultPrevented) return;
|
|
133
|
+
if (event.key === "ArrowDown" && event.altKey && !disabled) {
|
|
134
|
+
event.preventDefault();
|
|
135
|
+
setOpen(true);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const hasClear = clearable && !disabled && date !== "";
|
|
140
|
+
|
|
141
|
+
return (
|
|
142
|
+
<PopoverPrimitive.Root open={open} onOpenChange={setOpen}>
|
|
143
|
+
<PopoverPrimitive.Anchor asChild>
|
|
144
|
+
<div className={cn("relative flex w-full min-w-0", classNames?.root)}>
|
|
145
|
+
<input
|
|
146
|
+
{...props}
|
|
147
|
+
ref={composedRef}
|
|
148
|
+
type="text"
|
|
149
|
+
inputMode="numeric"
|
|
150
|
+
autoComplete="off"
|
|
151
|
+
maxLength={10}
|
|
152
|
+
disabled={disabled}
|
|
153
|
+
placeholder={
|
|
154
|
+
placeholder ??
|
|
155
|
+
displayPlaceholder(format, {
|
|
156
|
+
day: strings.dayPlaceholder,
|
|
157
|
+
month: strings.monthPlaceholder,
|
|
158
|
+
year: strings.yearPlaceholder,
|
|
159
|
+
})
|
|
160
|
+
}
|
|
161
|
+
aria-invalid={ariaInvalid ?? (invalid || rejected || undefined)}
|
|
162
|
+
value={draft ?? formatDisplay(date, format)}
|
|
163
|
+
onChange={(event) => onChange(event.target.value)}
|
|
164
|
+
onBlur={(event) => {
|
|
165
|
+
onBlur?.(event);
|
|
166
|
+
setDraft(null);
|
|
167
|
+
}}
|
|
168
|
+
onKeyDown={handleKeyDown}
|
|
169
|
+
className={cn(
|
|
170
|
+
"h-input min-h-(--touch-target) w-full min-w-0 rounded-input border-control bg-surface-control pl-3 text-control text-ink tabular-nums placeholder:text-ink-3 focus:border-transparent focus:outline-2 focus:outline-accent-ink focus:-outline-offset-1 disabled:cursor-not-allowed disabled:opacity-50 aria-[invalid=true]:border-danger",
|
|
171
|
+
hasClear ? "pr-16" : "pr-9",
|
|
172
|
+
classNames?.input,
|
|
173
|
+
className,
|
|
174
|
+
)}
|
|
175
|
+
/>
|
|
176
|
+
<div className="absolute inset-y-0 right-0 flex items-center">
|
|
177
|
+
{hasClear ? (
|
|
178
|
+
<button
|
|
179
|
+
type="button"
|
|
180
|
+
aria-label={clearLabel ?? strings.clear}
|
|
181
|
+
onClick={() => {
|
|
182
|
+
setCurrent("");
|
|
183
|
+
setDraft(null);
|
|
184
|
+
inputRef.current?.focus();
|
|
185
|
+
}}
|
|
186
|
+
className={cn(ICON_BUTTON, classNames?.clear)}
|
|
187
|
+
>
|
|
188
|
+
<X aria-hidden="true" className="size-3.5" />
|
|
189
|
+
</button>
|
|
190
|
+
) : null}
|
|
191
|
+
<PopoverPrimitive.Trigger
|
|
192
|
+
disabled={disabled}
|
|
193
|
+
aria-label={strings.openCalendar}
|
|
194
|
+
className={cn(ICON_BUTTON, classNames?.trigger)}
|
|
195
|
+
>
|
|
196
|
+
<CalendarIcon aria-hidden="true" className="size-4" />
|
|
197
|
+
</PopoverPrimitive.Trigger>
|
|
198
|
+
</div>
|
|
199
|
+
{name ? <input type="hidden" name={name} value={date} disabled={disabled} /> : null}
|
|
200
|
+
</div>
|
|
201
|
+
</PopoverPrimitive.Anchor>
|
|
202
|
+
<PopoverPrimitive.Portal container={container}>
|
|
203
|
+
<PopoverPrimitive.Content
|
|
204
|
+
aria-label={strings.calendar}
|
|
205
|
+
sideOffset={4}
|
|
206
|
+
collisionPadding={8}
|
|
207
|
+
align="start"
|
|
208
|
+
// The calendar takes focus itself; closing hands it back to the field.
|
|
209
|
+
onOpenAutoFocus={(event) => event.preventDefault()}
|
|
210
|
+
onCloseAutoFocus={(event) => {
|
|
211
|
+
event.preventDefault();
|
|
212
|
+
inputRef.current?.focus({ preventScroll: true });
|
|
213
|
+
}}
|
|
214
|
+
className={cn(
|
|
215
|
+
"z-85 max-w-(--ui-popper-available-width) overflow-y-auto rounded-popover surface-overlay p-3 text-ink shadow-overlay",
|
|
216
|
+
materialClass(material, "surface-solid"),
|
|
217
|
+
classNames?.content,
|
|
218
|
+
)}
|
|
219
|
+
>
|
|
220
|
+
<DateCalendar
|
|
221
|
+
autoFocus
|
|
222
|
+
value={date || undefined}
|
|
223
|
+
min={min}
|
|
224
|
+
max={max}
|
|
225
|
+
isDateDisabled={isDateDisabled}
|
|
226
|
+
locale={locale}
|
|
227
|
+
weekStartsOn={weekStartsOn}
|
|
228
|
+
className={classNames?.calendar}
|
|
229
|
+
onSelect={(next) => {
|
|
230
|
+
setCurrent(next);
|
|
231
|
+
setDraft(null);
|
|
232
|
+
setOpen(false);
|
|
233
|
+
}}
|
|
234
|
+
/>
|
|
235
|
+
</PopoverPrimitive.Content>
|
|
236
|
+
</PopoverPrimitive.Portal>
|
|
237
|
+
</PopoverPrimitive.Root>
|
|
238
|
+
);
|
|
239
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { type ComponentProps, useEffect, useId, useMemo, useState } from "react";
|
|
4
|
+
import { isIsoDate, type WeekStart } from "../../calendar/internal/date-utils.js";
|
|
5
|
+
import { useUiStrings } from "../../i18n/index.js";
|
|
6
|
+
import type { ClassNames } from "../../utils/class-names.js";
|
|
7
|
+
import { cn } from "../../utils/cn.js";
|
|
8
|
+
import type { Material } from "../../utils/material.js";
|
|
9
|
+
import { useControllableState } from "../../utils/use-controllable-state.js";
|
|
10
|
+
import { TimeInput } from "../time-input/time-input.js";
|
|
11
|
+
import { DatePicker } from "./date-picker.js";
|
|
12
|
+
import { dateTimeInRange, joinDateTime, splitDateTime } from "./model.js";
|
|
13
|
+
|
|
14
|
+
export type DateTimePickerPart = "root" | "date" | "time";
|
|
15
|
+
|
|
16
|
+
export interface DateTimePickerProps
|
|
17
|
+
extends Omit<
|
|
18
|
+
ComponentProps<"div">,
|
|
19
|
+
"defaultValue" | "onChange" | "children" | "role" | "id" | "aria-label" | "aria-labelledby"
|
|
20
|
+
> {
|
|
21
|
+
/**
|
|
22
|
+
* "YYYY-MM-DDTHH:mm" (controlled), a wall-clock date and time with no zone: the component never
|
|
23
|
+
* converts it, so "2026-09-07T09:30" stays that string whatever the browser's time zone is.
|
|
24
|
+
* `""` while either half is missing or the pair is outside `min`/`max`.
|
|
25
|
+
*/
|
|
26
|
+
value?: string;
|
|
27
|
+
defaultValue?: string;
|
|
28
|
+
onValueChange?: (value: string) => void;
|
|
29
|
+
/** Earliest allowed value: a date ("2026-09-07", whole day) or a date-time. */
|
|
30
|
+
min?: string;
|
|
31
|
+
/** Latest allowed value: a date (whole day) or a date-time. */
|
|
32
|
+
max?: string;
|
|
33
|
+
isDateDisabled?: (date: string) => boolean;
|
|
34
|
+
locale?: string;
|
|
35
|
+
weekStartsOn?: WeekStart;
|
|
36
|
+
/** Minutes moved by ArrowUp / ArrowDown on the minutes of the time half. Default 5. */
|
|
37
|
+
step?: number;
|
|
38
|
+
/** Submits the combined value with a surrounding `<form>` through a hidden input. */
|
|
39
|
+
name?: string;
|
|
40
|
+
id?: string;
|
|
41
|
+
disabled?: boolean;
|
|
42
|
+
required?: boolean;
|
|
43
|
+
invalid?: boolean;
|
|
44
|
+
"aria-label"?: string;
|
|
45
|
+
"aria-labelledby"?: string;
|
|
46
|
+
"aria-describedby"?: string;
|
|
47
|
+
"aria-invalid"?: boolean | "true" | "false";
|
|
48
|
+
container?: HTMLElement | null;
|
|
49
|
+
material?: Material;
|
|
50
|
+
/** Per-part classes merged with `cn`; consumer classes win. `className` still targets the root and is merged last. */
|
|
51
|
+
classNames?: ClassNames<DateTimePickerPart>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const EMPTY = { date: "", time: "" };
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Date field with calendar plus a 24h time field, as one value. Replaces
|
|
58
|
+
* `<input type="datetime-local">`; the value is "YYYY-MM-DDTHH:mm" without a time zone.
|
|
59
|
+
*/
|
|
60
|
+
export function DateTimePicker({
|
|
61
|
+
value: valueProp,
|
|
62
|
+
defaultValue = "",
|
|
63
|
+
onValueChange,
|
|
64
|
+
min,
|
|
65
|
+
max,
|
|
66
|
+
isDateDisabled,
|
|
67
|
+
locale,
|
|
68
|
+
weekStartsOn,
|
|
69
|
+
step,
|
|
70
|
+
name,
|
|
71
|
+
id,
|
|
72
|
+
disabled,
|
|
73
|
+
required,
|
|
74
|
+
invalid,
|
|
75
|
+
container,
|
|
76
|
+
material,
|
|
77
|
+
className,
|
|
78
|
+
classNames,
|
|
79
|
+
"aria-label": ariaLabel,
|
|
80
|
+
"aria-labelledby": ariaLabelledBy,
|
|
81
|
+
"aria-describedby": ariaDescribedBy,
|
|
82
|
+
"aria-invalid": ariaInvalid,
|
|
83
|
+
...props
|
|
84
|
+
}: DateTimePickerProps) {
|
|
85
|
+
const strings = useUiStrings();
|
|
86
|
+
const autoId = useId();
|
|
87
|
+
const dateId = id ?? autoId;
|
|
88
|
+
const [current, setCurrent] = useControllableState<string>({
|
|
89
|
+
prop: valueProp,
|
|
90
|
+
defaultProp: defaultValue,
|
|
91
|
+
onChange: onValueChange,
|
|
92
|
+
});
|
|
93
|
+
const [parts, setParts] = useState(() => splitDateTime(current) ?? EMPTY);
|
|
94
|
+
const [synced, setSynced] = useState(current);
|
|
95
|
+
const [timeLabel, setTimeLabel] = useState(strings.time);
|
|
96
|
+
// After a comma the word is lowercase in every locale ("Fecha, hora"); alone it keeps its capital.
|
|
97
|
+
const suffix = strings.time.toLowerCase();
|
|
98
|
+
|
|
99
|
+
// Adopt a value set from outside (form reset, prefill) without losing a half-typed pair.
|
|
100
|
+
if (synced !== current) {
|
|
101
|
+
setSynced(current);
|
|
102
|
+
if (joinDateTime(parts) !== current) setParts(splitDateTime(current) ?? EMPTY);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// The time half is named "<field label>, <time>" so it differs from the date half.
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
const label =
|
|
108
|
+
ariaLabel ??
|
|
109
|
+
ariaLabelledBy
|
|
110
|
+
?.split(" ")
|
|
111
|
+
.map((labelId) => document.getElementById(labelId)?.textContent ?? "")
|
|
112
|
+
.join(" ")
|
|
113
|
+
.trim() ??
|
|
114
|
+
(
|
|
115
|
+
document.getElementById(dateId) as HTMLInputElement | null
|
|
116
|
+
)?.labels?.[0]?.textContent?.trim();
|
|
117
|
+
setTimeLabel(label ? `${label}, ${suffix}` : strings.time);
|
|
118
|
+
}, [ariaLabel, ariaLabelledBy, dateId, strings.time, suffix]);
|
|
119
|
+
|
|
120
|
+
const joined = joinDateTime(parts);
|
|
121
|
+
const outOfRange = joined !== "" && !dateTimeInRange(joined, min, max);
|
|
122
|
+
const dateMin = min && isIsoDate(min.slice(0, 10)) ? min.slice(0, 10) : undefined;
|
|
123
|
+
const dateMax = max && isIsoDate(max.slice(0, 10)) ? max.slice(0, 10) : undefined;
|
|
124
|
+
|
|
125
|
+
const update = useMemo(
|
|
126
|
+
() => (next: { date: string; time: string }) => {
|
|
127
|
+
setParts(next);
|
|
128
|
+
const value = joinDateTime(next);
|
|
129
|
+
const committed = value && dateTimeInRange(value, min, max) ? value : "";
|
|
130
|
+
setCurrent(committed);
|
|
131
|
+
},
|
|
132
|
+
[min, max, setCurrent],
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
const shared = {
|
|
136
|
+
disabled,
|
|
137
|
+
"aria-describedby": ariaDescribedBy,
|
|
138
|
+
"aria-invalid": ariaInvalid ?? (invalid || undefined),
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
return (
|
|
142
|
+
<div
|
|
143
|
+
{...props}
|
|
144
|
+
className={cn("flex w-full min-w-0 flex-wrap gap-2", classNames?.root, className)}
|
|
145
|
+
>
|
|
146
|
+
<DatePicker
|
|
147
|
+
{...shared}
|
|
148
|
+
id={dateId}
|
|
149
|
+
value={parts.date}
|
|
150
|
+
onValueChange={(date) => update({ ...parts, date })}
|
|
151
|
+
min={dateMin}
|
|
152
|
+
max={dateMax}
|
|
153
|
+
isDateDisabled={isDateDisabled}
|
|
154
|
+
locale={locale}
|
|
155
|
+
weekStartsOn={weekStartsOn}
|
|
156
|
+
required={required}
|
|
157
|
+
aria-label={ariaLabel}
|
|
158
|
+
aria-labelledby={ariaLabelledBy}
|
|
159
|
+
container={container}
|
|
160
|
+
material={material}
|
|
161
|
+
className={undefined}
|
|
162
|
+
classNames={{ root: cn("flex-[3_1_10rem]", classNames?.date) }}
|
|
163
|
+
/>
|
|
164
|
+
<div className={cn("flex min-w-0 flex-[2_1_6rem]", classNames?.time)}>
|
|
165
|
+
<TimeInput
|
|
166
|
+
{...shared}
|
|
167
|
+
aria-invalid={outOfRange ? true : shared["aria-invalid"]}
|
|
168
|
+
aria-label={timeLabel}
|
|
169
|
+
value={parts.time}
|
|
170
|
+
onValueChange={(time) => update({ ...parts, time })}
|
|
171
|
+
required={required}
|
|
172
|
+
step={step}
|
|
173
|
+
/>
|
|
174
|
+
</div>
|
|
175
|
+
{name ? <input type="hidden" name={name} value={current} disabled={disabled} /> : null}
|
|
176
|
+
</div>
|
|
177
|
+
);
|
|
178
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { isIsoDate, toIsoDate } from "../../calendar/internal/date-utils.js";
|
|
2
|
+
|
|
3
|
+
/** Typed-date and date-time rules for `DatePicker` / `DateTimePicker`. Strings in, strings out: no `Date` in local time. */
|
|
4
|
+
|
|
5
|
+
export type DatePart = "day" | "month" | "year";
|
|
6
|
+
|
|
7
|
+
export interface DateFormat {
|
|
8
|
+
order: DatePart[];
|
|
9
|
+
separator: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const FALLBACK: DateFormat = { order: ["day", "month", "year"], separator: "/" };
|
|
13
|
+
|
|
14
|
+
/** Field order and separator a locale writes numeric dates with: es-PE "dd/mm/yyyy", en-US "mm/dd/yyyy", sv-SE "yyyy-mm-dd". */
|
|
15
|
+
export function dateFormatFor(locale: string): DateFormat {
|
|
16
|
+
try {
|
|
17
|
+
const parts = new Intl.DateTimeFormat(locale, {
|
|
18
|
+
year: "numeric",
|
|
19
|
+
month: "2-digit",
|
|
20
|
+
day: "2-digit",
|
|
21
|
+
timeZone: "UTC",
|
|
22
|
+
}).formatToParts(new Date(Date.UTC(2006, 10, 22)));
|
|
23
|
+
const order = parts
|
|
24
|
+
.map((part) => part.type)
|
|
25
|
+
.filter((type): type is DatePart => type === "day" || type === "month" || type === "year");
|
|
26
|
+
const separator = parts.find((part) => part.type === "literal")?.value.trim();
|
|
27
|
+
if (order.length !== 3) return FALLBACK;
|
|
28
|
+
return { order, separator: separator || "/" };
|
|
29
|
+
} catch {
|
|
30
|
+
return FALLBACK;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const SEGMENT: Record<DatePart, number> = { day: 2, month: 2, year: 4 };
|
|
35
|
+
|
|
36
|
+
/** "2026-09-07" as the locale writes it, or "" for anything that is not an ISO date. */
|
|
37
|
+
export function formatDisplay(iso: string, format: DateFormat): string {
|
|
38
|
+
if (!isIsoDate(iso)) return "";
|
|
39
|
+
const value: Record<DatePart, string> = {
|
|
40
|
+
year: iso.slice(0, 4),
|
|
41
|
+
month: iso.slice(5, 7),
|
|
42
|
+
day: iso.slice(8, 10),
|
|
43
|
+
};
|
|
44
|
+
return format.order.map((part) => value[part]).join(format.separator);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Keeps digits and inserts the separator once a segment is full and more digits follow. */
|
|
48
|
+
export function maskDisplay(raw: string, format: DateFormat): string {
|
|
49
|
+
let rest = raw.replace(/\D/g, "").slice(0, 8);
|
|
50
|
+
const segments: string[] = [];
|
|
51
|
+
for (const part of format.order) {
|
|
52
|
+
if (!rest) break;
|
|
53
|
+
segments.push(rest.slice(0, SEGMENT[part]));
|
|
54
|
+
rest = rest.slice(SEGMENT[part]);
|
|
55
|
+
}
|
|
56
|
+
return segments.join(format.separator);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Typed text to "YYYY-MM-DD"; null while it is incomplete or not a real date. */
|
|
60
|
+
export function parseDisplay(text: string, format: DateFormat): string | null {
|
|
61
|
+
const pieces = text.trim().split(format.separator);
|
|
62
|
+
if (pieces.length !== 3) return null;
|
|
63
|
+
const value: Partial<Record<DatePart, string>> = {};
|
|
64
|
+
for (const [index, part] of format.order.entries()) {
|
|
65
|
+
const piece = pieces[index] ?? "";
|
|
66
|
+
const valid = part === "year" ? /^\d{4}$/.test(piece) : /^\d{1,2}$/.test(piece);
|
|
67
|
+
if (!valid) return null;
|
|
68
|
+
value[part] = piece;
|
|
69
|
+
}
|
|
70
|
+
const iso = toIsoDate(Number(value.year), Number(value.month), Number(value.day));
|
|
71
|
+
return isIsoDate(iso) ? iso : null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** "dd/mm/aaaa" built from the locale's order and the localized segment words. */
|
|
75
|
+
export function displayPlaceholder(
|
|
76
|
+
format: DateFormat,
|
|
77
|
+
words: { day: string; month: string; year: string },
|
|
78
|
+
): string {
|
|
79
|
+
return format.order.map((part) => words[part]).join(format.separator);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function isValidTime(time: string): boolean {
|
|
83
|
+
return /^([01]\d|2[0-3]):[0-5]\d$/.test(time);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const DATE_TIME = /^(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2})$/;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Splits "YYYY-MM-DDTHH:mm". The value is a wall-clock time with no zone: anything carrying a
|
|
90
|
+
* "Z", an offset or seconds is rejected instead of being silently reinterpreted.
|
|
91
|
+
*/
|
|
92
|
+
export function splitDateTime(value: string): { date: string; time: string } | null {
|
|
93
|
+
const match = DATE_TIME.exec(value);
|
|
94
|
+
if (!match) return null;
|
|
95
|
+
const [, date = "", time = ""] = match;
|
|
96
|
+
return isIsoDate(date) && isValidTime(time) ? { date, time } : null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function joinDateTime(parts: { date: string; time: string }): string {
|
|
100
|
+
return parts.date && parts.time ? `${parts.date}T${parts.time}` : "";
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** `min`/`max` may be a date ("2026-09-07", whole day) or a date-time. Lexicographic compare is chronological. */
|
|
104
|
+
export function dateTimeInRange(value: string, min?: string, max?: string): boolean {
|
|
105
|
+
if (min && value < (min.length === 10 ? `${min}T00:00` : min)) return false;
|
|
106
|
+
if (max && value > (max.length === 10 ? `${max}T23:59` : max)) return false;
|
|
107
|
+
return true;
|
|
108
|
+
}
|