@dinachi/cli 1.0.0 → 1.3.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 +69 -0
- package/dist/index.js +244 -72
- package/package.json +5 -1
- package/templates/accordion/accordion.tsx +5 -1
- package/templates/agent/agent.tsx +35 -18
- package/templates/alert/alert.tsx +345 -0
- package/templates/alert/index.ts +16 -0
- package/templates/alert-dialog/alert-dialog.tsx +14 -3
- package/templates/animated-icon/animated-icon.tsx +1 -0
- package/templates/animated-list/animated-list.tsx +2 -1
- package/templates/animated-tabs/animated-tabs.tsx +4 -0
- package/templates/ask-bar/ask-bar.tsx +1061 -0
- package/templates/ask-bar/index.ts +1 -0
- package/templates/autocomplete/autocomplete.tsx +14 -4
- package/templates/avatar/avatar.tsx +3 -0
- package/templates/badge/badge.tsx +1 -0
- package/templates/breadcrumb/breadcrumb.tsx +606 -0
- package/templates/breadcrumb/index.ts +9 -0
- package/templates/button/button.tsx +1 -0
- package/templates/calendar/calendar.tsx +1155 -0
- package/templates/calendar/index.ts +15 -0
- package/templates/card/card.tsx +6 -1
- package/templates/carousel/carousel.tsx +7 -0
- package/templates/checkbox/checkbox.tsx +1 -0
- package/templates/checkbox-group/checkbox-group.tsx +1 -0
- package/templates/collapsible/collapsible.tsx +3 -1
- package/templates/combobox/combobox.tsx +15 -4
- package/templates/command-items/command-items.tsx +69 -0
- package/templates/command-items/index.ts +1 -0
- package/templates/command-palette/command-palette.tsx +954 -0
- package/templates/command-palette/index.ts +1 -0
- package/templates/compare-slider/compare-slider.tsx +1 -0
- package/templates/comparison-table/comparison-table.tsx +540 -0
- package/templates/comparison-table/index.ts +15 -0
- package/templates/context-menu/context-menu.tsx +61 -14
- package/templates/data-table/data-table.tsx +2077 -0
- package/templates/data-table/index.ts +14 -0
- package/templates/date-picker/date-picker.tsx +527 -0
- package/templates/date-picker/index.ts +2 -0
- package/templates/date-range-picker/date-range-picker.tsx +504 -0
- package/templates/date-range-picker/index.ts +2 -0
- package/templates/detail-list/detail-list.tsx +419 -0
- package/templates/detail-list/index.ts +12 -0
- package/templates/dialog/dialog.tsx +13 -3
- package/templates/drawer/drawer.tsx +19 -6
- package/templates/expandable-card/expandable-card.tsx +5 -1
- package/templates/field/field.tsx +5 -1
- package/templates/fieldset/fieldset.tsx +2 -0
- package/templates/form/form.tsx +1 -0
- package/templates/heat-table/heat-table.tsx +613 -0
- package/templates/heat-table/index.ts +9 -0
- package/templates/hold-to-confirm/hold-to-confirm.tsx +1 -0
- package/templates/input/input.tsx +1 -0
- package/templates/label/label.tsx +1 -0
- package/templates/link/link.tsx +1 -0
- package/templates/load-transition/load-transition.tsx +1 -0
- package/templates/marquee/marquee.tsx +1 -0
- package/templates/menu/menu.tsx +27 -6
- package/templates/menubar/menubar.tsx +15 -3
- package/templates/meter/meter.tsx +5 -1
- package/templates/month-picker/index.ts +1 -0
- package/templates/month-picker/month-picker.tsx +351 -0
- package/templates/navigation-menu/navigation-menu.tsx +13 -2
- package/templates/number-field/number-field.tsx +7 -1
- package/templates/number-ticker/number-ticker.tsx +3 -0
- package/templates/omnibar/index.ts +1 -0
- package/templates/omnibar/omnibar.tsx +1009 -0
- package/templates/otp-field/otp-field.tsx +4 -0
- package/templates/pagination/index.ts +8 -0
- package/templates/pagination/pagination.tsx +389 -0
- package/templates/popover/popover.tsx +19 -5
- package/templates/preview-card/preview-card.tsx +11 -3
- package/templates/progress/progress.tsx +5 -1
- package/templates/progress-ring/progress-ring.tsx +1 -0
- package/templates/radio/radio.tsx +3 -0
- package/templates/scroll-area/scroll-area.tsx +14 -4
- package/templates/scroll-progress/scroll-progress.tsx +1 -0
- package/templates/scroll-reveal/scroll-reveal.tsx +1 -0
- package/templates/select/select.tsx +18 -2
- package/templates/separator/separator.tsx +1 -0
- package/templates/skeleton/skeleton.tsx +1 -0
- package/templates/slider/slider.tsx +7 -0
- package/templates/sortable/sortable.tsx +3 -0
- package/templates/stagger-list/stagger-list.tsx +2 -0
- package/templates/streaming-text/streaming-text.tsx +1 -0
- package/templates/swipeable-row/swipeable-row.tsx +1 -0
- package/templates/switch/switch.tsx +2 -0
- package/templates/table/index.ts +13 -0
- package/templates/table/table.tsx +265 -0
- package/templates/tabs/tabs.tsx +5 -0
- package/templates/text/text.tsx +1 -0
- package/templates/text-morph/text-morph.tsx +2 -0
- package/templates/text-shimmer/text-shimmer.tsx +1 -0
- package/templates/textarea/textarea.tsx +1 -0
- package/templates/time-slots/index.ts +1 -0
- package/templates/time-slots/time-slots.tsx +589 -0
- package/templates/toast/toast.tsx +10 -1
- package/templates/toggle/toggle.tsx +1 -0
- package/templates/toggle-group/toggle-group.tsx +2 -0
- package/templates/toolbar/toolbar.tsx +6 -0
- package/templates/tooltip/tooltip.tsx +9 -3
- package/templates/week-strip/index.ts +1 -0
- package/templates/week-strip/week-strip.tsx +329 -0
- package/templates/year-view/index.ts +1 -0
- package/templates/year-view/year-view.tsx +362 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export {
|
|
2
|
+
DataTable,
|
|
3
|
+
DataTableAction,
|
|
4
|
+
DataTableRowTrigger,
|
|
5
|
+
createColumnHelper,
|
|
6
|
+
type ColumnDef,
|
|
7
|
+
type DataTableChangeDetails,
|
|
8
|
+
type DataTableEmptyContext,
|
|
9
|
+
type DataTableFacetOption,
|
|
10
|
+
type DataTableInstance,
|
|
11
|
+
type DataTableProps,
|
|
12
|
+
type DataTableSelection,
|
|
13
|
+
type DataTableState,
|
|
14
|
+
} from "./data-table"
|
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { Input as BaseInput } from "@base-ui/react/input";
|
|
5
|
+
import { format, type Locale } from "date-fns";
|
|
6
|
+
import { CalendarDays } from "lucide-react";
|
|
7
|
+
import { cn } from "@/lib/utils";
|
|
8
|
+
import { type Agent, AgentProvider, changeAsAgent, useAgentControl, useAgentState, useMergedRef } from "@/components/ui/agent";
|
|
9
|
+
import { Button } from "@/components/ui/button";
|
|
10
|
+
import {
|
|
11
|
+
Calendar,
|
|
12
|
+
type CalendarChangeDetails,
|
|
13
|
+
addDays,
|
|
14
|
+
dayFirst,
|
|
15
|
+
daysBetween,
|
|
16
|
+
fromISODate,
|
|
17
|
+
isSameDay,
|
|
18
|
+
parseDate as readDate,
|
|
19
|
+
startOfDay,
|
|
20
|
+
toISODate,
|
|
21
|
+
} from "@/components/ui/calendar";
|
|
22
|
+
import { Drawer, DrawerContent, DrawerTitle, DrawerTrigger } from "@/components/ui/drawer";
|
|
23
|
+
import { Popover, PopoverPopup, PopoverPortal, PopoverPositioner, PopoverTrigger } from "@/components/ui/popover";
|
|
24
|
+
|
|
25
|
+
/** The picker is one control to an agent, so the parts inside it don't register their own. */
|
|
26
|
+
const Unlisted = ({ children }: { children: React.ReactNode }) => (
|
|
27
|
+
<AgentProvider agent={null as unknown as Agent}>{children}</AgentProvider>
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
const WIDE = "(min-width: 768px)";
|
|
31
|
+
|
|
32
|
+
/** `popover` from 768px up, `drawer` below, unless one is asked for. */
|
|
33
|
+
function usePresentation(presentation: "auto" | "popover" | "drawer"): "popover" | "drawer" {
|
|
34
|
+
const wide = React.useSyncExternalStore(
|
|
35
|
+
(onChange) => {
|
|
36
|
+
const query = window.matchMedia?.(WIDE);
|
|
37
|
+
query?.addEventListener("change", onChange);
|
|
38
|
+
return () => query?.removeEventListener("change", onChange);
|
|
39
|
+
},
|
|
40
|
+
() => window.matchMedia?.(WIDE).matches ?? true,
|
|
41
|
+
() => true
|
|
42
|
+
);
|
|
43
|
+
return presentation === "auto" ? (wide ? "popover" : "drawer") : presentation;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function changeDetails(reason: string, event?: Event): CalendarChangeDetails {
|
|
47
|
+
const details: CalendarChangeDetails = {
|
|
48
|
+
reason,
|
|
49
|
+
event,
|
|
50
|
+
isCanceled: false,
|
|
51
|
+
cancel() {
|
|
52
|
+
details.isCanceled = true;
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
return details;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** How far `day` is from today in words the browser's language uses: tomorrow, in 4 days. */
|
|
59
|
+
function relative(day: Date, today: Date, locale?: Locale): string {
|
|
60
|
+
const n = daysBetween(today, day);
|
|
61
|
+
const words = new Intl.RelativeTimeFormat(locale?.code ?? "en-US", { numeric: "auto" });
|
|
62
|
+
if (Math.abs(n) < 60) return words.format(n, "day");
|
|
63
|
+
// Whole months and years passed, as an age is counted: 53 years and 8 months is 53 years.
|
|
64
|
+
if (Math.abs(n) < 730) return words.format(Math.trunc(n / 30.44), "month");
|
|
65
|
+
return words.format(Math.trunc(n / 365.25), "year");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface DatePickerPreset {
|
|
69
|
+
label: string;
|
|
70
|
+
value: Date;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface DatePickerProps
|
|
74
|
+
extends Omit<React.ComponentProps<"input">, "value" | "defaultValue" | "onChange" | "min" | "max" | "type" | "dir" | "children" | "size"> {
|
|
75
|
+
value?: Date | null;
|
|
76
|
+
defaultValue?: Date | null;
|
|
77
|
+
onValueChange?: (value: Date | null, details: CalendarChangeDetails) => void;
|
|
78
|
+
/** The earliest day that can be picked or typed. */
|
|
79
|
+
min?: Date;
|
|
80
|
+
/** The latest day that can be picked or typed. */
|
|
81
|
+
max?: Date;
|
|
82
|
+
/** Days that can't be picked, besides those outside `min` and `max`. */
|
|
83
|
+
isDateDisabled?: (date: Date) => boolean;
|
|
84
|
+
weekNumbers?: boolean;
|
|
85
|
+
/** A date-fns locale, for names, the first day of the week and how 3/12 is read. */
|
|
86
|
+
locale?: Locale;
|
|
87
|
+
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
88
|
+
dir?: "ltr" | "rtl";
|
|
89
|
+
/** Defaults to the current day. */
|
|
90
|
+
today?: Date;
|
|
91
|
+
/** Replaces the parser, for other languages or formats. Return null for text that isn't a date. */
|
|
92
|
+
parseDate?: (text: string, options: { today: Date; min?: Date; max?: Date }) => Date | null;
|
|
93
|
+
/** How a committed day reads in the field. Defaults to `Fri, 21 Nov 2026`. */
|
|
94
|
+
formatDate?: (date: Date) => string;
|
|
95
|
+
/** One-tap answers shown on phones. Defaults to today, tomorrow, Saturday, next Monday and in a week. */
|
|
96
|
+
presets?: DatePickerPreset[];
|
|
97
|
+
/** `auto` uses a popover from 768px up and a drawer below. */
|
|
98
|
+
presentation?: "auto" | "popover" | "drawer";
|
|
99
|
+
/** `dropdown` gives the calendar month and year selects, for days years away such as a birth date. */
|
|
100
|
+
captionLayout?: "label" | "dropdown";
|
|
101
|
+
/** Names the calendar's popover and titles the drawer. */
|
|
102
|
+
label?: string;
|
|
103
|
+
/** The drawer's confirm button. */
|
|
104
|
+
confirmLabel?: string;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
interface Reading {
|
|
108
|
+
kind: "empty" | "set" | "preview" | "error";
|
|
109
|
+
date?: Date;
|
|
110
|
+
message?: string;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* A date field you type into first: next fri, 21 nov and in 2 weeks all read. The day it
|
|
115
|
+
* reads shows under the field before it commits, on Enter or on leaving the field. The
|
|
116
|
+
* calendar is one button away, in a popover on wide screens and a drawer on phones.
|
|
117
|
+
*/
|
|
118
|
+
const DatePicker = React.forwardRef<HTMLInputElement, DatePickerProps>((props, ref) => {
|
|
119
|
+
const {
|
|
120
|
+
value: valueProp,
|
|
121
|
+
defaultValue,
|
|
122
|
+
onValueChange,
|
|
123
|
+
min: minProp,
|
|
124
|
+
max: maxProp,
|
|
125
|
+
isDateDisabled,
|
|
126
|
+
weekNumbers,
|
|
127
|
+
locale,
|
|
128
|
+
weekStartsOn = locale?.options?.weekStartsOn ?? 1,
|
|
129
|
+
dir,
|
|
130
|
+
today: todayProp,
|
|
131
|
+
parseDate,
|
|
132
|
+
formatDate,
|
|
133
|
+
presets: presetsProp,
|
|
134
|
+
presentation: presentationProp = "auto",
|
|
135
|
+
captionLayout,
|
|
136
|
+
label = "Choose a date",
|
|
137
|
+
confirmLabel = "Set date",
|
|
138
|
+
name,
|
|
139
|
+
required,
|
|
140
|
+
disabled,
|
|
141
|
+
readOnly,
|
|
142
|
+
placeholder = "next fri or 21 nov",
|
|
143
|
+
className,
|
|
144
|
+
onBlur,
|
|
145
|
+
onKeyDown,
|
|
146
|
+
"aria-describedby": describedBy,
|
|
147
|
+
...inputProps
|
|
148
|
+
} = props;
|
|
149
|
+
|
|
150
|
+
const today = React.useMemo(() => startOfDay(todayProp ?? new Date()), [todayProp ? toISODate(todayProp) : null]);
|
|
151
|
+
const min = minProp && startOfDay(minProp);
|
|
152
|
+
const max = maxProp && startOfDay(maxProp);
|
|
153
|
+
const presentation = usePresentation(presentationProp);
|
|
154
|
+
const [value, change] = useAgentState<Date | null, CalendarChangeDetails>(valueProp, defaultValue ?? null, onValueChange);
|
|
155
|
+
const [open, setOpenState] = React.useState(false);
|
|
156
|
+
// Each opening starts the drawer's draft afresh, from the value as it is then.
|
|
157
|
+
const [round, setRound] = React.useState(0);
|
|
158
|
+
const setOpen = (next: boolean) => {
|
|
159
|
+
if (next && !open) setRound((r) => r + 1);
|
|
160
|
+
setOpenState(next);
|
|
161
|
+
};
|
|
162
|
+
const [focused, setFocused] = React.useState(false);
|
|
163
|
+
const input = React.useRef<HTMLInputElement>(null);
|
|
164
|
+
const box = React.useRef<HTMLDivElement>(null);
|
|
165
|
+
const popup = React.useRef<HTMLDivElement>(null);
|
|
166
|
+
const hintId = React.useId();
|
|
167
|
+
|
|
168
|
+
const fmt = formatDate ?? ((d: Date) => format(d, "EEE, d MMM yyyy", { locale }));
|
|
169
|
+
const read = (text: string) =>
|
|
170
|
+
parseDate
|
|
171
|
+
? parseDate(text, { today, min, max })
|
|
172
|
+
: readDate(text, { today, order: dayFirst(locale) ? "day-first" : "month-first", weekStartsOn, min, max });
|
|
173
|
+
|
|
174
|
+
/** Why `d` can't be picked, in the words the field shows. */
|
|
175
|
+
const refusal = (d: Date): string | null => {
|
|
176
|
+
if (min && d < min) return isSameDay(min, today) ? `${fmt(d)} has passed.` : `The earliest is ${fmt(min)}.`;
|
|
177
|
+
if (max && d > max) return `The latest is ${fmt(max)}.`;
|
|
178
|
+
if (isDateDisabled?.(d)) return `${fmt(d)} isn't available.`;
|
|
179
|
+
return null;
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/** While `typing`, a word of two letters is on its way rather than wrong. */
|
|
183
|
+
const reading = (text: string, current: Date | null, typing = true): Reading => {
|
|
184
|
+
const t = text.trim();
|
|
185
|
+
if (!t) return { kind: "empty" };
|
|
186
|
+
if (current && text === fmt(current)) {
|
|
187
|
+
const why = refusal(current);
|
|
188
|
+
return why ? { kind: "error", message: why } : { kind: "set", date: current };
|
|
189
|
+
}
|
|
190
|
+
const d = read(t);
|
|
191
|
+
if (!d) return t.length > 2 || !typing ? { kind: "error", message: "Not a date. Try 21 nov or next fri." } : { kind: "empty" };
|
|
192
|
+
const why = refusal(d);
|
|
193
|
+
return why ? { kind: "error", message: why } : { kind: "preview", date: d };
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/* ---- the field ---- */
|
|
197
|
+
const [text, setText] = React.useState(() => (value ? fmt(value) : ""));
|
|
198
|
+
// A value set from outside, by the app or an agent, rewrites the field.
|
|
199
|
+
const key = value ? toISODate(value) : "";
|
|
200
|
+
const [seen, setSeen] = React.useState(key);
|
|
201
|
+
if (seen !== key) {
|
|
202
|
+
setSeen(key);
|
|
203
|
+
setText(value ? fmt(value) : "");
|
|
204
|
+
}
|
|
205
|
+
const now = reading(text, value, focused);
|
|
206
|
+
|
|
207
|
+
React.useEffect(() => {
|
|
208
|
+
input.current?.setCustomValidity(now.kind === "error" ? now.message! : "");
|
|
209
|
+
}, [now.kind, now.message]);
|
|
210
|
+
|
|
211
|
+
const commit = (d: Date | null, details: CalendarChangeDetails) => {
|
|
212
|
+
if (!isSameDay(d, value) && (d || value)) {
|
|
213
|
+
change(d, details);
|
|
214
|
+
// The app said no, so the field goes back to what it holds.
|
|
215
|
+
if (details.isCanceled) d = value;
|
|
216
|
+
}
|
|
217
|
+
setText(d ? fmt(d) : "");
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
/** Commits what the field reads, if it reads as a day or as nothing. */
|
|
221
|
+
const settle = (event?: Event): boolean => {
|
|
222
|
+
if (now.kind === "set") return true;
|
|
223
|
+
if (now.kind === "error") return false;
|
|
224
|
+
if (now.kind === "empty" && text.trim()) return false;
|
|
225
|
+
commit(now.date ?? null, changeDetails("input-commit", event));
|
|
226
|
+
return true;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
/* ---- agent ---- */
|
|
230
|
+
useAgentControl(() => ({
|
|
231
|
+
kind: "date",
|
|
232
|
+
element: input.current,
|
|
233
|
+
value: value ? toISODate(value) : undefined,
|
|
234
|
+
disabled,
|
|
235
|
+
required,
|
|
236
|
+
invalid: now.kind === "error" || undefined,
|
|
237
|
+
errors: now.kind === "error" ? [now.message!] : undefined,
|
|
238
|
+
actions: {
|
|
239
|
+
setValue: (wanted: unknown) => {
|
|
240
|
+
if (readOnly) return "The field is read-only.";
|
|
241
|
+
if (wanted === null || wanted === "") return changeAsAgent(commit, null);
|
|
242
|
+
const d =
|
|
243
|
+
wanted instanceof Date
|
|
244
|
+
? isNaN(wanted.getTime()) ? null : startOfDay(wanted)
|
|
245
|
+
: typeof wanted === "string" ? fromISODate(wanted) ?? read(wanted) : null;
|
|
246
|
+
if (!d) return `"${String(wanted)}" is not a date. Send YYYY-MM-DD, or a phrase such as next fri.`;
|
|
247
|
+
return refusal(d) ?? changeAsAgent(commit, d);
|
|
248
|
+
},
|
|
249
|
+
clear: () => (readOnly ? "The field is read-only." : changeAsAgent(commit, null)),
|
|
250
|
+
},
|
|
251
|
+
}));
|
|
252
|
+
|
|
253
|
+
/* ---- render ---- */
|
|
254
|
+
const calendarProps = { min, max, isDateDisabled, weekNumbers, locale, weekStartsOn, dir, today, captionLayout };
|
|
255
|
+
const iconButton = (
|
|
256
|
+
<button
|
|
257
|
+
type="button"
|
|
258
|
+
aria-label={value ? `${label}, ${fmt(value)}` : label}
|
|
259
|
+
disabled={disabled || readOnly}
|
|
260
|
+
data-slot="date-picker-trigger"
|
|
261
|
+
className={cn(
|
|
262
|
+
"me-1 inline-flex size-7 shrink-0 items-center justify-center rounded-[calc(var(--radius)-4px)] text-muted-foreground",
|
|
263
|
+
"hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
|
|
264
|
+
"disabled:pointer-events-none disabled:opacity-50"
|
|
265
|
+
)}
|
|
266
|
+
>
|
|
267
|
+
<CalendarDays className="size-4" aria-hidden />
|
|
268
|
+
</button>
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
return (
|
|
272
|
+
<div data-slot="date-picker" dir={dir} className={cn("w-full", className)}>
|
|
273
|
+
<div
|
|
274
|
+
ref={box}
|
|
275
|
+
data-slot="date-picker-field"
|
|
276
|
+
data-invalid={now.kind === "error" || undefined}
|
|
277
|
+
data-disabled={disabled || undefined}
|
|
278
|
+
className={cn(
|
|
279
|
+
"flex h-9 items-center rounded-md border border-input bg-transparent shadow-xs dark:bg-input/30",
|
|
280
|
+
"transition-[color,box-shadow] focus-within:border-ring focus-within:ring-[3px] focus-within:ring-ring/50",
|
|
281
|
+
"data-[invalid]:border-destructive data-[invalid]:focus-within:ring-destructive/20",
|
|
282
|
+
"data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50"
|
|
283
|
+
)}
|
|
284
|
+
>
|
|
285
|
+
<BaseInput
|
|
286
|
+
{...inputProps}
|
|
287
|
+
ref={useMergedRef(ref, input)}
|
|
288
|
+
data-slot="date-picker-input"
|
|
289
|
+
autoComplete="off"
|
|
290
|
+
value={text}
|
|
291
|
+
placeholder={placeholder}
|
|
292
|
+
required={required}
|
|
293
|
+
disabled={disabled}
|
|
294
|
+
readOnly={readOnly}
|
|
295
|
+
aria-invalid={now.kind === "error" || undefined}
|
|
296
|
+
aria-describedby={cn(describedBy, hintId) || undefined}
|
|
297
|
+
onValueChange={(next) => setText(next)}
|
|
298
|
+
onFocus={(e) => {
|
|
299
|
+
setFocused(true);
|
|
300
|
+
inputProps.onFocus?.(e);
|
|
301
|
+
}}
|
|
302
|
+
onBlur={(e) => {
|
|
303
|
+
setFocused(false);
|
|
304
|
+
settle(e.nativeEvent);
|
|
305
|
+
onBlur?.(e);
|
|
306
|
+
}}
|
|
307
|
+
onKeyDown={(e) => {
|
|
308
|
+
onKeyDown?.(e);
|
|
309
|
+
if (e.defaultPrevented) return;
|
|
310
|
+
// An empty field with nothing to clear lets Enter submit the form.
|
|
311
|
+
if (e.key === "Enter" && now.kind !== "set" && (text.trim() || value)) {
|
|
312
|
+
e.preventDefault();
|
|
313
|
+
if (settle(e.nativeEvent)) setOpen(false);
|
|
314
|
+
} else if (e.key === "ArrowDown" && !readOnly) {
|
|
315
|
+
e.preventDefault();
|
|
316
|
+
// Commit first, so the calendar opens on the day the field reads.
|
|
317
|
+
settle(e.nativeEvent);
|
|
318
|
+
setOpen(true);
|
|
319
|
+
} else if (e.key === "Escape" && !open && text !== (value ? fmt(value) : "")) {
|
|
320
|
+
e.stopPropagation();
|
|
321
|
+
setText(value ? fmt(value) : "");
|
|
322
|
+
}
|
|
323
|
+
}}
|
|
324
|
+
className="h-full min-w-0 flex-1 bg-transparent px-3 text-base outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed md:text-sm"
|
|
325
|
+
/>
|
|
326
|
+
<Unlisted>
|
|
327
|
+
{presentation === "popover" ? (
|
|
328
|
+
<Popover open={open} onOpenChange={setOpen}>
|
|
329
|
+
<PopoverTrigger render={iconButton} />
|
|
330
|
+
<PopoverPortal>
|
|
331
|
+
<PopoverPositioner anchor={box} align="start" sideOffset={6}>
|
|
332
|
+
<PopoverPopup
|
|
333
|
+
ref={popup}
|
|
334
|
+
dir={dir}
|
|
335
|
+
aria-label={label}
|
|
336
|
+
data-slot="date-picker-popup"
|
|
337
|
+
initialFocus={() => focusableDay(popup.current)}
|
|
338
|
+
finalFocus={input}
|
|
339
|
+
className="p-2"
|
|
340
|
+
>
|
|
341
|
+
<Calendar
|
|
342
|
+
{...calendarProps}
|
|
343
|
+
required
|
|
344
|
+
value={value}
|
|
345
|
+
onValueChange={(d, details) => {
|
|
346
|
+
commit(d, details);
|
|
347
|
+
setOpen(false);
|
|
348
|
+
}}
|
|
349
|
+
className="[--calendar-bg:var(--popover)] [--calendar-height:18.75rem]"
|
|
350
|
+
/>
|
|
351
|
+
</PopoverPopup>
|
|
352
|
+
</PopoverPositioner>
|
|
353
|
+
</PopoverPortal>
|
|
354
|
+
</Popover>
|
|
355
|
+
) : (
|
|
356
|
+
<Drawer open={open} onOpenChange={setOpen}>
|
|
357
|
+
<DrawerTrigger render={iconButton} />
|
|
358
|
+
<DrawerContent
|
|
359
|
+
ref={popup}
|
|
360
|
+
dir={dir}
|
|
361
|
+
side="bottom"
|
|
362
|
+
data-slot="date-picker-drawer"
|
|
363
|
+
// The calendar takes focus, so a phone keeps its keyboard down.
|
|
364
|
+
initialFocus={() => focusableDay(popup.current)}
|
|
365
|
+
finalFocus={input}
|
|
366
|
+
className="max-h-[92dvh] gap-0 rounded-t-2xl px-0 pb-6 pt-4"
|
|
367
|
+
>
|
|
368
|
+
<DrawerPick
|
|
369
|
+
key={`${round}:${key}`}
|
|
370
|
+
label={label}
|
|
371
|
+
confirmLabel={confirmLabel}
|
|
372
|
+
placeholder={placeholder}
|
|
373
|
+
initial={value}
|
|
374
|
+
presets={(presetsProp ?? phonePresets(today)).filter((p) => !refusal(p.value))}
|
|
375
|
+
reading={reading}
|
|
376
|
+
fmt={fmt}
|
|
377
|
+
relative={(d) => relative(d, today, locale)}
|
|
378
|
+
calendarProps={calendarProps}
|
|
379
|
+
onConfirm={(d) => {
|
|
380
|
+
commit(d, changeDetails("confirm"));
|
|
381
|
+
setOpen(false);
|
|
382
|
+
}}
|
|
383
|
+
/>
|
|
384
|
+
</DrawerContent>
|
|
385
|
+
</Drawer>
|
|
386
|
+
)}
|
|
387
|
+
</Unlisted>
|
|
388
|
+
</div>
|
|
389
|
+
<p id={hintId} data-slot="date-picker-hint" aria-live="polite" className={cn("mt-1.5 flex min-h-5 items-center gap-2 text-[0.8125rem] text-muted-foreground", now.kind === "error" && "text-destructive")}>
|
|
390
|
+
{now.kind === "error" && now.message}
|
|
391
|
+
{now.kind === "preview" && (
|
|
392
|
+
<>
|
|
393
|
+
<b className="font-medium text-foreground">{fmt(now.date!)}</b>
|
|
394
|
+
<span className="sr-only">, </span>
|
|
395
|
+
<span>{relative(now.date!, today, locale)}</span>
|
|
396
|
+
</>
|
|
397
|
+
)}
|
|
398
|
+
{now.kind === "set" && <span>{relative(now.date!, today, locale)}</span>}
|
|
399
|
+
</p>
|
|
400
|
+
{name && <input type="hidden" name={name} form={inputProps.form} disabled={disabled} value={value ? toISODate(value) : ""} />}
|
|
401
|
+
</div>
|
|
402
|
+
);
|
|
403
|
+
});
|
|
404
|
+
DatePicker.displayName = "DatePicker";
|
|
405
|
+
|
|
406
|
+
function focusableDay(root: HTMLElement | null): HTMLElement | true {
|
|
407
|
+
const days = root?.querySelectorAll<HTMLElement>("[data-slot=calendar-day][tabindex='0']") ?? [];
|
|
408
|
+
return days[days.length - 1] ?? true;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function phonePresets(today: Date): DatePickerPreset[] {
|
|
412
|
+
const saturday = readDate("sat", { today })!;
|
|
413
|
+
const monday = readDate("next mon", { today })!;
|
|
414
|
+
const all = [
|
|
415
|
+
{ label: "Today", value: today },
|
|
416
|
+
{ label: "Tomorrow", value: addDays(today, 1) },
|
|
417
|
+
{ label: "Saturday", value: saturday },
|
|
418
|
+
{ label: "Next Mon", value: monday },
|
|
419
|
+
{ label: "In a week", value: addDays(today, 7) },
|
|
420
|
+
];
|
|
421
|
+
return all.filter((p, i) => all.findIndex((q) => isSameDay(q.value, p.value)) === i);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** The drawer's own field, chips and calendar. Nothing commits until its button is pressed. */
|
|
425
|
+
function DrawerPick(props: {
|
|
426
|
+
label: string;
|
|
427
|
+
confirmLabel: string;
|
|
428
|
+
placeholder: string;
|
|
429
|
+
initial: Date | null;
|
|
430
|
+
presets: DatePickerPreset[];
|
|
431
|
+
reading: (text: string, current: Date | null) => Reading;
|
|
432
|
+
fmt: (d: Date) => string;
|
|
433
|
+
relative: (d: Date) => string;
|
|
434
|
+
calendarProps: Omit<React.ComponentProps<typeof Calendar>, "mode" | "value" | "defaultValue" | "onValueChange">;
|
|
435
|
+
onConfirm: (d: Date | null) => void;
|
|
436
|
+
}) {
|
|
437
|
+
const { label, confirmLabel, placeholder, initial, presets, reading, fmt, relative, calendarProps, onConfirm } = props;
|
|
438
|
+
const [draft, setDraft] = React.useState(initial);
|
|
439
|
+
const [text, setText] = React.useState(initial ? fmt(initial) : "");
|
|
440
|
+
const hintId = React.useId();
|
|
441
|
+
const now = reading(text, draft);
|
|
442
|
+
const ready = now.kind !== "error" && !(now.kind === "empty" && text.trim());
|
|
443
|
+
const hit = presets.findIndex((p) => isSameDay(p.value, draft));
|
|
444
|
+
const set = (d: Date) => {
|
|
445
|
+
setDraft(d);
|
|
446
|
+
setText(fmt(d));
|
|
447
|
+
};
|
|
448
|
+
return (
|
|
449
|
+
<div className="flex min-h-0 flex-col">
|
|
450
|
+
<DrawerTitle className="px-5 pb-3 text-[1.0625rem] font-semibold">{label}</DrawerTitle>
|
|
451
|
+
<div className="px-5 pb-2.5">
|
|
452
|
+
<BaseInput
|
|
453
|
+
aria-label={label}
|
|
454
|
+
aria-describedby={hintId}
|
|
455
|
+
data-slot="date-picker-drawer-input"
|
|
456
|
+
autoComplete="off"
|
|
457
|
+
value={text}
|
|
458
|
+
placeholder={placeholder}
|
|
459
|
+
aria-invalid={now.kind === "error" || undefined}
|
|
460
|
+
onValueChange={(next) => {
|
|
461
|
+
setText(next);
|
|
462
|
+
if (!next.trim()) return setDraft(null);
|
|
463
|
+
const r = reading(next, null);
|
|
464
|
+
if (r.kind === "preview") setDraft(r.date!);
|
|
465
|
+
}}
|
|
466
|
+
onKeyDown={(e) => {
|
|
467
|
+
if (e.key !== "Enter") return;
|
|
468
|
+
e.preventDefault();
|
|
469
|
+
if (ready) onConfirm(draft);
|
|
470
|
+
}}
|
|
471
|
+
className={cn(
|
|
472
|
+
"h-12 w-full rounded-xl border border-input bg-background px-3.5 text-[1.0625rem] outline-none placeholder:text-muted-foreground",
|
|
473
|
+
"focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 aria-invalid:border-destructive"
|
|
474
|
+
)}
|
|
475
|
+
/>
|
|
476
|
+
<p id={hintId} data-slot="date-picker-drawer-hint" aria-live="polite" className={cn("mt-1.5 flex min-h-5 items-center gap-2 text-[0.8125rem] text-muted-foreground", now.kind === "error" && "text-destructive")}>
|
|
477
|
+
{now.kind === "error" && now.message}
|
|
478
|
+
{now.kind === "preview" && hit < 0 && (
|
|
479
|
+
<>
|
|
480
|
+
<b className="font-medium text-foreground">{fmt(now.date!)}</b>
|
|
481
|
+
<span className="sr-only">, </span>
|
|
482
|
+
<span>{relative(now.date!)}</span>
|
|
483
|
+
</>
|
|
484
|
+
)}
|
|
485
|
+
{/* A lit chip already says when it is. */}
|
|
486
|
+
{now.kind === "set" && hit < 0 && <span>{relative(now.date!)}</span>}
|
|
487
|
+
</p>
|
|
488
|
+
</div>
|
|
489
|
+
{presets.length > 0 && (
|
|
490
|
+
<div data-slot="date-picker-presets" className="flex gap-2 overflow-x-auto px-5 pb-3 [scrollbar-width:none]">
|
|
491
|
+
{presets.map((p, i) => (
|
|
492
|
+
<button
|
|
493
|
+
key={p.label}
|
|
494
|
+
type="button"
|
|
495
|
+
aria-pressed={i === hit}
|
|
496
|
+
data-slot="date-picker-preset"
|
|
497
|
+
onClick={() => set(p.value)}
|
|
498
|
+
className={cn(
|
|
499
|
+
"shrink-0 whitespace-nowrap rounded-full border border-border bg-background px-3 py-1.5 text-[0.8125rem]",
|
|
500
|
+
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
|
|
501
|
+
"aria-pressed:border-transparent aria-pressed:bg-primary aria-pressed:text-primary-foreground"
|
|
502
|
+
)}
|
|
503
|
+
>
|
|
504
|
+
{p.label}
|
|
505
|
+
</button>
|
|
506
|
+
))}
|
|
507
|
+
</div>
|
|
508
|
+
)}
|
|
509
|
+
<div className="flex justify-center px-3">
|
|
510
|
+
<Calendar
|
|
511
|
+
{...calendarProps}
|
|
512
|
+
required
|
|
513
|
+
value={draft}
|
|
514
|
+
onValueChange={(d) => d && set(d)}
|
|
515
|
+
className="w-full [--calendar-bg:var(--background)] [--calendar-cell:2.75rem] [--calendar-height:18.75rem]"
|
|
516
|
+
/>
|
|
517
|
+
</div>
|
|
518
|
+
<div className="px-5 pt-3">
|
|
519
|
+
<Button className="w-full" data-slot="date-picker-confirm" disabled={!ready} onClick={() => onConfirm(draft)}>
|
|
520
|
+
{confirmLabel}
|
|
521
|
+
</Button>
|
|
522
|
+
</div>
|
|
523
|
+
</div>
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
export { DatePicker };
|