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): month and year picker scroller #3789

Open
wants to merge 20 commits into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
4895941
accordion tree
buchananwill Sep 18, 2024
7968f1a
Rename accordionTree.tsx to accordion-tree.tsx
buchananwill Sep 18, 2024
3b77e2e
Update accordion-tree.tsx
buchananwill Sep 18, 2024
5f35a84
fix(use-calendar-picker): fixed the UI month/year sync (#3785)
buchananwill Sep 22, 2024
66108cd
fix(use-calendar-picker): fixed the UI month/year sync (#3785)
buchananwill Sep 22, 2024
d6b00bc
fix(use-calendar-picker): added test for the fixed behaviour (#3785)
buchananwill Sep 23, 2024
e30ae3c
fix(use-calendar-picker): changeset (#3785)
buchananwill Sep 23, 2024
d4cc930
Delete .changeset/serious-snails-count.md
buchananwill Sep 23, 2024
cb2d7fe
Update sixty-weeks-help.md
buchananwill Sep 23, 2024
928237b
fix(use-calendar-picker): swapped the aria label for data-slot="heade…
buchananwill Sep 26, 2024
5bc25d5
fix(use-calendar-picker): added scrollTo(nextValue, list) into the ke…
buchananwill Sep 27, 2024
e6a2f99
fix(use-calendar-picker): prevent default behaviour for scroll keys
buchananwill Sep 27, 2024
0a2c4c9
fix(use-calendar-picker): add a key up event to further fix #3789
buchananwill Oct 2, 2024
5ec489b
fix(use-calendar-picker): changed the home/end values to 1 and 12
buchananwill Oct 2, 2024
151141c
fix(use-calendar-picker): sourced the mix/max values from the state
buchananwill Oct 3, 2024
cc5ca02
fix(use-calendar-picker): useCallback for the getBoundaryValue
buchananwill Oct 3, 2024
243befa
fix(use-calendar-picker): blocking repeat key strokes for home and end
buchananwill Oct 3, 2024
c3f924b
fix(use-calendar-picker): inlined arrow function
buchananwill Oct 3, 2024
b96542a
fix(use-calendar-picker): simplified control structure
buchananwill Oct 3, 2024
214385f
fix(use-calendar-picker): change the execution order to avoid stale l…
buchananwill Oct 3, 2024
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
6 changes: 6 additions & 0 deletions .changeset/serious-snails-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@nextui-org/accordion": minor
"@nextui-org/calendar": patch
---

Fixing the UI and data syncing of the month/year picker.
7 changes: 7 additions & 0 deletions .changeset/sixty-weeks-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@nextui-org/accordion": patch
"@nextui-org/calendar": patch
"@nextui-org/date-picker": patch
---

Added an aria and test label for the picker toggle.
3 changes: 3 additions & 0 deletions packages/components/calendar/src/calendar-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface CalendarHeaderProps extends HTMLNextUIProps<"header"> {
buttonPickerProps?: ButtonProps;
}

export const monthAndYearPickerToggle = "Month and year picker toggle";

export function CalendarHeader(props: CalendarHeaderProps) {
const {direction, date, currentMonth, buttonPickerProps} = props;

Expand Down Expand Up @@ -99,6 +101,7 @@ export function CalendarHeader(props: CalendarHeaderProps) {
return showMonthAndYearPickers ? (
<Button
{...headerProps}
aria-label={monthAndYearPickerToggle}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't hardcode aria-label internally. We can take it from users' side tho.

disableAnimation={disableAnimation}
endContent={<ChevronDownIcon className="chevron-icon" />}
onKeyDown={handleKeyDown}
Expand Down
70 changes: 5 additions & 65 deletions packages/components/calendar/src/use-calendar-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import type {PressEvent} from "@react-types/shared";

import {useDateFormatter} from "@react-aria/i18n";
import {HTMLNextUIProps} from "@nextui-org/system";
import {useCallback, useRef, useEffect} from "react";
import debounce from "lodash.debounce";
import {areRectsIntersecting} from "@nextui-org/react-utils";
import {useCallback, useEffect, useRef} from "react";
import scrollIntoView from "scroll-into-view-if-needed";

import {getMonthsInYear, getYearRange} from "./utils";
Expand All @@ -15,6 +13,7 @@ export type PickerValue = {
value: string;
label: string;
};

export interface CalendarPickerProps extends HTMLNextUIProps<"div"> {
date: CalendarDate;
currentMonth: CalendarDate;
Expand All @@ -23,8 +22,6 @@ export interface CalendarPickerProps extends HTMLNextUIProps<"div"> {
type ItemsRefMap = Map<number, HTMLElement>;
type CalendarPickerListType = "months" | "years";

const SCROLL_DEBOUNCE_TIME = 200;

export function useCalendarPicker(props: CalendarPickerProps) {
const {date, currentMonth} = props;

Expand Down Expand Up @@ -83,36 +80,6 @@ export function useCalendarPicker(props: CalendarPickerProps) {
}
}

const handleListScroll = useCallback(
(e: Event, highlightEl: HTMLElement | null, list: CalendarPickerListType) => {
if (!(e.target instanceof HTMLElement)) return;

const map = getItemsRefMap(list === "months" ? monthsItemsRef : yearsItemsRef);

const items = Array.from(map.values());

const item = items.find((itemEl) => {
const rect1 = itemEl.getBoundingClientRect();
const rect2 = highlightEl?.getBoundingClientRect();

if (!rect2) {
return false;
}

return areRectsIntersecting(rect1, rect2);
});

const itemValue = Number(item?.getAttribute("data-value"));

if (!itemValue) return;

let date = state.focusedDate.set(list === "months" ? {month: itemValue} : {year: itemValue});

state.setFocusedDate(date);
},
[state, isHeaderExpanded],
);

// scroll to the selected month/year when the component is mounted/opened/closed
useEffect(() => {
if (!isHeaderExpanded) return;
Expand All @@ -121,36 +88,6 @@ export function useCalendarPicker(props: CalendarPickerProps) {
scrollTo(date.year, "years", false);
}, [isHeaderExpanded]);

useEffect(() => {
// add scroll event listener to monthsListRef
const monthsList = monthsListRef.current;
const yearsList = yearsListRef.current;
const highlightEl = highlightRef.current;

if (!highlightEl) return;

const debouncedHandleMonthsScroll = debounce(
(e: Event) => handleListScroll(e, highlightEl, "months"),
SCROLL_DEBOUNCE_TIME,
);
const debouncedHandleYearsScroll = debounce(
(e: Event) => handleListScroll(e, highlightEl, "years"),
SCROLL_DEBOUNCE_TIME,
);

monthsList?.addEventListener("scroll", debouncedHandleMonthsScroll);
yearsList?.addEventListener("scroll", debouncedHandleYearsScroll);

return () => {
if (debouncedHandleMonthsScroll) {
monthsList?.removeEventListener("scroll", debouncedHandleMonthsScroll);
}
if (debouncedHandleYearsScroll) {
yearsList?.removeEventListener("scroll", debouncedHandleYearsScroll);
}
};
}, [handleListScroll]);

function scrollTo(value: number, list: CalendarPickerListType, smooth = true) {
const mapListRef = list === "months" ? monthsItemsRef : yearsItemsRef;
const listRef = list === "months" ? monthsListRef : yearsListRef;
Expand All @@ -160,6 +97,9 @@ export function useCalendarPicker(props: CalendarPickerProps) {
const node = map.get(value);

if (!node) return;
let date = state.focusedDate.set(list === "months" ? {month: value} : {year: value});

state.setFocusedDate(date);

// scroll picker list to the selected item
scrollIntoView(node, {
Expand Down
44 changes: 44 additions & 0 deletions packages/components/date-picker/__tests__/date-picker.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {pointerMap, triggerPress} from "@nextui-org/test-utils";
import userEvent from "@testing-library/user-event";
import {CalendarDate, CalendarDateTime} from "@internationalized/date";
import {NextUIProvider} from "@nextui-org/system";
import {monthAndYearPickerToggle} from "@nextui-org/calendar/src/calendar-header";

import {DatePicker as DatePickerBase, DatePickerProps} from "../src";

Expand Down Expand Up @@ -524,6 +525,7 @@ describe("DatePicker", () => {

describe("Month and Year Picker", () => {
const onHeaderExpandedChangeSpy = jest.fn();
const valueChangeSpy = jest.fn();

afterEach(() => {
onHeaderExpandedChangeSpy.mockClear();
Expand Down Expand Up @@ -600,6 +602,48 @@ describe("DatePicker", () => {
expect(onHeaderExpandedChangeSpy).not.toHaveBeenCalled();
});

it("should focus the month and year when pressed in the picker", () => {
ryo-manba marked this conversation as resolved.
Show resolved Hide resolved
const {getByRole, getByLabelText} = render(
<DatePicker
showMonthAndYearPickers
calendarProps={{
isHeaderExpanded: true,
onChange: valueChangeSpy,
}}
defaultValue={new CalendarDate(2024, 4, 26)}
label="Date"
/>,
);
const dialogButton = getByRole("button");

triggerPress(dialogButton);

const dialog = getByRole("dialog");
const month = getByRole("button", {name: "April"});
const year = getByRole("button", {name: "2024"});

expect(dialog).toBeVisible();
expect(month).toHaveAttribute("data-value", "4");
expect(year).toHaveAttribute("data-value", "2024");

const button = getByLabelText(monthAndYearPickerToggle);

triggerPress(button);

const monthBefore = getByRole("button", {name: "March"});
const yearBefore = getByRole("button", {name: "2023"});

expect(monthBefore).toHaveAttribute("data-value", "3");
expect(yearBefore).toHaveAttribute("data-value", "2023");
expect(monthBefore).toHaveAttribute("tabindex", "-1");
expect(yearBefore).toHaveAttribute("tabindex", "-1");
triggerPress(monthBefore);
triggerPress(yearBefore);
expect(valueChangeSpy).not.toHaveBeenCalled();
expect(monthBefore).toHaveAttribute("tabindex", "0");
expect(yearBefore).toHaveAttribute("tabindex", "0");
Comment on lines +641 to +643
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also check the following two points:

  1. Ensure that the value in the header is synchronized.
  2. Verify that pressing the Enter key triggers the valueChangeSpy.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests.

});
wingkwong marked this conversation as resolved.
Show resolved Hide resolved

it("CalendarBottomContent should render correctly", () => {
const {getByRole, getByTestId} = render(
<DatePicker
Expand Down