@dashforge/tw 0.9.0-beta → 0.10.0-beta
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 +60 -0
- package/dist/index.esm.js +1634 -256
- package/dist/src/components/Calendar/Calendar.d.ts +14 -0
- package/dist/src/components/Calendar/Calendar.d.ts.map +1 -0
- package/dist/src/components/Calendar/calendar.types.d.ts +72 -0
- package/dist/src/components/Calendar/calendar.types.d.ts.map +1 -0
- package/dist/src/components/Calendar/calendar.variants.d.ts +99 -0
- package/dist/src/components/Calendar/calendar.variants.d.ts.map +1 -0
- package/dist/src/components/DatePicker/DatePicker.d.ts +14 -0
- package/dist/src/components/DatePicker/DatePicker.d.ts.map +1 -0
- package/dist/src/components/DatePicker/datePicker.types.d.ts +88 -0
- package/dist/src/components/DatePicker/datePicker.types.d.ts.map +1 -0
- package/dist/src/components/DatePicker/datePicker.variants.d.ts +97 -0
- package/dist/src/components/DatePicker/datePicker.variants.d.ts.map +1 -0
- package/dist/src/components/DateRangePicker/DateRangePicker.d.ts +13 -0
- package/dist/src/components/DateRangePicker/DateRangePicker.d.ts.map +1 -0
- package/dist/src/components/DateRangePicker/dateRangePicker.types.d.ts +85 -0
- package/dist/src/components/DateRangePicker/dateRangePicker.types.d.ts.map +1 -0
- package/dist/src/components/DateRangePicker/dateRangePicker.variants.d.ts +183 -0
- package/dist/src/components/DateRangePicker/dateRangePicker.variants.d.ts.map +1 -0
- package/dist/src/components/DateTimePicker/DateTimePicker.d.ts +7 -43
- package/dist/src/components/DateTimePicker/DateTimePicker.d.ts.map +1 -1
- package/dist/src/components/DateTimePicker/dateTimePicker.types.d.ts +50 -55
- package/dist/src/components/DateTimePicker/dateTimePicker.types.d.ts.map +1 -1
- package/dist/src/components/DateTimePicker/dateTimePicker.variants.d.ts +36 -94
- package/dist/src/components/DateTimePicker/dateTimePicker.variants.d.ts.map +1 -1
- package/dist/src/components/TimePicker/TimePicker.d.ts +12 -0
- package/dist/src/components/TimePicker/TimePicker.d.ts.map +1 -0
- package/dist/src/components/TimePicker/timePicker.types.d.ts +80 -0
- package/dist/src/components/TimePicker/timePicker.types.d.ts.map +1 -0
- package/dist/src/components/TimePicker/timePicker.variants.d.ts +103 -0
- package/dist/src/components/TimePicker/timePicker.variants.d.ts.map +1 -0
- package/dist/src/index.d.ts +15 -3
- package/dist/src/index.d.ts.map +1 -1
- package/package.json +4 -3
- package/src/components/Calendar/Calendar.test.tsx +75 -0
- package/src/components/Calendar/Calendar.tsx +264 -0
- package/src/components/Calendar/calendar.types.ts +65 -0
- package/src/components/Calendar/calendar.variants.ts +64 -0
- package/src/components/DatePicker/DatePicker.test.tsx +76 -0
- package/src/components/DatePicker/DatePicker.tsx +305 -0
- package/src/components/DatePicker/datePicker.types.ts +77 -0
- package/src/components/DatePicker/datePicker.variants.ts +50 -0
- package/src/components/DateRangePicker/DateRangePicker.test.tsx +92 -0
- package/src/components/DateRangePicker/DateRangePicker.tsx +510 -0
- package/src/components/DateRangePicker/dateRangePicker.types.ts +74 -0
- package/src/components/DateRangePicker/dateRangePicker.variants.ts +101 -0
- package/src/components/DateTimePicker/DateTimePicker.test.tsx +60 -186
- package/src/components/DateTimePicker/DateTimePicker.tsx +209 -181
- package/src/components/DateTimePicker/dateTimePicker.types.ts +50 -54
- package/src/components/DateTimePicker/dateTimePicker.variants.ts +22 -51
- package/src/components/TimePicker/TimePicker.test.tsx +65 -0
- package/src/components/TimePicker/TimePicker.tsx +274 -0
- package/src/components/TimePicker/timePicker.types.ts +69 -0
- package/src/components/TimePicker/timePicker.variants.ts +53 -0
- package/src/index.ts +38 -6
- package/tsconfig.lib.json +7 -4
- package/src/components/DateTimePicker/DateTimePicker.perf.test.tsx +0 -153
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { tv } from 'tailwind-variants';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Structural slots for `<Calendar>`.
|
|
5
|
+
*
|
|
6
|
+
* Theme identity: every class is a bare neutral / brand token-backed
|
|
7
|
+
* Tailwind class. The `dashforgePreset()` CSS-variable swap auto-inverts
|
|
8
|
+
* the neutral palette, so there are NO `dark:` variants here (a `dark:` on
|
|
9
|
+
* a neutral class would double-invert — see `_shared/themeIdentity.test.ts`).
|
|
10
|
+
*/
|
|
11
|
+
export const calendarVariants = tv({
|
|
12
|
+
slots: {
|
|
13
|
+
root: 'inline-flex w-fit flex-col gap-2 rounded-lg border border-neutral-200 bg-neutral-50 p-3 select-none',
|
|
14
|
+
header: 'flex items-center justify-between gap-1',
|
|
15
|
+
navButton:
|
|
16
|
+
'inline-flex h-7 w-7 shrink-0 items-center justify-center rounded-md text-[1rem] text-neutral-600 transition-colors hover:bg-neutral-100 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary-500 disabled:pointer-events-none disabled:opacity-40',
|
|
17
|
+
monthLabel: 'flex-1 text-center text-sm font-semibold text-neutral-900',
|
|
18
|
+
grid: 'flex flex-col gap-1 focus:outline-none',
|
|
19
|
+
weekdayRow: 'grid grid-cols-7 gap-1',
|
|
20
|
+
weekday:
|
|
21
|
+
'flex h-8 items-center justify-center text-xs font-semibold text-neutral-500',
|
|
22
|
+
weekRow: 'grid grid-cols-7 gap-1',
|
|
23
|
+
},
|
|
24
|
+
variants: {
|
|
25
|
+
disabled: {
|
|
26
|
+
true: { root: 'opacity-60' },
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
defaultVariants: {
|
|
30
|
+
disabled: false,
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Per-day-cell recipe. Each of the 42 grid cells is rendered with its own
|
|
36
|
+
* state (`siblingMonth` / `today` / `selected` / `disabled`).
|
|
37
|
+
*/
|
|
38
|
+
export const calendarDayVariants = tv({
|
|
39
|
+
base: 'flex h-9 w-9 items-center justify-center rounded-md text-sm text-neutral-900 transition-colors hover:bg-neutral-100 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-primary-500',
|
|
40
|
+
variants: {
|
|
41
|
+
siblingMonth: {
|
|
42
|
+
true: 'text-neutral-400',
|
|
43
|
+
},
|
|
44
|
+
today: {
|
|
45
|
+
true: 'font-bold ring-1 ring-inset ring-primary-400',
|
|
46
|
+
},
|
|
47
|
+
selected: {
|
|
48
|
+
true: 'bg-primary-600 text-white hover:bg-primary-600',
|
|
49
|
+
},
|
|
50
|
+
disabled: {
|
|
51
|
+
true: 'cursor-default text-neutral-300 opacity-60 hover:bg-transparent',
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
compoundVariants: [
|
|
55
|
+
// A selected cell drops the "today" ring — the solid fill is enough.
|
|
56
|
+
{ selected: true, today: true, class: 'ring-0' },
|
|
57
|
+
],
|
|
58
|
+
defaultVariants: {
|
|
59
|
+
siblingMonth: false,
|
|
60
|
+
today: false,
|
|
61
|
+
selected: false,
|
|
62
|
+
disabled: false,
|
|
63
|
+
},
|
|
64
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { describe, it, expect, vi, beforeAll } from 'vitest';
|
|
3
|
+
import { render, screen, fireEvent } from '@testing-library/react';
|
|
4
|
+
|
|
5
|
+
import { DatePicker } from './DatePicker.js';
|
|
6
|
+
|
|
7
|
+
beforeAll(() => {
|
|
8
|
+
// Radix Popover (Floating UI) needs ResizeObserver — jsdom lacks it.
|
|
9
|
+
if (!('ResizeObserver' in globalThis)) {
|
|
10
|
+
class ResizeObserverStub {
|
|
11
|
+
observe(): void {
|
|
12
|
+
/* no-op */
|
|
13
|
+
}
|
|
14
|
+
unobserve(): void {
|
|
15
|
+
/* no-op */
|
|
16
|
+
}
|
|
17
|
+
disconnect(): void {
|
|
18
|
+
/* no-op */
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
(globalThis as { ResizeObserver?: unknown }).ResizeObserver =
|
|
22
|
+
ResizeObserverStub;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe('DatePicker', () => {
|
|
27
|
+
it('renders a labelled trigger with the placeholder', () => {
|
|
28
|
+
render(
|
|
29
|
+
<DatePicker name="birthday" label="Birthday" placeholder="Pick one" />,
|
|
30
|
+
);
|
|
31
|
+
expect(screen.queryByText('Birthday')).not.toBeNull();
|
|
32
|
+
expect(screen.getByRole('button').textContent).toContain('Pick one');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('shows a localized controlled value', () => {
|
|
36
|
+
render(<DatePicker name="d" value="2026-05-20" />);
|
|
37
|
+
expect(screen.getByRole('button').textContent).toContain('May 20, 2026');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('opens and toggles the calendar popover from the trigger', () => {
|
|
41
|
+
render(<DatePicker name="d" defaultValue="2026-05-20" />);
|
|
42
|
+
const trigger = screen.getByRole('button');
|
|
43
|
+
expect(screen.queryByRole('dialog')).toBeNull();
|
|
44
|
+
fireEvent.click(trigger);
|
|
45
|
+
expect(screen.queryByRole('dialog')).not.toBeNull();
|
|
46
|
+
fireEvent.click(trigger);
|
|
47
|
+
expect(screen.queryByRole('dialog')).toBeNull();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('selects a date from the popover, updates the trigger and closes', () => {
|
|
51
|
+
const onChange = vi.fn();
|
|
52
|
+
render(
|
|
53
|
+
<DatePicker name="d" defaultValue="2026-05-20" onChange={onChange} />,
|
|
54
|
+
);
|
|
55
|
+
fireEvent.click(screen.getByRole('button'));
|
|
56
|
+
fireEvent.click(screen.getByRole('button', { name: /May 15, 2026/ }));
|
|
57
|
+
expect(onChange).toHaveBeenCalledWith('2026-05-15');
|
|
58
|
+
expect(screen.queryByRole('dialog')).toBeNull();
|
|
59
|
+
expect(screen.getByRole('button').textContent).toContain('May 15, 2026');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('keeps a controlled value fixed but still reports the change', () => {
|
|
63
|
+
const onChange = vi.fn();
|
|
64
|
+
render(<DatePicker name="d" value="2026-05-20" onChange={onChange} />);
|
|
65
|
+
fireEvent.click(screen.getByRole('button'));
|
|
66
|
+
fireEvent.click(screen.getByRole('button', { name: /May 15, 2026/ }));
|
|
67
|
+
expect(onChange).toHaveBeenCalledWith('2026-05-15');
|
|
68
|
+
expect(screen.getByRole('button').textContent).toContain('May 20, 2026');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('does not open the popover when disabled', () => {
|
|
72
|
+
render(<DatePicker name="d" disabled />);
|
|
73
|
+
fireEvent.click(screen.getByRole('button'));
|
|
74
|
+
expect(screen.queryByRole('dialog')).toBeNull();
|
|
75
|
+
});
|
|
76
|
+
});
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import { useCallback, useContext, useEffect, useId, useRef, useState } from 'react';
|
|
2
|
+
import { DashFormContext, useEngineVisibility } from '@dashforge/ui-core';
|
|
3
|
+
import type { DashFormBridge, FieldRegistration } from '@dashforge/ui-core';
|
|
4
|
+
import { useDashFieldMeta } from '@dashforge/forms';
|
|
5
|
+
import { parseISODate } from '@dashforge/calendar-core';
|
|
6
|
+
import type { ISODate } from '@dashforge/calendar-core';
|
|
7
|
+
import { cn } from '../../utils/cn.js';
|
|
8
|
+
import { useAccessState } from '../../hooks/useAccessState.js';
|
|
9
|
+
import { resolveValidationState } from '../_shared/resolveValidationState.js';
|
|
10
|
+
import { Popover } from '../Popover/Popover.js';
|
|
11
|
+
import { Calendar } from '../Calendar/Calendar.js';
|
|
12
|
+
import { datePickerVariants } from './datePicker.variants.js';
|
|
13
|
+
import type { DatePickerProps } from './datePicker.types.js';
|
|
14
|
+
|
|
15
|
+
// Inline 16×16 stroke calendar glyph — no icon dependency (tw convention).
|
|
16
|
+
function CalendarIcon() {
|
|
17
|
+
return (
|
|
18
|
+
<svg
|
|
19
|
+
width="1em"
|
|
20
|
+
height="1em"
|
|
21
|
+
viewBox="0 0 16 16"
|
|
22
|
+
fill="none"
|
|
23
|
+
aria-hidden="true"
|
|
24
|
+
>
|
|
25
|
+
<rect
|
|
26
|
+
x="2.5"
|
|
27
|
+
y="3.5"
|
|
28
|
+
width="11"
|
|
29
|
+
height="10"
|
|
30
|
+
rx="1.5"
|
|
31
|
+
stroke="currentColor"
|
|
32
|
+
strokeWidth="1.5"
|
|
33
|
+
/>
|
|
34
|
+
<path
|
|
35
|
+
d="M2.5 6.5h11M5.5 2v3M10.5 2v3"
|
|
36
|
+
stroke="currentColor"
|
|
37
|
+
strokeWidth="1.5"
|
|
38
|
+
strokeLinecap="round"
|
|
39
|
+
/>
|
|
40
|
+
</svg>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Formats a stored ISO date for the trigger display, localized to a medium
|
|
46
|
+
* date style (e.g. `"May 20, 2026"`). Formatting is done in UTC so the
|
|
47
|
+
* displayed day always matches the stored `YYYY-MM-DD`.
|
|
48
|
+
*/
|
|
49
|
+
function formatDisplayDate(
|
|
50
|
+
iso: ISODate | null | undefined,
|
|
51
|
+
locale: string,
|
|
52
|
+
): string {
|
|
53
|
+
if (iso == null || iso === '') {
|
|
54
|
+
return '';
|
|
55
|
+
}
|
|
56
|
+
const parts = parseISODate(iso);
|
|
57
|
+
if (parts === null) {
|
|
58
|
+
return iso;
|
|
59
|
+
}
|
|
60
|
+
return new Intl.DateTimeFormat(locale, {
|
|
61
|
+
dateStyle: 'medium',
|
|
62
|
+
timeZone: 'UTC',
|
|
63
|
+
}).format(Date.UTC(parts.year, parts.month - 1, parts.day));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Dashforge TW `<DatePicker>` — a form-bound single-date field.
|
|
68
|
+
*
|
|
69
|
+
* A read-only trigger button paired with a `<Calendar>` popover (Radix
|
|
70
|
+
* Popover — toggle / Escape / outside-click / focus-trap are Radix-managed).
|
|
71
|
+
* Integrates with the Dashforge form bridge + RBAC, mirroring the other
|
|
72
|
+
* `@dashforge/tw` form fields.
|
|
73
|
+
*
|
|
74
|
+
* Storage contract: an ISO `YYYY-MM-DD` string, or `null` — shared with the
|
|
75
|
+
* MUI `@dashforge/ui` `DatePicker`.
|
|
76
|
+
*/
|
|
77
|
+
export function DatePicker(props: DatePickerProps) {
|
|
78
|
+
const {
|
|
79
|
+
name,
|
|
80
|
+
rules,
|
|
81
|
+
label,
|
|
82
|
+
helperText,
|
|
83
|
+
error,
|
|
84
|
+
required,
|
|
85
|
+
disabled,
|
|
86
|
+
placeholder,
|
|
87
|
+
layout = 'stacked',
|
|
88
|
+
visibleWhen,
|
|
89
|
+
access,
|
|
90
|
+
value: explicitValue,
|
|
91
|
+
defaultValue,
|
|
92
|
+
onChange,
|
|
93
|
+
minDate,
|
|
94
|
+
maxDate,
|
|
95
|
+
disabledDates,
|
|
96
|
+
isDateDisabled,
|
|
97
|
+
weekStartDay,
|
|
98
|
+
locale,
|
|
99
|
+
fullWidth,
|
|
100
|
+
sx,
|
|
101
|
+
slotProps,
|
|
102
|
+
testId,
|
|
103
|
+
} = props;
|
|
104
|
+
|
|
105
|
+
// ───── Hooks (unconditional, before any early return) ─────
|
|
106
|
+
const bridge = useContext(DashFormContext) as DashFormBridge | null;
|
|
107
|
+
const isVisible = useEngineVisibility(bridge?.engine, visibleWhen);
|
|
108
|
+
const fieldMeta = useDashFieldMeta(name);
|
|
109
|
+
const accessState = useAccessState(access);
|
|
110
|
+
|
|
111
|
+
const fieldId = useId();
|
|
112
|
+
const helperId = `${fieldId}-help`;
|
|
113
|
+
|
|
114
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
115
|
+
const [internalValue, setInternalValue] = useState<ISODate | null>(
|
|
116
|
+
defaultValue ?? null,
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
// StrictMode-safe unregister-on-unmount.
|
|
120
|
+
const unregisterRef = useRef({ bridge, name });
|
|
121
|
+
unregisterRef.current = { bridge, name };
|
|
122
|
+
const isMountedRef = useRef(false);
|
|
123
|
+
useEffect(() => {
|
|
124
|
+
isMountedRef.current = true;
|
|
125
|
+
return () => {
|
|
126
|
+
isMountedRef.current = false;
|
|
127
|
+
const { bridge: cap, name: capName } = unregisterRef.current;
|
|
128
|
+
queueMicrotask(() => {
|
|
129
|
+
if (!isMountedRef.current) cap?.unregister?.(capName);
|
|
130
|
+
});
|
|
131
|
+
};
|
|
132
|
+
}, []);
|
|
133
|
+
|
|
134
|
+
// ───── Derived ─────
|
|
135
|
+
const effectiveDisabled = Boolean(disabled) || accessState.disabled;
|
|
136
|
+
const effectiveReadOnly = accessState.readonly;
|
|
137
|
+
const isInteractive = !effectiveDisabled && !effectiveReadOnly;
|
|
138
|
+
const isFormMode = Boolean(bridge?.register);
|
|
139
|
+
|
|
140
|
+
let resolvedError = error;
|
|
141
|
+
let resolvedHelperText: typeof helperText = helperText;
|
|
142
|
+
let registration: FieldRegistration | null = null;
|
|
143
|
+
let resolvedValue: ISODate | null;
|
|
144
|
+
|
|
145
|
+
if (isFormMode && bridge) {
|
|
146
|
+
registration = bridge.register(name, rules);
|
|
147
|
+
const validation = resolveValidationState(name, bridge, error, helperText);
|
|
148
|
+
resolvedError = validation.error;
|
|
149
|
+
resolvedHelperText = validation.helperText;
|
|
150
|
+
if (explicitValue !== undefined) {
|
|
151
|
+
resolvedValue = explicitValue;
|
|
152
|
+
} else {
|
|
153
|
+
const bv = fieldMeta.value;
|
|
154
|
+
resolvedValue = bv == null ? null : String(bv);
|
|
155
|
+
}
|
|
156
|
+
} else if (explicitValue !== undefined) {
|
|
157
|
+
resolvedValue = explicitValue;
|
|
158
|
+
} else {
|
|
159
|
+
resolvedValue = internalValue;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// RHF threads its own callback ref through here in form mode.
|
|
163
|
+
const registrationRefFn = registration?.ref;
|
|
164
|
+
const triggerRef = useCallback(
|
|
165
|
+
(instance: HTMLButtonElement | null) => {
|
|
166
|
+
if (typeof registrationRefFn === 'function') {
|
|
167
|
+
registrationRefFn(instance);
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
[registrationRefFn],
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
// ───── Render-time guards (after all hooks) ─────
|
|
174
|
+
if (!isVisible) return null;
|
|
175
|
+
if (!accessState.visible) return null;
|
|
176
|
+
|
|
177
|
+
const v = datePickerVariants({
|
|
178
|
+
layout,
|
|
179
|
+
error: resolvedError,
|
|
180
|
+
fullWidth,
|
|
181
|
+
});
|
|
182
|
+
const resolvedLocale = locale ?? 'en-US';
|
|
183
|
+
const displayValue = formatDisplayDate(resolvedValue, resolvedLocale);
|
|
184
|
+
|
|
185
|
+
const commitValue = (next: ISODate | null) => {
|
|
186
|
+
if (isFormMode && bridge) {
|
|
187
|
+
bridge.setValue?.(name, next);
|
|
188
|
+
void registration?.onChange?.({
|
|
189
|
+
target: { name, value: next ?? '' },
|
|
190
|
+
type: 'change',
|
|
191
|
+
});
|
|
192
|
+
} else if (explicitValue === undefined) {
|
|
193
|
+
setInternalValue(next);
|
|
194
|
+
}
|
|
195
|
+
onChange?.(next);
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
// Mark the field touched on popover close. Reads the freshest committed
|
|
199
|
+
// value straight from the bridge so the touched-blur never reverts a
|
|
200
|
+
// value just chosen in the calendar.
|
|
201
|
+
const markTouched = () => {
|
|
202
|
+
if (isFormMode && bridge) {
|
|
203
|
+
const committed = bridge.getValue(name);
|
|
204
|
+
void registration?.onBlur?.({
|
|
205
|
+
target: { name, value: committed == null ? '' : String(committed) },
|
|
206
|
+
type: 'blur',
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const handleOpenChange = (next: boolean) => {
|
|
212
|
+
if (next && !isInteractive) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
setIsOpen(next);
|
|
216
|
+
if (!next) {
|
|
217
|
+
markTouched();
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const handleCalendarChange = (next: ISODate) => {
|
|
222
|
+
commitValue(next);
|
|
223
|
+
setIsOpen(false);
|
|
224
|
+
markTouched();
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
return (
|
|
228
|
+
<div
|
|
229
|
+
data-testid={testId}
|
|
230
|
+
className={cn(v.root(), sx, slotProps?.root?.className)}
|
|
231
|
+
>
|
|
232
|
+
{label && (
|
|
233
|
+
<label
|
|
234
|
+
htmlFor={fieldId}
|
|
235
|
+
className={cn(v.label(), slotProps?.label?.className)}
|
|
236
|
+
>
|
|
237
|
+
{label}
|
|
238
|
+
{required && (
|
|
239
|
+
<span
|
|
240
|
+
aria-hidden="true"
|
|
241
|
+
className={cn(
|
|
242
|
+
v.requiredMark(),
|
|
243
|
+
slotProps?.requiredMark?.className,
|
|
244
|
+
)}
|
|
245
|
+
>
|
|
246
|
+
*
|
|
247
|
+
</span>
|
|
248
|
+
)}
|
|
249
|
+
</label>
|
|
250
|
+
)}
|
|
251
|
+
|
|
252
|
+
<Popover
|
|
253
|
+
open={isOpen}
|
|
254
|
+
onOpenChange={handleOpenChange}
|
|
255
|
+
side="bottom"
|
|
256
|
+
align="start"
|
|
257
|
+
content={
|
|
258
|
+
<Calendar
|
|
259
|
+
value={resolvedValue}
|
|
260
|
+
onChange={handleCalendarChange}
|
|
261
|
+
minDate={minDate}
|
|
262
|
+
maxDate={maxDate}
|
|
263
|
+
disabledDates={disabledDates}
|
|
264
|
+
isDateDisabled={isDateDisabled}
|
|
265
|
+
weekStartDay={weekStartDay}
|
|
266
|
+
locale={locale}
|
|
267
|
+
aria-label="Choose date"
|
|
268
|
+
sx="border-0 bg-transparent p-0"
|
|
269
|
+
/>
|
|
270
|
+
}
|
|
271
|
+
>
|
|
272
|
+
<button
|
|
273
|
+
type="button"
|
|
274
|
+
id={fieldId}
|
|
275
|
+
ref={triggerRef}
|
|
276
|
+
disabled={effectiveDisabled}
|
|
277
|
+
aria-invalid={resolvedError ? true : undefined}
|
|
278
|
+
aria-describedby={resolvedHelperText ? helperId : undefined}
|
|
279
|
+
className={cn(v.trigger(), slotProps?.trigger?.className)}
|
|
280
|
+
>
|
|
281
|
+
<span className={displayValue ? v.value() : v.placeholder()}>
|
|
282
|
+
{displayValue || placeholder || 'Select a date'}
|
|
283
|
+
</span>
|
|
284
|
+
<span className={v.icon()}>
|
|
285
|
+
<CalendarIcon />
|
|
286
|
+
</span>
|
|
287
|
+
</button>
|
|
288
|
+
</Popover>
|
|
289
|
+
|
|
290
|
+
{resolvedHelperText && (
|
|
291
|
+
<p
|
|
292
|
+
id={helperId}
|
|
293
|
+
className={cn(
|
|
294
|
+
resolvedError ? v.errorText() : v.helperText(),
|
|
295
|
+
resolvedError
|
|
296
|
+
? slotProps?.errorText?.className
|
|
297
|
+
: slotProps?.helperText?.className,
|
|
298
|
+
)}
|
|
299
|
+
>
|
|
300
|
+
{resolvedHelperText}
|
|
301
|
+
</p>
|
|
302
|
+
)}
|
|
303
|
+
</div>
|
|
304
|
+
);
|
|
305
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { Engine } from '@dashforge/ui-core';
|
|
3
|
+
import type { AccessRequirement } from '@dashforge/rbac';
|
|
4
|
+
import type { ISODate, WeekDay } from '@dashforge/calendar-core';
|
|
5
|
+
|
|
6
|
+
/** Per-slot `className` overrides for `<DatePicker>`. */
|
|
7
|
+
export interface DatePickerSlotProps {
|
|
8
|
+
root?: { className?: string };
|
|
9
|
+
label?: { className?: string };
|
|
10
|
+
requiredMark?: { className?: string };
|
|
11
|
+
trigger?: { className?: string };
|
|
12
|
+
helperText?: { className?: string };
|
|
13
|
+
errorText?: { className?: string };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Props for the `<DatePicker>` form field (Tailwind skin).
|
|
18
|
+
*
|
|
19
|
+
* A read-only trigger button paired with a `<Calendar>` popover, integrated
|
|
20
|
+
* with the Dashforge form bridge + RBAC. The stored value is a plain ISO
|
|
21
|
+
* calendar date — `YYYY-MM-DD` — or `null`. (A pure date carries no time or
|
|
22
|
+
* timezone, which removes the whole class of DST round-trip hazards.)
|
|
23
|
+
*
|
|
24
|
+
* The prop surface mirrors the MUI `@dashforge/ui` `DatePicker` so the API
|
|
25
|
+
* is consistent across both ecosystems; only the styling escape hatches
|
|
26
|
+
* (`sx` / `slotProps`) follow the Tailwind conventions.
|
|
27
|
+
*/
|
|
28
|
+
export interface DatePickerProps {
|
|
29
|
+
/** Field name — the bridge registration key. Required. */
|
|
30
|
+
name: string;
|
|
31
|
+
/** Validation rules forwarded to the form bridge. */
|
|
32
|
+
rules?: unknown;
|
|
33
|
+
/** Field label. */
|
|
34
|
+
label?: ReactNode;
|
|
35
|
+
/** Helper text below the control (overrides a bridge error message). */
|
|
36
|
+
helperText?: ReactNode;
|
|
37
|
+
/** Explicit error state (overrides the bridge's auto error). */
|
|
38
|
+
error?: boolean;
|
|
39
|
+
/** Marks the field required (adds the label asterisk). */
|
|
40
|
+
required?: boolean;
|
|
41
|
+
/** Disables the field. */
|
|
42
|
+
disabled?: boolean;
|
|
43
|
+
/** Placeholder shown when no date is selected. */
|
|
44
|
+
placeholder?: string;
|
|
45
|
+
/** Label/control layout. */
|
|
46
|
+
layout?: 'stacked' | 'inline';
|
|
47
|
+
/** Reactive visibility predicate evaluated against the form engine. */
|
|
48
|
+
visibleWhen?: (engine: Engine) => boolean;
|
|
49
|
+
/** RBAC access requirement. */
|
|
50
|
+
access?: AccessRequirement;
|
|
51
|
+
/** Controlled value — ISO `YYYY-MM-DD` or `null`. */
|
|
52
|
+
value?: ISODate | null;
|
|
53
|
+
/** Uncontrolled initial value. */
|
|
54
|
+
defaultValue?: ISODate | null;
|
|
55
|
+
/** Fired with the new ISO date (or `null` when cleared). */
|
|
56
|
+
onChange?: (value: ISODate | null) => void;
|
|
57
|
+
/** Earliest selectable date (inclusive). */
|
|
58
|
+
minDate?: ISODate;
|
|
59
|
+
/** Latest selectable date (inclusive). */
|
|
60
|
+
maxDate?: ISODate;
|
|
61
|
+
/** Explicit list of disabled dates. */
|
|
62
|
+
disabledDates?: readonly ISODate[];
|
|
63
|
+
/** Predicate marking arbitrary dates disabled. */
|
|
64
|
+
isDateDisabled?: (date: ISODate) => boolean;
|
|
65
|
+
/** Weekday of the calendar's first column (`0` = Sunday). */
|
|
66
|
+
weekStartDay?: WeekDay;
|
|
67
|
+
/** BCP-47 locale for the calendar and the display format. */
|
|
68
|
+
locale?: string;
|
|
69
|
+
/** Stretches the field to its container width. */
|
|
70
|
+
fullWidth?: boolean;
|
|
71
|
+
/** Root-level Tailwind class override. */
|
|
72
|
+
sx?: string;
|
|
73
|
+
/** Per-slot `className` overrides. */
|
|
74
|
+
slotProps?: DatePickerSlotProps;
|
|
75
|
+
/** Test id applied to the field root. */
|
|
76
|
+
testId?: string;
|
|
77
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { tv } from 'tailwind-variants';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Slots for `<DatePicker>` — a bridge-integrated date field whose trigger
|
|
5
|
+
* is a `<button>` styled as an input, opening a `<Calendar>` popover.
|
|
6
|
+
*
|
|
7
|
+
* Theme identity: bare neutral / brand token-backed classes only — no
|
|
8
|
+
* `dark:` variants on the neutral palette (the preset CSS-var swap
|
|
9
|
+
* auto-inverts). See `_shared/themeIdentity.test.ts`.
|
|
10
|
+
*/
|
|
11
|
+
export const datePickerVariants = tv({
|
|
12
|
+
slots: {
|
|
13
|
+
root: 'flex flex-col',
|
|
14
|
+
label: 'block text-sm font-medium text-neutral-900 mb-1',
|
|
15
|
+
requiredMark: 'text-danger-500 ml-0.5',
|
|
16
|
+
trigger:
|
|
17
|
+
'flex items-center gap-2 h-10 min-w-[12rem] rounded-md border border-neutral-300 bg-neutral-50 px-3 text-base text-neutral-900 cursor-pointer transition-colors hover:border-neutral-400 focus-visible:outline-none focus-visible:border-primary-500 focus-visible:ring-2 focus-visible:ring-primary-500/30 disabled:cursor-not-allowed disabled:opacity-60 disabled:bg-neutral-200',
|
|
18
|
+
value: 'flex-1 truncate text-left',
|
|
19
|
+
placeholder: 'flex-1 truncate text-left text-neutral-400',
|
|
20
|
+
icon: 'shrink-0 text-[1rem] text-neutral-500',
|
|
21
|
+
helperText: 'mt-1 text-sm text-neutral-600',
|
|
22
|
+
errorText: 'mt-1 text-sm text-danger-600',
|
|
23
|
+
},
|
|
24
|
+
variants: {
|
|
25
|
+
layout: {
|
|
26
|
+
stacked: {},
|
|
27
|
+
inline: {
|
|
28
|
+
root: 'flex-row items-start gap-3',
|
|
29
|
+
label: 'mb-0 pt-2.5 whitespace-nowrap shrink-0',
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
error: {
|
|
33
|
+
true: {
|
|
34
|
+
trigger:
|
|
35
|
+
'border-danger-500 hover:border-danger-500 focus-visible:border-danger-500 focus-visible:ring-danger-500/30',
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
fullWidth: {
|
|
39
|
+
true: {
|
|
40
|
+
root: 'w-full',
|
|
41
|
+
trigger: 'w-full min-w-0',
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
defaultVariants: {
|
|
46
|
+
layout: 'stacked',
|
|
47
|
+
error: false,
|
|
48
|
+
fullWidth: false,
|
|
49
|
+
},
|
|
50
|
+
});
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { describe, it, expect, vi, beforeAll } from 'vitest';
|
|
3
|
+
import { render, screen, fireEvent } from '@testing-library/react';
|
|
4
|
+
|
|
5
|
+
import { DateRangePicker } from './DateRangePicker.js';
|
|
6
|
+
|
|
7
|
+
beforeAll(() => {
|
|
8
|
+
// Radix Popover (Floating UI) needs ResizeObserver — jsdom lacks it.
|
|
9
|
+
if (!('ResizeObserver' in globalThis)) {
|
|
10
|
+
class ResizeObserverStub {
|
|
11
|
+
observe(): void {
|
|
12
|
+
/* no-op */
|
|
13
|
+
}
|
|
14
|
+
unobserve(): void {
|
|
15
|
+
/* no-op */
|
|
16
|
+
}
|
|
17
|
+
disconnect(): void {
|
|
18
|
+
/* no-op */
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
(globalThis as { ResizeObserver?: unknown }).ResizeObserver =
|
|
22
|
+
ResizeObserverStub;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe('DateRangePicker', () => {
|
|
27
|
+
it('renders a labelled trigger with the placeholder', () => {
|
|
28
|
+
render(
|
|
29
|
+
<DateRangePicker name="period" label="Period" placeholder="Pick a range" />,
|
|
30
|
+
);
|
|
31
|
+
expect(screen.queryByText('Period')).not.toBeNull();
|
|
32
|
+
expect(screen.getByRole('button').textContent).toContain('Pick a range');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('shows a localized controlled range', () => {
|
|
36
|
+
render(
|
|
37
|
+
<DateRangePicker
|
|
38
|
+
name="p"
|
|
39
|
+
value={{ start: '2026-05-10', end: '2026-05-15' }}
|
|
40
|
+
/>,
|
|
41
|
+
);
|
|
42
|
+
expect(screen.getByRole('button').textContent).toContain(
|
|
43
|
+
'May 10, 2026 – May 15, 2026',
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('opens a dual-month popover from the trigger', () => {
|
|
48
|
+
render(
|
|
49
|
+
<DateRangePicker name="p" defaultValue={{ start: '2026-05-10', end: null }} />,
|
|
50
|
+
);
|
|
51
|
+
expect(screen.queryByRole('dialog')).toBeNull();
|
|
52
|
+
fireEvent.click(screen.getByRole('button', { name: /Select|May/ }));
|
|
53
|
+
expect(screen.queryByRole('dialog')).not.toBeNull();
|
|
54
|
+
expect(screen.getAllByRole('grid')).toHaveLength(2);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('completes the range on the second click and closes the popover', () => {
|
|
58
|
+
const onChange = vi.fn();
|
|
59
|
+
render(
|
|
60
|
+
<DateRangePicker
|
|
61
|
+
name="p"
|
|
62
|
+
defaultValue={{ start: '2026-05-10', end: null }}
|
|
63
|
+
onChange={onChange}
|
|
64
|
+
/>,
|
|
65
|
+
);
|
|
66
|
+
fireEvent.click(screen.getByRole('button', { name: /May 10/ }));
|
|
67
|
+
fireEvent.click(screen.getByRole('button', { name: /May 18, 2026/ }));
|
|
68
|
+
expect(onChange).toHaveBeenCalledWith({ start: '2026-05-10', end: '2026-05-18' });
|
|
69
|
+
expect(screen.queryByRole('dialog')).toBeNull();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('restarts the range on a fresh click and keeps the popover open', () => {
|
|
73
|
+
const onChange = vi.fn();
|
|
74
|
+
render(
|
|
75
|
+
<DateRangePicker
|
|
76
|
+
name="p"
|
|
77
|
+
defaultValue={{ start: '2026-05-20', end: '2026-05-25' }}
|
|
78
|
+
onChange={onChange}
|
|
79
|
+
/>,
|
|
80
|
+
);
|
|
81
|
+
fireEvent.click(screen.getByRole('button', { name: /May 20/ }));
|
|
82
|
+
fireEvent.click(screen.getByRole('button', { name: /May 12, 2026/ }));
|
|
83
|
+
expect(onChange).toHaveBeenCalledWith({ start: '2026-05-12', end: null });
|
|
84
|
+
expect(screen.queryByRole('dialog')).not.toBeNull();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('does not open the popover when disabled', () => {
|
|
88
|
+
render(<DateRangePicker name="p" disabled />);
|
|
89
|
+
fireEvent.click(screen.getByRole('button'));
|
|
90
|
+
expect(screen.queryByRole('dialog')).toBeNull();
|
|
91
|
+
});
|
|
92
|
+
});
|