Skip to content

Commit 2c25922

Browse files
authored
Fix readonly range calendar behavior (#2903)
1 parent f5fd94d commit 2c25922

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

packages/@react-aria/calendar/src/useCalendarCell.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ export function useCalendarCell(props: AriaCalendarCellProps, state: CalendarSta
129129
preventFocusOnPress: true,
130130
isDisabled,
131131
onPressStart(e) {
132+
if (state.isReadOnly) {
133+
state.setFocusedDate(date);
134+
return;
135+
}
136+
132137
if ('highlightedRange' in state && !state.anchorDate && (e.pointerType === 'mouse' || e.pointerType === 'touch')) {
133138
// Allow dragging the start or end date of a range to modify it
134139
// rather than starting a new selection.
@@ -174,12 +179,16 @@ export function useCalendarCell(props: AriaCalendarCellProps, state: CalendarSta
174179
},
175180
onPress() {
176181
// For non-range selection, always select on press up.
177-
if (!('anchorDate' in state)) {
182+
if (!('anchorDate' in state) && !state.isReadOnly) {
178183
state.selectDate(date);
179184
state.setFocusedDate(date);
180185
}
181186
},
182187
onPressUp(e) {
188+
if (state.isReadOnly) {
189+
return;
190+
}
191+
183192
// If the user tapped quickly, the date won't be selected yet and the
184193
// timer will still be in progress. In this case, select the date on touch up.
185194
// Timer is cleared in onPressEnd.

packages/@react-spectrum/calendar/test/RangeCalendar.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,29 @@ describe('RangeCalendar', () => {
839839
expect(document.activeElement).toBe(cell);
840840
});
841841

842+
it('does not enter selection mode with the mouse on range end if isReadOnly', () => {
843+
let {getAllByLabelText, getByText} = render(<RangeCalendar isReadOnly autoFocus defaultValue={{start: new CalendarDate(2019, 6, 10), end: new CalendarDate(2019, 6, 20)}} />);
844+
845+
let cell = getByText('10').parentElement;
846+
expect(document.activeElement).toBe(cell);
847+
848+
// try to enter selection mode
849+
act(() => userEvent.click(cell));
850+
expect(document.activeElement).toBe(cell);
851+
852+
let selectedDates = getAllByLabelText('selected', {exact: false});
853+
expect(selectedDates[0].textContent).toBe('10');
854+
expect(selectedDates[selectedDates.length - 1].textContent).toBe('20');
855+
856+
cell = getByText('15').parentElement;
857+
act(() => userEvent.click(cell));
858+
expect(document.activeElement).toBe(cell);
859+
860+
selectedDates = getAllByLabelText('selected', {exact: false});
861+
expect(selectedDates[0].textContent).toBe('10');
862+
expect(selectedDates[selectedDates.length - 1].textContent).toBe('20');
863+
});
864+
842865
it.each`
843866
Name | RangeCalendar | props
844867
${'v3'} | ${RangeCalendar} | ${{isDisabled: true}}

0 commit comments

Comments
 (0)