@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.
- package/CHANGELOG.md +100 -2
- package/COMPONENTS.md +47 -5
- package/README.id.md +5 -3
- package/README.md +5 -3
- package/USAGE.md +116 -2
- 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 +561 -225
- package/dist/freeday.css +15 -0
- package/dist/freeday.js +150 -7
- package/dist/freeday.tokens.css +546 -225
- package/docs/agent-onboarding.md +25 -12
- package/docs/getting-started.md +3 -3
- package/package.json +2 -2
- package/src/components/datepicker.css +15 -0
- package/tokens/tokens.json +69 -45
|
@@ -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');
|
|
@@ -11,6 +11,11 @@
|
|
|
11
11
|
* <div data-fdy-datepicker data-role="to" data-placeholder="To"></div>
|
|
12
12
|
* </div>
|
|
13
13
|
* The range links the two: the end can never precede the start (out-of-range days disable).
|
|
14
|
+
* - Range in ONE field: <div data-fdy-daterange data-layout="single" data-value="2026-11-01/2026-11-30"
|
|
15
|
+
* data-label="Period"></div>
|
|
16
|
+
* One trigger, one calendar: the first day picked is the start, the second the end. The value is
|
|
17
|
+
* "start/end" (ISO 8601 interval) or "", and it changes only when the range is whole, announced
|
|
18
|
+
* by "fdy-daterange-settled" {from, to} on the root. Escape or a click outside drops half a range.
|
|
14
19
|
* - Month: <div data-fdy-datepicker data-mode="month" data-value="2026-10"></div>
|
|
15
20
|
* The calendar opens ON the month grid and stops there; the value is "YYYY-MM".
|
|
16
21
|
*
|
|
@@ -54,7 +59,14 @@
|
|
|
54
59
|
nextYears: 'Next years',
|
|
55
60
|
chooseMonth: '{label}, choose month',
|
|
56
61
|
chooseYear: '{label}, choose year',
|
|
57
|
-
backToMonths: '{start} to {end}, back to months'
|
|
62
|
+
backToMonths: '{start} to {end}, back to months',
|
|
63
|
+
/* The single-field range's own pair, for the same reason month mode has one: "Choose a date"
|
|
64
|
+
on a field that wants two of them tells the reader half the job. */
|
|
65
|
+
labelRange: 'Date range',
|
|
66
|
+
placeholderRange: 'Choose dates',
|
|
67
|
+
/* Appended to a day's accessible name, so an end of the range is not told by colour alone. */
|
|
68
|
+
rangeStart: 'range start',
|
|
69
|
+
rangeEnd: 'range end'
|
|
58
70
|
};
|
|
59
71
|
/* HTML lowercases attribute names, so a camelCase key like `prevMonth` can only ever be written
|
|
60
72
|
as `data-fdy-text-prevmonth`, while the kebab form anybody would reach for,
|
|
@@ -95,6 +107,16 @@
|
|
|
95
107
|
var d = new Date(Number(p[0]), Number(p[1]) - 1, p.length > 2 ? Number(p[2]) : 1);
|
|
96
108
|
return isNaN(d.getTime()) ? null : d;
|
|
97
109
|
}
|
|
110
|
+
/* "start/end", the ISO 8601 interval form, so a range is ONE `data-value` like every other kit
|
|
111
|
+
control and `freeday-form.js` can require it without knowing it is a range. Half an interval,
|
|
112
|
+
or an end before its start, reads as empty: the field never shows a range it would not commit. */
|
|
113
|
+
function parseInterval(s) {
|
|
114
|
+
var p = String(s || '').split('/');
|
|
115
|
+
var a = p.length === 2 ? parseISO(p[0]) : null;
|
|
116
|
+
var b = p.length === 2 ? parseISO(p[1]) : null;
|
|
117
|
+
if (!a || !b || b.getTime() < a.getTime()) return { start: null, end: null };
|
|
118
|
+
return { start: a, end: b };
|
|
119
|
+
}
|
|
98
120
|
function startOfDay(d) { return new Date(d.getFullYear(), d.getMonth(), d.getDate()); }
|
|
99
121
|
function sameDay(a, b) { return !!a && !!b && a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate(); }
|
|
100
122
|
function addDays(d, n) { var x = new Date(d); x.setDate(x.getDate() + n); return x; }
|
|
@@ -126,10 +148,26 @@
|
|
|
126
148
|
opens on it, Enter and a click commit from it, the day grid is never built, and the value is
|
|
127
149
|
"YYYY-MM". Anything else, including the attribute's absence, is the day picker unchanged. */
|
|
128
150
|
var monthOnly = wrap.getAttribute('data-mode') === 'month';
|
|
151
|
+
/* A `data-fdy-daterange` root reaches this function only for `data-layout="single"`: the range
|
|
152
|
+
in one field. It is the same calendar with a different answer, so it lives here rather than
|
|
153
|
+
in a second copy of everything below. `selected` holds the start and `selectedEnd` the end;
|
|
154
|
+
`anchor` is a start picked in this opening and not yet answered by an end, and `preview` the
|
|
155
|
+
day the pointer or the keyboard is on while it waits. */
|
|
156
|
+
var rangeMode = wrap.hasAttribute('data-fdy-daterange');
|
|
129
157
|
var parseVal = monthOnly ? parseISOMonth : parseISO;
|
|
130
|
-
var placeholder = wrap.getAttribute('data-placeholder') || textOf(wrap, monthOnly ? 'placeholderMonth' : 'placeholder');
|
|
131
|
-
var label = wrap.getAttribute('data-label') || textOf(wrap, monthOnly ? 'labelMonth' : 'label');
|
|
132
|
-
var
|
|
158
|
+
var placeholder = wrap.getAttribute('data-placeholder') || textOf(wrap, monthOnly ? 'placeholderMonth' : rangeMode ? 'placeholderRange' : 'placeholder');
|
|
159
|
+
var label = wrap.getAttribute('data-label') || (rangeMode && wrap.getAttribute('aria-label')) || textOf(wrap, monthOnly ? 'labelMonth' : rangeMode ? 'labelRange' : 'label');
|
|
160
|
+
var seed = rangeMode ? parseInterval(wrap.getAttribute('data-value')) : null;
|
|
161
|
+
var selected = rangeMode ? seed.start : parseVal(wrap.getAttribute('data-value'));
|
|
162
|
+
var selectedEnd = rangeMode ? seed.end : null;
|
|
163
|
+
var anchor = null, preview = null;
|
|
164
|
+
if (rangeMode) {
|
|
165
|
+
wrap.classList.add('fdy-datepicker--range');
|
|
166
|
+
/* The group role the split layout needs would wrap one button here; the trigger carries
|
|
167
|
+
the name instead. */
|
|
168
|
+
wrap.removeAttribute('role');
|
|
169
|
+
wrap.removeAttribute('aria-label');
|
|
170
|
+
}
|
|
133
171
|
var minDate = parseVal(wrap.getAttribute('data-min'));
|
|
134
172
|
var maxDate = parseVal(wrap.getAttribute('data-max'));
|
|
135
173
|
var rangeStart = null, rangeEnd = null; // for in-range shading
|
|
@@ -175,7 +213,22 @@
|
|
|
175
213
|
wrap.appendChild(trigger);
|
|
176
214
|
wrap.appendChild(panel);
|
|
177
215
|
|
|
216
|
+
function rangeValue() {
|
|
217
|
+
return selected && selectedEnd ? toISO(selected) + '/' + toISO(selectedEnd) : '';
|
|
218
|
+
}
|
|
219
|
+
|
|
178
220
|
function updateDisplay() {
|
|
221
|
+
if (rangeMode) {
|
|
222
|
+
wrap.setAttribute('data-value', rangeValue());
|
|
223
|
+
if (selected && selectedEnd) {
|
|
224
|
+
valueSpan.textContent = valueFmt.format(selected) + ' – ' + valueFmt.format(selectedEnd);
|
|
225
|
+
valueSpan.classList.remove('fdy-datepicker__value--placeholder');
|
|
226
|
+
} else {
|
|
227
|
+
valueSpan.textContent = placeholder;
|
|
228
|
+
valueSpan.classList.add('fdy-datepicker__value--placeholder');
|
|
229
|
+
}
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
179
232
|
/* Mirror the value onto the ROOT, which until now only seeded it. `freeday-select.js` has
|
|
180
233
|
always done this, so this makes the family agree rather than inventing a second idea, and
|
|
181
234
|
it is what lets `freeday-form.js` require a kit control without knowing which control it
|
|
@@ -284,7 +337,8 @@
|
|
|
284
337
|
btn.textContent = d.getDate();
|
|
285
338
|
if (d.getMonth() !== view.getMonth()) btn.classList.add('is-outside');
|
|
286
339
|
if (sameDay(d, today)) btn.classList.add('is-today');
|
|
287
|
-
if (
|
|
340
|
+
if (rangeMode) paintRange(btn, d);
|
|
341
|
+
else if (selected && sameDay(d, selected)) { btn.classList.add('is-selected'); btn.setAttribute('aria-selected', 'true'); }
|
|
288
342
|
if (rangeStart && rangeEnd) {
|
|
289
343
|
var t = startOfDay(d).getTime();
|
|
290
344
|
if (t > startOfDay(rangeStart).getTime() && t < startOfDay(rangeEnd).getTime()) btn.classList.add('in-range');
|
|
@@ -296,9 +350,84 @@
|
|
|
296
350
|
grid.appendChild(btn);
|
|
297
351
|
}
|
|
298
352
|
grid.addEventListener('keydown', onGridKey);
|
|
353
|
+
if (rangeMode) grid.addEventListener('mouseover', onRangeHover);
|
|
299
354
|
panel.appendChild(grid);
|
|
300
355
|
}
|
|
301
356
|
|
|
357
|
+
/* The two ends the grid shows. While a start waits for its end, the end is the day under the
|
|
358
|
+
pointer or the keyboard, and only when it does not precede the start: a day before it would
|
|
359
|
+
become the new start, so drawing a band backwards would promise something a click will not do. */
|
|
360
|
+
function shownEnds() {
|
|
361
|
+
if (anchor) {
|
|
362
|
+
var to = preview && startOfDay(preview).getTime() >= anchor.getTime() ? startOfDay(preview) : null;
|
|
363
|
+
return { from: anchor, to: to, pending: true };
|
|
364
|
+
}
|
|
365
|
+
return { from: selected, to: selectedEnd, pending: false };
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/* Every range state of one day cell, written in full each time, so the same function serves the
|
|
369
|
+
render and the cheaper repaint a hover asks for (rebuilding the grid under a moving pointer
|
|
370
|
+
would replace the element the pointer is on). The ends are rounded on their outer side only,
|
|
371
|
+
and a committed end is filled like a picked day. A previewed end is not filled: it is not an
|
|
372
|
+
answer yet. Each end also says so in its accessible name, so the range is not colour alone. */
|
|
373
|
+
function paintRange(btn, d) {
|
|
374
|
+
var ends = shownEnds();
|
|
375
|
+
var t = startOfDay(d).getTime();
|
|
376
|
+
var isFrom = !!ends.from && sameDay(d, ends.from);
|
|
377
|
+
var isTo = !!ends.to && sameDay(d, ends.to);
|
|
378
|
+
var span = !!ends.from && !!ends.to && !sameDay(ends.from, ends.to);
|
|
379
|
+
var filled = isFrom || (isTo && !ends.pending);
|
|
380
|
+
btn.classList.toggle('is-selected', filled);
|
|
381
|
+
if (filled) btn.setAttribute('aria-selected', 'true'); else btn.removeAttribute('aria-selected');
|
|
382
|
+
btn.classList.toggle('is-range-start', isFrom && span);
|
|
383
|
+
btn.classList.toggle('is-range-end', isTo && span);
|
|
384
|
+
btn.classList.toggle('in-range', span && ((t > ends.from.getTime() && t < ends.to.getTime()) || (isTo && ends.pending)));
|
|
385
|
+
var name = dayLabelFmt.format(d);
|
|
386
|
+
if (isFrom) name += ', ' + textOf(wrap, 'rangeStart');
|
|
387
|
+
else if (isTo && !ends.pending) name += ', ' + textOf(wrap, 'rangeEnd');
|
|
388
|
+
btn.setAttribute('aria-label', name);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function repaintRange() {
|
|
392
|
+
Array.prototype.forEach.call(panel.querySelectorAll('.fdy-cal__day'), function (btn) {
|
|
393
|
+
if (btn._date) paintRange(btn, btn._date);
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function onRangeHover(e) {
|
|
398
|
+
if (!anchor) return;
|
|
399
|
+
var cell = e.target && e.target.closest ? e.target.closest('.fdy-cal__day') : null;
|
|
400
|
+
if (!cell || !cell._date || cell.disabled) return;
|
|
401
|
+
if (preview && sameDay(preview, cell._date)) return;
|
|
402
|
+
preview = cell._date;
|
|
403
|
+
repaintRange();
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/* First pick: the start, and the panel stays open for the end. Second pick on or after it: the
|
|
407
|
+
range is whole, committed and announced, and the panel closes. A second pick BEFORE the start
|
|
408
|
+
becomes the new start instead of silently swapping the two, which would leave the reader
|
|
409
|
+
wondering which end they just moved. */
|
|
410
|
+
function pickRange(d) {
|
|
411
|
+
var day = startOfDay(d);
|
|
412
|
+
focusDate = day;
|
|
413
|
+
if (!anchor || day.getTime() < anchor.getTime()) {
|
|
414
|
+
anchor = day;
|
|
415
|
+
preview = day;
|
|
416
|
+
render();
|
|
417
|
+
focusFocusDate();
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
selected = anchor;
|
|
421
|
+
selectedEnd = day;
|
|
422
|
+
anchor = null;
|
|
423
|
+
preview = null;
|
|
424
|
+
updateDisplay();
|
|
425
|
+
wrap.dispatchEvent(new CustomEvent('fdy-daterange-settled', {
|
|
426
|
+
bubbles: true, detail: { from: toISO(selected), to: toISO(selectedEnd) }
|
|
427
|
+
}));
|
|
428
|
+
close(true);
|
|
429
|
+
}
|
|
430
|
+
|
|
302
431
|
function renderMonths() {
|
|
303
432
|
panel.innerHTML = '';
|
|
304
433
|
var year = view.getFullYear();
|
|
@@ -518,6 +647,9 @@
|
|
|
518
647
|
|
|
519
648
|
function moveFocus(next) {
|
|
520
649
|
focusDate = next;
|
|
650
|
+
/* The keyboard previews the band exactly as the pointer does, or a keyboard user would pick
|
|
651
|
+
the end without ever seeing the range they are about to commit. */
|
|
652
|
+
if (anchor) preview = startOfDay(next);
|
|
521
653
|
if (next.getMonth() !== view.getMonth() || next.getFullYear() !== view.getFullYear()) {
|
|
522
654
|
view = new Date(next.getFullYear(), next.getMonth(), 1);
|
|
523
655
|
}
|
|
@@ -546,6 +678,7 @@
|
|
|
546
678
|
|
|
547
679
|
function pick(d) {
|
|
548
680
|
if (isDisabled(d)) return;
|
|
681
|
+
if (rangeMode) { pickRange(d); return; }
|
|
549
682
|
selected = startOfDay(d);
|
|
550
683
|
focusDate = selected;
|
|
551
684
|
updateDisplay();
|
|
@@ -622,6 +755,10 @@
|
|
|
622
755
|
|
|
623
756
|
function close(returnFocus) {
|
|
624
757
|
if (panel.hidden) return;
|
|
758
|
+
/* Every way out that is not a second pick drops half a range: the value never held it, so
|
|
759
|
+
there is nothing to restore, only the start this opening was waiting on. */
|
|
760
|
+
anchor = null;
|
|
761
|
+
preview = null;
|
|
625
762
|
var p = popCtl(); if (p) p.hide(); else panel.hidden = true;
|
|
626
763
|
trigger.setAttribute('aria-expanded', 'false');
|
|
627
764
|
trigger.classList.remove('is-open');
|
|
@@ -643,8 +780,11 @@
|
|
|
643
780
|
var api = {
|
|
644
781
|
wrap: wrap,
|
|
645
782
|
setState: setState,
|
|
646
|
-
getValue: function () {
|
|
647
|
-
|
|
783
|
+
getValue: function () {
|
|
784
|
+
if (rangeMode) return rangeValue();
|
|
785
|
+
return selected ? (monthOnly ? toISOMonth(selected) : toISO(selected)) : '';
|
|
786
|
+
},
|
|
787
|
+
clear: function () { selected = null; selectedEnd = null; updateDisplay(); if (!panel.hidden) render(); },
|
|
648
788
|
setMin: function (iso) { minDate = parseVal(iso); if (!panel.hidden) render(); },
|
|
649
789
|
setMax: function (iso) { maxDate = parseVal(iso); if (!panel.hidden) render(); },
|
|
650
790
|
setRange: function (fromIso, toIso) {
|
|
@@ -661,6 +801,9 @@
|
|
|
661
801
|
if (root.dataset.fdyDrReady === '1') return;
|
|
662
802
|
if (root.closest('[data-fdy-ignore]')) return;
|
|
663
803
|
root.dataset.fdyDrReady = '1';
|
|
804
|
+
/* One field: the root IS the picker, and the range logic lives in initPicker beside the
|
|
805
|
+
calendar it draws on. The split layout below is unchanged. */
|
|
806
|
+
if (root.getAttribute('data-layout') === 'single') { initPicker(root); return; }
|
|
664
807
|
var fromEl = root.querySelector('[data-fdy-datepicker][data-role="from"]');
|
|
665
808
|
var toEl = root.querySelector('[data-fdy-datepicker][data-role="to"]');
|
|
666
809
|
if (!fromEl || !toEl) return;
|