Skip to content

Commit

Permalink
fix(date-picker): clearing input and selecting a date (#17669)
Browse files Browse the repository at this point in the history
* fix(date-picker): keep open when clearing input

* fix(date-picker): add type single to condition
  • Loading branch information
ariellalgilmore authored Oct 9, 2024
1 parent fc45491 commit f10cb92
Showing 1 changed file with 13 additions and 6 deletions.
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

0 comments on commit f10cb92

Please sign in to comment.