Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(date-picker): clearing input and selecting a date #17669

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions packages/react/src/components/DatePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -710,10 +710,6 @@ const DatePicker = React.forwardRef(function DatePicker(
}

function handleOnChange(event) {
if (datePickerType == 'single' && closeOnSelect) {
calendar.calendarContainer.classList.remove('open');
}

const { target } = event;
if (target === start) {
lastStartValue.current = start.value;
Expand All @@ -730,14 +726,22 @@ const DatePicker = React.forwardRef(function DatePicker(
if (calendar.selectedDates.length === 0) {
return;
}
}

calendar.clear();
calendar.input.focus();
function handleKeyPress(event) {
if (
match(event, keys.Enter) &&
closeOnSelect &&
datePickerType == 'single'
) {
calendar.calendarContainer.classList.remove('open');
}
}

if (start) {
start.addEventListener('keydown', handleArrowDown);
start.addEventListener('change', handleOnChange);
start.addEventListener('keypress', handleKeyPress);

if (calendar && calendar.calendarContainer) {
// Flatpickr's calendar dialog is not rendered in a landmark causing an
Expand All @@ -755,6 +759,7 @@ const DatePicker = React.forwardRef(function DatePicker(
if (end) {
end.addEventListener('keydown', handleArrowDown);
end.addEventListener('change', handleOnChange);
end.addEventListener('keypress', handleKeyPress);
}

//component did unmount equivalent
Expand All @@ -779,11 +784,13 @@ const DatePicker = React.forwardRef(function DatePicker(
if (start) {
start.removeEventListener('keydown', handleArrowDown);
start.removeEventListener('change', handleOnChange);
start.removeEventListener('keypress', handleKeyPress);
}

if (end) {
end.removeEventListener('keydown', handleArrowDown);
end.removeEventListener('change', handleOnChange);
end.removeEventListener('change', handleKeyPress);
}
};
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
Loading