Skip to content

Commit 7417956

Browse files
authored
adds null check to selectTime in useDatePickerState (#4647)
* adds null check in selectTime * add test * remove only from test
1 parent f6d37e1 commit 7417956

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

packages/@react-spectrum/datepicker/test/DatePicker.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,55 @@ describe('DatePicker', function () {
470470
expect(getTextValue(combobox)).toBe('2/4/2019, 9:45 AM');
471471
});
472472

473+
it('should not throw error when deleting values from time field when CalendarDateTime value is used', function () {
474+
let onChange = jest.fn();
475+
let {getByRole, getAllByRole, getAllByLabelText} = render(
476+
<Provider theme={theme}>
477+
<DatePicker label="Date" defaultValue={new CalendarDateTime(2019, 2, 3, 10, 45)} onChange={onChange} />
478+
</Provider>
479+
);
480+
481+
let combobox = getAllByRole('group')[0];
482+
expect(getTextValue(combobox)).toBe('2/3/2019, 10:45 AM');
483+
484+
let button = getByRole('button');
485+
triggerPress(button);
486+
487+
let dialog = getByRole('dialog');
488+
expect(dialog).toBeVisible();
489+
490+
let cells = getAllByRole('gridcell');
491+
let selected = cells.find(cell => cell.getAttribute('aria-selected') === 'true');
492+
expect(selected.children[0]).toHaveAttribute('aria-label', 'Sunday, February 3, 2019 selected');
493+
494+
let timeField = getAllByLabelText('Time')[0];
495+
expect(getTextValue(timeField)).toBe('10:45 AM');
496+
497+
// selecting a date should not close the popover
498+
triggerPress(selected.nextSibling.children[0]);
499+
500+
expect(dialog).toBeVisible();
501+
expect(onChange).toHaveBeenCalledTimes(1);
502+
expect(onChange).toHaveBeenCalledWith(new CalendarDateTime(2019, 2, 4, 10, 45));
503+
expect(getTextValue(combobox)).toBe('2/4/2019, 10:45 AM');
504+
505+
let hour = within(timeField).getByLabelText('hour,');
506+
expect(hour).toHaveAttribute('role', 'spinbutton');
507+
expect(hour).toHaveAttribute('aria-valuetext', '10 AM');
508+
509+
act(() => hour.focus());
510+
fireEvent.keyDown(hour, {key: 'Backspace'});
511+
expect(hour).toHaveAttribute('aria-valuetext', '1 AM');
512+
513+
fireEvent.keyDown(hour, {key: 'Backspace'});
514+
expect(hour).toHaveAttribute('aria-valuetext', '1 AM');
515+
516+
expect(dialog).toBeVisible();
517+
expect(onChange).toHaveBeenCalledTimes(2);
518+
expect(onChange).toHaveBeenCalledWith(new CalendarDateTime(2019, 2, 4, 1, 45));
519+
expect(getTextValue(combobox)).toBe('2/4/2019, 1:45 AM');
520+
});
521+
473522
it('should not fire onChange until both date and time are selected', function () {
474523
let onChange = jest.fn();
475524
let {getByRole, getAllByRole, getAllByLabelText} = render(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export function useDatePickerState<T extends DateValue = DateValue>(props: DateP
113113
};
114114

115115
let selectTime = (newValue: TimeValue) => {
116-
if (selectedDate) {
116+
if (selectedDate && newValue) {
117117
commitValue(selectedDate, newValue);
118118
} else {
119119
setSelectedTime(newValue);

0 commit comments

Comments
 (0)