@cahyo-dimas/freeday 3.19.0 → 3.20.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 +72 -0
- package/COMPONENTS.md +33 -4
- package/README.id.md +5 -5
- package/README.md +4 -4
- package/adapters/blazor/FdyDateRange.razor +42 -20
- package/adapters/blazor/FdyDateRange.razor.cs +62 -1
- package/adapters/core/date-range.d.ts +37 -0
- package/adapters/core/date-range.js +61 -0
- package/adapters/react/components/FdyDateRange.tsx +61 -5
- package/adapters/react/components/FdyDatepicker.tsx +93 -12
- package/adapters/react/index.d.ts +1 -1
- package/adapters/react/rangeField.ts +25 -0
- package/adapters/vue/components/FdyDateRange.vue +57 -7
- package/adapters/vue/components/FdyDatepicker.vue +92 -8
- package/adapters/vue/index.d.ts +1 -1
- package/adapters/vue/rangeField.ts +31 -0
- package/dist/freeday-datepicker.js +150 -7
- package/dist/freeday.bundle.css +15 -0
- package/dist/freeday.css +15 -0
- package/dist/freeday.js +150 -7
- package/docs/agent-onboarding.md +10 -9
- package/docs/getting-started.md +4 -4
- package/package.json +2 -2
- package/src/components/datepicker.css +15 -0
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { JSX } from 'react';
|
|
2
|
-
import { useEffect, useId, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import { useContext, useEffect, useId, useMemo, useRef, useState } from 'react';
|
|
3
3
|
import { usePopover } from '../usePopover';
|
|
4
|
+
import { FdyRangeField, type FdyRangeFieldContext } from '../rangeField';
|
|
5
|
+
import { rangeAfterPick, rangeDayState, shownEnds, type FdyRangeDayState, type FdyShownEnds } from '../../core/date-range.js';
|
|
4
6
|
/* The month and year grids, which `FdyMonthpicker` reads from the same module. They were written
|
|
5
7
|
here and moved out when the month picker arrived: a second copy of the bounds rule is a second
|
|
6
8
|
place to fix it, and this release spent three items on exactly that failure in the panel-position
|
|
@@ -31,8 +33,9 @@ import {
|
|
|
31
33
|
// `.fdy-cal__*` classes (see src/components/datepicker.css). Re-implements the single-date
|
|
32
34
|
// calendar behavior (month grid, keyboard nav, min/max clamping, Intl locale formatting) as a
|
|
33
35
|
// controlled React component (`value` + `onChange` in place of Vue's `v-model`). Open/close
|
|
34
|
-
// (outside-click + focusout) mirrors the already-approved FdyCombo.tsx pattern.
|
|
35
|
-
//
|
|
36
|
+
// (outside-click + focusout) mirrors the already-approved FdyCombo.tsx pattern. The range in ONE
|
|
37
|
+
// field is drawn here too, when FdyDateRange layout="single" wraps it in the FdyRangeField context
|
|
38
|
+
// (see ../rangeField.ts); without it this is the single-date picker, unchanged.
|
|
36
39
|
|
|
37
40
|
/** Which grid is on screen. The drill goes days -> months -> years and back down. */
|
|
38
41
|
type CalendarLevel = 'days' | 'months' | 'years';
|
|
@@ -50,6 +53,10 @@ interface DayCell {
|
|
|
50
53
|
selected: boolean;
|
|
51
54
|
disabled: boolean;
|
|
52
55
|
focusable: boolean;
|
|
56
|
+
/** Range layout only; all false for a single date. */
|
|
57
|
+
rangeStart: boolean;
|
|
58
|
+
rangeEnd: boolean;
|
|
59
|
+
inRange: boolean;
|
|
53
60
|
}
|
|
54
61
|
|
|
55
62
|
export interface FdyDatepickerProps {
|
|
@@ -135,9 +142,20 @@ function dayClassName(cell: DayCell): string {
|
|
|
135
142
|
if (cell.outside) cls += ' is-outside';
|
|
136
143
|
if (cell.today) cls += ' is-today';
|
|
137
144
|
if (cell.selected) cls += ' is-selected';
|
|
145
|
+
if (cell.rangeStart) cls += ' is-range-start';
|
|
146
|
+
if (cell.rangeEnd) cls += ' is-range-end';
|
|
147
|
+
if (cell.inRange) cls += ' in-range';
|
|
138
148
|
return cls;
|
|
139
149
|
}
|
|
140
150
|
|
|
151
|
+
/** The committed range, or neither end: half an interval, or one running backwards, is no range. */
|
|
152
|
+
function committedRange(range: FdyRangeFieldContext | null): { start: Date | null; end: Date | null } {
|
|
153
|
+
const start: Date | null = range === null ? null : parseISO(range.start);
|
|
154
|
+
const end: Date | null = range === null ? null : parseISO(range.end);
|
|
155
|
+
if (start === null || end === null || end.getTime() < start.getTime()) return { start: null, end: null };
|
|
156
|
+
return { start, end };
|
|
157
|
+
}
|
|
158
|
+
|
|
141
159
|
export function FdyDatepicker(props: FdyDatepickerProps): JSX.Element {
|
|
142
160
|
const baseId: string = useId();
|
|
143
161
|
const triggerId: string = props.id ?? `${baseId}-trigger`;
|
|
@@ -205,6 +223,20 @@ export function FdyDatepicker(props: FdyDatepickerProps): JSX.Element {
|
|
|
205
223
|
const nextMonthLabelText: string = props.nextMonthLabel ?? 'Next month';
|
|
206
224
|
const clearLabelText: string = props.clearLabel ?? 'Clear date';
|
|
207
225
|
|
|
226
|
+
/* The range in one field. `anchor` is a start picked in this opening and not yet answered by an
|
|
227
|
+
end, `preview` the day the pointer or keyboard is on while it waits. Both live here, with the
|
|
228
|
+
panel they belong to, and are dropped by every close that is not a second pick: the value never
|
|
229
|
+
held them, so a half range is never committed. */
|
|
230
|
+
const range: FdyRangeFieldContext | null = useContext(FdyRangeField);
|
|
231
|
+
const [anchor, setAnchor] = useState<Date | null>(null);
|
|
232
|
+
const [preview, setPreview] = useState<Date | null>(null);
|
|
233
|
+
const committed: { start: Date | null; end: Date | null } = committedRange(range);
|
|
234
|
+
const ends: FdyShownEnds = shownEnds({ start: committed.start, end: committed.end, anchor, preview });
|
|
235
|
+
/** "start/end" (ISO 8601 interval) or "": one `data-value`, so a form can require the range. */
|
|
236
|
+
const rangeValue: string = committed.start === null || committed.end === null
|
|
237
|
+
? ''
|
|
238
|
+
: `${toISO(committed.start)}/${toISO(committed.end)}`;
|
|
239
|
+
|
|
208
240
|
const [viewMonth, setViewMonth] = useState<Date>((): Date => startOfMonth(parseValue(props.value) ?? new Date()));
|
|
209
241
|
const [focusDate, setFocusDate] = useState<Date>((): Date => parseValue(props.value) ?? new Date());
|
|
210
242
|
|
|
@@ -236,9 +268,14 @@ export function FdyDatepicker(props: FdyDatepickerProps): JSX.Element {
|
|
|
236
268
|
/* Month mode shows the month through the SAME formatter the month cells and the day grid's title
|
|
237
269
|
already use, which is the whole point of the mode: <input type="month"> names the month in the
|
|
238
270
|
browser's language, this names it in the page's. */
|
|
239
|
-
const
|
|
240
|
-
|
|
241
|
-
|
|
271
|
+
const hasValue: boolean = range === null ? selectedDate !== null : rangeValue !== '';
|
|
272
|
+
const displayValue: string = range !== null
|
|
273
|
+
? (committed.start === null || committed.end === null
|
|
274
|
+
? displayPlaceholder
|
|
275
|
+
: `${valueFmt.format(committed.start)} – ${valueFmt.format(committed.end)}`)
|
|
276
|
+
: selectedDate === null
|
|
277
|
+
? displayPlaceholder
|
|
278
|
+
: (isMonthMode ? formatters.name : valueFmt).format(selectedDate);
|
|
242
279
|
|
|
243
280
|
const weekdayHeaders: string[] = useMemo((): string[] => {
|
|
244
281
|
const monday: Date = new Date(2024, 0, 1); // a known Monday
|
|
@@ -275,23 +312,34 @@ export function FdyDatepicker(props: FdyDatepickerProps): JSX.Element {
|
|
|
275
312
|
const cells: DayCell[] = [];
|
|
276
313
|
for (let i = 0; i < 42; i++) {
|
|
277
314
|
const d: Date = addDays(startCell, i);
|
|
315
|
+
const name: string = dayLabelFmt.format(d);
|
|
316
|
+
const r: FdyRangeDayState | null = range === null ? null : rangeDayState(startOfDay(d), ends);
|
|
317
|
+
const role: string | null = range === null || r === null || r.role === null
|
|
318
|
+
? null
|
|
319
|
+
: r.role === 'start' ? range.rangeStartLabel : range.rangeEndLabel;
|
|
278
320
|
cells.push({
|
|
279
321
|
date: d,
|
|
280
322
|
iso: toISO(d),
|
|
281
323
|
label: d.getDate(),
|
|
282
|
-
ariaLabel:
|
|
324
|
+
ariaLabel: role === null ? name : `${name}, ${role}`,
|
|
283
325
|
outside: d.getMonth() !== viewMonth.getMonth(),
|
|
284
326
|
today: sameDay(d, today),
|
|
285
|
-
selected: sameDay(d, selectedDate),
|
|
327
|
+
selected: r === null ? sameDay(d, selectedDate) : r.filled,
|
|
286
328
|
disabled: isDayDisabled(d),
|
|
287
329
|
focusable: sameDay(d, focus),
|
|
330
|
+
rangeStart: r !== null && r.rangeStart,
|
|
331
|
+
rangeEnd: r !== null && r.rangeEnd,
|
|
332
|
+
inRange: r !== null && r.inRange,
|
|
288
333
|
});
|
|
289
334
|
}
|
|
290
335
|
return cells;
|
|
291
336
|
// Deps intentionally mirror the port recipe, not every closure read: minDate/maxDate,
|
|
292
337
|
// dayLabelFmt and effectiveFocusDate all derive deterministically from the listed deps
|
|
293
338
|
// within the same render pass, so this stays in sync without listing them separately.
|
|
294
|
-
|
|
339
|
+
// The range ends are listed by what they derive from: the context's value and labels, and the
|
|
340
|
+
// anchor and preview state.
|
|
341
|
+
}, [viewMonth, props.value, props.min, props.max, activeLocale, isMonthMode,
|
|
342
|
+
range?.start, range?.end, range?.rangeStartLabel, range?.rangeEndLabel, anchor, preview]);
|
|
295
343
|
|
|
296
344
|
// Focus the roving-tabindex cell after a render triggered by moveFocus/openPanel (React's
|
|
297
345
|
// analogue of Vue's `nextTick(focusCell)`). Runs after every render; cheap no-op otherwise.
|
|
@@ -415,10 +463,38 @@ export function FdyDatepicker(props: FdyDatepickerProps): JSX.Element {
|
|
|
415
463
|
setFocusDate(next);
|
|
416
464
|
setViewMonth(new Date(next.getFullYear(), next.getMonth(), 1));
|
|
417
465
|
pendingFocusRef.current = toISO(next);
|
|
466
|
+
// The keyboard previews the band exactly as the pointer does.
|
|
467
|
+
if (anchor !== null) setPreview(startOfDay(next));
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/** The day under the pointer previews the band while a start waits. Ignored otherwise. */
|
|
471
|
+
function onDayHover(d: Date): void {
|
|
472
|
+
if (anchor === null || isDayDisabled(d)) return;
|
|
473
|
+
setPreview(startOfDay(d));
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/* First pick anchors and keeps the panel open; a pick on or after it commits the whole range and
|
|
477
|
+
closes; a pick before it becomes the new start. The rule itself is adapters/core/date-range.js. */
|
|
478
|
+
function pickRange(field: FdyRangeFieldContext, d: Date): void {
|
|
479
|
+
const day: Date = startOfDay(d);
|
|
480
|
+
setFocusDate(day);
|
|
481
|
+
const next = rangeAfterPick(anchor, day);
|
|
482
|
+
if (next.kind === 'anchor') {
|
|
483
|
+
setAnchor(next.anchor);
|
|
484
|
+
setPreview(next.anchor);
|
|
485
|
+
pendingFocusRef.current = toISO(day);
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
field.commit(toISO(next.start), toISO(next.end));
|
|
489
|
+
closePanel(true);
|
|
418
490
|
}
|
|
419
491
|
|
|
420
492
|
function pick(d: Date): void {
|
|
421
493
|
if (isDayDisabled(d)) return;
|
|
494
|
+
if (range !== null) {
|
|
495
|
+
pickRange(range, d);
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
422
498
|
const day: Date = startOfDay(d);
|
|
423
499
|
setFocusDate(day);
|
|
424
500
|
const iso: string = toISO(day);
|
|
@@ -453,6 +529,8 @@ export function FdyDatepicker(props: FdyDatepickerProps): JSX.Element {
|
|
|
453
529
|
|
|
454
530
|
function closePanel(returnFocus: boolean): void {
|
|
455
531
|
if (!open) return;
|
|
532
|
+
setAnchor(null);
|
|
533
|
+
setPreview(null);
|
|
456
534
|
setOpen(false);
|
|
457
535
|
if (returnFocus) triggerRef.current?.focus();
|
|
458
536
|
}
|
|
@@ -535,8 +613,9 @@ export function FdyDatepicker(props: FdyDatepickerProps): JSX.Element {
|
|
|
535
613
|
}, [open]);
|
|
536
614
|
|
|
537
615
|
return (
|
|
538
|
-
<div ref={rootRef}
|
|
539
|
-
|
|
616
|
+
<div ref={rootRef}
|
|
617
|
+
className={['fdy-datepicker', isInvalid ? 'fdy-datepicker--error' : '', range !== null ? 'fdy-datepicker--range' : ''].filter(Boolean).join(' ')}
|
|
618
|
+
data-value={range === null ? (props.value ?? '') : rangeValue} data-fdy-required={props.required === true ? '' : undefined} onBlur={onFocusout}>
|
|
540
619
|
<button
|
|
541
620
|
id={triggerId}
|
|
542
621
|
ref={triggerRef}
|
|
@@ -544,6 +623,7 @@ export function FdyDatepicker(props: FdyDatepickerProps): JSX.Element {
|
|
|
544
623
|
className={open ? 'fdy-datepicker__trigger is-open' : 'fdy-datepicker__trigger'}
|
|
545
624
|
aria-haspopup="dialog"
|
|
546
625
|
aria-expanded={open}
|
|
626
|
+
aria-label={range?.ariaLabel}
|
|
547
627
|
aria-labelledby={props.ariaLabelledby}
|
|
548
628
|
aria-invalid={isInvalid ? 'true' : undefined}
|
|
549
629
|
aria-readonly={isReadonly ? 'true' : undefined}
|
|
@@ -551,7 +631,7 @@ export function FdyDatepicker(props: FdyDatepickerProps): JSX.Element {
|
|
|
551
631
|
disabled={isDisabled}
|
|
552
632
|
onClick={toggle}
|
|
553
633
|
>
|
|
554
|
-
<span className={
|
|
634
|
+
<span className={hasValue ? 'fdy-datepicker__value' : 'fdy-datepicker__value fdy-datepicker__value--placeholder'}>
|
|
555
635
|
{displayValue}
|
|
556
636
|
</span>
|
|
557
637
|
<span className={showClear ? 'fdy-datepicker__icon is-hidden' : 'fdy-datepicker__icon'} aria-hidden="true">
|
|
@@ -669,6 +749,7 @@ export function FdyDatepicker(props: FdyDatepickerProps): JSX.Element {
|
|
|
669
749
|
aria-selected={cell.selected ? 'true' : undefined}
|
|
670
750
|
disabled={cell.disabled}
|
|
671
751
|
tabIndex={cell.focusable ? 0 : -1}
|
|
752
|
+
onMouseEnter={(): void => onDayHover(cell.date)}
|
|
672
753
|
onClick={(): void => pick(cell.date)}
|
|
673
754
|
>
|
|
674
755
|
{cell.label}
|
|
@@ -63,7 +63,7 @@ export { FdyDatepicker, type FdyDatepickerProps, type FdyDatepickerMode } from '
|
|
|
63
63
|
* `mode="month"`, where one prop carried two formats and another prop chose between them. */
|
|
64
64
|
export { FdyMonthpicker, type FdyMonthpickerProps } from './components/FdyMonthpicker';
|
|
65
65
|
export type { FdyMonth } from '../core/month-grid';
|
|
66
|
-
export { FdyDateRange, type FdyDateRangeProps, type DateRangeValue } from './components/FdyDateRange';
|
|
66
|
+
export { FdyDateRange, type FdyDateRangeProps, type DateRangeValue, type FdyDateRangeLayout } from './components/FdyDateRange';
|
|
67
67
|
export { FdyTimepicker, type FdyTimepickerProps } from './components/FdyTimepicker';
|
|
68
68
|
export { FdyAutocomplete, type FdyAutocompleteProps } from './components/FdyAutocomplete';
|
|
69
69
|
export { FdyCascade, type FdyCascadeProps, type CascadeNode } from './components/FdyCascade';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createContext } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What `FdyDateRange layout="single"` hands the one `FdyDatepicker` it renders, so the range is drawn
|
|
5
|
+
* by the same calendar as a single date rather than by a second copy of it.
|
|
6
|
+
*
|
|
7
|
+
* Internal, and deliberately not a prop: a range prop on `FdyDatepicker` would widen that
|
|
8
|
+
* component's public type with a value shape decided by another prop, which is the defect that
|
|
9
|
+
* retired `mode="month"`. Only `FdyDateRange` provides it, around the single-field picker alone, and
|
|
10
|
+
* `index.js` does not export it.
|
|
11
|
+
*/
|
|
12
|
+
export interface FdyRangeFieldContext {
|
|
13
|
+
/** ISO `YYYY-MM-DD` ends of the committed range; either one empty means no range. */
|
|
14
|
+
start: string | null;
|
|
15
|
+
end: string | null;
|
|
16
|
+
/** The trigger's accessible name, since there is no group around one button to carry it. */
|
|
17
|
+
ariaLabel: string | undefined;
|
|
18
|
+
/** Appended to an end day's accessible name. */
|
|
19
|
+
rangeStartLabel: string;
|
|
20
|
+
rangeEndLabel: string;
|
|
21
|
+
/** A whole range was picked. */
|
|
22
|
+
commit: (start: string, end: string) => void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const FdyRangeField = createContext<FdyRangeFieldContext | null>(null);
|
|
@@ -3,20 +3,37 @@ export interface DateRangeValue {
|
|
|
3
3
|
start: string | null;
|
|
4
4
|
end: string | null;
|
|
5
5
|
}
|
|
6
|
+
|
|
7
|
+
/** `split`: two linked pickers. `single`: one field, one calendar, both ends picked in it. */
|
|
8
|
+
export type FdyDateRangeLayout = 'split' | 'single';
|
|
6
9
|
</script>
|
|
7
10
|
|
|
8
11
|
<script setup lang="ts">
|
|
9
|
-
import { computed, type ComputedRef } from 'vue';
|
|
12
|
+
import { computed, provide, type ComputedRef } from 'vue';
|
|
10
13
|
import FdyDatepicker from './FdyDatepicker.vue';
|
|
14
|
+
import { FDY_RANGE_FIELD, type FdyRangeFieldContext } from '../rangeField';
|
|
11
15
|
|
|
12
|
-
// A controlled date range
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
16
|
+
// A controlled date range, in one of two layouts that share the value and every prop below.
|
|
17
|
+
//
|
|
18
|
+
// `split` (the default) is freeday's `.fdy-daterange` (src/components/datepicker.css): two linked
|
|
19
|
+
// FdyDatepickers where the end can't precede the start (start.max = end, end.min = start). The
|
|
20
|
+
// vanilla enhancer's cross-calendar `.in-range` shading is not reproduced there (each picker is
|
|
21
|
+
// independent); the min/max linkage is what keeps the range valid.
|
|
22
|
+
//
|
|
23
|
+
// `single` is ONE FdyDatepicker that draws the range itself, told so through FDY_RANGE_FIELD rather
|
|
24
|
+
// than a prop of its own. The first day picked is the start, the second the end, and the value
|
|
25
|
+
// changes only when the range is whole, so `change` and `settled` arrive together.
|
|
17
26
|
|
|
18
27
|
const props = defineProps<{
|
|
19
28
|
modelValue: DateRangeValue;
|
|
29
|
+
/** `'split'` (default): two pickers. `'single'`: one field holding both dates. */
|
|
30
|
+
layout?: FdyDateRangeLayout;
|
|
31
|
+
/** The empty field's text under `layout="single"`. Default 'Select dates'. */
|
|
32
|
+
placeholder?: string;
|
|
33
|
+
/** Added to the accessible name of the range's first and last day under `layout="single"`, so
|
|
34
|
+
* an end is not told by colour alone. Defaults 'range start' / 'range end'. */
|
|
35
|
+
rangeStartLabel?: string;
|
|
36
|
+
rangeEndLabel?: string;
|
|
20
37
|
min?: string;
|
|
21
38
|
max?: string;
|
|
22
39
|
locale?: string;
|
|
@@ -71,10 +88,43 @@ function setStart(start: string): void {
|
|
|
71
88
|
function setEnd(end: string): void {
|
|
72
89
|
commit({ start: props.modelValue.start, end });
|
|
73
90
|
}
|
|
91
|
+
|
|
92
|
+
const rangeField: FdyRangeFieldContext = {
|
|
93
|
+
enabled: computed((): boolean => props.layout === 'single'),
|
|
94
|
+
start: computed((): string | null => props.modelValue.start),
|
|
95
|
+
end: computed((): string | null => props.modelValue.end),
|
|
96
|
+
ariaLabel: computed((): string | undefined => props.ariaLabel),
|
|
97
|
+
rangeStartLabel: computed((): string => props.rangeStartLabel ?? 'range start'),
|
|
98
|
+
rangeEndLabel: computed((): string => props.rangeEndLabel ?? 'range end'),
|
|
99
|
+
commit: (start: string, end: string): void => commit({ start, end }),
|
|
100
|
+
};
|
|
101
|
+
provide(FDY_RANGE_FIELD, rangeField);
|
|
74
102
|
</script>
|
|
75
103
|
|
|
76
104
|
<template>
|
|
77
|
-
<
|
|
105
|
+
<FdyDatepicker
|
|
106
|
+
v-if="layout === 'single'"
|
|
107
|
+
:model-value="modelValue.start"
|
|
108
|
+
:min="min"
|
|
109
|
+
:max="max"
|
|
110
|
+
:locale="locale"
|
|
111
|
+
:disabled="disabled"
|
|
112
|
+
:readonly="readonly"
|
|
113
|
+
:invalid="invalid"
|
|
114
|
+
:required="required"
|
|
115
|
+
:describedby="describedby"
|
|
116
|
+
:aria-labelledby="ariaLabelledby"
|
|
117
|
+
:placeholder="placeholder ?? 'Select dates'"
|
|
118
|
+
:prev-month-label="prevMonthLabel"
|
|
119
|
+
:next-month-label="nextMonthLabel"
|
|
120
|
+
:prev-year-label="prevYearLabel"
|
|
121
|
+
:next-year-label="nextYearLabel"
|
|
122
|
+
:prev-years-label="prevYearsLabel"
|
|
123
|
+
:next-years-label="nextYearsLabel"
|
|
124
|
+
:choose-month-label="chooseMonthLabel"
|
|
125
|
+
:choose-year-label="chooseYearLabel"
|
|
126
|
+
/>
|
|
127
|
+
<div v-else class="fdy-daterange" role="group" :aria-label="ariaLabel" :aria-labelledby="ariaLabelledby">
|
|
78
128
|
<FdyDatepicker
|
|
79
129
|
:model-value="modelValue.start"
|
|
80
130
|
:min="min"
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
|
-
import { computed, nextTick, onBeforeUnmount, onMounted, ref, useId, type ComputedRef, type Ref } from 'vue';
|
|
2
|
+
import { computed, inject, nextTick, onBeforeUnmount, onMounted, ref, useId, type ComputedRef, type Ref } from 'vue';
|
|
3
3
|
import { usePopover } from '../usePopover';
|
|
4
|
+
import { FDY_RANGE_FIELD, type FdyRangeFieldContext } from '../rangeField';
|
|
5
|
+
import { rangeAfterPick, rangeDayState, shownEnds, type FdyRangeDayState, type FdyShownEnds } from '../../core/date-range.js';
|
|
4
6
|
/* The month and year grids, which `FdyMonthpicker` reads from the same module. They were written
|
|
5
7
|
here and moved out when the month picker arrived: a second copy of the bounds rule is a second
|
|
6
8
|
place to fix it, and this release spent three items on exactly that failure in the panel-position
|
|
@@ -35,7 +37,9 @@ import {
|
|
|
35
37
|
// re-implements its single-date calendar behavior (month grid, keyboard nav,
|
|
36
38
|
// min/max clamping, Intl locale formatting) in Vue, giving a real `v-model`
|
|
37
39
|
// over the same kit classes. Open/close (outside-click + focusout) mirrors the
|
|
38
|
-
// already-approved FdyCombo.vue pattern.
|
|
40
|
+
// already-approved FdyCombo.vue pattern. The range in ONE field is drawn here too, when
|
|
41
|
+
// FdyDateRange layout="single" provides FDY_RANGE_FIELD (see ../rangeField.ts); without it
|
|
42
|
+
// this is the single-date picker, unchanged.
|
|
39
43
|
|
|
40
44
|
/** Which grid is on screen. The drill goes days -> months -> years and back down. */
|
|
41
45
|
type CalendarLevel = 'days' | 'months' | 'years';
|
|
@@ -53,6 +57,10 @@ interface DayCell {
|
|
|
53
57
|
selected: boolean;
|
|
54
58
|
disabled: boolean;
|
|
55
59
|
focusable: boolean;
|
|
60
|
+
/** Range layout only; all false for a single date. */
|
|
61
|
+
rangeStart: boolean;
|
|
62
|
+
rangeEnd: boolean;
|
|
63
|
+
inRange: boolean;
|
|
56
64
|
}
|
|
57
65
|
|
|
58
66
|
const props = defineProps<{
|
|
@@ -181,6 +189,33 @@ const chooseYearLabelText: ComputedRef<string> = computed((): string => props.ch
|
|
|
181
189
|
const prevYearsLabelText: ComputedRef<string> = computed((): string => props.prevYearsLabel ?? 'Previous years');
|
|
182
190
|
const nextYearsLabelText: ComputedRef<string> = computed((): string => props.nextYearsLabel ?? 'Next years');
|
|
183
191
|
|
|
192
|
+
/* The range in one field. `anchor` is a start picked in this opening and not yet answered by an end,
|
|
193
|
+
`preview` the day the pointer or keyboard is on while it waits. Both live here, with the panel
|
|
194
|
+
they belong to, and are dropped by every close that is not a second pick: the value never held
|
|
195
|
+
them, so a half range is never committed. */
|
|
196
|
+
const injectedRange: FdyRangeFieldContext | null = inject(FDY_RANGE_FIELD, null);
|
|
197
|
+
const range: FdyRangeFieldContext | null = injectedRange !== null && injectedRange.enabled.value ? injectedRange : null;
|
|
198
|
+
const anchor: Ref<Date | null> = ref(null);
|
|
199
|
+
const preview: Ref<Date | null> = ref(null);
|
|
200
|
+
/** The committed range, or neither end: half an interval, or one running backwards, is no range. */
|
|
201
|
+
const committedRange: ComputedRef<{ start: Date | null; end: Date | null }> = computed(() => {
|
|
202
|
+
const start: Date | null = range === null ? null : parseISO(range.start.value);
|
|
203
|
+
const end: Date | null = range === null ? null : parseISO(range.end.value);
|
|
204
|
+
if (start === null || end === null || end.getTime() < start.getTime()) return { start: null, end: null };
|
|
205
|
+
return { start, end };
|
|
206
|
+
});
|
|
207
|
+
/** "start/end" (ISO 8601 interval) or "": one `data-value`, so a form can require the range. */
|
|
208
|
+
const rangeValue: ComputedRef<string> = computed((): string => {
|
|
209
|
+
const { start, end } = committedRange.value;
|
|
210
|
+
return start === null || end === null ? '' : `${toISO(start)}/${toISO(end)}`;
|
|
211
|
+
});
|
|
212
|
+
const ends: ComputedRef<FdyShownEnds> = computed((): FdyShownEnds => shownEnds({
|
|
213
|
+
start: committedRange.value.start,
|
|
214
|
+
end: committedRange.value.end,
|
|
215
|
+
anchor: anchor.value,
|
|
216
|
+
preview: preview.value,
|
|
217
|
+
}));
|
|
218
|
+
|
|
184
219
|
const view: Ref<Date> = ref(startOfMonth(selectedDate.value ?? new Date()));
|
|
185
220
|
const focusDate: Ref<Date> = ref(selectedDate.value ?? new Date());
|
|
186
221
|
|
|
@@ -250,6 +285,10 @@ const dowFmt: ComputedRef<Intl.DateTimeFormat> = computed(
|
|
|
250
285
|
already use, which is the whole point of the mode: <input type="month"> names the month in the
|
|
251
286
|
browser's language, this names it in the page's. */
|
|
252
287
|
const displayValue: ComputedRef<string> = computed((): string => {
|
|
288
|
+
if (range !== null) {
|
|
289
|
+
const { start, end } = committedRange.value;
|
|
290
|
+
return start === null || end === null ? displayPlaceholder.value : `${valueFmt.value.format(start)} – ${valueFmt.value.format(end)}`;
|
|
291
|
+
}
|
|
253
292
|
if (selectedDate.value === null) return displayPlaceholder.value;
|
|
254
293
|
const fmt: Intl.DateTimeFormat = isMonthMode.value ? formatters.value.name : valueFmt.value;
|
|
255
294
|
return fmt.format(selectedDate.value);
|
|
@@ -275,21 +314,53 @@ const gridCells: ComputedRef<DayCell[]> = computed((): DayCell[] => {
|
|
|
275
314
|
const cells: DayCell[] = [];
|
|
276
315
|
for (let i = 0; i < 42; i++) {
|
|
277
316
|
const d: Date = addDays(startCell, i);
|
|
317
|
+
const name: string = dayLabelFmt.value.format(d);
|
|
318
|
+
const r: FdyRangeDayState | null = range === null ? null : rangeDayState(startOfDay(d), ends.value);
|
|
319
|
+
const role: string | null = range === null || r === null || r.role === null
|
|
320
|
+
? null
|
|
321
|
+
: r.role === 'start' ? range.rangeStartLabel.value : range.rangeEndLabel.value;
|
|
278
322
|
cells.push({
|
|
279
323
|
date: d,
|
|
280
324
|
iso: toISO(d),
|
|
281
325
|
label: d.getDate(),
|
|
282
|
-
ariaLabel:
|
|
326
|
+
ariaLabel: role === null ? name : `${name}, ${role}`,
|
|
283
327
|
outside: d.getMonth() !== view.value.getMonth(),
|
|
284
328
|
today: sameDay(d, today),
|
|
285
|
-
selected: sameDay(d, selectedDate.value),
|
|
329
|
+
selected: r === null ? sameDay(d, selectedDate.value) : r.filled,
|
|
286
330
|
disabled: isDayDisabled(d),
|
|
287
331
|
focusable: sameDay(d, focus),
|
|
332
|
+
rangeStart: r !== null && r.rangeStart,
|
|
333
|
+
rangeEnd: r !== null && r.rangeEnd,
|
|
334
|
+
inRange: r !== null && r.inRange,
|
|
288
335
|
});
|
|
289
336
|
}
|
|
290
337
|
return cells;
|
|
291
338
|
});
|
|
292
339
|
|
|
340
|
+
/** The day under the pointer previews the band while a start waits. Ignored otherwise. */
|
|
341
|
+
function onDayHover(d: Date): void {
|
|
342
|
+
if (anchor.value === null || isDayDisabled(d)) return;
|
|
343
|
+
preview.value = startOfDay(d);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/* First pick anchors and keeps the panel open; a pick on or after it commits the whole range and
|
|
347
|
+
closes; a pick before it becomes the new start. The rule itself is adapters/core/date-range.js. */
|
|
348
|
+
function pickRange(field: FdyRangeFieldContext, d: Date): void {
|
|
349
|
+
const day: Date = startOfDay(d);
|
|
350
|
+
focusDate.value = day;
|
|
351
|
+
const next = rangeAfterPick(anchor.value, day);
|
|
352
|
+
if (next.kind === 'anchor') {
|
|
353
|
+
anchor.value = next.anchor;
|
|
354
|
+
preview.value = next.anchor;
|
|
355
|
+
void nextTick((): void => focusCell());
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
anchor.value = null;
|
|
359
|
+
preview.value = null;
|
|
360
|
+
field.commit(toISO(next.start), toISO(next.end));
|
|
361
|
+
closePanel(true);
|
|
362
|
+
}
|
|
363
|
+
|
|
293
364
|
function setDayRef(iso: string, el: Element | null): void {
|
|
294
365
|
dayRefs[iso] = el as HTMLButtonElement | null;
|
|
295
366
|
}
|
|
@@ -300,6 +371,8 @@ function focusCell(): void {
|
|
|
300
371
|
|
|
301
372
|
function moveFocus(next: Date): void {
|
|
302
373
|
focusDate.value = next;
|
|
374
|
+
// The keyboard previews the band exactly as the pointer does.
|
|
375
|
+
if (anchor.value !== null) preview.value = startOfDay(next);
|
|
303
376
|
view.value = new Date(next.getFullYear(), next.getMonth(), 1);
|
|
304
377
|
void nextTick((): void => focusCell());
|
|
305
378
|
}
|
|
@@ -400,6 +473,10 @@ function onMonthKeydown(e: KeyboardEvent): void {
|
|
|
400
473
|
|
|
401
474
|
function pick(d: Date): void {
|
|
402
475
|
if (isDayDisabled(d)) return;
|
|
476
|
+
if (range !== null) {
|
|
477
|
+
pickRange(range, d);
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
403
480
|
const day: Date = startOfDay(d);
|
|
404
481
|
focusDate.value = day;
|
|
405
482
|
const iso: string = toISO(day);
|
|
@@ -435,6 +512,8 @@ function openPanel(): void {
|
|
|
435
512
|
|
|
436
513
|
function closePanel(returnFocus: boolean): void {
|
|
437
514
|
if (!open.value) return;
|
|
515
|
+
anchor.value = null;
|
|
516
|
+
preview.value = null;
|
|
438
517
|
open.value = false;
|
|
439
518
|
if (returnFocus) triggerEl.value?.focus();
|
|
440
519
|
}
|
|
@@ -524,8 +603,8 @@ onBeforeUnmount((): void => {
|
|
|
524
603
|
</script>
|
|
525
604
|
|
|
526
605
|
<template>
|
|
527
|
-
<div ref="rootEl" class="fdy-datepicker" :class="{ 'fdy-datepicker--error': isInvalid }"
|
|
528
|
-
:data-value="modelValue ?? ''" :data-fdy-required="required === true ? '' : undefined" @focusout="onFocusout">
|
|
606
|
+
<div ref="rootEl" class="fdy-datepicker" :class="{ 'fdy-datepicker--error': isInvalid, 'fdy-datepicker--range': range !== null }"
|
|
607
|
+
:data-value="range === null ? (modelValue ?? '') : rangeValue" :data-fdy-required="required === true ? '' : undefined" @focusout="onFocusout">
|
|
529
608
|
<button
|
|
530
609
|
:id="triggerId"
|
|
531
610
|
ref="triggerEl"
|
|
@@ -534,6 +613,7 @@ onBeforeUnmount((): void => {
|
|
|
534
613
|
:class="{ 'is-open': open }"
|
|
535
614
|
aria-haspopup="dialog"
|
|
536
615
|
:aria-expanded="open"
|
|
616
|
+
:aria-label="range?.ariaLabel.value"
|
|
537
617
|
:aria-labelledby="ariaLabelledby"
|
|
538
618
|
:aria-invalid="isInvalid ? 'true' : undefined"
|
|
539
619
|
:aria-readonly="isReadonly ? 'true' : undefined"
|
|
@@ -541,7 +621,7 @@ onBeforeUnmount((): void => {
|
|
|
541
621
|
:disabled="isDisabled"
|
|
542
622
|
@click="toggle"
|
|
543
623
|
>
|
|
544
|
-
<span class="fdy-datepicker__value" :class="{ 'fdy-datepicker__value--placeholder': selectedDate === null }">{{ displayValue }}</span>
|
|
624
|
+
<span class="fdy-datepicker__value" :class="{ 'fdy-datepicker__value--placeholder': range === null ? selectedDate === null : rangeValue === '' }">{{ displayValue }}</span>
|
|
545
625
|
<span class="fdy-datepicker__icon" :class="{ 'is-hidden': showClear }" aria-hidden="true">
|
|
546
626
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
547
627
|
<rect x="3" y="4" width="18" height="18" rx="2"></rect>
|
|
@@ -637,12 +717,16 @@ onBeforeUnmount((): void => {
|
|
|
637
717
|
:ref="(el) => setDayRef(cell.iso, el as Element | null)"
|
|
638
718
|
type="button"
|
|
639
719
|
class="fdy-cal__day"
|
|
640
|
-
:class="{
|
|
720
|
+
:class="{
|
|
721
|
+
'is-outside': cell.outside, 'is-today': cell.today, 'is-selected': cell.selected,
|
|
722
|
+
'is-range-start': cell.rangeStart, 'is-range-end': cell.rangeEnd, 'in-range': cell.inRange,
|
|
723
|
+
}"
|
|
641
724
|
role="gridcell"
|
|
642
725
|
:aria-label="cell.ariaLabel"
|
|
643
726
|
:aria-selected="cell.selected ? 'true' : undefined"
|
|
644
727
|
:disabled="cell.disabled"
|
|
645
728
|
:tabindex="cell.focusable ? 0 : -1"
|
|
729
|
+
@mouseenter="onDayHover(cell.date)"
|
|
646
730
|
@click="pick(cell.date)"
|
|
647
731
|
>{{ cell.label }}</button>
|
|
648
732
|
</div>
|
package/adapters/vue/index.d.ts
CHANGED
|
@@ -72,7 +72,7 @@ export { default as FdyMonthpicker } from './components/FdyMonthpicker.vue';
|
|
|
72
72
|
export type { FdyMonth } from '../core/month-grid';
|
|
73
73
|
export { default as FdyDateRange } from './components/FdyDateRange.vue';
|
|
74
74
|
export { default as FdyTimepicker } from './components/FdyTimepicker.vue';
|
|
75
|
-
export type { DateRangeValue } from './components/FdyDateRange.vue';
|
|
75
|
+
export type { DateRangeValue, FdyDateRangeLayout } from './components/FdyDateRange.vue';
|
|
76
76
|
export { default as FdyAutocomplete } from './components/FdyAutocomplete.vue';
|
|
77
77
|
export { default as FdyCascade } from './components/FdyCascade.vue';
|
|
78
78
|
export type { CascadeNode } from './components/FdyCascade.vue';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ComputedRef, InjectionKey } from 'vue';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What `FdyDateRange layout="single"` hands the one `FdyDatepicker` it renders, so the range is drawn
|
|
5
|
+
* by the same calendar as a single date rather than by a second copy of it.
|
|
6
|
+
*
|
|
7
|
+
* Internal, and deliberately not a prop: a range prop on `FdyDatepicker` would widen that
|
|
8
|
+
* component's public type with a value shape decided by another prop, which is the defect that
|
|
9
|
+
* retired `mode="month"`. Nothing outside `FdyDateRange` provides this, and `index.js` does not
|
|
10
|
+
* export it.
|
|
11
|
+
*/
|
|
12
|
+
export interface FdyRangeFieldContext {
|
|
13
|
+
/**
|
|
14
|
+
* False under `layout="split"`, whose two pickers are single dates. `FdyDateRange` provides the
|
|
15
|
+
* context either way, because `provide` runs once per instance; a picker reads this when it mounts,
|
|
16
|
+
* and the two layouts sit in `v-if`/`v-else` branches, so a layout change mounts new pickers.
|
|
17
|
+
*/
|
|
18
|
+
enabled: ComputedRef<boolean>;
|
|
19
|
+
/** ISO `YYYY-MM-DD` ends of the committed range; either one empty means no range. */
|
|
20
|
+
start: ComputedRef<string | null>;
|
|
21
|
+
end: ComputedRef<string | null>;
|
|
22
|
+
/** The trigger's accessible name, since there is no group around one button to carry it. */
|
|
23
|
+
ariaLabel: ComputedRef<string | undefined>;
|
|
24
|
+
/** Appended to an end day's accessible name. */
|
|
25
|
+
rangeStartLabel: ComputedRef<string>;
|
|
26
|
+
rangeEndLabel: ComputedRef<string>;
|
|
27
|
+
/** A whole range was picked. */
|
|
28
|
+
commit: (start: string, end: string) => void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const FDY_RANGE_FIELD: InjectionKey<FdyRangeFieldContext> = Symbol('fdy-range-field');
|