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: Datepicker closeOnSelect prop #15431

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/react/src/components/DatePicker/DatePicker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,46 @@ describe('Single date picker', () => {
await userEvent.click(screen.getByText('clear'));
expect(screen.getByLabelText('Date Picker label')).toHaveValue('');
});

it('should respect closeOnSelect prop', async () => {
const DatePickerExample = () => {
const [date, setDate] = useState();
return (
<DatePicker
datePickerType="single"
value={date}
closeOnSelect={false}
minDate="11/25/2023"
maxDate="11/28/2023"
onChange={(value) => {
setDate(value);
}}>
<DatePickerInput
placeholder="mm/dd/yyyy"
labelText="Date Picker label"
id="date-picker-simple"
/>
</DatePicker>
);
};
render(<DatePickerExample />);
const input = screen.getByLabelText('Date Picker label');
expect(screen.getByRole('application')).not.toHaveClass('open');

await userEvent.click(input);
expect(screen.getByRole('application')).toHaveClass('open');

// eslint-disable-next-line testing-library/no-node-access
const belowMinDate = document.querySelector(
'[aria-label="November 26, 2023"]'
);
await userEvent.click(belowMinDate);

expect(screen.getByLabelText('Date Picker label')).toHaveValue(
'11/26/2023'
);
expect(screen.getByRole('application')).toHaveClass('open');
});
});

describe('Date picker with locale', () => {
Expand Down
12 changes: 10 additions & 2 deletions packages/react/src/components/DatePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ const DatePicker = React.forwardRef(function DatePicker(
}

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

Expand Down Expand Up @@ -720,7 +720,15 @@ const DatePicker = React.forwardRef(function DatePicker(
end.removeEventListener('change', handleOnChange);
}
};
}, [savedOnChange, savedOnClose, savedOnOpen, readOnly, hasInput]); //eslint-disable-line react-hooks/exhaustive-deps
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
savedOnChange,
savedOnClose,
savedOnOpen,
readOnly,
closeOnSelect,
hasInput,
]);

// this hook allows consumers to access the flatpickr calendar
// instance for cases where functions like open() or close()
Expand Down