Skip to content

Commit 58e3257

Browse files
authored
fix: Set a placeholder time when closing a DateRangePicker with time granularity (#8278)
1 parent 8f684df commit 58e3257

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

packages/@react-stately/datepicker/src/useDateRangePickerState.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ export function useDateRangePickerState<T extends DateValue = DateValue>(props:
131131
let setDateRange = (range: RangeValue<DateValue | null>) => {
132132
let shouldClose = typeof shouldCloseOnSelect === 'function' ? shouldCloseOnSelect() : shouldCloseOnSelect;
133133
if (hasTime) {
134-
if (isCompleteRange(range) && timeRange?.start && timeRange?.end) {
134+
// Set a placeholder time if the popover is closing so we don't leave the field in an incomplete state.
135+
if (isCompleteRange(range) && (shouldClose || (timeRange?.start && timeRange?.end))) {
135136
commitValue(range, {
136137
start: timeRange?.start || getPlaceholderTime(props.placeholderValue),
137138
end: timeRange?.end || getPlaceholderTime(props.placeholderValue)

packages/react-aria-components/test/DateRangePicker.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ let TestDateRangePicker = (props) => (
3333
<Text slot="errorMessage">Error</Text>
3434
<Popover>
3535
<Dialog>
36-
<RangeCalendar>
36+
<RangeCalendar focusedValue={props.focusedValue}>
3737
<header>
3838
<Button slot="previous"></Button>
3939
<Heading />
@@ -280,6 +280,26 @@ describe('DateRangePicker', () => {
280280
expect(dialog).toBeInTheDocument();
281281
});
282282

283+
it('should set a placeholder time when closing', async () => {
284+
let {getByRole, getAllByRole} = render(<TestDateRangePicker granularity="second" focusedValue={new CalendarDate(2023, 1, 10)} />);
285+
286+
let button = getByRole('button');
287+
await user.click(button);
288+
289+
let dialog = getByRole('dialog');
290+
let cells = getAllByRole('gridcell');
291+
292+
await user.click(cells[5].children[0]);
293+
await user.click(cells[10].children[0]);
294+
expect(dialog).not.toBeInTheDocument();
295+
296+
let group = getByRole('group');
297+
let inputs = group.querySelectorAll('.react-aria-DateInput');
298+
let normalize = (s) => s.replace(/\s/g, ' ').replace(/[\u2066\u2069]/g, '');
299+
expect(normalize(inputs[0].textContent)).toBe(normalize(new Date(2023, 0, 6).toLocaleString()));
300+
expect(normalize(inputs[1].textContent)).toBe(normalize(new Date(2023, 0, 11).toLocaleString()));
301+
});
302+
283303
it('should disable button and date input when DatePicker is disabled', () => {
284304
let {getByRole} = render(<TestDateRangePicker isDisabled />);
285305

0 commit comments

Comments
 (0)