Skip to content
Open
Show file tree
Hide file tree
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
27 changes: 24 additions & 3 deletions src/test/year_picker_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,29 @@ describe("YearPicker", () => {
expect(selectedDay ? getYear(selectedDay) : selectedDay).toBe(2021);
});

it("should select a year when Enter is pressed without a selected date", () => {
const onDayClickMock = jest.fn();
const date = newDate("2024-01-01");
const { container } = render(
<Year
date={date}
preSelection={date}
onDayClick={onDayClickMock}
onYearMouseEnter={() => {}}
onYearMouseLeave={() => {}}
/>,
);

const target = safeQuerySelector(
container,
".react-datepicker__year-2024",
);
fireEvent.keyDown(target, getKey(KeyType.Enter));

expect(onDayClickMock).toHaveBeenCalledTimes(1);
expect(getYear(onDayClickMock.mock.calls[0][0])).toBe(2024);
});

it("should call onKeyDown handler on any key press", () => {
const onKeyDownSpy = jest.fn();

Expand Down Expand Up @@ -1039,9 +1062,7 @@ describe("YearPicker", () => {

fireEvent.keyDown(currentYear, getKey(KeyType.Enter));

// When selected is null and Enter is pressed, onDayClick should not be called
// because of the early return at line 297
expect(onDayClickMock).not.toHaveBeenCalled();
expect(onDayClickMock).toHaveBeenCalledTimes(1);
});

it("should handle keyboard navigation with null preSelection (Arrow keys)", () => {
Expand Down
7 changes: 3 additions & 4 deletions src/year.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,10 @@ export default class Year extends Component<YearProps> {
if (!this.props.disabledKeyboardNavigation) {
switch (key) {
case KeyType.Enter:
if (this.props.selected == null) {
break;
}
this.onYearClick(event, y);
this.props.setPreSelection?.(this.props.selected);
if (this.props.selected != null) {
this.props.setPreSelection?.(this.props.selected);
}
break;
case KeyType.ArrowRight:
if (this.props.preSelection == null) {
Expand Down