Skip to content

Commit

Permalink
fix(calendar-input): call onchange with empty value
Browse files Browse the repository at this point in the history
  • Loading branch information
reme3d2y committed Apr 9, 2021
1 parent 744250a commit 3fbc73a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/calendar-input/src/Component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,25 @@ describe('CalendarInput', () => {

expect(cb).toBeCalledTimes(1);
});

it('should call onChange if empty date is entered', async () => {
const cb = jest.fn();
const { queryByRole } = render(
<CalendarInput calendarPosition='static' defaultValue='1' onChange={cb} />,
);

const input = queryByRole('textbox') as HTMLInputElement;

input.setSelectionRange(0, 2);
await userEvent.type(input, '{backspace}');

expect(cb).toBeCalledTimes(1);

const { date, value } = cb.mock.calls[0][1];

expect(value).toBe('');
expect((date as Date).getTime()).toBeNaN();
});
});

describe('Render tests', () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/calendar-input/src/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,13 @@ export const CalendarInput = forwardRef<HTMLInputElement, CalendarInputProps>(
const newValue = event.target.value;
const newDate = parseDateString(newValue);

changeHandler(event, newValue, newDate, 'input', isCompleteDateInput(newValue));
changeHandler(
event,
newValue,
newDate,
'input',
!newValue || isCompleteDateInput(newValue),
);
},
[changeHandler],
);
Expand Down

0 comments on commit 3fbc73a

Please sign in to comment.