@cahyo-dimas/freeday 3.19.1 → 3.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,61 @@
1
+ // Freeday, the date range in one field, framework-agnostic (pure, zero dependencies).
2
+ //
3
+ // What `FdyDateRange layout="single"` decides on top of the ordinary day grid: which two ends the
4
+ // grid shows while a start waits for its end, what state each day cell is in, and what a pick means.
5
+ // Shared by the Vue and React `FdyDatepicker`, which draw the calendar for that layout, so the two
6
+ // read one copy of the rule.
7
+ //
8
+ // The vanilla enhancer (src/freeday-datepicker.js) holds its own copy, for the reason
9
+ // adapters/core/month-grid.js records: dist/freeday.js is an IIFE with no module system.
10
+ // browser/daterange-single.mjs holds all three to the same observable behaviour.
11
+ //
12
+ // Every Date here is a local midnight; callers normalise with startOfDay before passing one in.
13
+
14
+ function same(a, b) {
15
+ return a !== null && b !== null && a.getTime() === b.getTime();
16
+ }
17
+
18
+ /**
19
+ * The ends the grid shows. With no start pending, the committed range. With one pending, the start
20
+ * and the day being previewed, and that day only when it does not precede the start: a day before it
21
+ * becomes the new start on a click, so a band drawn backwards would promise something a click will
22
+ * not do.
23
+ */
24
+ export function shownEnds(range) {
25
+ if (range.anchor !== null) {
26
+ const to = range.preview !== null && range.preview.getTime() >= range.anchor.getTime() ? range.preview : null;
27
+ return { from: range.anchor, to, pending: true };
28
+ }
29
+ return { from: range.start, to: range.end, pending: false };
30
+ }
31
+
32
+ /**
33
+ * One day cell's range state. `filled` is an end that is an answer (a committed end, or the start
34
+ * just picked); a previewed end is not one. `rangeStart`/`rangeEnd` shape the ends of a band of two
35
+ * days or more, and `role` is what the cell's accessible name adds, so an end is not told by colour.
36
+ */
37
+ export function rangeDayState(day, ends) {
38
+ const isFrom = ends.from !== null && same(day, ends.from);
39
+ const isTo = ends.to !== null && same(day, ends.to);
40
+ const span = ends.from !== null && ends.to !== null && !same(ends.from, ends.to);
41
+ const t = day.getTime();
42
+ const between = span && t > ends.from.getTime() && t < ends.to.getTime();
43
+ return {
44
+ filled: isFrom || (isTo && !ends.pending),
45
+ rangeStart: isFrom && span,
46
+ rangeEnd: isTo && span,
47
+ inRange: between || (span && isTo && ends.pending),
48
+ role: isFrom ? 'start' : isTo && !ends.pending ? 'end' : null,
49
+ };
50
+ }
51
+
52
+ /**
53
+ * What picking `day` does. With no start pending, or a day before the pending one, it becomes the
54
+ * start and the panel stays open. Otherwise the range is whole: same day included, a one-day range.
55
+ * A pick before the start is not swapped into the end, which would leave the reader unsure which
56
+ * end they just moved.
57
+ */
58
+ export function rangeAfterPick(anchor, day) {
59
+ if (anchor === null || day.getTime() < anchor.getTime()) return { kind: 'anchor', anchor: day };
60
+ return { kind: 'commit', start: anchor, end: day };
61
+ }
@@ -1,19 +1,37 @@
1
1
  import type { JSX } from 'react';
2
2
  import { FdyDatepicker } from './FdyDatepicker';
3
+ import { FdyRangeField, type FdyRangeFieldContext } from '../rangeField';
3
4
 
4
- // A controlled date range over freeday's `.fdy-daterange` layout (src/components/datepicker.css):
5
- // two linked FdyDatepickers where the end can't precede the start (start.max = end, end.min = start).
6
- // `value` + `onChange` in place of Vue's `v-model`; this composes the single-date picker rather than
7
- // re-implementing the calendar. Note: the vanilla enhancer's cross-calendar `.in-range` day shading
8
- // is not reproduced (each picker is independent), the min/max linkage is what keeps the range valid.
5
+ // A controlled date range, in one of two layouts that share the value and every prop below.
6
+ // `value` + `onChange` in place of Vue's `v-model`.
7
+ //
8
+ // `split` (the default) is freeday's `.fdy-daterange` (src/components/datepicker.css): two linked
9
+ // FdyDatepickers where the end can't precede the start (start.max = end, end.min = start). The
10
+ // vanilla enhancer's cross-calendar `.in-range` shading is not reproduced there (each picker is
11
+ // independent); the min/max linkage is what keeps the range valid.
12
+ //
13
+ // `single` is ONE FdyDatepicker that draws the range itself, told so through the FdyRangeField
14
+ // context rather than a prop of its own. The first day picked is the start, the second the end, and
15
+ // the value changes only when the range is whole, so `onChange` and `onSettled` arrive together.
9
16
 
