@barqjs/aria 0.0.0-stage → 0.2.0-next.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/LICENSE +21 -0
- package/README.md +293 -2
- package/package.json +347 -4
- package/src/breadcrumbs.tsx +296 -0
- package/src/button.tsx +342 -0
- package/src/calendar.tsx +1389 -0
- package/src/checkbox.tsx +570 -0
- package/src/collections.ts +976 -0
- package/src/color.ts +562 -0
- package/src/colorpicker.tsx +1478 -0
- package/src/combobox.tsx +2481 -0
- package/src/command.tsx +1184 -0
- package/src/date.ts +652 -0
- package/src/datefield.tsx +951 -0
- package/src/datepicker.tsx +556 -0
- package/src/dialog.tsx +890 -0
- package/src/disclosure.tsx +659 -0
- package/src/dom.ts +706 -0
- package/src/drawer.ts +674 -0
- package/src/focus.ts +1370 -0
- package/src/form.tsx +94 -0
- package/src/gridlist.tsx +563 -0
- package/src/i18n.ts +823 -0
- package/src/index.ts +1270 -0
- package/src/interactions/description.ts +73 -0
- package/src/interactions/flags.ts +35 -0
- package/src/interactions/focus-events.ts +218 -0
- package/src/interactions/focusable.ts +86 -0
- package/src/interactions/hover.ts +213 -0
- package/src/interactions/interact-outside.ts +123 -0
- package/src/interactions/keyboard.ts +105 -0
- package/src/interactions/listeners.ts +78 -0
- package/src/interactions/long-press.ts +160 -0
- package/src/interactions/modality.ts +416 -0
- package/src/interactions/move.ts +257 -0
- package/src/interactions/open-link.ts +193 -0
- package/src/interactions/press.ts +898 -0
- package/src/interactions/prevent-focus.ts +102 -0
- package/src/interactions/scroll-wheel.ts +43 -0
- package/src/interactions/text-selection.ts +77 -0
- package/src/interactions.ts +81 -0
- package/src/label.ts +128 -0
- package/src/link.tsx +381 -0
- package/src/listbox.tsx +721 -0
- package/src/live.ts +204 -0
- package/src/menu.tsx +2290 -0
- package/src/menubar.tsx +575 -0
- package/src/navigation.ts +735 -0
- package/src/numberfield.tsx +838 -0
- package/src/otp.ts +430 -0
- package/src/overlays.ts +1247 -0
- package/src/platform.ts +74 -0
- package/src/presence.ts +164 -0
- package/src/questionnaire.tsx +1712 -0
- package/src/radio.tsx +567 -0
- package/src/register-dom.ts +12 -0
- package/src/scroller.ts +1165 -0
- package/src/select.tsx +1665 -0
- package/src/selection.ts +1415 -0
- package/src/slider.tsx +1042 -0
- package/src/splitter.ts +1152 -0
- package/src/static-collection.ts +211 -0
- package/src/switch.tsx +186 -0
- package/src/table.tsx +0 -0
- package/src/tabs.tsx +1018 -0
- package/src/tag.tsx +452 -0
- package/src/textfield.tsx +417 -0
- package/src/toast.ts +658 -0
- package/src/toggle-input.tsx +48 -0
- package/src/toggle.ts +321 -0
- package/src/togglegroup.ts +365 -0
- package/src/toolbar.tsx +179 -0
- package/src/tooltip.tsx +657 -0
- package/src/utils.ts +758 -0
- package/src/validation.ts +447 -0
- package/src/virtualizer.tsx +674 -0
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A date picker: a date field, and a calendar behind a button.
|
|
3
|
+
*
|
|
4
|
+
* Both halves matter and neither is optional. The field is how anyone who
|
|
5
|
+
* knows the date enters it — typing 03072024 is three seconds and reaching
|
|
6
|
+
* into a calendar for a date six months out is thirty. The calendar is how
|
|
7
|
+
* anyone who is choosing rather than recalling does it, and it is the only way
|
|
8
|
+
* to see which days are available.
|
|
9
|
+
*
|
|
10
|
+
* So the field is the control, and the calendar is a popover the button opens:
|
|
11
|
+
*
|
|
12
|
+
* - **The button is not a Tab stop of its own** in the sense that matters: it
|
|
13
|
+
* is one, but it sits after the segments, so tabbing through a form goes
|
|
14
|
+
* month, day, year, calendar, next field.
|
|
15
|
+
* - **Choosing a date closes the popover** and puts the value in the field,
|
|
16
|
+
* because the calendar exists to answer one question.
|
|
17
|
+
* - **`aria-haspopup="dialog"`,** not `menu` or `listbox`: what opens is a
|
|
18
|
+
* dialog holding a grid, and saying otherwise makes a screen reader promise
|
|
19
|
+
* the wrong keys.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import {
|
|
23
|
+
type Accessor,
|
|
24
|
+
type Child,
|
|
25
|
+
createContext,
|
|
26
|
+
getContext,
|
|
27
|
+
getOwner,
|
|
28
|
+
type Incoming,
|
|
29
|
+
install,
|
|
30
|
+
} from "@barqjs/core";
|
|
31
|
+
import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
|
|
32
|
+
import { button, type ButtonOptions } from "./button.tsx";
|
|
33
|
+
import { Calendar, RangeCalendar, type DateRange } from "./calendar.tsx";
|
|
34
|
+
import { CalendarDate, CalendarDateTime, toCalendarDate, today, type DateValue } from "./date.ts";
|
|
35
|
+
import { Popover } from "./dialog.tsx";
|
|
36
|
+
import { focusRing } from "./focus.ts";
|
|
37
|
+
import { hover } from "./interactions/hover.ts";
|
|
38
|
+
import type { ElementRef } from "./interactions/press.ts";
|
|
39
|
+
import { field, type FieldOptions } from "./label.ts";
|
|
40
|
+
import type { ValidateFunction, ValidationBehavior } from "./validation.ts";
|
|
41
|
+
import {
|
|
42
|
+
overlayTriggerState,
|
|
43
|
+
type OverlayTriggerState,
|
|
44
|
+
type OverlayTriggerStateOptions,
|
|
45
|
+
type Placement,
|
|
46
|
+
} from "./overlays.ts";
|
|
47
|
+
import { DateField, type Granularity } from "./datefield.tsx";
|
|
48
|
+
import {
|
|
49
|
+
access,
|
|
50
|
+
callback,
|
|
51
|
+
controllable,
|
|
52
|
+
type DOMProps,
|
|
53
|
+
filterDOMProps,
|
|
54
|
+
fromProps,
|
|
55
|
+
id,
|
|
56
|
+
type MaybeAccessor,
|
|
57
|
+
mergeProps,
|
|
58
|
+
type StyleProps,
|
|
59
|
+
styleProps,
|
|
60
|
+
} from "./utils.ts";
|
|
61
|
+
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
// State
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
|
|
66
|
+
export interface DatePickerStateOptions extends OverlayTriggerStateOptions {
|
|
67
|
+
value?: MaybeAccessor<DateValue | null | undefined>;
|
|
68
|
+
defaultValue?: MaybeAccessor<DateValue | null | undefined>;
|
|
69
|
+
minValue?: MaybeAccessor<DateValue | null | undefined>;
|
|
70
|
+
maxValue?: MaybeAccessor<DateValue | null | undefined>;
|
|
71
|
+
/** @default "day" */
|
|
72
|
+
granularity?: MaybeAccessor<Granularity | undefined>;
|
|
73
|
+
isDisabled?: MaybeAccessor<boolean | undefined>;
|
|
74
|
+
isReadOnly?: MaybeAccessor<boolean | undefined>;
|
|
75
|
+
onChange?: (value: DateValue | null) => void;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface DatePickerState extends OverlayTriggerState {
|
|
79
|
+
value: Accessor<DateValue | null>;
|
|
80
|
+
setValue(value: DateValue | null): void;
|
|
81
|
+
/** The day part, for the calendar, which knows nothing about time. */
|
|
82
|
+
dateValue: Accessor<CalendarDate | null>;
|
|
83
|
+
setDateValue(date: CalendarDate | null): void;
|
|
84
|
+
granularity: Accessor<Granularity>;
|
|
85
|
+
isDisabled: Accessor<boolean>;
|
|
86
|
+
isReadOnly: Accessor<boolean>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function datePickerState(options: DatePickerStateOptions): DatePickerState {
|
|
90
|
+
const overlay = overlayTriggerState(options);
|
|
91
|
+
|
|
92
|
+
const [value, setValue] = controllable<DateValue | null>(
|
|
93
|
+
() => access(options.value),
|
|
94
|
+
() => access(options.defaultValue) ?? null,
|
|
95
|
+
options.onChange,
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
const granularity = (): Granularity => access(options.granularity) ?? "day";
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
...overlay,
|
|
102
|
+
value,
|
|
103
|
+
setValue,
|
|
104
|
+
granularity,
|
|
105
|
+
isDisabled: () => access(options.isDisabled) === true,
|
|
106
|
+
isReadOnly: () => access(options.isReadOnly) === true,
|
|
107
|
+
dateValue: () => {
|
|
108
|
+
const current = value();
|
|
109
|
+
return current === null ? null : toCalendarDate(current);
|
|
110
|
+
},
|
|
111
|
+
setDateValue: (date) => {
|
|
112
|
+
if (date === null) {
|
|
113
|
+
setValue(null);
|
|
114
|
+
overlay.close();
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
// The time the field already holds is KEPT: choosing a day in the
|
|
118
|
+
// calendar is choosing a day, not resetting the meeting to midnight.
|
|
119
|
+
const current = value();
|
|
120
|
+
const next =
|
|
121
|
+
granularity() === "day"
|
|
122
|
+
? date
|
|
123
|
+
: CalendarDateTime.from(
|
|
124
|
+
date,
|
|
125
|
+
current instanceof CalendarDateTime ? current.time : undefined,
|
|
126
|
+
);
|
|
127
|
+
setValue(next);
|
|
128
|
+
overlay.close();
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// ---------------------------------------------------------------------------
|
|
134
|
+
// Hooks
|
|
135
|
+
// ---------------------------------------------------------------------------
|
|
136
|
+
|
|
137
|
+
export interface DatePickerOptions extends FieldOptions {
|
|
138
|
+
ref: ElementRef;
|
|
139
|
+
isDisabled?: MaybeAccessor<boolean | undefined>;
|
|
140
|
+
isReadOnly?: MaybeAccessor<boolean | undefined>;
|
|
141
|
+
isRequired?: MaybeAccessor<boolean | undefined>;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export interface DatePickerResult {
|
|
145
|
+
labelProps: DOMProps;
|
|
146
|
+
/** For the element holding the field and the button. */
|
|
147
|
+
groupProps: DOMProps;
|
|
148
|
+
fieldProps: DOMProps;
|
|
149
|
+
/** Options for {@link button}, not props for an element. */
|
|
150
|
+
buttonProps: DOMProps;
|
|
151
|
+
dialogProps: DOMProps;
|
|
152
|
+
calendarProps: DOMProps;
|
|
153
|
+
descriptionProps: DOMProps;
|
|
154
|
+
errorMessageProps: DOMProps;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function datePicker(options: DatePickerOptions, state: DatePickerState): DatePickerResult {
|
|
158
|
+
const groupId = id();
|
|
159
|
+
const buttonId = id();
|
|
160
|
+
const dialogId = id();
|
|
161
|
+
|
|
162
|
+
const {
|
|
163
|
+
labelProps,
|
|
164
|
+
fieldProps: labelledProps,
|
|
165
|
+
descriptionProps,
|
|
166
|
+
errorMessageProps,
|
|
167
|
+
} = field({ ...options, labelElementType: "span" });
|
|
168
|
+
|
|
169
|
+
return {
|
|
170
|
+
descriptionProps,
|
|
171
|
+
errorMessageProps,
|
|
172
|
+
labelProps: mergeProps(labelProps, {
|
|
173
|
+
// A `<span>` label cannot wrap the control, so clicking it has to move
|
|
174
|
+
// focus by hand — to the first SEGMENT, which is where typing goes.
|
|
175
|
+
onClick: () => {
|
|
176
|
+
if (state.isDisabled()) return;
|
|
177
|
+
const group = access(options.ref) as HTMLElement | null;
|
|
178
|
+
group?.querySelector<HTMLElement>('[role="spinbutton"]')?.focus();
|
|
179
|
+
},
|
|
180
|
+
}),
|
|
181
|
+
groupProps: mergeProps(filterDOMProps(options, { labelable: true }), labelledProps, {
|
|
182
|
+
role: "group",
|
|
183
|
+
id: groupId,
|
|
184
|
+
"aria-disabled": () => state.isDisabled() || undefined,
|
|
185
|
+
}),
|
|
186
|
+
fieldProps: {
|
|
187
|
+
"aria-labelledby": () => access(labelProps.id as MaybeAccessor<string | undefined>),
|
|
188
|
+
},
|
|
189
|
+
buttonProps: {
|
|
190
|
+
id: buttonId,
|
|
191
|
+
// What opens is a dialog holding a grid. `menu` would promise the arrow
|
|
192
|
+
// keys move between menu items.
|
|
193
|
+
"aria-haspopup": "dialog",
|
|
194
|
+
"aria-expanded": () => state.isOpen(),
|
|
195
|
+
"aria-controls": () => (state.isOpen() ? dialogId() : undefined),
|
|
196
|
+
// Named by ITSELF and by the field, so it is announced as "Calendar,
|
|
197
|
+
// Departure" rather than as one of a page of identical buttons.
|
|
198
|
+
"aria-label": "Calendar",
|
|
199
|
+
"aria-labelledby": () =>
|
|
200
|
+
`${buttonId()} ${access(labelProps.id as MaybeAccessor<string | undefined>) ?? groupId()}`,
|
|
201
|
+
isDisabled: () => state.isDisabled() || state.isReadOnly(),
|
|
202
|
+
onPress: () => state.toggle(),
|
|
203
|
+
},
|
|
204
|
+
dialogProps: {
|
|
205
|
+
id: dialogId,
|
|
206
|
+
role: "dialog",
|
|
207
|
+
"aria-labelledby": () =>
|
|
208
|
+
access(labelProps.id as MaybeAccessor<string | undefined>) ?? groupId(),
|
|
209
|
+
},
|
|
210
|
+
calendarProps: {
|
|
211
|
+
"aria-label": "Choose a date",
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// ---------------------------------------------------------------------------
|
|
217
|
+
// Components
|
|
218
|
+
// ---------------------------------------------------------------------------
|
|
219
|
+
|
|
220
|
+
interface DatePickerContextValue {
|
|
221
|
+
state: DatePickerState;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const DatePickerContext = createContext<DatePickerContextValue | null>(null);
|
|
225
|
+
|
|
226
|
+
export function useDatePicker(): DatePickerContextValue {
|
|
227
|
+
const value = getContext(DatePickerContext);
|
|
228
|
+
if (value === null || value === undefined) {
|
|
229
|
+
throw new Error("This must be rendered inside a DatePicker.");
|
|
230
|
+
}
|
|
231
|
+
return value;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export interface DatePickerComponentProps extends StyleProps {
|
|
235
|
+
label?: Child;
|
|
236
|
+
description?: Child;
|
|
237
|
+
errorMessage?: Child;
|
|
238
|
+
value?: DateValue | null;
|
|
239
|
+
defaultValue?: DateValue | null;
|
|
240
|
+
placeholderValue?: DateValue;
|
|
241
|
+
minValue?: DateValue | null;
|
|
242
|
+
maxValue?: DateValue | null;
|
|
243
|
+
isDateUnavailable?: (date: CalendarDate) => boolean;
|
|
244
|
+
/** @default "day" */
|
|
245
|
+
granularity?: Granularity;
|
|
246
|
+
hourCycle?: 12 | 24;
|
|
247
|
+
isDisabled?: boolean;
|
|
248
|
+
isReadOnly?: boolean;
|
|
249
|
+
isRequired?: boolean;
|
|
250
|
+
isInvalid?: boolean;
|
|
251
|
+
/** What the page thinks of the date, checked as it changes. */
|
|
252
|
+
validate?: ValidateFunction<DateValue | null>;
|
|
253
|
+
/** @default "aria" */
|
|
254
|
+
validationBehavior?: ValidationBehavior;
|
|
255
|
+
isOpen?: boolean;
|
|
256
|
+
defaultOpen?: boolean;
|
|
257
|
+
name?: string;
|
|
258
|
+
/** @default "bottom start" */
|
|
259
|
+
placement?: Placement;
|
|
260
|
+
"aria-label"?: string;
|
|
261
|
+
"aria-labelledby"?: string;
|
|
262
|
+
ref?: RefTarget<HTMLDivElement>;
|
|
263
|
+
onChange?: (value: DateValue | null) => void;
|
|
264
|
+
onOpenChange?: (isOpen: boolean) => void;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* ```tsx
|
|
269
|
+
* <DatePicker label="Departure" value={date()} onChange={date.set} minValue={today()} />
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
export function DatePicker(props: Incoming<DatePickerComponentProps>) {
|
|
273
|
+
const groupRef = makeRef<HTMLDivElement>();
|
|
274
|
+
const triggerRef = makeRef<HTMLButtonElement>();
|
|
275
|
+
|
|
276
|
+
const state = datePickerState({
|
|
277
|
+
value: () => props.value?.(),
|
|
278
|
+
defaultValue: () => props.defaultValue?.(),
|
|
279
|
+
minValue: () => props.minValue?.(),
|
|
280
|
+
maxValue: () => props.maxValue?.(),
|
|
281
|
+
granularity: () => props.granularity?.(),
|
|
282
|
+
isDisabled: () => props.isDisabled?.(),
|
|
283
|
+
isReadOnly: () => props.isReadOnly?.(),
|
|
284
|
+
isOpen: () => props.isOpen?.(),
|
|
285
|
+
defaultOpen: () => props.defaultOpen?.(),
|
|
286
|
+
onChange: (value) => props.onChange?.()?.(value),
|
|
287
|
+
onOpenChange: (isOpen) => props.onOpenChange?.()?.(isOpen),
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
const {
|
|
291
|
+
labelProps,
|
|
292
|
+
groupProps,
|
|
293
|
+
buttonProps,
|
|
294
|
+
dialogProps,
|
|
295
|
+
calendarProps,
|
|
296
|
+
descriptionProps,
|
|
297
|
+
errorMessageProps,
|
|
298
|
+
} = datePicker(
|
|
299
|
+
{
|
|
300
|
+
ref: groupRef,
|
|
301
|
+
label: () => props.label?.(),
|
|
302
|
+
description: () => props.description?.(),
|
|
303
|
+
errorMessage: () => props.errorMessage?.(),
|
|
304
|
+
isDisabled: () => props.isDisabled?.(),
|
|
305
|
+
isReadOnly: () => props.isReadOnly?.(),
|
|
306
|
+
isRequired: () => props.isRequired?.(),
|
|
307
|
+
isInvalid: () => props.isInvalid?.(),
|
|
308
|
+
"aria-label": () => props["aria-label"]?.(),
|
|
309
|
+
"aria-labelledby": () => props["aria-labelledby"]?.(),
|
|
310
|
+
},
|
|
311
|
+
state,
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
const trigger = button(buttonProps, triggerRef);
|
|
315
|
+
const { hoverProps, isHovered } = hover({ isDisabled: () => props.isDisabled?.() });
|
|
316
|
+
const { focusProps, isFocusVisible } = focusRing();
|
|
317
|
+
|
|
318
|
+
const owner = getOwner();
|
|
319
|
+
if (owner !== null) install(owner, DatePickerContext, () => ({ state }));
|
|
320
|
+
|
|
321
|
+
const elementProps = mergeProps(
|
|
322
|
+
groupProps,
|
|
323
|
+
filterDOMProps(fromProps(props), { global: true }),
|
|
324
|
+
styleProps(props),
|
|
325
|
+
{
|
|
326
|
+
"data-open": state.isOpen,
|
|
327
|
+
"data-disabled": () => props.isDisabled?.() === true,
|
|
328
|
+
"data-testid": () => props["data-testid"]?.(),
|
|
329
|
+
},
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
const triggerElementProps = mergeProps(trigger.buttonProps, hoverProps, focusProps, {
|
|
333
|
+
id: buttonProps.id,
|
|
334
|
+
"data-hovered": isHovered,
|
|
335
|
+
"data-focus-visible": isFocusVisible,
|
|
336
|
+
"data-pressed": trigger.isPressed,
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
return (
|
|
340
|
+
<>
|
|
341
|
+
<span {...labelProps}>{props.label}</span>
|
|
342
|
+
<div {...elementProps} ref={mergeRefs(groupRef.set, props.ref?.())}>
|
|
343
|
+
{/* Not labelled again: the group around it already carries the
|
|
344
|
+
label, and naming both makes a screen reader say "Departure
|
|
345
|
+
group, Departure group". The segments name themselves. */}
|
|
346
|
+
<DateField
|
|
347
|
+
value={state.value()}
|
|
348
|
+
placeholderValue={props.placeholderValue?.()}
|
|
349
|
+
minValue={props.minValue?.()}
|
|
350
|
+
maxValue={props.maxValue?.()}
|
|
351
|
+
granularity={props.granularity?.()}
|
|
352
|
+
hourCycle={props.hourCycle?.()}
|
|
353
|
+
isDisabled={props.isDisabled?.()}
|
|
354
|
+
isReadOnly={props.isReadOnly?.()}
|
|
355
|
+
isRequired={props.isRequired?.()}
|
|
356
|
+
isInvalid={props.isInvalid?.()}
|
|
357
|
+
validate={callback<[DateValue | null], string | string[] | true | null | undefined>(
|
|
358
|
+
props.validate,
|
|
359
|
+
)}
|
|
360
|
+
validationBehavior={props.validationBehavior?.()}
|
|
361
|
+
name={props.name?.()}
|
|
362
|
+
onChange={(value) => state.setValue(value)}
|
|
363
|
+
/>
|
|
364
|
+
<button {...triggerElementProps} type="button" ref={triggerRef.set}>
|
|
365
|
+
<span aria-hidden="true">📅</span>
|
|
366
|
+
</button>
|
|
367
|
+
</div>
|
|
368
|
+
<Popover
|
|
369
|
+
triggerRef={triggerRef}
|
|
370
|
+
isOpen={state.isOpen()}
|
|
371
|
+
onOpenChange={state.setOpen}
|
|
372
|
+
placement={props.placement?.() ?? "bottom start"}
|
|
373
|
+
>
|
|
374
|
+
<div {...dialogProps}>
|
|
375
|
+
<Calendar
|
|
376
|
+
aria-label={String(calendarProps["aria-label"])}
|
|
377
|
+
value={state.dateValue()}
|
|
378
|
+
defaultFocusedValue={
|
|
379
|
+
state.dateValue() ?? toCalendarDate(props.placeholderValue?.() ?? today())
|
|
380
|
+
}
|
|
381
|
+
minValue={props.minValue?.()}
|
|
382
|
+
maxValue={props.maxValue?.()}
|
|
383
|
+
isDateUnavailable={callback<[CalendarDate], boolean>(props.isDateUnavailable)}
|
|
384
|
+
isDisabled={props.isDisabled?.()}
|
|
385
|
+
isReadOnly={props.isReadOnly?.()}
|
|
386
|
+
autoFocus
|
|
387
|
+
onChange={(date) => state.setDateValue(date)}
|
|
388
|
+
/>
|
|
389
|
+
</div>
|
|
390
|
+
</Popover>
|
|
391
|
+
<span {...descriptionProps}>{props.description}</span>
|
|
392
|
+
<span {...errorMessageProps}>{props.errorMessage}</span>
|
|
393
|
+
</>
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// ---------------------------------------------------------------------------
|
|
398
|
+
// A range of days
|
|
399
|
+
// ---------------------------------------------------------------------------
|
|
400
|
+
|
|
401
|
+
export interface DateRangePickerComponentProps extends Omit<
|
|
402
|
+
DatePickerComponentProps,
|
|
403
|
+
"value" | "defaultValue" | "onChange" | "name"
|
|
404
|
+
> {
|
|
405
|
+
value?: DateRange | null;
|
|
406
|
+
defaultValue?: DateRange | null;
|
|
407
|
+
startName?: string;
|
|
408
|
+
endName?: string;
|
|
409
|
+
onChange?: (value: DateRange | null) => void;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Two fields and one calendar.
|
|
414
|
+
*
|
|
415
|
+
* The fields are separate controls — a start and an end are two dates, and one
|
|
416
|
+
* field holding "3 Mar – 7 Mar" is a string nobody can edit a part of — but
|
|
417
|
+
* they share the calendar, where the range is drawn in one gesture.
|
|
418
|
+
*/
|
|
419
|
+
export function DateRangePicker(props: Incoming<DateRangePickerComponentProps>) {
|
|
420
|
+
const groupRef = makeRef<HTMLDivElement>();
|
|
421
|
+
const triggerRef = makeRef<HTMLButtonElement>();
|
|
422
|
+
|
|
423
|
+
const [value, setValue] = controllable<DateRange | null>(
|
|
424
|
+
() => props.value?.(),
|
|
425
|
+
() => props.defaultValue?.() ?? null,
|
|
426
|
+
(next) => props.onChange?.()?.(next),
|
|
427
|
+
);
|
|
428
|
+
|
|
429
|
+
const overlay = overlayTriggerState({
|
|
430
|
+
isOpen: () => props.isOpen?.(),
|
|
431
|
+
defaultOpen: () => props.defaultOpen?.(),
|
|
432
|
+
onOpenChange: (isOpen) => props.onOpenChange?.()?.(isOpen),
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
const labelId = id();
|
|
436
|
+
const buttonId = id();
|
|
437
|
+
const dialogId = id();
|
|
438
|
+
|
|
439
|
+
const isDisabled = (): boolean => props.isDisabled?.() === true;
|
|
440
|
+
|
|
441
|
+
const trigger = button(
|
|
442
|
+
{
|
|
443
|
+
id: buttonId,
|
|
444
|
+
"aria-haspopup": "dialog",
|
|
445
|
+
"aria-expanded": () => overlay.isOpen(),
|
|
446
|
+
"aria-controls": () => (overlay.isOpen() ? dialogId() : undefined),
|
|
447
|
+
"aria-label": "Calendar",
|
|
448
|
+
"aria-labelledby": () => `${buttonId()} ${labelId()}`,
|
|
449
|
+
isDisabled: () => isDisabled() || props.isReadOnly?.() === true,
|
|
450
|
+
onPress: () => overlay.toggle(),
|
|
451
|
+
} as ButtonOptions,
|
|
452
|
+
triggerRef,
|
|
453
|
+
);
|
|
454
|
+
|
|
455
|
+
const { hoverProps, isHovered } = hover({ isDisabled });
|
|
456
|
+
const { focusProps, isFocusVisible } = focusRing();
|
|
457
|
+
|
|
458
|
+
/** One end changed. The other is kept, so the range is edited rather than reset. */
|
|
459
|
+
const setEnd = (which: "start" | "end", date: DateValue | null): void => {
|
|
460
|
+
const current = value();
|
|
461
|
+
if (date === null) {
|
|
462
|
+
setValue(null);
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
const day = toCalendarDate(date);
|
|
466
|
+
const other = which === "start" ? current?.end : current?.start;
|
|
467
|
+
if (other === undefined) {
|
|
468
|
+
setValue({ start: day, end: day });
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
setValue(which === "start" ? { start: day, end: other } : { start: other, end: day });
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
const elementProps = mergeProps(
|
|
475
|
+
filterDOMProps(fromProps(props), { global: true }),
|
|
476
|
+
styleProps(props),
|
|
477
|
+
{
|
|
478
|
+
role: "group",
|
|
479
|
+
"aria-labelledby": labelId,
|
|
480
|
+
"aria-disabled": () => isDisabled() || undefined,
|
|
481
|
+
"data-open": overlay.isOpen,
|
|
482
|
+
"data-testid": () => props["data-testid"]?.(),
|
|
483
|
+
},
|
|
484
|
+
);
|
|
485
|
+
|
|
486
|
+
return (
|
|
487
|
+
<>
|
|
488
|
+
<span id={labelId}>{props.label}</span>
|
|
489
|
+
<div {...elementProps} ref={mergeRefs(groupRef.set, props.ref?.())}>
|
|
490
|
+
<DateField
|
|
491
|
+
aria-label="Start date"
|
|
492
|
+
value={value()?.start ?? null}
|
|
493
|
+
placeholderValue={props.placeholderValue?.()}
|
|
494
|
+
minValue={props.minValue?.()}
|
|
495
|
+
maxValue={props.maxValue?.()}
|
|
496
|
+
granularity={props.granularity?.()}
|
|
497
|
+
isDisabled={props.isDisabled?.()}
|
|
498
|
+
isReadOnly={props.isReadOnly?.()}
|
|
499
|
+
name={props.startName?.()}
|
|
500
|
+
onChange={(next) => setEnd("start", next)}
|
|
501
|
+
/>
|
|
502
|
+
<span aria-hidden="true">–</span>
|
|
503
|
+
<DateField
|
|
504
|
+
aria-label="End date"
|
|
505
|
+
value={value()?.end ?? null}
|
|
506
|
+
placeholderValue={props.placeholderValue?.()}
|
|
507
|
+
minValue={props.minValue?.()}
|
|
508
|
+
maxValue={props.maxValue?.()}
|
|
509
|
+
granularity={props.granularity?.()}
|
|
510
|
+
isDisabled={props.isDisabled?.()}
|
|
511
|
+
isReadOnly={props.isReadOnly?.()}
|
|
512
|
+
name={props.endName?.()}
|
|
513
|
+
onChange={(next) => setEnd("end", next)}
|
|
514
|
+
/>
|
|
515
|
+
<button
|
|
516
|
+
{...mergeProps(trigger.buttonProps, hoverProps, focusProps, {
|
|
517
|
+
id: buttonId,
|
|
518
|
+
"data-hovered": isHovered,
|
|
519
|
+
"data-focus-visible": isFocusVisible,
|
|
520
|
+
})}
|
|
521
|
+
type="button"
|
|
522
|
+
ref={triggerRef.set}
|
|
523
|
+
>
|
|
524
|
+
<span aria-hidden="true">📅</span>
|
|
525
|
+
</button>
|
|
526
|
+
</div>
|
|
527
|
+
<Popover
|
|
528
|
+
triggerRef={triggerRef}
|
|
529
|
+
isOpen={overlay.isOpen()}
|
|
530
|
+
onOpenChange={overlay.setOpen}
|
|
531
|
+
placement={props.placement?.() ?? "bottom start"}
|
|
532
|
+
>
|
|
533
|
+
<div id={dialogId} role="dialog" aria-labelledby={labelId}>
|
|
534
|
+
<RangeCalendar
|
|
535
|
+
aria-label="Choose dates"
|
|
536
|
+
value={value()}
|
|
537
|
+
defaultFocusedValue={
|
|
538
|
+
value()?.start ?? toCalendarDate(props.placeholderValue?.() ?? today())
|
|
539
|
+
}
|
|
540
|
+
minValue={props.minValue?.()}
|
|
541
|
+
maxValue={props.maxValue?.()}
|
|
542
|
+
isDateUnavailable={callback<[CalendarDate], boolean>(props.isDateUnavailable)}
|
|
543
|
+
isDisabled={props.isDisabled?.()}
|
|
544
|
+
isReadOnly={props.isReadOnly?.()}
|
|
545
|
+
autoFocus
|
|
546
|
+
onChange={(range) => {
|
|
547
|
+
setValue(range);
|
|
548
|
+
overlay.close();
|
|
549
|
+
}}
|
|
550
|
+
/>
|
|
551
|
+
</div>
|
|
552
|
+
</Popover>
|
|
553
|
+
<span>{props.description}</span>
|
|
554
|
+
</>
|
|
555
|
+
);
|
|
556
|
+
}
|