nish-ui 2.1.2 → 2.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/README.md +1 -1
- package/dist/base-tailwind.css +3 -1
- package/dist/base.css +3 -1
- package/dist/chart.js +1 -1
- package/dist/elements-all.js +1 -1
- package/dist/elements-charts.js +1 -1
- package/dist/elements-data.js +1 -1
- package/dist/elements-forms.js +1 -1
- package/dist/elements.js +3 -3
- package/dist/esm/components/calendar-base.js +291 -0
- package/dist/esm/components/calendar-classes.js +78 -0
- package/dist/esm/components/calendar.js +22 -254
- package/dist/esm/components/date-range-picker.js +213 -0
- package/dist/esm/components/forms.js +1 -0
- package/dist/esm/components/layout.js +8 -1
- package/dist/esm/components/range-calendar.js +122 -0
- package/dist/esm/components/strings.js +9 -0
- package/dist/esm/components/time-field-classes.js +110 -0
- package/dist/esm/components/time-field.js +308 -0
- package/dist/esm/components/tree-view.js +40 -46
- package/dist/esm/index.js +6 -0
- package/dist/esm/runtime.js +1 -1
- package/dist/format.js +1 -1
- package/dist/index.d.ts +161 -10
- package/dist/nish.css +3 -1
- package/dist/theme.d.ts +18 -0
- package/dist/theme.global.js +10 -1
- package/dist/theme.js +10 -1
- package/package.json +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// src/components/calendar-classes.ts
|
|
2
|
+
import { cn } from "../cn.js";
|
|
2
3
|
var ISO_RE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
3
4
|
var MONTH_RE = /^(\d{4})-(\d{2})$/;
|
|
4
5
|
function pad(n, width) {
|
|
@@ -292,10 +293,80 @@ function fullDate(iso, locale) {
|
|
|
292
293
|
utcDate(p.y, p.m, p.d)
|
|
293
294
|
);
|
|
294
295
|
}
|
|
296
|
+
function orderRange(a, b) {
|
|
297
|
+
return a <= b ? { from: a, to: b } : { from: b, to: a };
|
|
298
|
+
}
|
|
299
|
+
function sameRange(a, b) {
|
|
300
|
+
return a === b || !!a && !!b && a.from === b.from && a.to === b.to;
|
|
301
|
+
}
|
|
302
|
+
function toRange(v) {
|
|
303
|
+
if (!v || typeof v !== "object") return null;
|
|
304
|
+
const { from, to } = v;
|
|
305
|
+
if (typeof from !== "string" || typeof to !== "string" || !parseIso(from) || !parseIso(to)) return null;
|
|
306
|
+
return orderRange(from, to);
|
|
307
|
+
}
|
|
308
|
+
function firstDisabledIn(from, to, isDisabled) {
|
|
309
|
+
let d = from;
|
|
310
|
+
for (let n = 0; n < 3660 && d <= to; n++) {
|
|
311
|
+
if (isDisabled(d)) return d;
|
|
312
|
+
const next = addDays(d, 1);
|
|
313
|
+
if (next === d) break;
|
|
314
|
+
d = next;
|
|
315
|
+
}
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
function formatRangeField(range, locale) {
|
|
319
|
+
return `${formatDateField(range.from, locale)} \u2013 ${formatDateField(range.to, locale)}`;
|
|
320
|
+
}
|
|
321
|
+
function rangePlaceholder(locale, tokens) {
|
|
322
|
+
const one = datePlaceholder(locale, tokens);
|
|
323
|
+
return `${one} \u2013 ${one}`;
|
|
324
|
+
}
|
|
325
|
+
var RANGE_JUNK_RE = /[^\d\s/.,\-–—]/;
|
|
326
|
+
var DIGIT_RUN_RE = /\d+/g;
|
|
327
|
+
function parseRangeField(text, locale) {
|
|
328
|
+
const t = text.replace(BIDI_MARKS_RE, "").trim();
|
|
329
|
+
if (t === "" || RANGE_JUNK_RE.test(t)) return null;
|
|
330
|
+
const runs = [...t.matchAll(DIGIT_RUN_RE)];
|
|
331
|
+
let a;
|
|
332
|
+
let b;
|
|
333
|
+
if (runs.length === 6) {
|
|
334
|
+
a = t.slice(0, runs[2].index + runs[2][0].length);
|
|
335
|
+
b = t.slice(runs[3].index);
|
|
336
|
+
} else if (runs.length === 2 && runs[0][0].length === 8 && runs[1][0].length === 8) {
|
|
337
|
+
a = runs[0][0];
|
|
338
|
+
b = runs[1][0];
|
|
339
|
+
} else if (runs.length === 3 || runs.length === 1 && runs[0][0].length === 8) {
|
|
340
|
+
a = t;
|
|
341
|
+
b = t;
|
|
342
|
+
} else {
|
|
343
|
+
return null;
|
|
344
|
+
}
|
|
345
|
+
const from = parseDateField(a, locale);
|
|
346
|
+
const to = parseDateField(b, locale);
|
|
347
|
+
return from && to ? orderRange(from, to) : null;
|
|
348
|
+
}
|
|
349
|
+
var DAY_BASE = "flex size-10 items-center justify-center rounded-lg text-sm tabular-nums text-th-hi2 transition-colors pointer-coarse:h-11 pointer-coarse:w-[min(2.75rem,calc((100vw_-_2.6rem)/7))]";
|
|
350
|
+
function calendarDayClass({ filled, today, disabled, band = null }) {
|
|
351
|
+
return cn(
|
|
352
|
+
DAY_BASE,
|
|
353
|
+
!filled && !band && !disabled && "hover:bg-th-elevated hover:text-th-hi",
|
|
354
|
+
today && !filled && "font-semibold text-th-accent ring-1 ring-inset ring-th-accent/50",
|
|
355
|
+
band && today && !filled && "ring-th-accent",
|
|
356
|
+
band === "start" && "rounded-e-none",
|
|
357
|
+
band === "end" && "rounded-s-none",
|
|
358
|
+
band === "middle" && "rounded-none",
|
|
359
|
+
band && !filled && "bg-th-accent-soft font-medium text-th-accent forced-colors:bg-[Highlight] forced-colors:text-[HighlightText] forced-colors:forced-color-adjust-none",
|
|
360
|
+
filled && "bg-th-accent font-semibold text-th-on-accent forced-colors:bg-[Highlight] forced-colors:text-[HighlightText] forced-colors:forced-color-adjust-none",
|
|
361
|
+
filled && band && "forced-colors:outline forced-colors:outline-2 forced-colors:-outline-offset-2 forced-colors:outline-[CanvasText]",
|
|
362
|
+
disabled && "cursor-not-allowed text-th-ghost line-through forced-colors:text-[GrayText]"
|
|
363
|
+
);
|
|
364
|
+
}
|
|
295
365
|
export {
|
|
296
366
|
addDays,
|
|
297
367
|
addMonths,
|
|
298
368
|
addYears,
|
|
369
|
+
calendarDayClass,
|
|
299
370
|
calendarKeyTarget,
|
|
300
371
|
clampIso,
|
|
301
372
|
dateFieldParts,
|
|
@@ -303,8 +374,10 @@ export {
|
|
|
303
374
|
dayNumber,
|
|
304
375
|
daysInMonth,
|
|
305
376
|
endOfWeek,
|
|
377
|
+
firstDisabledIn,
|
|
306
378
|
firstOfMonth,
|
|
307
379
|
formatDateField,
|
|
380
|
+
formatRangeField,
|
|
308
381
|
fullDate,
|
|
309
382
|
inRange,
|
|
310
383
|
isoToday,
|
|
@@ -312,13 +385,18 @@ export {
|
|
|
312
385
|
monthNames,
|
|
313
386
|
monthOf,
|
|
314
387
|
monthTitle,
|
|
388
|
+
orderRange,
|
|
315
389
|
parseDateField,
|
|
316
390
|
parseIso,
|
|
317
391
|
parseMonth,
|
|
392
|
+
parseRangeField,
|
|
393
|
+
rangePlaceholder,
|
|
318
394
|
resolveLocale,
|
|
395
|
+
sameRange,
|
|
319
396
|
shiftMonth,
|
|
320
397
|
startOfWeek,
|
|
321
398
|
toIso,
|
|
399
|
+
toRange,
|
|
322
400
|
weekStartFor,
|
|
323
401
|
weekday,
|
|
324
402
|
weekdayNames
|
|
@@ -1,262 +1,30 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
3
|
// src/components/calendar.tsx
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
dayNumber,
|
|
12
|
-
firstOfMonth,
|
|
13
|
-
fullDate,
|
|
14
|
-
inRange,
|
|
15
|
-
isoToday,
|
|
16
|
-
monthGrid,
|
|
17
|
-
monthNames,
|
|
18
|
-
monthOf,
|
|
19
|
-
monthTitle,
|
|
20
|
-
parseIso,
|
|
21
|
-
parseMonth,
|
|
22
|
-
resolveLocale,
|
|
23
|
-
shiftMonth,
|
|
24
|
-
weekStartFor,
|
|
25
|
-
weekdayNames
|
|
26
|
-
} from "./calendar-classes.js";
|
|
27
|
-
import { isRtl, useIsoLayoutEffect } from "./client.js";
|
|
28
|
-
import { ChevronLeftIcon, ChevronRightIcon } from "./icons.js";
|
|
29
|
-
import { IconButton, Select } from "./primitives.js";
|
|
30
|
-
import { useNishConfig, useNishStrings } from "./provider.js";
|
|
31
|
-
import { jsx, jsxs } from "react/jsx-runtime";
|
|
32
|
-
var warned = /* @__PURE__ */ new Set();
|
|
33
|
-
var noopSubscribe = () => () => {
|
|
34
|
-
};
|
|
35
|
-
var EMPTY_GRID = Array.from({ length: 6 }, () => Array.from({ length: 7 }, () => null));
|
|
36
|
-
var pad2 = (n) => String(n).padStart(2, "0");
|
|
37
|
-
var pad4 = (n) => String(n).padStart(4, "0");
|
|
38
|
-
function Calendar({
|
|
39
|
-
value,
|
|
40
|
-
defaultValue,
|
|
41
|
-
onValueChange,
|
|
42
|
-
month: monthProp,
|
|
43
|
-
defaultMonth,
|
|
44
|
-
onMonthChange,
|
|
45
|
-
min: minProp,
|
|
46
|
-
max: maxProp,
|
|
47
|
-
isDateDisabled,
|
|
48
|
-
locale,
|
|
49
|
-
weekStartsOn,
|
|
50
|
-
captionLayout = "label",
|
|
51
|
-
fromYear,
|
|
52
|
-
toYear,
|
|
53
|
-
autoFocus = false,
|
|
54
|
-
previousMonthLabel,
|
|
55
|
-
nextMonthLabel,
|
|
56
|
-
className,
|
|
57
|
-
ref,
|
|
58
|
-
...rest
|
|
59
|
-
}) {
|
|
60
|
-
const strings = useNishStrings();
|
|
61
|
-
const config = useNishConfig();
|
|
62
|
-
const loc = resolveLocale(locale ?? config.locale ?? getDefaultLocale());
|
|
63
|
-
const ws = weekStartsOn ?? weekStartFor(loc);
|
|
64
|
-
const titleId = useId();
|
|
65
|
-
const rootRef = useRef(null);
|
|
66
|
-
const focusPending = useRef(false);
|
|
67
|
-
const check = (prop, v, isMonth = false) => {
|
|
68
|
-
if (v == null) return null;
|
|
69
|
-
const ok = isMonth ? parseMonth(v) !== null : parseIso(v) !== null;
|
|
70
|
-
if (ok) return v;
|
|
71
|
-
if (process.env.NODE_ENV !== "production" && !warned.has(prop)) {
|
|
72
|
-
warned.add(prop);
|
|
73
|
-
console.warn(`Calendar: "${prop}" must be ${isMonth ? "YYYY-MM" : "YYYY-MM-DD"}`);
|
|
74
|
-
}
|
|
75
|
-
return null;
|
|
76
|
-
};
|
|
77
|
-
const valueIso = check("value", value);
|
|
78
|
-
const defaultIso = check("defaultValue", defaultValue);
|
|
79
|
-
const min = check("min", minProp);
|
|
80
|
-
const max = check("max", maxProp);
|
|
81
|
-
const monthControlled = check("month", monthProp, true);
|
|
82
|
-
const defaultMonthIso = check("defaultMonth", defaultMonth, true);
|
|
4
|
+
import { useState } from "react";
|
|
5
|
+
import { monthOf } from "./calendar-classes.js";
|
|
6
|
+
import { CalendarBase, checkIso } from "./calendar-base.js";
|
|
7
|
+
import { jsx } from "react/jsx-runtime";
|
|
8
|
+
function Calendar({ value, defaultValue, onValueChange, ...rest }) {
|
|
9
|
+
const valueIso = checkIso("Calendar", "value", value);
|
|
10
|
+
const defaultIso = checkIso("Calendar", "defaultValue", defaultValue);
|
|
83
11
|
const controlled = value !== void 0;
|
|
84
|
-
const [
|
|
85
|
-
const selected = controlled ? valueIso :
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
onMonthChange?.(next);
|
|
99
|
-
};
|
|
100
|
-
const disabled = (iso) => !inRange(iso, min, max) || isDateDisabled?.(iso) === true;
|
|
101
|
-
const inShown = (iso) => iso !== null && shown !== null && monthOf(iso) === shown;
|
|
102
|
-
let tabStop = null;
|
|
103
|
-
if (shown) {
|
|
104
|
-
if (inShown(focusedDate)) tabStop = focusedDate;
|
|
105
|
-
else if (inShown(selected)) tabStop = selected;
|
|
106
|
-
else if (inShown(today)) tabStop = today;
|
|
107
|
-
else {
|
|
108
|
-
const days = monthGrid(shown, ws).flat().filter((d) => d !== null);
|
|
109
|
-
tabStop = days.find((d) => !disabled(d)) ?? days[0] ?? null;
|
|
12
|
+
const [own, setOwn] = useState(defaultIso);
|
|
13
|
+
const selected = controlled ? valueIso : own;
|
|
14
|
+
return /* @__PURE__ */ jsx(
|
|
15
|
+
CalendarBase,
|
|
16
|
+
{
|
|
17
|
+
...rest,
|
|
18
|
+
componentName: "Calendar",
|
|
19
|
+
initialMonth: selected ? monthOf(selected) : null,
|
|
20
|
+
focusPreference: selected,
|
|
21
|
+
dayLook: (iso) => ({ filled: iso === selected, band: null, ariaSelected: iso === selected }),
|
|
22
|
+
onPick: (iso) => {
|
|
23
|
+
if (!controlled) setOwn(iso);
|
|
24
|
+
onValueChange?.(iso);
|
|
25
|
+
}
|
|
110
26
|
}
|
|
111
|
-
|
|
112
|
-
useIsoLayoutEffect(() => {
|
|
113
|
-
if (!focusPending.current) return;
|
|
114
|
-
focusPending.current = false;
|
|
115
|
-
if (!focusedDate) return;
|
|
116
|
-
rootRef.current?.querySelector(`[data-date="${focusedDate}"]`)?.focus();
|
|
117
|
-
});
|
|
118
|
-
const setRoot = (node) => {
|
|
119
|
-
rootRef.current = node;
|
|
120
|
-
if (typeof ref === "function") ref(node);
|
|
121
|
-
else if (ref) ref.current = node;
|
|
122
|
-
};
|
|
123
|
-
const pick = (iso) => {
|
|
124
|
-
if (disabled(iso)) return;
|
|
125
|
-
setFocusedDate(iso);
|
|
126
|
-
if (!controlled) setOwnValue(iso);
|
|
127
|
-
onValueChange?.(iso);
|
|
128
|
-
};
|
|
129
|
-
const onGridKey = (e) => {
|
|
130
|
-
const from = e.target.closest?.("[data-date]")?.getAttribute("data-date") ?? tabStop;
|
|
131
|
-
if (!from) return;
|
|
132
|
-
const raw = calendarKeyTarget(e.key, e.shiftKey, from, ws, isRtl(e.currentTarget));
|
|
133
|
-
if (!raw) return;
|
|
134
|
-
e.preventDefault();
|
|
135
|
-
const target = clampIso(raw, min ?? "0001-01-01", max ?? "9999-12-31");
|
|
136
|
-
const moved = target !== focusedDate || monthOf(target) !== shown;
|
|
137
|
-
setFocusedDate(target);
|
|
138
|
-
setMonth(monthOf(target));
|
|
139
|
-
if (moved) {
|
|
140
|
-
focusPending.current = true;
|
|
141
|
-
} else {
|
|
142
|
-
e.currentTarget.querySelector(`[data-date="${target}"]`)?.focus();
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
|
-
const grid = shown ? monthGrid(shown, ws) : EMPTY_GRID;
|
|
146
|
-
const names = weekdayNames(loc, ws);
|
|
147
|
-
const dropdowns = captionLayout === "dropdowns";
|
|
148
|
-
const shownYear = shown ? Number(shown.slice(0, 4)) : null;
|
|
149
|
-
const shownMonthNo = shown ? Number(shown.slice(5, 7)) : null;
|
|
150
|
-
const prevDisabled = !shown || shown <= "0001-01" || min !== null && addDays(firstOfMonth(shown), -1) < min;
|
|
151
|
-
const nextDisabled = !shown || shown >= "9999-12" || max !== null && firstOfMonth(shiftMonth(shown, 1)) > max;
|
|
152
|
-
let years = [];
|
|
153
|
-
if (dropdowns) {
|
|
154
|
-
const baseYear = today ? Number(today.slice(0, 4)) : shownYear ?? 2026;
|
|
155
|
-
const from = fromYear ?? (min ? Number(min.slice(0, 4)) : baseYear - 100);
|
|
156
|
-
const to = toYear ?? (max ? Number(max.slice(0, 4)) : baseYear + 10);
|
|
157
|
-
for (let y = from; y <= to; y++) years.push(y);
|
|
158
|
-
if (shownYear !== null && !years.includes(shownYear)) years = [...years, shownYear].sort((a, b) => a - b);
|
|
159
|
-
}
|
|
160
|
-
return /* @__PURE__ */ jsxs("div", { ...rest, ref: setRoot, className: cn("inline-flex select-none flex-col gap-2", className), children: [
|
|
161
|
-
/* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-1", children: [
|
|
162
|
-
/* @__PURE__ */ jsx(
|
|
163
|
-
IconButton,
|
|
164
|
-
{
|
|
165
|
-
size: "sm",
|
|
166
|
-
label: previousMonthLabel ?? strings.previousMonth,
|
|
167
|
-
"aria-disabled": prevDisabled || void 0,
|
|
168
|
-
className: "aria-disabled:cursor-not-allowed aria-disabled:opacity-40",
|
|
169
|
-
onClick: () => !prevDisabled && shown && setMonth(shiftMonth(shown, -1)),
|
|
170
|
-
children: /* @__PURE__ */ jsx(ChevronLeftIcon, { className: "rtl:-scale-x-100" })
|
|
171
|
-
}
|
|
172
|
-
),
|
|
173
|
-
/* @__PURE__ */ jsx("div", { id: titleId, "aria-live": "polite", className: cn("text-sm font-semibold text-th-hi", dropdowns && "sr-only"), children: shown ? monthTitle(shown, loc) : "" }),
|
|
174
|
-
dropdowns && /* @__PURE__ */ jsxs("div", { className: "flex min-w-0 items-center gap-1", children: [
|
|
175
|
-
/* @__PURE__ */ jsx(
|
|
176
|
-
Select,
|
|
177
|
-
{
|
|
178
|
-
size: "sm",
|
|
179
|
-
"aria-label": strings.month,
|
|
180
|
-
value: shownMonthNo ?? "",
|
|
181
|
-
disabled: !shown,
|
|
182
|
-
onChange: (e) => shownYear !== null && setMonth(`${pad4(shownYear)}-${pad2(Number(e.target.value))}`),
|
|
183
|
-
children: monthNames(loc).map((name, i) => /* @__PURE__ */ jsx("option", { value: i + 1, children: name }, i))
|
|
184
|
-
}
|
|
185
|
-
),
|
|
186
|
-
/* @__PURE__ */ jsx(
|
|
187
|
-
Select,
|
|
188
|
-
{
|
|
189
|
-
size: "sm",
|
|
190
|
-
"aria-label": strings.year,
|
|
191
|
-
value: shownYear ?? "",
|
|
192
|
-
disabled: !shown,
|
|
193
|
-
onChange: (e) => shownMonthNo !== null && setMonth(`${pad4(Number(e.target.value))}-${pad2(shownMonthNo)}`),
|
|
194
|
-
children: years.map((y) => /* @__PURE__ */ jsx("option", { value: y, children: y }, y))
|
|
195
|
-
}
|
|
196
|
-
)
|
|
197
|
-
] }),
|
|
198
|
-
/* @__PURE__ */ jsx(
|
|
199
|
-
IconButton,
|
|
200
|
-
{
|
|
201
|
-
size: "sm",
|
|
202
|
-
label: nextMonthLabel ?? strings.nextMonth,
|
|
203
|
-
"aria-disabled": nextDisabled || void 0,
|
|
204
|
-
className: "aria-disabled:cursor-not-allowed aria-disabled:opacity-40",
|
|
205
|
-
onClick: () => !nextDisabled && shown && setMonth(shiftMonth(shown, 1)),
|
|
206
|
-
children: /* @__PURE__ */ jsx(ChevronRightIcon, { className: "rtl:-scale-x-100" })
|
|
207
|
-
}
|
|
208
|
-
)
|
|
209
|
-
] }),
|
|
210
|
-
/* @__PURE__ */ jsxs("table", { role: "grid", "aria-labelledby": titleId, className: "border-collapse", onKeyDown: onGridKey, children: [
|
|
211
|
-
/* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsx("tr", { children: names.map((n) => /* @__PURE__ */ jsx(
|
|
212
|
-
"th",
|
|
213
|
-
{
|
|
214
|
-
scope: "col",
|
|
215
|
-
abbr: n.long,
|
|
216
|
-
className: "size-10 overflow-hidden px-0.5 py-0 text-center text-xs font-medium text-th-hi3 pointer-coarse:h-11 pointer-coarse:w-[min(2.75rem,calc((100vw_-_2.6rem)/7))]",
|
|
217
|
-
children: n.short
|
|
218
|
-
},
|
|
219
|
-
n.long
|
|
220
|
-
)) }) }),
|
|
221
|
-
/* @__PURE__ */ jsx("tbody", { children: grid.map((row, r) => /* @__PURE__ */ jsx("tr", { children: row.map((iso, c) => {
|
|
222
|
-
if (!iso) {
|
|
223
|
-
return /* @__PURE__ */ jsx("td", { className: "p-0", children: /* @__PURE__ */ jsx(
|
|
224
|
-
"div",
|
|
225
|
-
{
|
|
226
|
-
"aria-hidden": "true",
|
|
227
|
-
className: "size-10 pointer-coarse:h-11 pointer-coarse:w-[min(2.75rem,calc((100vw_-_2.6rem)/7))]"
|
|
228
|
-
}
|
|
229
|
-
) }, c);
|
|
230
|
-
}
|
|
231
|
-
const isSelected = iso === selected;
|
|
232
|
-
const isDisabled = disabled(iso);
|
|
233
|
-
const isTabStop = iso === tabStop;
|
|
234
|
-
return /* @__PURE__ */ jsx("td", { role: "gridcell", "aria-selected": isSelected, className: "p-0", children: /* @__PURE__ */ jsx(
|
|
235
|
-
"button",
|
|
236
|
-
{
|
|
237
|
-
type: "button",
|
|
238
|
-
tabIndex: isTabStop ? 0 : -1,
|
|
239
|
-
"data-date": iso,
|
|
240
|
-
"data-autofocus": autoFocus && isTabStop ? "" : void 0,
|
|
241
|
-
"aria-label": fullDate(iso, loc),
|
|
242
|
-
"aria-current": iso === today ? "date" : void 0,
|
|
243
|
-
"aria-disabled": isDisabled || void 0,
|
|
244
|
-
"data-state": isSelected ? "active" : "inactive",
|
|
245
|
-
"data-disabled": isDisabled ? "" : void 0,
|
|
246
|
-
onClick: () => pick(iso),
|
|
247
|
-
className: cn(
|
|
248
|
-
"flex size-10 items-center justify-center rounded-lg text-sm tabular-nums text-th-hi2 transition-colors pointer-coarse:h-11 pointer-coarse:w-[min(2.75rem,calc((100vw_-_2.6rem)/7))]",
|
|
249
|
-
!isSelected && !isDisabled && "hover:bg-th-elevated hover:text-th-hi",
|
|
250
|
-
iso === today && !isSelected && "font-semibold text-th-accent ring-1 ring-inset ring-th-accent/50",
|
|
251
|
-
isSelected && "bg-th-accent font-semibold text-th-on-accent forced-colors:bg-[Highlight] forced-colors:text-[HighlightText] forced-colors:forced-color-adjust-none",
|
|
252
|
-
isDisabled && "cursor-not-allowed text-th-ghost line-through forced-colors:text-[GrayText]"
|
|
253
|
-
),
|
|
254
|
-
children: dayNumber(iso, loc)
|
|
255
|
-
}
|
|
256
|
-
) }, c);
|
|
257
|
-
}) }, r)) })
|
|
258
|
-
] })
|
|
259
|
-
] });
|
|
27
|
+
);
|
|
260
28
|
}
|
|
261
29
|
export {
|
|
262
30
|
Calendar
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/date-range-picker.tsx
|
|
4
|
+
import { useEffect, useId, useRef, useState } from "react";
|
|
5
|
+
import { cn } from "../cn.js";
|
|
6
|
+
import { getDefaultLocale } from "../format.js";
|
|
7
|
+
import {
|
|
8
|
+
firstDisabledIn,
|
|
9
|
+
formatRangeField,
|
|
10
|
+
fullDate,
|
|
11
|
+
inRange,
|
|
12
|
+
parseIso,
|
|
13
|
+
parseRangeField,
|
|
14
|
+
rangePlaceholder,
|
|
15
|
+
resolveLocale,
|
|
16
|
+
sameRange,
|
|
17
|
+
toRange
|
|
18
|
+
} from "./calendar-classes.js";
|
|
19
|
+
import { setRef } from "./forms-classes.js";
|
|
20
|
+
import { Popover } from "./popover.js";
|
|
21
|
+
import { CalendarIcon } from "./icons.js";
|
|
22
|
+
import { RangeCalendar } from "./range-calendar.js";
|
|
23
|
+
import { fieldClass } from "./primitives-classes.js";
|
|
24
|
+
import { useNishConfig, useNishStrings } from "./provider.js";
|
|
25
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
26
|
+
function DateRangePicker({
|
|
27
|
+
ref,
|
|
28
|
+
value: valueProp,
|
|
29
|
+
defaultValue = null,
|
|
30
|
+
onValueChange,
|
|
31
|
+
min: minProp,
|
|
32
|
+
max: maxProp,
|
|
33
|
+
isDateDisabled,
|
|
34
|
+
allowDisabledInRange = false,
|
|
35
|
+
locale,
|
|
36
|
+
weekStartsOn,
|
|
37
|
+
captionLayout,
|
|
38
|
+
placeholder,
|
|
39
|
+
"aria-label": ariaLabel,
|
|
40
|
+
"aria-labelledby": ariaLabelledBy,
|
|
41
|
+
"aria-describedby": ariaDescribedBy,
|
|
42
|
+
"aria-invalid": ariaInvalid,
|
|
43
|
+
invalid,
|
|
44
|
+
id,
|
|
45
|
+
name,
|
|
46
|
+
form,
|
|
47
|
+
required,
|
|
48
|
+
disabled = false,
|
|
49
|
+
size = "md",
|
|
50
|
+
calendarLabel,
|
|
51
|
+
className,
|
|
52
|
+
...rest
|
|
53
|
+
}) {
|
|
54
|
+
const strings = useNishStrings();
|
|
55
|
+
const config = useNishConfig();
|
|
56
|
+
const loc = resolveLocale(locale ?? config.locale ?? getDefaultLocale());
|
|
57
|
+
const autoId = useId().replace(/[^a-zA-Z0-9_-]/g, "");
|
|
58
|
+
const inputId = id ?? `nish-drp-${autoId}`;
|
|
59
|
+
const min = parseIso(minProp) ? minProp : null;
|
|
60
|
+
const max = parseIso(maxProp) ? maxProp : null;
|
|
61
|
+
const controlled = valueProp !== void 0;
|
|
62
|
+
const [own, setOwn] = useState(() => toRange(defaultValue));
|
|
63
|
+
const value = controlled ? toRange(valueProp) : own;
|
|
64
|
+
const [open, setOpen] = useState(false);
|
|
65
|
+
const [text, setText] = useState("");
|
|
66
|
+
const [dirty, setDirty] = useState(false);
|
|
67
|
+
const [badText, setBadText] = useState(false);
|
|
68
|
+
const inputRef = useRef(null);
|
|
69
|
+
const buttonRef = useRef(null);
|
|
70
|
+
const shown = dirty ? text : value ? formatRangeField(value, loc) : "";
|
|
71
|
+
const isInvalid = invalid === true || ariaInvalid === true || ariaInvalid === "true" || badText;
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
inputRef.current?.setCustomValidity(badText ? strings.invalidDateRange : "");
|
|
74
|
+
}, [badText, strings.invalidDateRange]);
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
const f = inputRef.current?.form;
|
|
77
|
+
if (!f || controlled) return;
|
|
78
|
+
const onReset = () => {
|
|
79
|
+
setOwn(toRange(defaultValue));
|
|
80
|
+
setDirty(false);
|
|
81
|
+
setBadText(false);
|
|
82
|
+
};
|
|
83
|
+
f.addEventListener("reset", onReset);
|
|
84
|
+
return () => f.removeEventListener("reset", onReset);
|
|
85
|
+
}, [name, form, controlled, defaultValue]);
|
|
86
|
+
const commit = (v) => {
|
|
87
|
+
if (!controlled) setOwn(v);
|
|
88
|
+
onValueChange?.(v);
|
|
89
|
+
};
|
|
90
|
+
const commitText = () => {
|
|
91
|
+
if (!dirty) return;
|
|
92
|
+
if (text.trim() === "") {
|
|
93
|
+
if (value !== null) commit(null);
|
|
94
|
+
setDirty(false);
|
|
95
|
+
setBadText(false);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const r = parseRangeField(text, loc);
|
|
99
|
+
if (r && inRange(r.from, min, max) && inRange(r.to, min, max) && isDateDisabled?.(r.from) !== true && isDateDisabled?.(r.to) !== true && (allowDisabledInRange || !isDateDisabled || firstDisabledIn(r.from, r.to, isDateDisabled) === null)) {
|
|
100
|
+
if (!sameRange(r, value)) commit(r);
|
|
101
|
+
setDirty(false);
|
|
102
|
+
setBadText(false);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
setBadText(true);
|
|
106
|
+
};
|
|
107
|
+
const pickFromCalendar = (r) => {
|
|
108
|
+
commit(r);
|
|
109
|
+
setDirty(false);
|
|
110
|
+
setBadText(false);
|
|
111
|
+
setOpen(false);
|
|
112
|
+
};
|
|
113
|
+
const onKeyDown = (e) => {
|
|
114
|
+
if (disabled) return;
|
|
115
|
+
if (e.key === "Enter") {
|
|
116
|
+
if (dirty) {
|
|
117
|
+
e.preventDefault();
|
|
118
|
+
commitText();
|
|
119
|
+
}
|
|
120
|
+
} else if (e.key === "ArrowDown" && e.altKey) {
|
|
121
|
+
e.preventDefault();
|
|
122
|
+
setOpen(true);
|
|
123
|
+
} else if (e.key === "Escape" && dirty) {
|
|
124
|
+
e.preventDefault();
|
|
125
|
+
e.stopPropagation();
|
|
126
|
+
setDirty(false);
|
|
127
|
+
setBadText(false);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
const buttonLabel = calendarLabel ?? (value ? strings.changeDates(strings.dateRange(fullDate(value.from, loc), fullDate(value.to, loc))) : strings.chooseDates);
|
|
131
|
+
return /* @__PURE__ */ jsxs(
|
|
132
|
+
"div",
|
|
133
|
+
{
|
|
134
|
+
...rest,
|
|
135
|
+
"data-state": open ? "open" : "closed",
|
|
136
|
+
"data-disabled": disabled ? "" : void 0,
|
|
137
|
+
className: cn("relative w-full min-w-0", className),
|
|
138
|
+
children: [
|
|
139
|
+
/* @__PURE__ */ jsx(
|
|
140
|
+
"input",
|
|
141
|
+
{
|
|
142
|
+
ref: (node) => {
|
|
143
|
+
inputRef.current = node;
|
|
144
|
+
setRef(ref, node);
|
|
145
|
+
},
|
|
146
|
+
id: inputId,
|
|
147
|
+
type: "text",
|
|
148
|
+
inputMode: "numeric",
|
|
149
|
+
autoComplete: "off",
|
|
150
|
+
spellCheck: false,
|
|
151
|
+
value: shown,
|
|
152
|
+
placeholder: placeholder ?? rangePlaceholder(loc, {
|
|
153
|
+
day: strings.dayToken,
|
|
154
|
+
month: strings.monthToken,
|
|
155
|
+
year: strings.yearToken
|
|
156
|
+
}),
|
|
157
|
+
disabled,
|
|
158
|
+
required,
|
|
159
|
+
"aria-label": ariaLabel,
|
|
160
|
+
"aria-labelledby": ariaLabelledBy,
|
|
161
|
+
"aria-describedby": ariaDescribedBy,
|
|
162
|
+
"aria-invalid": isInvalid || void 0,
|
|
163
|
+
"aria-required": required || void 0,
|
|
164
|
+
onChange: (e) => {
|
|
165
|
+
setText(e.target.value);
|
|
166
|
+
setDirty(true);
|
|
167
|
+
},
|
|
168
|
+
onBlur: commitText,
|
|
169
|
+
onKeyDown,
|
|
170
|
+
className: fieldClass({
|
|
171
|
+
size,
|
|
172
|
+
invalid: isInvalid,
|
|
173
|
+
className: "pe-11 tabular-nums"
|
|
174
|
+
})
|
|
175
|
+
}
|
|
176
|
+
),
|
|
177
|
+
name != null ? /* @__PURE__ */ jsx("input", { type: "hidden", name, value: value ? `${value.from}/${value.to}` : "", form, disabled }) : null,
|
|
178
|
+
/* @__PURE__ */ jsx(
|
|
179
|
+
"button",
|
|
180
|
+
{
|
|
181
|
+
ref: buttonRef,
|
|
182
|
+
type: "button",
|
|
183
|
+
"aria-label": buttonLabel,
|
|
184
|
+
"aria-haspopup": "dialog",
|
|
185
|
+
"aria-expanded": open,
|
|
186
|
+
disabled,
|
|
187
|
+
onClick: () => setOpen((o) => !o),
|
|
188
|
+
className: "absolute inset-y-0 end-0 flex w-11 items-center justify-center text-th-hi3 hover:text-th-hi disabled:pointer-events-none disabled:opacity-50",
|
|
189
|
+
children: /* @__PURE__ */ jsx("span", { className: "flex size-8 items-center justify-center rounded-md hover:bg-th-elevated", children: /* @__PURE__ */ jsx(CalendarIcon, { size: 16 }) })
|
|
190
|
+
}
|
|
191
|
+
),
|
|
192
|
+
/* @__PURE__ */ jsx(Popover, { open, onOpenChange: (o) => setOpen(o), anchor: buttonRef, align: "end", "aria-label": strings.chooseDates, className: "p-3", children: /* @__PURE__ */ jsx(
|
|
193
|
+
RangeCalendar,
|
|
194
|
+
{
|
|
195
|
+
value,
|
|
196
|
+
onValueChange: pickFromCalendar,
|
|
197
|
+
min: minProp,
|
|
198
|
+
max: maxProp,
|
|
199
|
+
isDateDisabled,
|
|
200
|
+
allowDisabledInRange,
|
|
201
|
+
locale: loc,
|
|
202
|
+
weekStartsOn,
|
|
203
|
+
captionLayout,
|
|
204
|
+
autoFocus: true
|
|
205
|
+
}
|
|
206
|
+
) })
|
|
207
|
+
]
|
|
208
|
+
}
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
export {
|
|
212
|
+
DateRangePicker
|
|
213
|
+
};
|
|
@@ -148,7 +148,14 @@ function PageContainer({
|
|
|
148
148
|
Tag,
|
|
149
149
|
{
|
|
150
150
|
ref,
|
|
151
|
-
className: cn(
|
|
151
|
+
className: cn(
|
|
152
|
+
"mx-auto w-full px-[var(--nish-page-gutter)]",
|
|
153
|
+
CONTAINER_SIZE[size],
|
|
154
|
+
// The bottom padding is a utility, not only .nish-pad-bottom-inset: that class is in the
|
|
155
|
+
// base layer, so a py-6 utility here used to beat it and the page ended under the BottomNav.
|
|
156
|
+
padBottom ? "nish-pad-bottom-inset pt-6 pb-[max(calc(var(--nish-bottom-inset,0px)+1rem),calc(1rem+env(safe-area-inset-bottom)))]" : "py-6",
|
|
157
|
+
className
|
|
158
|
+
),
|
|
152
159
|
...rest,
|
|
153
160
|
children
|
|
154
161
|
}
|