@react-aria/datepicker 3.0.0-alpha.4 → 3.0.0-alpha.5
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/dist/main.js +417 -281
- package/dist/main.js.map +1 -1
- package/dist/module.js +409 -275
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +67 -28
- package/dist/types.d.ts.map +1 -1
- package/package.json +10 -9
- package/src/index.ts +4 -4
- package/src/useDateField.ts +77 -21
- package/src/useDatePicker.ts +52 -10
- package/src/useDatePickerGroup.ts +34 -11
- package/src/useDateRangePicker.ts +66 -18
- package/src/useDateSegment.ts +55 -36
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-aria/datepicker",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.5",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -19,16 +19,17 @@
|
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@babel/runtime": "^7.6.2",
|
|
21
21
|
"@internationalized/message": "^3.0.5",
|
|
22
|
-
"@internationalized/number": "^3.0.
|
|
23
|
-
"@react-aria/focus": "^3.5.
|
|
24
|
-
"@react-aria/i18n": "^3.3.
|
|
25
|
-
"@react-aria/interactions": "^3.8.
|
|
22
|
+
"@internationalized/number": "^3.0.6",
|
|
23
|
+
"@react-aria/focus": "^3.5.4",
|
|
24
|
+
"@react-aria/i18n": "^3.3.8",
|
|
25
|
+
"@react-aria/interactions": "^3.8.3",
|
|
26
26
|
"@react-aria/label": "^3.2.3",
|
|
27
|
-
"@react-aria/spinbutton": "^3.0.
|
|
27
|
+
"@react-aria/spinbutton": "^3.0.5",
|
|
28
28
|
"@react-aria/utils": "^3.11.2",
|
|
29
|
-
"@react-stately/datepicker": "3.0.0-alpha.
|
|
29
|
+
"@react-stately/datepicker": "3.0.0-alpha.4",
|
|
30
30
|
"@react-types/button": "^3.4.3",
|
|
31
|
-
"@react-types/
|
|
31
|
+
"@react-types/calendar": "3.0.0-alpha.4",
|
|
32
|
+
"@react-types/datepicker": "3.0.0-alpha.4",
|
|
32
33
|
"@react-types/dialog": "^3.3.3",
|
|
33
34
|
"@react-types/shared": "^3.11.1"
|
|
34
35
|
},
|
|
@@ -39,5 +40,5 @@
|
|
|
39
40
|
"publishConfig": {
|
|
40
41
|
"access": "public"
|
|
41
42
|
},
|
|
42
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "e7708349a637642a30d33a11ca4a75ad5829eaa3"
|
|
43
44
|
}
|
package/src/index.ts
CHANGED
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
export
|
|
14
|
-
export
|
|
15
|
-
export
|
|
16
|
-
export
|
|
13
|
+
export {useDatePicker} from './useDatePicker';
|
|
14
|
+
export {useDateSegment} from './useDateSegment';
|
|
15
|
+
export {useDateField, useTimeField} from './useDateField';
|
|
16
|
+
export {useDateRangePicker} from './useDateRangePicker';
|
|
17
17
|
export * from './useDisplayNames';
|
package/src/useDateField.ts
CHANGED
|
@@ -10,12 +10,11 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import {AriaDatePickerProps, DateValue} from '@react-types/datepicker';
|
|
14
|
-
import {createFocusManager} from '@react-aria/focus';
|
|
15
|
-
import {
|
|
16
|
-
import {HTMLAttributes,
|
|
13
|
+
import {AriaDatePickerProps, AriaTimeFieldProps, DateValue, TimeValue} from '@react-types/datepicker';
|
|
14
|
+
import {createFocusManager, FocusManager} from '@react-aria/focus';
|
|
15
|
+
import {DateFieldState} from '@react-stately/datepicker';
|
|
16
|
+
import {HTMLAttributes, RefObject, useEffect, useMemo, useRef} from 'react';
|
|
17
17
|
import {mergeProps, useDescription} from '@react-aria/utils';
|
|
18
|
-
import {useDateFormatter} from '@react-aria/i18n';
|
|
19
18
|
import {useDatePickerGroup} from './useDatePickerGroup';
|
|
20
19
|
import {useField} from '@react-aria/label';
|
|
21
20
|
import {useFocusWithin} from '@react-aria/interactions';
|
|
@@ -24,7 +23,9 @@ import {useFocusWithin} from '@react-aria/interactions';
|
|
|
24
23
|
interface DateFieldProps<T extends DateValue> extends Omit<AriaDatePickerProps<T>, 'value' | 'defaultValue' | 'onChange' | 'minValue' | 'maxValue' | 'placeholderValue'> {}
|
|
25
24
|
|
|
26
25
|
interface DateFieldAria {
|
|
27
|
-
|
|
26
|
+
/** Props for the field's visible label element, if any. */
|
|
27
|
+
labelProps: HTMLAttributes<HTMLElement>,
|
|
28
|
+
/** Props for the field grouping element. */
|
|
28
29
|
fieldProps: HTMLAttributes<HTMLElement>,
|
|
29
30
|
/** Props for the description element, if any. */
|
|
30
31
|
descriptionProps: HTMLAttributes<HTMLElement>,
|
|
@@ -32,9 +33,27 @@ interface DateFieldAria {
|
|
|
32
33
|
errorMessageProps: HTMLAttributes<HTMLElement>
|
|
33
34
|
}
|
|
34
35
|
|
|
35
|
-
|
|
36
|
+
// Data that is passed between useDateField and useDateSegment.
|
|
37
|
+
interface HookData {
|
|
38
|
+
ariaLabel: string,
|
|
39
|
+
ariaLabelledBy: string,
|
|
40
|
+
ariaDescribedBy: string,
|
|
41
|
+
focusManager: FocusManager
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const hookData = new WeakMap<DateFieldState, HookData>();
|
|
45
|
+
|
|
46
|
+
// Private props that we pass from useDatePicker/useDateRangePicker.
|
|
47
|
+
// Ideally we'd use a Symbol for this, but React doesn't support them: https://github.com/facebook/react/issues/7552
|
|
48
|
+
export const roleSymbol = '__role_' + Date.now();
|
|
49
|
+
export const focusManagerSymbol = '__focusManager_' + Date.now();
|
|
36
50
|
|
|
37
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Provides the behavior and accessibility implementation for a date field component.
|
|
53
|
+
* A date field allows users to enter and edit date and time values using a keyboard.
|
|
54
|
+
* Each part of a date value is displayed in an individually editable segment.
|
|
55
|
+
*/
|
|
56
|
+
export function useDateField<T extends DateValue>(props: DateFieldProps<T>, state: DateFieldState, ref: RefObject<HTMLElement>): DateFieldAria {
|
|
38
57
|
let {labelProps, fieldProps, descriptionProps, errorMessageProps} = useField({
|
|
39
58
|
...props,
|
|
40
59
|
labelElementType: 'span'
|
|
@@ -48,31 +67,68 @@ export function useDateField<T extends DateValue>(props: DateFieldProps<T>, stat
|
|
|
48
67
|
}
|
|
49
68
|
});
|
|
50
69
|
|
|
51
|
-
let
|
|
52
|
-
let descProps = useDescription(state.value ? formatter.format(state.dateValue) : null);
|
|
70
|
+
let descProps = useDescription(state.formatValue({month: 'long'}));
|
|
53
71
|
|
|
54
|
-
|
|
55
|
-
|
|
72
|
+
// If within a date picker or date range picker, the date field will have role="presentation" and an aria-describedby
|
|
73
|
+
// will be passed in that references the value (e.g. entire range). Otherwise, add the field's value description.
|
|
74
|
+
let describedBy = props[roleSymbol] === 'presentation'
|
|
75
|
+
? fieldProps['aria-describedby']
|
|
76
|
+
: [descProps['aria-describedby'], fieldProps['aria-describedby']].filter(Boolean).join(' ') || undefined;
|
|
77
|
+
let propsFocusManager = props[focusManagerSymbol];
|
|
78
|
+
let focusManager = useMemo(() => propsFocusManager || createFocusManager(ref), [propsFocusManager, ref]);
|
|
56
79
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
80
|
+
// Pass labels and other information to segments.
|
|
81
|
+
hookData.set(state, {
|
|
82
|
+
ariaLabel: props['aria-label'],
|
|
83
|
+
ariaLabelledBy: [props['aria-labelledby'], labelProps.id].filter(Boolean).join(' ') || undefined,
|
|
84
|
+
ariaDescribedBy: describedBy,
|
|
85
|
+
focusManager
|
|
60
86
|
});
|
|
61
87
|
|
|
88
|
+
let autoFocusRef = useRef(props.autoFocus);
|
|
89
|
+
|
|
90
|
+
// When used within a date picker or date range picker, the field gets role="presentation"
|
|
91
|
+
// rather than role="group". Since the date picker/date range picker already has a role="group"
|
|
92
|
+
// with a label and description, and the segments are already labeled by this as well, this
|
|
93
|
+
// avoids very verbose duplicate announcements.
|
|
94
|
+
let fieldDOMProps: HTMLAttributes<HTMLElement>;
|
|
95
|
+
if (props[roleSymbol] === 'presentation') {
|
|
96
|
+
fieldDOMProps = {
|
|
97
|
+
role: 'presentation'
|
|
98
|
+
};
|
|
99
|
+
} else {
|
|
100
|
+
fieldDOMProps = mergeProps(fieldProps, {
|
|
101
|
+
role: 'group',
|
|
102
|
+
'aria-disabled': props.isDisabled || undefined,
|
|
103
|
+
'aria-describedby': describedBy
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
useEffect(() => {
|
|
108
|
+
if (autoFocusRef.current) {
|
|
109
|
+
focusManager.focusFirst();
|
|
110
|
+
}
|
|
111
|
+
autoFocusRef.current = false;
|
|
112
|
+
}, [focusManager]);
|
|
113
|
+
|
|
62
114
|
return {
|
|
63
115
|
labelProps: {
|
|
64
116
|
...labelProps,
|
|
65
117
|
onClick: () => {
|
|
66
|
-
let focusManager = createFocusManager(ref);
|
|
67
118
|
focusManager.focusFirst();
|
|
68
119
|
}
|
|
69
120
|
},
|
|
70
|
-
fieldProps: mergeProps(
|
|
71
|
-
role: 'group',
|
|
72
|
-
'aria-disabled': props.isDisabled || undefined,
|
|
73
|
-
'aria-describedby': describedBy
|
|
74
|
-
}),
|
|
121
|
+
fieldProps: mergeProps(fieldDOMProps, groupProps, focusWithinProps),
|
|
75
122
|
descriptionProps,
|
|
76
123
|
errorMessageProps
|
|
77
124
|
};
|
|
78
125
|
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Provides the behavior and accessibility implementation for a time field component.
|
|
129
|
+
* A time field allows users to enter and edit time values using a keyboard.
|
|
130
|
+
* Each part of a time value is displayed in an individually editable segment.
|
|
131
|
+
*/
|
|
132
|
+
export function useTimeField<T extends TimeValue>(props: AriaTimeFieldProps<T>, state: DateFieldState, ref: RefObject<HTMLElement>): DateFieldAria {
|
|
133
|
+
return useDateField(props, state, ref);
|
|
134
|
+
}
|
package/src/useDatePicker.ts
CHANGED
|
@@ -13,29 +13,42 @@
|
|
|
13
13
|
import {AriaButtonProps} from '@react-types/button';
|
|
14
14
|
import {AriaDatePickerProps, DateValue} from '@react-types/datepicker';
|
|
15
15
|
import {AriaDialogProps} from '@react-types/dialog';
|
|
16
|
+
import {CalendarProps} from '@react-types/calendar';
|
|
16
17
|
import {createFocusManager} from '@react-aria/focus';
|
|
17
18
|
import {DatePickerState} from '@react-stately/datepicker';
|
|
18
|
-
import {HTMLAttributes,
|
|
19
|
+
import {HTMLAttributes, RefObject} from 'react';
|
|
19
20
|
// @ts-ignore
|
|
20
21
|
import intlMessages from '../intl/*.json';
|
|
21
22
|
import {mergeProps, useDescription, useId} from '@react-aria/utils';
|
|
23
|
+
import {roleSymbol} from './useDateField';
|
|
22
24
|
import {useDatePickerGroup} from './useDatePickerGroup';
|
|
23
25
|
import {useField} from '@react-aria/label';
|
|
24
26
|
import {useLocale, useMessageFormatter} from '@react-aria/i18n';
|
|
25
27
|
|
|
26
|
-
interface DatePickerAria
|
|
28
|
+
interface DatePickerAria {
|
|
29
|
+
/** Props for the date picker's visible label element, if any. */
|
|
30
|
+
labelProps: HTMLAttributes<HTMLElement>,
|
|
31
|
+
/** Props for the grouping element containing the date field and button. */
|
|
27
32
|
groupProps: HTMLAttributes<HTMLElement>,
|
|
28
|
-
|
|
29
|
-
fieldProps: AriaDatePickerProps<
|
|
33
|
+
/** Props for the date field. */
|
|
34
|
+
fieldProps: AriaDatePickerProps<DateValue>,
|
|
35
|
+
/** Props for the popover trigger button. */
|
|
36
|
+
buttonProps: AriaButtonProps,
|
|
30
37
|
/** Props for the description element, if any. */
|
|
31
38
|
descriptionProps: HTMLAttributes<HTMLElement>,
|
|
32
39
|
/** Props for the error message element, if any. */
|
|
33
40
|
errorMessageProps: HTMLAttributes<HTMLElement>,
|
|
34
|
-
|
|
35
|
-
dialogProps: AriaDialogProps
|
|
41
|
+
/** Props for the popover dialog. */
|
|
42
|
+
dialogProps: AriaDialogProps,
|
|
43
|
+
/** Props for the calendar within the popover dialog. */
|
|
44
|
+
calendarProps: CalendarProps<DateValue>
|
|
36
45
|
}
|
|
37
46
|
|
|
38
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Provides the behavior and accessibility implementation for a date picker component.
|
|
49
|
+
* A date picker combines a DateField and a Calendar popover to allow users to enter or select a date and time value.
|
|
50
|
+
*/
|
|
51
|
+
export function useDatePicker<T extends DateValue>(props: AriaDatePickerProps<T>, state: DatePickerState, ref: RefObject<HTMLElement>): DatePickerAria {
|
|
39
52
|
let buttonId = useId();
|
|
40
53
|
let dialogId = useId();
|
|
41
54
|
let formatMessage = useMessageFormatter(intlMessages);
|
|
@@ -54,7 +67,7 @@ export function useDatePicker<T extends DateValue>(props: AriaDatePickerProps<T>
|
|
|
54
67
|
let ariaDescribedBy = [descProps['aria-describedby'], fieldProps['aria-describedby']].filter(Boolean).join(' ') || undefined;
|
|
55
68
|
|
|
56
69
|
return {
|
|
57
|
-
groupProps: mergeProps(groupProps, descProps, {
|
|
70
|
+
groupProps: mergeProps(groupProps, fieldProps, descProps, {
|
|
58
71
|
role: 'group',
|
|
59
72
|
'aria-disabled': props.isDisabled || null,
|
|
60
73
|
'aria-labelledby': labelledBy,
|
|
@@ -67,7 +80,24 @@ export function useDatePicker<T extends DateValue>(props: AriaDatePickerProps<T>
|
|
|
67
80
|
focusManager.focusFirst();
|
|
68
81
|
}
|
|
69
82
|
},
|
|
70
|
-
fieldProps
|
|
83
|
+
fieldProps: {
|
|
84
|
+
...fieldProps,
|
|
85
|
+
[roleSymbol]: 'presentation',
|
|
86
|
+
'aria-describedby': ariaDescribedBy,
|
|
87
|
+
value: state.value,
|
|
88
|
+
onChange: state.setValue,
|
|
89
|
+
minValue: props.minValue,
|
|
90
|
+
maxValue: props.maxValue,
|
|
91
|
+
placeholderValue: props.placeholderValue,
|
|
92
|
+
hideTimeZone: props.hideTimeZone,
|
|
93
|
+
hourCycle: props.hourCycle,
|
|
94
|
+
granularity: props.granularity,
|
|
95
|
+
isDisabled: props.isDisabled,
|
|
96
|
+
isReadOnly: props.isReadOnly,
|
|
97
|
+
isRequired: props.isRequired,
|
|
98
|
+
validationState: state.validationState,
|
|
99
|
+
autoFocus: props.autoFocus
|
|
100
|
+
},
|
|
71
101
|
descriptionProps,
|
|
72
102
|
errorMessageProps,
|
|
73
103
|
buttonProps: {
|
|
@@ -77,11 +107,23 @@ export function useDatePicker<T extends DateValue>(props: AriaDatePickerProps<T>
|
|
|
77
107
|
'aria-haspopup': 'dialog',
|
|
78
108
|
'aria-label': formatMessage('calendar'),
|
|
79
109
|
'aria-labelledby': `${labelledBy} ${buttonId}`,
|
|
80
|
-
'aria-describedby': ariaDescribedBy
|
|
110
|
+
'aria-describedby': ariaDescribedBy,
|
|
111
|
+
onPress: () => state.setOpen(true)
|
|
81
112
|
},
|
|
82
113
|
dialogProps: {
|
|
83
114
|
id: dialogId,
|
|
84
115
|
'aria-labelledby': `${labelledBy} ${buttonId}`
|
|
116
|
+
},
|
|
117
|
+
calendarProps: {
|
|
118
|
+
autoFocus: true,
|
|
119
|
+
value: state.dateValue,
|
|
120
|
+
onChange: state.setDateValue,
|
|
121
|
+
minValue: props.minValue,
|
|
122
|
+
maxValue: props.maxValue,
|
|
123
|
+
isDisabled: props.isDisabled,
|
|
124
|
+
isReadOnly: props.isReadOnly,
|
|
125
|
+
isDateUnavailable: props.isDateUnavailable,
|
|
126
|
+
defaultFocusedValue: state.dateValue ? undefined : props.placeholderValue
|
|
85
127
|
}
|
|
86
128
|
};
|
|
87
129
|
}
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {DateFieldState, DatePickerState, DateRangePickerState} from '@react-stately/datepicker';
|
|
2
|
+
import {getFocusableTreeWalker} from '@react-aria/focus';
|
|
2
3
|
import {KeyboardEvent} from '@react-types/shared';
|
|
3
4
|
import {mergeProps} from '@react-aria/utils';
|
|
4
5
|
import {RefObject} from 'react';
|
|
5
6
|
import {usePress} from '@react-aria/interactions';
|
|
6
7
|
|
|
7
|
-
export function useDatePickerGroup(state: DatePickerState | DateRangePickerState |
|
|
8
|
+
export function useDatePickerGroup(state: DatePickerState | DateRangePickerState | DateFieldState, ref: RefObject<HTMLElement>) {
|
|
8
9
|
// Open the popover on alt + arrow down
|
|
9
10
|
let onKeyDown = (e: KeyboardEvent) => {
|
|
10
|
-
if (e.altKey && e.key === 'ArrowDown' && 'setOpen' in state) {
|
|
11
|
+
if (e.altKey && (e.key === 'ArrowDown' || e.key === 'ArrowUp') && 'setOpen' in state) {
|
|
11
12
|
e.preventDefault();
|
|
12
13
|
e.stopPropagation();
|
|
13
14
|
state.setOpen(true);
|
|
@@ -16,15 +17,37 @@ export function useDatePickerGroup(state: DatePickerState | DateRangePickerState
|
|
|
16
17
|
|
|
17
18
|
// Focus the first placeholder segment from the end on mouse down/touch up in the field.
|
|
18
19
|
let focusLast = () => {
|
|
19
|
-
|
|
20
|
-
let
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
// Try to find the segment prior to the element that was clicked on.
|
|
21
|
+
let target = window.event?.target as HTMLElement;
|
|
22
|
+
let walker = getFocusableTreeWalker(ref.current, {tabbable: true});
|
|
23
|
+
if (target) {
|
|
24
|
+
walker.currentNode = target;
|
|
25
|
+
target = walker.previousNode() as HTMLElement;
|
|
23
26
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (
|
|
27
|
-
|
|
27
|
+
|
|
28
|
+
// If no target found, find the last element from the end.
|
|
29
|
+
if (!target) {
|
|
30
|
+
let last: HTMLElement;
|
|
31
|
+
do {
|
|
32
|
+
last = walker.lastChild() as HTMLElement;
|
|
33
|
+
if (last) {
|
|
34
|
+
target = last;
|
|
35
|
+
}
|
|
36
|
+
} while (last);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Now go backwards until we find an element that is not a placeholder.
|
|
40
|
+
while (target?.getAttribute('aria-placeholder')) {
|
|
41
|
+
let prev = walker.previousNode() as HTMLElement;
|
|
42
|
+
if (prev && prev.getAttribute('aria-placeholder')) {
|
|
43
|
+
target = prev;
|
|
44
|
+
} else {
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (target) {
|
|
50
|
+
target.focus();
|
|
28
51
|
}
|
|
29
52
|
};
|
|
30
53
|
|
|
@@ -15,29 +15,44 @@ import {AriaDatePickerProps, AriaDateRangePickerProps, DateValue} from '@react-t
|
|
|
15
15
|
import {AriaDialogProps} from '@react-types/dialog';
|
|
16
16
|
import {createFocusManager} from '@react-aria/focus';
|
|
17
17
|
import {DateRangePickerState} from '@react-stately/datepicker';
|
|
18
|
-
import {
|
|
18
|
+
import {focusManagerSymbol, roleSymbol} from './useDateField';
|
|
19
|
+
import {HTMLAttributes, RefObject, useMemo} from 'react';
|
|
19
20
|
// @ts-ignore
|
|
20
21
|
import intlMessages from '../intl/*.json';
|
|
21
|
-
import {mergeProps, useDescription, useId
|
|
22
|
+
import {mergeProps, useDescription, useId} from '@react-aria/utils';
|
|
23
|
+
import {RangeCalendarProps} from '@react-types/calendar';
|
|
22
24
|
import {useDatePickerGroup} from './useDatePickerGroup';
|
|
23
25
|
import {useField} from '@react-aria/label';
|
|
24
26
|
import {useFocusWithin} from '@react-aria/interactions';
|
|
25
27
|
import {useLocale, useMessageFormatter} from '@react-aria/i18n';
|
|
26
28
|
|
|
27
|
-
interface DateRangePickerAria
|
|
28
|
-
|
|
29
|
+
interface DateRangePickerAria {
|
|
30
|
+
/** Props for the date range picker's visible label element, if any. */
|
|
31
|
+
labelProps: HTMLAttributes<HTMLElement>,
|
|
32
|
+
/** Props for the grouping element containing the date fields and button. */
|
|
29
33
|
groupProps: HTMLAttributes<HTMLElement>,
|
|
30
|
-
|
|
31
|
-
|
|
34
|
+
/** Props for the start date field. */
|
|
35
|
+
startFieldProps: AriaDatePickerProps<DateValue>,
|
|
36
|
+
/** Props for the end date field. */
|
|
37
|
+
endFieldProps: AriaDatePickerProps<DateValue>,
|
|
38
|
+
/** Props for the popover trigger button. */
|
|
39
|
+
buttonProps: AriaButtonProps,
|
|
32
40
|
/** Props for the description element, if any. */
|
|
33
41
|
descriptionProps: HTMLAttributes<HTMLElement>,
|
|
34
42
|
/** Props for the error message element, if any. */
|
|
35
43
|
errorMessageProps: HTMLAttributes<HTMLElement>,
|
|
36
|
-
|
|
37
|
-
dialogProps:
|
|
44
|
+
/** Props for the popover dialog. */
|
|
45
|
+
dialogProps: AriaDialogProps,
|
|
46
|
+
/** Props for the range calendar within the popover dialog. */
|
|
47
|
+
calendarProps: RangeCalendarProps<DateValue>
|
|
38
48
|
}
|
|
39
49
|
|
|
40
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Provides the behavior and accessibility implementation for a date picker component.
|
|
52
|
+
* A date range picker combines two DateFields and a RangeCalendar popover to allow
|
|
53
|
+
* users to enter or select a date and time range.
|
|
54
|
+
*/
|
|
55
|
+
export function useDateRangePicker<T extends DateValue>(props: AriaDateRangePickerProps<T>, state: DateRangePickerState, ref: RefObject<HTMLElement>): DateRangePickerAria {
|
|
41
56
|
let formatMessage = useMessageFormatter(intlMessages);
|
|
42
57
|
let {labelProps, fieldProps, descriptionProps, errorMessageProps} = useField({
|
|
43
58
|
...props,
|
|
@@ -50,15 +65,15 @@ export function useDateRangePicker<T extends DateValue>(props: AriaDateRangePick
|
|
|
50
65
|
let description = state.formatValue(locale, {month: 'long'});
|
|
51
66
|
let descProps = useDescription(description);
|
|
52
67
|
|
|
53
|
-
let startFieldProps =
|
|
68
|
+
let startFieldProps = {
|
|
54
69
|
'aria-label': formatMessage('startDate'),
|
|
55
70
|
'aria-labelledby': labelledBy
|
|
56
|
-
}
|
|
71
|
+
};
|
|
57
72
|
|
|
58
|
-
let endFieldProps =
|
|
73
|
+
let endFieldProps = {
|
|
59
74
|
'aria-label': formatMessage('endDate'),
|
|
60
75
|
'aria-labelledby': labelledBy
|
|
61
|
-
}
|
|
76
|
+
};
|
|
62
77
|
|
|
63
78
|
let buttonId = useId();
|
|
64
79
|
let dialogId = useId();
|
|
@@ -71,6 +86,22 @@ export function useDateRangePicker<T extends DateValue>(props: AriaDateRangePick
|
|
|
71
86
|
});
|
|
72
87
|
|
|
73
88
|
let ariaDescribedBy = [descProps['aria-describedby'], fieldProps['aria-describedby']].filter(Boolean).join(' ') || undefined;
|
|
89
|
+
let focusManager = useMemo(() => createFocusManager(ref), [ref]);
|
|
90
|
+
let commonFieldProps = {
|
|
91
|
+
[focusManagerSymbol]: focusManager,
|
|
92
|
+
[roleSymbol]: 'presentation',
|
|
93
|
+
'aria-describedby': ariaDescribedBy,
|
|
94
|
+
minValue: props.minValue,
|
|
95
|
+
maxValue: props.maxValue,
|
|
96
|
+
placeholderValue: props.placeholderValue,
|
|
97
|
+
hideTimeZone: props.hideTimeZone,
|
|
98
|
+
hourCycle: props.hourCycle,
|
|
99
|
+
granularity: props.granularity,
|
|
100
|
+
isDisabled: props.isDisabled,
|
|
101
|
+
isReadOnly: props.isReadOnly,
|
|
102
|
+
isRequired: props.isRequired,
|
|
103
|
+
validationState: state.validationState
|
|
104
|
+
};
|
|
74
105
|
|
|
75
106
|
return {
|
|
76
107
|
groupProps: mergeProps(groupProps, fieldProps, descProps, focusWithinProps, {
|
|
@@ -81,7 +112,6 @@ export function useDateRangePicker<T extends DateValue>(props: AriaDateRangePick
|
|
|
81
112
|
labelProps: {
|
|
82
113
|
...labelProps,
|
|
83
114
|
onClick: () => {
|
|
84
|
-
let focusManager = createFocusManager(ref);
|
|
85
115
|
focusManager.focusFirst();
|
|
86
116
|
}
|
|
87
117
|
},
|
|
@@ -92,7 +122,8 @@ export function useDateRangePicker<T extends DateValue>(props: AriaDateRangePick
|
|
|
92
122
|
'aria-haspopup': 'dialog',
|
|
93
123
|
'aria-label': formatMessage('calendar'),
|
|
94
124
|
'aria-labelledby': `${labelledBy} ${buttonId}`,
|
|
95
|
-
'aria-describedby': ariaDescribedBy
|
|
125
|
+
'aria-describedby': ariaDescribedBy,
|
|
126
|
+
onPress: () => state.setOpen(true)
|
|
96
127
|
},
|
|
97
128
|
dialogProps: {
|
|
98
129
|
id: dialogId,
|
|
@@ -100,13 +131,30 @@ export function useDateRangePicker<T extends DateValue>(props: AriaDateRangePick
|
|
|
100
131
|
},
|
|
101
132
|
startFieldProps: {
|
|
102
133
|
...startFieldProps,
|
|
103
|
-
|
|
134
|
+
...commonFieldProps,
|
|
135
|
+
value: state.value?.start,
|
|
136
|
+
onChange: start => state.setDateTime('start', start),
|
|
137
|
+
autoFocus: props.autoFocus
|
|
104
138
|
},
|
|
105
139
|
endFieldProps: {
|
|
106
140
|
...endFieldProps,
|
|
107
|
-
|
|
141
|
+
...commonFieldProps,
|
|
142
|
+
value: state.value?.end,
|
|
143
|
+
onChange: end => state.setDateTime('end', end)
|
|
108
144
|
},
|
|
109
145
|
descriptionProps,
|
|
110
|
-
errorMessageProps
|
|
146
|
+
errorMessageProps,
|
|
147
|
+
calendarProps: {
|
|
148
|
+
autoFocus: true,
|
|
149
|
+
value: state.dateRange,
|
|
150
|
+
onChange: state.setDateRange,
|
|
151
|
+
minValue: props.minValue,
|
|
152
|
+
maxValue: props.maxValue,
|
|
153
|
+
isDisabled: props.isDisabled,
|
|
154
|
+
isReadOnly: props.isReadOnly,
|
|
155
|
+
isDateUnavailable: props.isDateUnavailable,
|
|
156
|
+
allowsNonContiguousRanges: props.allowsNonContiguousRanges,
|
|
157
|
+
defaultFocusedValue: state.dateRange ? undefined : props.placeholderValue
|
|
158
|
+
}
|
|
111
159
|
};
|
|
112
160
|
}
|