@react-aria/calendar 3.0.0-alpha.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 +201 -0
- package/README.md +3 -0
- package/dist/main.js +777 -0
- package/dist/main.js.map +1 -0
- package/dist/module.js +724 -0
- package/dist/module.js.map +1 -0
- package/dist/types.d.ts +39 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +40 -0
- package/src/index.ts +18 -0
- package/src/types.ts +25 -0
- package/src/useCalendar.ts +20 -0
- package/src/useCalendarBase.ts +70 -0
- package/src/useCalendarCell.ts +244 -0
- package/src/useCalendarGrid.ts +122 -0
- package/src/useCalendarTableHeader.ts +11 -0
- package/src/useRangeCalendar.ts +71 -0
- package/src/utils.ts +74 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {CalendarDate} from '@internationalized/date';
|
|
14
|
+
import {CalendarGridAria} from './types';
|
|
15
|
+
import {calendarIds, useSelectedDateDescription, useVisibleRangeDescription} from './utils';
|
|
16
|
+
import {CalendarPropsBase} from '@react-types/calendar';
|
|
17
|
+
import {CalendarState, RangeCalendarState} from '@react-stately/calendar';
|
|
18
|
+
import {KeyboardEvent} from 'react';
|
|
19
|
+
import {mergeProps, useDescription, useLabels} from '@react-aria/utils';
|
|
20
|
+
import {useLocale} from '@react-aria/i18n';
|
|
21
|
+
|
|
22
|
+
interface CalendarGridProps extends CalendarPropsBase {
|
|
23
|
+
startDate?: CalendarDate,
|
|
24
|
+
endDate?: CalendarDate
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function useCalendarGrid(props: CalendarGridProps, state: CalendarState | RangeCalendarState): CalendarGridAria {
|
|
28
|
+
let {
|
|
29
|
+
isReadOnly = false,
|
|
30
|
+
isDisabled = false,
|
|
31
|
+
startDate = state.visibleRange.start,
|
|
32
|
+
endDate = state.visibleRange.end
|
|
33
|
+
} = props;
|
|
34
|
+
|
|
35
|
+
let {direction} = useLocale();
|
|
36
|
+
|
|
37
|
+
let onKeyDown = (e: KeyboardEvent) => {
|
|
38
|
+
switch (e.key) {
|
|
39
|
+
case 'Enter':
|
|
40
|
+
case ' ':
|
|
41
|
+
e.preventDefault();
|
|
42
|
+
state.selectFocusedDate();
|
|
43
|
+
break;
|
|
44
|
+
case 'PageUp':
|
|
45
|
+
e.preventDefault();
|
|
46
|
+
if (e.shiftKey) {
|
|
47
|
+
state.focusPreviousSection();
|
|
48
|
+
} else {
|
|
49
|
+
state.focusPreviousPage();
|
|
50
|
+
}
|
|
51
|
+
break;
|
|
52
|
+
case 'PageDown':
|
|
53
|
+
e.preventDefault();
|
|
54
|
+
if (e.shiftKey) {
|
|
55
|
+
state.focusNextSection();
|
|
56
|
+
} else {
|
|
57
|
+
state.focusNextPage();
|
|
58
|
+
}
|
|
59
|
+
break;
|
|
60
|
+
case 'End':
|
|
61
|
+
e.preventDefault();
|
|
62
|
+
state.focusPageEnd();
|
|
63
|
+
break;
|
|
64
|
+
case 'Home':
|
|
65
|
+
e.preventDefault();
|
|
66
|
+
state.focusPageStart();
|
|
67
|
+
break;
|
|
68
|
+
case 'ArrowLeft':
|
|
69
|
+
e.preventDefault();
|
|
70
|
+
if (direction === 'rtl') {
|
|
71
|
+
state.focusNextDay();
|
|
72
|
+
} else {
|
|
73
|
+
state.focusPreviousDay();
|
|
74
|
+
}
|
|
75
|
+
break;
|
|
76
|
+
case 'ArrowUp':
|
|
77
|
+
e.preventDefault();
|
|
78
|
+
state.focusPreviousRow();
|
|
79
|
+
break;
|
|
80
|
+
case 'ArrowRight':
|
|
81
|
+
e.preventDefault();
|
|
82
|
+
if (direction === 'rtl') {
|
|
83
|
+
state.focusPreviousDay();
|
|
84
|
+
} else {
|
|
85
|
+
state.focusNextDay();
|
|
86
|
+
}
|
|
87
|
+
break;
|
|
88
|
+
case 'ArrowDown':
|
|
89
|
+
e.preventDefault();
|
|
90
|
+
state.focusNextRow();
|
|
91
|
+
break;
|
|
92
|
+
case 'Escape':
|
|
93
|
+
// Cancel the selection.
|
|
94
|
+
if ('setAnchorDate' in state) {
|
|
95
|
+
e.preventDefault();
|
|
96
|
+
state.setAnchorDate(null);
|
|
97
|
+
}
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
let selectedDateDescription = useSelectedDateDescription(state);
|
|
103
|
+
let descriptionProps = useDescription(selectedDateDescription);
|
|
104
|
+
let visibleRangeDescription = useVisibleRangeDescription(startDate, endDate, state.timeZone);
|
|
105
|
+
|
|
106
|
+
let labelProps = useLabels({
|
|
107
|
+
'aria-label': visibleRangeDescription,
|
|
108
|
+
'aria-labelledby': calendarIds.get(state)
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
gridProps: mergeProps(descriptionProps, labelProps, {
|
|
113
|
+
role: 'grid',
|
|
114
|
+
'aria-readonly': isReadOnly || null,
|
|
115
|
+
'aria-disabled': isDisabled || null,
|
|
116
|
+
'aria-multiselectable': ('highlightedRange' in state) || undefined,
|
|
117
|
+
onKeyDown,
|
|
118
|
+
onFocus: () => state.setFocused(true),
|
|
119
|
+
onBlur: () => state.setFocused(false)
|
|
120
|
+
})
|
|
121
|
+
};
|
|
122
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {CalendarAria} from './types';
|
|
14
|
+
import {DateValue, RangeCalendarProps} from '@react-types/calendar';
|
|
15
|
+
import {RangeCalendarState} from '@react-stately/calendar';
|
|
16
|
+
import {RefObject, useRef} from 'react';
|
|
17
|
+
import {useCalendarBase} from './useCalendarBase';
|
|
18
|
+
import {useEvent, useId} from '@react-aria/utils';
|
|
19
|
+
|
|
20
|
+
export function useRangeCalendar<T extends DateValue>(props: RangeCalendarProps<T>, state: RangeCalendarState, ref: RefObject<HTMLElement>): CalendarAria {
|
|
21
|
+
let res = useCalendarBase(props, state);
|
|
22
|
+
res.nextButtonProps.id = useId();
|
|
23
|
+
res.prevButtonProps.id = useId();
|
|
24
|
+
|
|
25
|
+
// We need to ignore virtual pointer events from VoiceOver due to these bugs.
|
|
26
|
+
// https://bugs.webkit.org/show_bug.cgi?id=222627
|
|
27
|
+
// https://bugs.webkit.org/show_bug.cgi?id=223202
|
|
28
|
+
// usePress also does this and waits for the following click event before firing.
|
|
29
|
+
// We need to match that here otherwise this will fire before the press event in
|
|
30
|
+
// useCalendarCell, causing range selection to not work properly.
|
|
31
|
+
let isVirtualClick = useRef(false);
|
|
32
|
+
useEvent(useRef(window), 'pointerdown', e => {
|
|
33
|
+
isVirtualClick.current = e.width === 0 && e.height === 0;
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Stop range selection when pressing or releasing a pointer outside the calendar body,
|
|
37
|
+
// except when pressing the next or previous buttons to switch months.
|
|
38
|
+
let endDragging = e => {
|
|
39
|
+
if (isVirtualClick.current) {
|
|
40
|
+
isVirtualClick.current = false;
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
state.setDragging(false);
|
|
45
|
+
if (!state.anchorDate) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let target = e.target as HTMLElement;
|
|
50
|
+
let body = document.getElementById(res.calendarProps.id);
|
|
51
|
+
if (
|
|
52
|
+
(!body.contains(target) || target.getAttribute('role') !== 'button') &&
|
|
53
|
+
!document.getElementById(res.nextButtonProps.id)?.contains(target) &&
|
|
54
|
+
!document.getElementById(res.prevButtonProps.id)?.contains(target)
|
|
55
|
+
) {
|
|
56
|
+
state.selectFocusedDate();
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
useEvent(useRef(window), 'pointerup', endDragging);
|
|
61
|
+
useEvent(useRef(window), 'pointercancel', endDragging);
|
|
62
|
+
|
|
63
|
+
// Prevent touch scrolling while dragging
|
|
64
|
+
useEvent(ref, 'touchmove', e => {
|
|
65
|
+
if (state.isDragging) {
|
|
66
|
+
e.preventDefault();
|
|
67
|
+
}
|
|
68
|
+
}, {passive: false, capture: true});
|
|
69
|
+
|
|
70
|
+
return res;
|
|
71
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {CalendarDate, endOfMonth, isSameDay, startOfMonth, toDate} from '@internationalized/date';
|
|
14
|
+
import {CalendarState, RangeCalendarState} from '@react-stately/calendar';
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
import intlMessages from '../intl/*.json';
|
|
17
|
+
import {useDateFormatter, useMessageFormatter} from '@react-aria/i18n';
|
|
18
|
+
import {useMemo} from 'react';
|
|
19
|
+
|
|
20
|
+
export const calendarIds = new WeakMap<CalendarState | RangeCalendarState, string>();
|
|
21
|
+
|
|
22
|
+
export function useSelectedDateDescription(state: CalendarState | RangeCalendarState) {
|
|
23
|
+
let formatMessage = useMessageFormatter(intlMessages);
|
|
24
|
+
|
|
25
|
+
let start: CalendarDate, end: CalendarDate;
|
|
26
|
+
if ('highlightedRange' in state) {
|
|
27
|
+
({start, end} = state.highlightedRange || {});
|
|
28
|
+
} else {
|
|
29
|
+
start = end = state.value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let anchorDate = 'anchorDate' in state ? state.anchorDate : null;
|
|
33
|
+
return useMemo(() => {
|
|
34
|
+
// No message if currently selecting a range, or there is nothing highlighted.
|
|
35
|
+
if (!anchorDate && start && end) {
|
|
36
|
+
// Use a single date message if the start and end dates are the same day,
|
|
37
|
+
// otherwise include both dates.
|
|
38
|
+
if (isSameDay(start, end)) {
|
|
39
|
+
return formatMessage('selectedDateDescription', {date: toDate(start, state.timeZone)});
|
|
40
|
+
} else {
|
|
41
|
+
return formatMessage('selectedRangeDescription', {start: toDate(start, state.timeZone), end: toDate(end, state.timeZone)});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return '';
|
|
45
|
+
}, [start, end, anchorDate, state.timeZone, formatMessage]);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function useVisibleRangeDescription(startDate: CalendarDate, endDate: CalendarDate, timeZone: string) {
|
|
49
|
+
let monthFormatter = useDateFormatter({
|
|
50
|
+
month: 'long',
|
|
51
|
+
year: 'numeric',
|
|
52
|
+
era: startDate.calendar.identifier !== 'gregory' ? 'long' : undefined,
|
|
53
|
+
calendar: startDate.calendar.identifier
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
let dateFormatter = useDateFormatter({
|
|
57
|
+
dateStyle: 'long',
|
|
58
|
+
calendar: startDate.calendar.identifier
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
return useMemo(() => {
|
|
62
|
+
// Special case for month granularity. Format as a single month if only a
|
|
63
|
+
// single month is visible, otherwise format as a range of months.
|
|
64
|
+
if (isSameDay(startDate, startOfMonth(startDate))) {
|
|
65
|
+
if (isSameDay(endDate, endOfMonth(startDate))) {
|
|
66
|
+
return monthFormatter.format(startDate.toDate(timeZone));
|
|
67
|
+
} else if (isSameDay(endDate, endOfMonth(endDate))) {
|
|
68
|
+
return monthFormatter.formatRange(startDate.toDate(timeZone), endDate.toDate(timeZone));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return dateFormatter.formatRange(startDate.toDate(timeZone), endDate.toDate(timeZone));
|
|
73
|
+
}, [startDate, endDate, monthFormatter, dateFormatter, timeZone]);
|
|
74
|
+
}
|