@sereno-ds/ui 0.25.7 → 0.26.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.
@@ -0,0 +1,37 @@
1
+ import * as React from 'react';
2
+
3
+ interface DatePickerProps {
4
+ label?: string;
5
+ hint?: string;
6
+ error?: string;
7
+ required?: boolean;
8
+ placeholder?: string;
9
+ size?: 'sm' | 'md' | 'lg';
10
+ disabled?: boolean;
11
+ /** Controlled value, ISO "YYYY-MM-DD". */
12
+ value?: string;
13
+ /** Uncontrolled initial value, ISO "YYYY-MM-DD". */
14
+ defaultValue?: string;
15
+ onChange?: (value: string) => void;
16
+ /** Earliest selectable date, ISO "YYYY-MM-DD" — every day before it is unavailable. */
17
+ min?: string;
18
+ /** Latest selectable date, ISO "YYYY-MM-DD" — every day after it is unavailable. */
19
+ max?: string;
20
+ id?: string;
21
+ containerStyle?: React.CSSProperties;
22
+ }
23
+ /**
24
+ * Field for picking a single date — no time, no slots (`DateTimePicker`'s
25
+ * job). A text-field-styled trigger showing the selected date opens a
26
+ * popover with the same calendar grid `DateTimePicker` uses (roving
27
+ * tabindex, month/year jump — see `./_internal/CalendarGrid`), so the two
28
+ * share every bit of calendar behavior (SS-243).
29
+ *
30
+ * Value contract matches the rest of the forms package: `value` /
31
+ * `defaultValue` + `onChange(value)`, a plain ISO "YYYY-MM-DD" string —
32
+ * drops in wherever `<input type="date">` would go, but themed and in
33
+ * pt-BR instead of the browser's own (English, unstyled) date picker.
34
+ */
35
+ declare function DatePicker({ label, hint, error, required, placeholder, size, disabled, value, defaultValue, onChange, min, max, id, containerStyle, }: DatePickerProps): React.JSX.Element;
36
+
37
+ export { DatePicker, type DatePickerProps };
@@ -0,0 +1,182 @@
1
+ "use client";
2
+ import { jsx, jsxs } from "react/jsx-runtime";
3
+ import * as React from "react";
4
+ import { Calendar as CalendarIcon } from "lucide-react";
5
+ import { sx } from "../_internal/style";
6
+ import { Field } from "../_internal/Field";
7
+ import { CalendarGrid } from "./_internal/CalendarGrid";
8
+ import { fieldBoxStyle } from "./_internal/fieldBoxStyle";
9
+ function parseISO(iso) {
10
+ if (!iso) return null;
11
+ const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(iso);
12
+ if (!m) return null;
13
+ return { year: Number(m[1]), month: Number(m[2]) - 1, day: Number(m[3]) };
14
+ }
15
+ function toISO(year, month, day) {
16
+ return `${String(year).padStart(4, "0")}-${String(month + 1).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
17
+ }
18
+ function formatDisplay(iso) {
19
+ const d = parseISO(iso);
20
+ if (!d) return iso;
21
+ return new Intl.DateTimeFormat("pt-BR", { day: "2-digit", month: "2-digit", year: "numeric" }).format(new Date(d.year, d.month, d.day));
22
+ }
23
+ function daysIn(year, month) {
24
+ return new Date(year, month + 1, 0).getDate();
25
+ }
26
+ function unavailableForMonth(year, month, min, max) {
27
+ if (!min && !max) return [];
28
+ const minD = parseISO(min);
29
+ const maxD = parseISO(max);
30
+ const out = [];
31
+ const total = daysIn(year, month);
32
+ for (let day = 1; day <= total; day++) {
33
+ if (minD && year * 372 + month * 31 + day < minD.year * 372 + minD.month * 31 + minD.day) {
34
+ out.push(day);
35
+ continue;
36
+ }
37
+ if (maxD && year * 372 + month * 31 + day > maxD.year * 372 + maxD.month * 31 + maxD.day) {
38
+ out.push(day);
39
+ }
40
+ }
41
+ return out;
42
+ }
43
+ function DatePicker({
44
+ label,
45
+ hint,
46
+ error,
47
+ required,
48
+ placeholder = "Selecionar data",
49
+ size = "md",
50
+ disabled = false,
51
+ value,
52
+ defaultValue,
53
+ onChange,
54
+ min,
55
+ max,
56
+ id,
57
+ containerStyle
58
+ }) {
59
+ const [inner, setInner] = React.useState(defaultValue ?? "");
60
+ const iso = value !== void 0 ? value : inner;
61
+ const parsed = parseISO(iso);
62
+ const now = React.useMemo(() => /* @__PURE__ */ new Date(), []);
63
+ const [open, setOpen] = React.useState(false);
64
+ const [viewYear, setViewYear] = React.useState(parsed?.year ?? now.getFullYear());
65
+ const [viewMonth, setViewMonth] = React.useState(parsed?.month ?? now.getMonth());
66
+ const rootRef = React.useRef(null);
67
+ const triggerRef = React.useRef(null);
68
+ const generatedId = React.useId();
69
+ const rid = id || generatedId;
70
+ React.useEffect(() => {
71
+ if (!open) return;
72
+ const onDown = (e) => {
73
+ if (rootRef.current && !rootRef.current.contains(e.target)) {
74
+ setOpen(false);
75
+ }
76
+ };
77
+ const onKey = (e) => {
78
+ if (e.key === "Escape") {
79
+ setOpen(false);
80
+ triggerRef.current?.focus();
81
+ }
82
+ };
83
+ document.addEventListener("pointerdown", onDown, true);
84
+ document.addEventListener("keydown", onKey, true);
85
+ return () => {
86
+ document.removeEventListener("pointerdown", onDown, true);
87
+ document.removeEventListener("keydown", onKey, true);
88
+ };
89
+ }, [open]);
90
+ const openPopover = () => {
91
+ if (disabled) return;
92
+ setViewYear(parsed?.year ?? now.getFullYear());
93
+ setViewMonth(parsed?.month ?? now.getMonth());
94
+ setOpen(true);
95
+ };
96
+ const commit = (day) => {
97
+ const next = toISO(viewYear, viewMonth, day);
98
+ if (value === void 0) setInner(next);
99
+ onChange?.(next);
100
+ setOpen(false);
101
+ triggerRef.current?.focus();
102
+ };
103
+ const unavailable = React.useMemo(() => unavailableForMonth(viewYear, viewMonth, min, max), [viewYear, viewMonth, min, max]);
104
+ return /* @__PURE__ */ jsx(Field, { label, hint, error, required, htmlFor: rid, style: containerStyle, children: /* @__PURE__ */ jsxs("div", { ref: rootRef, style: { position: "relative", width: "100%" }, children: [
105
+ /* @__PURE__ */ jsxs(
106
+ "button",
107
+ {
108
+ ref: triggerRef,
109
+ id: rid,
110
+ type: "button",
111
+ disabled,
112
+ "aria-haspopup": "dialog",
113
+ "aria-expanded": open,
114
+ onClick: () => open ? setOpen(false) : openPopover(),
115
+ style: fieldBoxStyle(size, !!error, open, disabled),
116
+ children: [
117
+ /* @__PURE__ */ jsx(CalendarIcon, { size: 16, strokeWidth: 1.75, style: { flex: "0 0 auto", opacity: 0.7 } }),
118
+ /* @__PURE__ */ jsx(
119
+ "span",
120
+ {
121
+ style: sx({
122
+ flex: 1,
123
+ textAlign: "left",
124
+ overflow: "hidden",
125
+ textOverflow: "ellipsis",
126
+ whiteSpace: "nowrap",
127
+ color: iso ? "inherit" : "var(--text-muted)"
128
+ }),
129
+ children: iso ? formatDisplay(iso) : placeholder
130
+ }
131
+ )
132
+ ]
133
+ }
134
+ ),
135
+ open && /* @__PURE__ */ jsx(
136
+ "div",
137
+ {
138
+ role: "dialog",
139
+ "aria-label": label || "Escolher data",
140
+ tabIndex: -1,
141
+ style: sx({
142
+ position: "absolute",
143
+ top: "calc(100% + 6px)",
144
+ left: 0,
145
+ right: 0,
146
+ // The 7-column grid needs some floor width to stay readable —
147
+ // a narrow field (e.g. a compact "Data" next to "Motivo")
148
+ // would otherwise squeeze it. `left`/`right: 0` already
149
+ // matches the trigger's own width for anything wider.
150
+ minWidth: 272,
151
+ zIndex: 20,
152
+ background: "var(--bg-surface)",
153
+ border: "var(--border-width-hairline) solid var(--border-default)",
154
+ borderRadius: "var(--radius-md)",
155
+ boxShadow: "var(--shadow-lg)",
156
+ padding: "var(--space-3)",
157
+ outline: "none"
158
+ }),
159
+ children: /* @__PURE__ */ jsx(
160
+ CalendarGrid,
161
+ {
162
+ year: viewYear,
163
+ month: viewMonth,
164
+ selectedDate: parsed && parsed.year === viewYear && parsed.month === viewMonth ? parsed.day : void 0,
165
+ unavailable,
166
+ onMonthChange: (y, m) => {
167
+ setViewYear(y);
168
+ setViewMonth(m);
169
+ },
170
+ onSelectDate: commit,
171
+ squareCells: true,
172
+ trimEmptyRows: true
173
+ }
174
+ )
175
+ }
176
+ )
177
+ ] }) });
178
+ }
179
+ export {
180
+ DatePicker
181
+ };
182
+ //# sourceMappingURL=DatePicker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/forms/DatePicker.tsx"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { Calendar as CalendarIcon } from 'lucide-react';\nimport { sx } from '../_internal/style';\nimport { Field } from '../_internal/Field';\nimport { CalendarGrid } from './_internal/CalendarGrid';\nimport { fieldBoxStyle } from './_internal/fieldBoxStyle';\n\nexport interface DatePickerProps {\n label?: string;\n hint?: string;\n error?: string;\n required?: boolean;\n placeholder?: string;\n size?: 'sm' | 'md' | 'lg';\n disabled?: boolean;\n /** Controlled value, ISO \"YYYY-MM-DD\". */\n value?: string;\n /** Uncontrolled initial value, ISO \"YYYY-MM-DD\". */\n defaultValue?: string;\n onChange?: (value: string) => void;\n /** Earliest selectable date, ISO \"YYYY-MM-DD\" — every day before it is unavailable. */\n min?: string;\n /** Latest selectable date, ISO \"YYYY-MM-DD\" — every day after it is unavailable. */\n max?: string;\n id?: string;\n containerStyle?: React.CSSProperties;\n}\n\ninterface YMD {\n year: number;\n month: number;\n day: number;\n}\n\nfunction parseISO(iso: string | undefined): YMD | null {\n if (!iso) return null;\n const m = /^(\\d{4})-(\\d{2})-(\\d{2})$/.exec(iso);\n if (!m) return null;\n return { year: Number(m[1]), month: Number(m[2]) - 1, day: Number(m[3]) };\n}\n\nfunction toISO(year: number, month: number, day: number): string {\n return `${String(year).padStart(4, '0')}-${String(month + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`;\n}\n\nfunction formatDisplay(iso: string): string {\n const d = parseISO(iso);\n if (!d) return iso;\n return new Intl.DateTimeFormat('pt-BR', { day: '2-digit', month: '2-digit', year: 'numeric' }).format(new Date(d.year, d.month, d.day));\n}\n\nfunction daysIn(year: number, month: number): number {\n return new Date(year, month + 1, 0).getDate();\n}\n\n/** Every day of (year, month) that falls before `min` or after `max`. */\nfunction unavailableForMonth(year: number, month: number, min?: string, max?: string): number[] {\n if (!min && !max) return [];\n const minD = parseISO(min);\n const maxD = parseISO(max);\n const out: number[] = [];\n const total = daysIn(year, month);\n for (let day = 1; day <= total; day++) {\n if (minD && year * 372 + month * 31 + day < minD.year * 372 + minD.month * 31 + minD.day) {\n out.push(day);\n continue;\n }\n if (maxD && year * 372 + month * 31 + day > maxD.year * 372 + maxD.month * 31 + maxD.day) {\n out.push(day);\n }\n }\n return out;\n}\n\n/**\n * Field for picking a single date — no time, no slots (`DateTimePicker`'s\n * job). A text-field-styled trigger showing the selected date opens a\n * popover with the same calendar grid `DateTimePicker` uses (roving\n * tabindex, month/year jump — see `./_internal/CalendarGrid`), so the two\n * share every bit of calendar behavior (SS-243).\n *\n * Value contract matches the rest of the forms package: `value` /\n * `defaultValue` + `onChange(value)`, a plain ISO \"YYYY-MM-DD\" string —\n * drops in wherever `<input type=\"date\">` would go, but themed and in\n * pt-BR instead of the browser's own (English, unstyled) date picker.\n */\nexport function DatePicker({\n label,\n hint,\n error,\n required,\n placeholder = 'Selecionar data',\n size = 'md',\n disabled = false,\n value,\n defaultValue,\n onChange,\n min,\n max,\n id,\n containerStyle,\n}: DatePickerProps) {\n const [inner, setInner] = React.useState(defaultValue ?? '');\n const iso = value !== undefined ? value : inner;\n const parsed = parseISO(iso);\n const now = React.useMemo(() => new Date(), []);\n\n const [open, setOpen] = React.useState(false);\n // The month currently shown in the popover — resets to the value's month\n // (or today's) every time the popover opens, via the conditional mount\n // below; CalendarGrid itself only takes year/month as its *initial* view.\n const [viewYear, setViewYear] = React.useState(parsed?.year ?? now.getFullYear());\n const [viewMonth, setViewMonth] = React.useState(parsed?.month ?? now.getMonth());\n\n const rootRef = React.useRef<HTMLDivElement>(null);\n const triggerRef = React.useRef<HTMLButtonElement>(null);\n const generatedId = React.useId();\n const rid = id || generatedId;\n\n // A plain `position: absolute` child of `rootRef` (itself `position:\n // relative`) — the same \"inline\" strategy Select's panel uses for its\n // common case, and the same one DateTimePicker's own month/year popover\n // already uses. The browser glues it to the trigger through *any* scroll\n // (page or a nested overflow container) for free — no JS measuring\n // position, no listening for scroll/resize, and critically, it never has\n // to close on scroll the way a `position: fixed` popover computed once\n // would (that was tried first here and didn't hold up — see PR history).\n React.useEffect(() => {\n if (!open) return;\n const onDown = (e: PointerEvent) => {\n if (rootRef.current && !rootRef.current.contains(e.target as Node)) {\n setOpen(false);\n }\n };\n const onKey = (e: KeyboardEvent) => {\n if (e.key === 'Escape') {\n setOpen(false);\n triggerRef.current?.focus();\n }\n };\n document.addEventListener('pointerdown', onDown, true);\n document.addEventListener('keydown', onKey, true);\n return () => {\n document.removeEventListener('pointerdown', onDown, true);\n document.removeEventListener('keydown', onKey, true);\n };\n }, [open]);\n\n const openPopover = () => {\n if (disabled) return;\n setViewYear(parsed?.year ?? now.getFullYear());\n setViewMonth(parsed?.month ?? now.getMonth());\n setOpen(true);\n };\n\n const commit = (day: number) => {\n const next = toISO(viewYear, viewMonth, day);\n if (value === undefined) setInner(next);\n onChange?.(next);\n setOpen(false);\n triggerRef.current?.focus();\n };\n\n const unavailable = React.useMemo(() => unavailableForMonth(viewYear, viewMonth, min, max), [viewYear, viewMonth, min, max]);\n\n return (\n <Field label={label} hint={hint} error={error} required={required} htmlFor={rid} style={containerStyle}>\n <div ref={rootRef} style={{ position: 'relative', width: '100%' }}>\n <button\n ref={triggerRef}\n id={rid}\n type=\"button\"\n disabled={disabled}\n aria-haspopup=\"dialog\"\n aria-expanded={open}\n onClick={() => (open ? setOpen(false) : openPopover())}\n style={fieldBoxStyle(size, !!error, open, disabled)}\n >\n <CalendarIcon size={16} strokeWidth={1.75} style={{ flex: '0 0 auto', opacity: 0.7 }} />\n <span\n style={sx({\n flex: 1,\n textAlign: 'left',\n overflow: 'hidden',\n textOverflow: 'ellipsis',\n whiteSpace: 'nowrap',\n color: iso ? 'inherit' : 'var(--text-muted)',\n })}\n >\n {iso ? formatDisplay(iso) : placeholder}\n </span>\n </button>\n\n {open && (\n <div\n role=\"dialog\"\n aria-label={label || 'Escolher data'}\n tabIndex={-1}\n style={sx({\n position: 'absolute',\n top: 'calc(100% + 6px)',\n left: 0,\n right: 0,\n // The 7-column grid needs some floor width to stay readable —\n // a narrow field (e.g. a compact \"Data\" next to \"Motivo\")\n // would otherwise squeeze it. `left`/`right: 0` already\n // matches the trigger's own width for anything wider.\n minWidth: 272,\n zIndex: 20,\n background: 'var(--bg-surface)',\n border: 'var(--border-width-hairline) solid var(--border-default)',\n borderRadius: 'var(--radius-md)',\n boxShadow: 'var(--shadow-lg)',\n padding: 'var(--space-3)',\n outline: 'none',\n })}\n >\n <CalendarGrid\n year={viewYear}\n month={viewMonth}\n selectedDate={parsed && parsed.year === viewYear && parsed.month === viewMonth ? parsed.day : undefined}\n unavailable={unavailable}\n onMonthChange={(y, m) => {\n setViewYear(y);\n setViewMonth(m);\n }}\n onSelectDate={commit}\n squareCells\n trimEmptyRows\n />\n </div>\n )}\n </div>\n </Field>\n );\n}\n"],"mappings":";AA0KQ,SAUE,KAVF;AAxKR,YAAY,WAAW;AACvB,SAAS,YAAY,oBAAoB;AACzC,SAAS,UAAU;AACnB,SAAS,aAAa;AACtB,SAAS,oBAAoB;AAC7B,SAAS,qBAAqB;AA6B9B,SAAS,SAAS,KAAqC;AACrD,MAAI,CAAC,IAAK,QAAO;AACjB,QAAM,IAAI,4BAA4B,KAAK,GAAG;AAC9C,MAAI,CAAC,EAAG,QAAO;AACf,SAAO,EAAE,MAAM,OAAO,EAAE,CAAC,CAAC,GAAG,OAAO,OAAO,EAAE,CAAC,CAAC,IAAI,GAAG,KAAK,OAAO,EAAE,CAAC,CAAC,EAAE;AAC1E;AAEA,SAAS,MAAM,MAAc,OAAe,KAAqB;AAC/D,SAAO,GAAG,OAAO,IAAI,EAAE,SAAS,GAAG,GAAG,CAAC,IAAI,OAAO,QAAQ,CAAC,EAAE,SAAS,GAAG,GAAG,CAAC,IAAI,OAAO,GAAG,EAAE,SAAS,GAAG,GAAG,CAAC;AAC/G;AAEA,SAAS,cAAc,KAAqB;AAC1C,QAAM,IAAI,SAAS,GAAG;AACtB,MAAI,CAAC,EAAG,QAAO;AACf,SAAO,IAAI,KAAK,eAAe,SAAS,EAAE,KAAK,WAAW,OAAO,WAAW,MAAM,UAAU,CAAC,EAAE,OAAO,IAAI,KAAK,EAAE,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AAO,MAAM;AAAA,YAC9F;AAAA,YACA,eAAe,CAAC,GAAG,MAAM;AACvB,0BAAY,CAAC;AACb,2BAAa,CAAC;AAAA,YAChB;AAAA,YACA,cAAc;AAAA,YACd,aAAW;AAAA,YACX,eAAa;AAAA;AAAA,QACf;AAAA;AAAA,IACF;AAAA,KAEJ,GACF;AAEJ;","names":[]}
@@ -20,6 +20,10 @@ interface TimeSlot {
20
20
  * month. An `unavailable` day stays focusable (`aria-disabled`, not the
21
21
  * native `disabled` — a disabled button can't receive focus at all) so the
22
22
  * cursor can still land on it, just not select it.
23
+ *
24
+ * The calendar body itself (header + day grid) is `./_internal/CalendarGrid`
25
+ * — shared with `DatePicker` (SS-243), which wraps the same grid in a
26
+ * trigger+popover shell instead of this component's always-visible card.
23
27
  */
24
28
  interface DateTimePickerProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'> {
25
29
  /** Initial year. Defaults to the current year. */
@@ -1,15 +1,7 @@
1
1
  "use client";
2
2
  import { jsx, jsxs } from "react/jsx-runtime";
3
- import * as React from "react";
4
- import { ChevronDown, ChevronLeft, ChevronRight } from "lucide-react";
5
3
  import { sx } from "../_internal/style";
6
- import { IconButton } from "../core/IconButton";
7
- const DOW = ["D", "S", "T", "Q", "Q", "S", "S"];
8
- const MONTHS = ["janeiro", "fevereiro", "mar\xE7o", "abril", "maio", "junho", "julho", "agosto", "setembro", "outubro", "novembro", "dezembro"];
9
- const MONTHS_SHORT = ["jan", "fev", "mar", "abr", "mai", "jun", "jul", "ago", "set", "out", "nov", "dez"];
10
- function daysIn(y, m) {
11
- return new Date(y, m + 1, 0).getDate();
12
- }
4
+ import { CalendarGrid } from "./_internal/CalendarGrid";
13
5
  function DateTimePicker({
14
6
  year,
15
7
  month,
@@ -25,129 +17,10 @@ function DateTimePicker({
25
17
  style,
26
18
  ...rest
27
19
  }) {
28
- const now = React.useMemo(() => /* @__PURE__ */ new Date(), []);
29
- const initialIndex = (year ?? now.getFullYear()) * 12 + (month ?? now.getMonth());
30
- const [viewIndex, setViewIndex] = React.useState(initialIndex);
31
- const viewYear = Math.floor(viewIndex / 12);
32
- const viewMonth = viewIndex % 12;
33
- const [selectionIndex, setSelectionIndex] = React.useState(initialIndex);
34
- const [open, setOpen] = React.useState(false);
35
- const [picker, setPicker] = React.useState("month");
36
- const [yearBase, setYearBase] = React.useState(() => (year ?? now.getFullYear()) - 5);
37
- const rootRef = React.useRef(null);
38
- const titleRef = React.useRef(null);
39
- const popRef = React.useRef(null);
40
- const gridRef = React.useRef(null);
41
- const [activeDay, setActiveDay] = React.useState(() => {
42
- if (selectedDate) return selectedDate;
43
- const todayIndex = now.getFullYear() * 12 + now.getMonth();
44
- return todayIndex === initialIndex ? now.getDate() : 1;
45
- });
46
- const shouldFocusDayRef = React.useRef(false);
47
- const onMonthChangeRef = React.useRef(onMonthChange);
48
- React.useEffect(() => {
49
- onMonthChangeRef.current = onMonthChange;
50
- });
51
- const didMount = React.useRef(false);
52
- React.useEffect(() => {
53
- if (!didMount.current) {
54
- didMount.current = true;
55
- return;
56
- }
57
- onMonthChangeRef.current?.(Math.floor(viewIndex / 12), viewIndex % 12);
58
- }, [viewIndex]);
59
- React.useEffect(() => {
60
- if (!open) return;
61
- const onDown = (e) => {
62
- if (rootRef.current && !rootRef.current.contains(e.target)) {
63
- setOpen(false);
64
- setPicker("month");
65
- }
66
- };
67
- document.addEventListener("pointerdown", onDown, true);
68
- const id = requestAnimationFrame(() => popRef.current?.focus());
69
- return () => {
70
- document.removeEventListener("pointerdown", onDown, true);
71
- cancelAnimationFrame(id);
72
- };
73
- }, [open]);
74
- const closePopover = React.useCallback(() => {
75
- setOpen(false);
76
- setPicker("month");
77
- titleRef.current?.focus();
78
- }, []);
79
- const toggleOpen = React.useCallback(() => {
80
- setOpen((v) => {
81
- if (v) setPicker("month");
82
- return !v;
83
- });
84
- }, []);
85
- const first = new Date(viewYear, viewMonth, 1).getDay();
86
- const total = daysIn(viewYear, viewMonth);
87
- const cells = [...Array(first).fill(null), ...Array.from({ length: total }, (_, i) => i + 1)];
88
- while (cells.length < 42) cells.push(null);
89
- const cellH = renderDay ? 48 : 38;
90
- const years = Array.from({ length: 12 }, (_, i) => yearBase + i);
91
- const rovingDay = Math.min(Math.max(1, activeDay), total);
92
- const onGridKeyDown = (e) => {
93
- let delta = 0;
94
- switch (e.key) {
95
- case "ArrowRight":
96
- delta = 1;
97
- break;
98
- case "ArrowLeft":
99
- delta = -1;
100
- break;
101
- case "ArrowDown":
102
- delta = 7;
103
- break;
104
- case "ArrowUp":
105
- delta = -7;
106
- break;
107
- case "Home": {
108
- e.preventDefault();
109
- const row = Math.floor((first + rovingDay - 1) / 7);
110
- shouldFocusDayRef.current = true;
111
- setActiveDay(Math.max(1, row * 7 - first + 1));
112
- return;
113
- }
114
- case "End": {
115
- e.preventDefault();
116
- const row = Math.floor((first + rovingDay - 1) / 7);
117
- shouldFocusDayRef.current = true;
118
- setActiveDay(Math.min(total, row * 7 + 7 - first));
119
- return;
120
- }
121
- case "PageUp":
122
- e.preventDefault();
123
- shouldFocusDayRef.current = true;
124
- setViewIndex((i) => i - 1);
125
- return;
126
- case "PageDown":
127
- e.preventDefault();
128
- shouldFocusDayRef.current = true;
129
- setViewIndex((i) => i + 1);
130
- return;
131
- default:
132
- return;
133
- }
134
- e.preventDefault();
135
- const next = new Date(viewYear, viewMonth, rovingDay + delta);
136
- const nextViewIndex = next.getFullYear() * 12 + next.getMonth();
137
- shouldFocusDayRef.current = true;
138
- if (nextViewIndex !== viewIndex) setViewIndex(nextViewIndex);
139
- setActiveDay(next.getDate());
140
- };
141
- React.useLayoutEffect(() => {
142
- if (!shouldFocusDayRef.current) return;
143
- shouldFocusDayRef.current = false;
144
- gridRef.current?.querySelector(`[data-day="${rovingDay}"]`)?.focus();
145
- }, [viewIndex, rovingDay]);
146
20
  return /* @__PURE__ */ jsxs(
147
21
  "div",
148
22
  {
149
23
  ...rest,
150
- ref: rootRef,
151
24
  style: sx({
152
25
  position: "relative",
153
26
  background: "var(--bg-surface)",
@@ -158,263 +31,17 @@ function DateTimePicker({
158
31
  ...style
159
32
  }),
160
33
  children: [
161
- /* @__PURE__ */ jsxs(
162
- "div",
163
- {
164
- style: sx({
165
- position: "relative",
166
- display: "grid",
167
- gridTemplateColumns: "auto 1fr auto",
168
- alignItems: "center",
169
- gap: "var(--space-1)",
170
- marginBottom: "var(--space-3)"
171
- }),
172
- children: [
173
- /* @__PURE__ */ jsx(IconButton, { label: "M\xEAs anterior", size: "sm", onClick: () => setViewIndex((i) => i - 1), children: /* @__PURE__ */ jsx(ChevronLeft, { size: 18, strokeWidth: 1.75 }) }),
174
- /* @__PURE__ */ jsxs(
175
- "button",
176
- {
177
- ref: titleRef,
178
- type: "button",
179
- className: "sereno-dtp-title",
180
- "aria-haspopup": "dialog",
181
- "aria-expanded": open,
182
- onClick: toggleOpen,
183
- style: sx({
184
- display: "inline-flex",
185
- alignItems: "center",
186
- justifyContent: "center",
187
- gap: 4,
188
- margin: "0 auto",
189
- padding: "4px 8px",
190
- border: "none",
191
- borderRadius: "var(--radius-sm)",
192
- background: "transparent",
193
- cursor: "pointer",
194
- fontFamily: "var(--font-display)",
195
- fontSize: "var(--text-base)",
196
- fontWeight: "var(--weight-bold)",
197
- color: "var(--text-primary)",
198
- textTransform: "capitalize"
199
- }),
200
- children: [
201
- MONTHS[viewMonth],
202
- " ",
203
- viewYear,
204
- /* @__PURE__ */ jsx(ChevronDown, { size: 15, strokeWidth: 2, style: { opacity: 0.6 } })
205
- ]
206
- }
207
- ),
208
- /* @__PURE__ */ jsx(IconButton, { label: "Pr\xF3ximo m\xEAs", size: "sm", onClick: () => setViewIndex((i) => i + 1), children: /* @__PURE__ */ jsx(ChevronRight, { size: 18, strokeWidth: 1.75 }) }),
209
- open && /* @__PURE__ */ jsxs(
210
- "div",
211
- {
212
- ref: popRef,
213
- role: "dialog",
214
- "aria-label": "Escolher m\xEAs e ano",
215
- tabIndex: -1,
216
- onKeyDown: (e) => {
217
- if (e.key === "Escape") {
218
- e.stopPropagation();
219
- closePopover();
220
- }
221
- },
222
- className: "sereno-dtp-pop",
223
- style: sx({
224
- position: "absolute",
225
- top: "calc(100% + 6px)",
226
- left: "50%",
227
- transform: "translateX(-50%)",
228
- zIndex: 20,
229
- width: 248,
230
- maxWidth: "calc(100vw - 24px)",
231
- background: "var(--bg-surface)",
232
- border: "var(--border-width-hairline) solid var(--border-default)",
233
- borderRadius: "var(--radius-md)",
234
- boxShadow: "var(--shadow-lg)",
235
- padding: "var(--space-3)",
236
- outline: "none"
237
- }),
238
- children: [
239
- /* @__PURE__ */ jsxs(
240
- "div",
241
- {
242
- style: sx({
243
- display: "grid",
244
- gridTemplateColumns: "auto 1fr auto",
245
- alignItems: "center",
246
- marginBottom: "var(--space-2)"
247
- }),
248
- children: [
249
- /* @__PURE__ */ jsx(
250
- IconButton,
251
- {
252
- label: picker === "month" ? "Ano anterior" : "Anos anteriores",
253
- size: "sm",
254
- onClick: () => picker === "month" ? setViewIndex((i) => i - 12) : setYearBase((b) => b - 12),
255
- children: /* @__PURE__ */ jsx(ChevronLeft, { size: 18, strokeWidth: 1.75 })
256
- }
257
- ),
258
- picker === "month" ? /* @__PURE__ */ jsx(
259
- "button",
260
- {
261
- type: "button",
262
- className: "sereno-dtp-title",
263
- onClick: () => {
264
- setYearBase(viewYear - 5);
265
- setPicker("year");
266
- },
267
- style: sx({
268
- margin: "0 auto",
269
- padding: "4px 8px",
270
- border: "none",
271
- borderRadius: "var(--radius-sm)",
272
- background: "transparent",
273
- cursor: "pointer",
274
- fontFamily: "var(--font-display)",
275
- fontSize: "var(--text-sm)",
276
- fontWeight: "var(--weight-bold)",
277
- color: "var(--text-primary)"
278
- }),
279
- children: viewYear
280
- }
281
- ) : /* @__PURE__ */ jsxs(
282
- "span",
283
- {
284
- style: sx({
285
- textAlign: "center",
286
- fontFamily: "var(--font-body)",
287
- fontSize: "var(--text-sm)",
288
- fontWeight: "var(--weight-semibold)",
289
- color: "var(--text-secondary)"
290
- }),
291
- children: [
292
- years[0],
293
- "\u2013",
294
- years[11]
295
- ]
296
- }
297
- ),
298
- /* @__PURE__ */ jsx(
299
- IconButton,
300
- {
301
- label: picker === "month" ? "Pr\xF3ximo ano" : "Pr\xF3ximos anos",
302
- size: "sm",
303
- onClick: () => picker === "month" ? setViewIndex((i) => i + 12) : setYearBase((b) => b + 12),
304
- children: /* @__PURE__ */ jsx(ChevronRight, { size: 18, strokeWidth: 1.75 })
305
- }
306
- )
307
- ]
308
- }
309
- ),
310
- /* @__PURE__ */ jsx("div", { className: "sereno-dtp-grid", style: sx({ display: "grid", gridTemplateColumns: "repeat(3, 1fr)", gap: "var(--space-1)" }), children: picker === "month" ? MONTHS_SHORT.map((mo, i) => /* @__PURE__ */ jsx(
311
- "button",
312
- {
313
- type: "button",
314
- "data-current": i === viewMonth || void 0,
315
- "aria-current": i === viewMonth ? "true" : void 0,
316
- onClick: () => {
317
- setViewIndex(viewYear * 12 + i);
318
- closePopover();
319
- },
320
- children: mo
321
- },
322
- mo
323
- )) : years.map((y) => /* @__PURE__ */ jsx(
324
- "button",
325
- {
326
- type: "button",
327
- "data-current": y === viewYear || void 0,
328
- "aria-current": y === viewYear ? "true" : void 0,
329
- onClick: () => {
330
- setViewIndex(y * 12 + viewMonth);
331
- setPicker("month");
332
- },
333
- children: y
334
- },
335
- y
336
- )) })
337
- ]
338
- }
339
- )
340
- ]
341
- }
342
- ),
343
- /* @__PURE__ */ jsx("div", { style: sx({ display: "grid", gridTemplateColumns: "repeat(7,1fr)", gap: "var(--space-1)", marginBottom: "var(--space-1)" }), children: DOW.map((d, i) => /* @__PURE__ */ jsx(
344
- "span",
345
- {
346
- style: sx({
347
- textAlign: "center",
348
- fontFamily: "var(--font-body)",
349
- fontSize: "var(--text-2xs)",
350
- fontWeight: "var(--weight-semibold)",
351
- color: "var(--text-muted)",
352
- letterSpacing: "var(--tracking-caps)"
353
- }),
354
- children: d
355
- },
356
- i
357
- )) }),
358
34
  /* @__PURE__ */ jsx(
359
- "div",
35
+ CalendarGrid,
360
36
  {
361
- ref: gridRef,
362
- onKeyDown: onGridKeyDown,
363
- className: "sereno-dtp-days",
364
- style: sx({ display: "grid", gridTemplateColumns: "repeat(7,1fr)", gap: "var(--space-1)" }),
365
- children: cells.map((d, i) => {
366
- if (d === null) return /* @__PURE__ */ jsx("span", { "aria-hidden": true, style: { height: cellH } }, i);
367
- const off = unavailable.includes(d);
368
- const sel = selectedDate === d && viewIndex === selectionIndex;
369
- const extra = renderDay?.(d);
370
- return /* @__PURE__ */ jsxs(
371
- "button",
372
- {
373
- type: "button",
374
- "data-day": d,
375
- "aria-disabled": off || void 0,
376
- tabIndex: d === rovingDay ? 0 : -1,
377
- "aria-pressed": sel,
378
- onClick: () => {
379
- if (off) return;
380
- setSelectionIndex(viewIndex);
381
- setActiveDay(d);
382
- onSelectDate?.(d);
383
- },
384
- style: sx({
385
- height: cellH,
386
- display: "flex",
387
- flexDirection: "column",
388
- alignItems: "center",
389
- justifyContent: "center",
390
- gap: 1,
391
- borderRadius: "var(--radius-sm)",
392
- border: "none",
393
- padding: 0,
394
- cursor: off ? "not-allowed" : "pointer",
395
- background: sel ? "var(--interactive-primary)" : "transparent",
396
- color: sel ? "var(--interactive-primary-fg)" : off ? "var(--text-disabled)" : "var(--text-primary)",
397
- fontFamily: "var(--font-body)",
398
- fontSize: "var(--text-sm)",
399
- fontWeight: sel ? "var(--weight-bold)" : "var(--weight-medium)",
400
- textDecoration: off ? "line-through" : "none",
401
- transition: "var(--transition-control)"
402
- // Not inline outline:none — that would always beat the
403
- // .sereno-dtp-days:focus-visible rule below no matter what
404
- // it says (inline style always outranks a class selector).
405
- // Suppressed as a class rule instead, same specificity as
406
- // the :focus-visible override that re-enables it.
407
- }),
408
- children: [
409
- /* @__PURE__ */ jsx("span", { children: d }),
410
- renderDay ? /* @__PURE__ */ jsx("span", { style: sx({ display: "flex", minHeight: 12, lineHeight: 1 }), children: extra }) : null
411
- ]
412
- },
413
- i
414
- );
415
- })
416
- },
417
- viewIndex
37
+ year,
38
+ month,
39
+ selectedDate,
40
+ unavailable,
41
+ onSelectDate,
42
+ onMonthChange,
43
+ renderDay
44
+ }
418
45
  ),
419
46
  times.length > 0 && /* @__PURE__ */ jsxs("div", { style: sx({ marginTop: "var(--space-4)", paddingTop: "var(--space-4)", borderTop: "var(--border-width-hairline) solid var(--border-subtle)" }), children: [
420
47
  /* @__PURE__ */ jsx(