Skip to content

Commit

Permalink
fix(datePicker): prevent year, month change in showMonthAndYearPicker…
Browse files Browse the repository at this point in the history
…s mode (#3088)

* fix: prevent year, month change error in showMonthAndYearPickers mode

* docs: add changeset

* docs: edit changeset

* test:  add test code about date clear issue in date picker

* test: update test code

* test: check application aria is same with current date

* chore(changeset): revise package and add issue number

---------

Co-authored-by: WK Wong <[email protected]>
  • Loading branch information
KumJungMin and wingkwong committed Jul 8, 2024
1 parent 89ccf17 commit 134f371
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/tidy-squids-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@nextui-org/calendar": patch
"@nextui-org/date-picker": patch
---

fix hours, month clear issue in `showMonthAndYearPickers` mode (#3072).
2 changes: 2 additions & 0 deletions packages/components/calendar/src/use-calendar-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ export function useCalendarPicker(props: CalendarPickerProps) {

// scroll to the selected month/year when the component is mounted/opened/closed
useEffect(() => {
if (!isHeaderExpanded) return;

scrollTo(date.month, "months", false);
scrollTo(date.year, "years", false);
}, [isHeaderExpanded]);
Expand Down
22 changes: 22 additions & 0 deletions packages/components/date-picker/__tests__/date-picker.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,28 @@ describe("DatePicker", () => {
expect(onChange).toHaveBeenCalledWith(new CalendarDate(2019, 2, 4));
expect(getTextValue(combobox)).toBe("2/4/2019"); // uncontrolled
});

it("should keep the selected date when the picker is opened, in showMonthAndYearPickers mode", function () {
const {getByRole, getAllByRole} = render(
<DatePicker showMonthAndYearPickers label="Date" value={new CalendarDate(2024, 5, 1)} />,
);

let combobox = getAllByRole("group")[0];

expect(getTextValue(combobox)).toBe("5/1/2024");

let button = getByRole("button");

triggerPress(button);

let dialog = getByRole("dialog");

expect(dialog).toBeVisible();

const content = getByRole("application");

expect(content).toHaveAttribute("aria-label", "May 2024");
});
});

describe("Month and Year Picker", () => {
Expand Down

0 comments on commit 134f371

Please sign in to comment.