@@ -22,7 +22,8 @@ import {useRef} from 'react';
22
22
* A range calendar displays one or more date grids and allows users to select a contiguous range of dates.
23
23
*/
24
24
export function useRangeCalendar < T extends DateValue > ( props : AriaRangeCalendarProps < T > , state : RangeCalendarState , ref : RefObject < FocusableElement | null > ) : CalendarAria {
25
- let res = useCalendarBase ( props , state ) ;
25
+ let { pointerUpOutsideAction = 'select' , ...otherProps } = props ;
26
+ let res = useCalendarBase ( otherProps , state ) ;
26
27
27
28
// We need to ignore virtual pointer events from VoiceOver due to these bugs.
28
29
// https://bugs.webkit.org/show_bug.cgi?id=222627
@@ -36,7 +37,13 @@ export function useRangeCalendar<T extends DateValue>(props: AriaRangeCalendarPr
36
37
isVirtualClick . current = e . width === 0 && e . height === 0 ;
37
38
} ) ;
38
39
39
- // Stop range selection when pressing or releasing a pointer outside the calendar body,
40
+ const pointerUpOutsideActionMapping = {
41
+ clear : ( ) => state . clearSelection ( ) ,
42
+ reset : ( ) => state . setAnchorDate ( null ) ,
43
+ select : ( ) => state . selectFocusedDate ( )
44
+ } ;
45
+
46
+ // Execute method corresponding to `pointerUpOutsideAction` when pressing or releasing a pointer outside the calendar body,
40
47
// except when pressing the next or previous buttons to switch months.
41
48
let endDragging = ( e : PointerEvent ) => {
42
49
if ( isVirtualClick . current ) {
@@ -55,19 +62,20 @@ export function useRangeCalendar<T extends DateValue>(props: AriaRangeCalendarPr
55
62
ref . current . contains ( document . activeElement ) &&
56
63
( ! ref . current . contains ( target ) || ! target . closest ( 'button, [role="button"]' ) )
57
64
) {
58
- state . selectFocusedDate ( ) ;
65
+ pointerUpOutsideActionMapping [ pointerUpOutsideAction ] ( ) ;
59
66
}
60
67
} ;
61
68
62
69
useEvent ( windowRef , 'pointerup' , endDragging ) ;
63
70
64
- // Also stop range selection on blur, e.g. tabbing away from the calendar.
71
+ // Also execute method corresponding to `pointerUpOutsideAction` on blur,
72
+ // e.g. tabbing away from the calendar.
65
73
res . calendarProps . onBlur = e => {
66
74
if ( ! ref . current ) {
67
75
return ;
68
76
}
69
77
if ( ( ! e . relatedTarget || ! ref . current . contains ( e . relatedTarget ) ) && state . anchorDate ) {
70
- state . selectFocusedDate ( ) ;
78
+ pointerUpOutsideActionMapping [ pointerUpOutsideAction ] ( ) ;
71
79
}
72
80
} ;
73
81
0 commit comments