10
17
  export interface DateRangeValue {
11
18
  start: string | null;
12
19
  end: string | null;
13
20
  }
14
21
 
22
+ /** `split`: two linked pickers. `single`: one field, one calendar, both ends picked in it. */
23
+ export type FdyDateRangeLayout = 'split' | 'single';
24
+
15
25
  export interface FdyDateRangeProps {
16
26
  value: DateRangeValue;
27
+ /** `'split'` (default): two pickers. `'single'`: one field holding both dates. */
28
+ layout?: FdyDateRangeLayout;
29
+ /** The empty field's text under `layout="single"`. Default 'Select dates'. */
30
+ placeholder?: string;
31
+ /** Added to the accessible name of the range's first and last day under `layout="single"`, so
32
+ * an end is not told by colour alone. Defaults 'range start' / 'range end'. */
33
+ rangeStartLabel?: string;
34
+ rangeEndLabel?: string;
17
35
  onChange: (value: DateRangeValue) => void;
18
36
  /** Both ends chosen, or both cleared: the only states worth reloading a list for (#063). */
19
37
  onSettled?: (value: DateRangeValue) => void;
@@ -55,6 +73,44 @@ export function FdyDateRange(props: FdyDateRangeProps): JSX.Element {
55
73
  const onStart = (start: string): void => commit({ start, end: props.value.end });
56
74
  const onEnd = (end: string): void => commit({ start: props.value.start, end });
57
75
 
76
+ if (props.layout === 'single') {
77
+ const field: FdyRangeFieldContext = {
78
+ start: props.value.start,
79
+ end: props.value.end,
80
+ ariaLabel: props.ariaLabel,
81
+ rangeStartLabel: props.rangeStartLabel ?? 'range start',
82
+ rangeEndLabel: props.rangeEndLabel ?? 'range end',
83
+ commit: (start: string, end: string): void => commit({ start, end }),
84
+ };
85
+ return (
86
+ <FdyRangeField.Provider value={field}>
87
+ <FdyDatepicker
88
+ value={props.value.start}
89
+ /* Never called: under the range context the picker commits through `field.commit`. */
90
+ onChange={(): void => undefined}
91
+ min={props.min}
92
+ max={props.max}
93
+ locale={props.locale}
94
+ disabled={props.disabled}
95
+ readonly={props.readonly}
96
+ invalid={props.invalid}
97
+ required={props.required}
98
+ describedby={props.describedby}
99
+ ariaLabelledby={props.ariaLabelledby}
100
+ placeholder={props.placeholder ?? 'Select dates'}
101
+ prevMonthLabel={props.prevMonthLabel}
102
+ nextMonthLabel={props.nextMonthLabel}
103
+ prevYearLabel={props.prevYearLabel}
104
+ nextYearLabel={props.nextYearLabel}
105
+ prevYearsLabel={props.prevYearsLabel}
106
+ nextYearsLabel={props.nextYearsLabel}
107
+ chooseMonthLabel={props.chooseMonthLabel}
108
+ chooseYearLabel={props.chooseYearLabel}
109
+ />
110
+ </FdyRangeField.Provider>
111
+ );
112
+ }
113
+
58
114
  return (
59
115
  <div
60
116
  className="fdy-daterange"
@@ -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. Range mode is
35
- // out of scope (v1 = single date).
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 displayValue: string = selectedDate === null
240
- ? displayPlaceholder
241
- : (isMonthMode ? formatters.name : valueFmt).format(selectedDate);
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: dayLabelFmt.format(d),
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
- }, [viewMonth, props.value, props.min, props.max, activeLocale, isMonthMode]);
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} className={isInvalid ? 'fdy-datepicker fdy-datepicker--error' : 'fdy-datepicker'}
539
- data-value={props.value ?? ''} data-fdy-required={props.required === true ? '' : undefined} onBlur={onFocusout}>
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={selectedDate === null ? 'fdy-datepicker__value fdy-datepicker__value--placeholder' : 'fdy-datepicker__value'}>
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 over freeday's `.fdy-daterange` layout (src/components/datepicker.css):
13
- // two linked FdyDatepickers where the end can't precede the start (start.max = end, end.min =
14
- // start). A real `v-model:DateRangeValue` that composes the single-date picker rather than
15
- // re-implementing the calendar. The vanilla enhancer's cross-calendar `.in-range` day shading is
16
- // not reproduced (each picker is independent), the min/max linkage is what keeps the range valid.
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
- <div class="fdy-daterange" role="group" :aria-label="ariaLabel" :aria-labelledby="ariaLabelledby">
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"