From 114d8ca16a1a9250df83bce2744d5b4ef336826b Mon Sep 17 00:00:00 2001 From: Atakan Bayrak Date: Fri, 6 Nov 2026 15:09:03 +0300 Subject: [PATCH 1/3] month picker introduced --- .../DatePicker/DatePicker.stories.tsx | 1 + .../components/DatePicker/DatePicker.test.tsx | 12 ++++++++++++ src/lib/components/DatePicker/DatePicker.tsx | 2 ++ .../DateSelector/MonthYear/MonthSelector.tsx | 15 +++++++++++++-- .../DatePicker/context/DatePickerProvider.tsx | 7 +++++-- src/lib/components/DatePicker/types.ts | 4 ++++ .../components/InputDate/InputDate.stories.tsx | 1 + src/lib/components/InputDate/InputDate.tsx | 17 +++++++++++++++-- src/lib/components/InputDate/helper.ts | 2 +- src/lib/components/InputDate/types.ts | 6 ++++-- 10 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/lib/components/DatePicker/DatePicker.stories.tsx b/src/lib/components/DatePicker/DatePicker.stories.tsx index 72d6195e..c2075369 100644 --- a/src/lib/components/DatePicker/DatePicker.stories.tsx +++ b/src/lib/components/DatePicker/DatePicker.stories.tsx @@ -11,6 +11,7 @@ const meta: Meta = { variant: { table: { defaultValue: { summary: "borderless" } } }, size: { table: { defaultValue: { summary: "md" } } }, firstDayOfWeek: { table: { defaultValue: { summary: "1" } } }, + pickerFormat: { control: { type: "radio" }, options: ["day", "month"], table: { defaultValue: { summary: "day" } } }, }, args: { variant: "shadow", diff --git a/src/lib/components/DatePicker/DatePicker.test.tsx b/src/lib/components/DatePicker/DatePicker.test.tsx index 9241c36f..19bb8f82 100644 --- a/src/lib/components/DatePicker/DatePicker.test.tsx +++ b/src/lib/components/DatePicker/DatePicker.test.tsx @@ -432,4 +432,16 @@ describe("DatePicker", () => { expect(getByText(getDateLocale(t).months[dateToSelect.getMonth()])).toBeInTheDocument(); expect(getByText(dateToSelect.getFullYear())).toBeInTheDocument(); }); + + it("should switch the view when the pickerFormat prop is changed", () => { + const { rerender, getByTestId, queryByTestId } = render(); + expect(getByTestId("DatePickerDayContainer")).toBeInTheDocument(); + + rerender(); + expect(getByTestId("DatePickerMonthsContainer")).toBeInTheDocument(); + expect(queryByTestId("DatePickerDayContainer")).not.toBeInTheDocument(); + + rerender(); + expect(getByTestId("DatePickerDayContainer")).toBeInTheDocument(); + }); }); diff --git a/src/lib/components/DatePicker/DatePicker.tsx b/src/lib/components/DatePicker/DatePicker.tsx index 4027b808..8c0eb5f7 100644 --- a/src/lib/components/DatePicker/DatePicker.tsx +++ b/src/lib/components/DatePicker/DatePicker.tsx @@ -19,6 +19,7 @@ export const DatePicker = (props: PropsWithRef) onClearClick, removeActionButtons, firstDayOfWeek = 1, + pickerFormat = "day", className, style, ref, @@ -31,6 +32,7 @@ export const DatePicker = (props: PropsWithRef) value={value} locale={locale} firstDayOfWeek={firstDayOfWeek} + pickerFormat={pickerFormat} fluid={!!fluid} onDateChange={onDateChange} onPickerChange={onPickerChange} diff --git a/src/lib/components/DatePicker/components/DateSelector/MonthYear/MonthSelector.tsx b/src/lib/components/DatePicker/components/DateSelector/MonthYear/MonthSelector.tsx index 89a67e4a..db8d9367 100644 --- a/src/lib/components/DatePicker/components/DateSelector/MonthYear/MonthSelector.tsx +++ b/src/lib/components/DatePicker/components/DateSelector/MonthYear/MonthSelector.tsx @@ -7,7 +7,8 @@ import Header from "@/components/DatePicker/components/Header/Header"; import MonthYearButton from "./MonthYearButton"; const MonthSelector = () => { - const { pickerDate, setPickerDate, setPicker, locale, onPickerChange, size } = useContext(DatePickerContext); + const { pickerDate, setPickerDate, setPicker, locale, onPickerChange, size, isMonthOnly, selectedDate, setSelectedDate, onDateChange } = + useContext(DatePickerContext); const [activeDate] = useState(pickerDate); const prevClickHandler = useCallback(() => { @@ -19,6 +20,12 @@ const MonthSelector = () => { }, [pickerDate, setPickerDate]); const handleMonthClick = (month: number) => { + if (isMonthOnly) { + const date = new Date(pickerDate.getFullYear(), month, 1); + selectedDate?.getTime() !== date.getTime() && onDateChange?.(date); + setSelectedDate(date); + return; + } const newPickerDate = new Date(pickerDate.getTime()); newPickerDate.setMonth(month); setPickerDate(newPickerDate); @@ -36,7 +43,11 @@ const MonthSelector = () => { key={mIndex} label={locale.monthsShort[mIndex]} onClick={() => handleMonthClick(mIndex)} - selected={pickerDate.getMonth() === mIndex && pickerDate.getFullYear() === activeDate.getFullYear()} + selected={ + isMonthOnly + ? selectedDate?.getMonth() === mIndex && selectedDate.getFullYear() === pickerDate.getFullYear() + : pickerDate.getMonth() === mIndex && pickerDate.getFullYear() === activeDate.getFullYear() + } /> ))} diff --git a/src/lib/components/DatePicker/context/DatePickerProvider.tsx b/src/lib/components/DatePicker/context/DatePickerProvider.tsx index f8d311da..9a1aa5c5 100644 --- a/src/lib/components/DatePicker/context/DatePickerProvider.tsx +++ b/src/lib/components/DatePicker/context/DatePickerProvider.tsx @@ -7,15 +7,16 @@ import { DateUtils } from "../../../../utils/dateUtils"; export const DatePickerContext = createContext(datePickerContextDefaultValues); export const DatePickerProvider = (props: PropsWithChildren) => { - const { children, value, size, locale, fluid, onDateChange, onPickerChange, onClearClick, firstDayOfWeek } = props; + const { children, value, size, locale, fluid, onDateChange, onPickerChange, onClearClick, firstDayOfWeek, pickerFormat = "day" } = props; - const [picker, setPicker] = useState("day"); + const [picker, setPicker] = useState(pickerFormat); const [previousTab, setPreviousTab] = useState<"day" | "month">("day"); const [selectedDate, setSelectedDate] = useState(DateUtils.getDateTimeless(value)); const [pickerDate, setPickerDate] = useState(DateUtils.getDateTimeless(value) ?? DateUtils.getTodayTimeless()); const today = useMemo(() => DateUtils.getTodayTimeless(), []); useEffect(() => setSelectedDate(value), [value]); + useEffect(() => setPicker(pickerFormat), [pickerFormat]); const onYearMonthTabSelected = useCallback( (nextTab: "year" | "month", previousTab: "month" | "day") => { @@ -38,6 +39,7 @@ export const DatePickerProvider = (props: PropsWithChildren ({ size, + isMonthOnly: pickerFormat === "month", pickerDate, selectedDate, previousTab, @@ -68,6 +70,7 @@ export const DatePickerProvider = (props: PropsWithChildren void; onDateChange?: (date?: Date) => void; onPickerChange?: (picker: DatePickerPickerType) => void; + pickerFormat?: Exclude; } & DatePickerDefaultableProps; export type DatePickerDefaultableProps = { @@ -40,12 +41,14 @@ export type DatePickerProviderProps = { onPickerChange?: (picker: DatePickerPickerType) => void; onClearClick?: () => void; firstDayOfWeek: DaysOfWeek; + pickerFormat?: Exclude; }; export type DatePickerPickerType = "day" | "month" | "year"; export type DatePickerContextProps = { size: Size4SM; + isMonthOnly: boolean; firstDayOfWeek: DaysOfWeek; locale: DatePickerLocale; pickerDate: Date; // The date that is used to hold the values when choosing temporary values like months or years. @@ -71,6 +74,7 @@ export type DatePickerLocale = { export const datePickerContextDefaultValues: DatePickerContextProps = { size: "md", + isMonthOnly: false, pickerDate: DateUtils.getTodayTimeless(), picker: "day", previousTab: "day", diff --git a/src/lib/components/InputDate/InputDate.stories.tsx b/src/lib/components/InputDate/InputDate.stories.tsx index 4dab16d7..c5b6648d 100644 --- a/src/lib/components/InputDate/InputDate.stories.tsx +++ b/src/lib/components/InputDate/InputDate.stories.tsx @@ -18,6 +18,7 @@ const meta: Meta = { size: { table: { defaultValue: { summary: "md" } } }, placeholder: { table: { defaultValue: { summary: "Reflects format prop" } } }, firstDayOfWeek: { table: { defaultValue: { summary: "1" } } }, + pickerFormat: { control: { type: "radio" }, options: ["day", "month"], table: { defaultValue: { summary: "day" } } }, }, args: {}, }; diff --git a/src/lib/components/InputDate/InputDate.tsx b/src/lib/components/InputDate/InputDate.tsx index b96c2553..f71c7f24 100644 --- a/src/lib/components/InputDate/InputDate.tsx +++ b/src/lib/components/InputDate/InputDate.tsx @@ -26,8 +26,20 @@ const pickerSizeMap = { const InputDate = (p: PropsWithRef) => { const props = usePropsWithThemeDefaults("InputDate", p); - const { editable, pill, value, onChange, ref, style, className, locale: propsLocale, firstDayOfWeek, icon } = props; - const format = useMemo(() => ({ ...defaultDateFormat, ...props.format }), [props.format]); + const { editable, pill, value, onChange, ref, style, className, locale: propsLocale, firstDayOfWeek, icon, pickerFormat } = props; + const format = useMemo(() => { + const mergedFormat = { ...defaultDateFormat, ...props.format }; + if (pickerFormat !== "month") return mergedFormat; + + const dayIndex = mergedFormat.order.indexOf("day"); + if (dayIndex === -1) return mergedFormat; + + return { + ...mergedFormat, + order: mergedFormat.order.filter((_, index) => index !== dayIndex), + prefix: mergedFormat.prefix?.filter((_, index) => index !== dayIndex), + }; + }, [pickerFormat, props.format]); const locale = useDateLocale(propsLocale); const placeholder = useMemo( @@ -144,6 +156,7 @@ const InputDate = (p: PropsWithRef) => { onDateChange={dateChangeHandler} className={styles.datePicker} locale={locale} + pickerFormat={pickerFormat} /> )} diff --git a/src/lib/components/InputDate/helper.ts b/src/lib/components/InputDate/helper.ts index e3280110..49136984 100644 --- a/src/lib/components/InputDate/helper.ts +++ b/src/lib/components/InputDate/helper.ts @@ -128,7 +128,7 @@ export const parseDate = (dateString: string, format: DateFormat, locale: DatePi const year = parseInt(dateValues.year ?? "", 10); const fullYear = yearFormat === "YY" ? 2000 + year : year; const month = parseMonth(dateValues.month ?? ""); - const day = parseInt(dateValues.day ?? "", 10); + const day = order.includes("day") ? parseInt(dateValues.day ?? "", 10) : 1; // Create and validate date const date = new Date(fullYear, month - 1, day); diff --git a/src/lib/components/InputDate/types.ts b/src/lib/components/InputDate/types.ts index b6955ea9..51f23496 100644 --- a/src/lib/components/InputDate/types.ts +++ b/src/lib/components/InputDate/types.ts @@ -1,6 +1,6 @@ import { InputCommonProps, InputSize } from "../Form/types"; import { DateFormat, DaysOfWeek } from "../Motif/Pickers/types"; -import { DatePickerLocale } from "../DatePicker/types"; +import { DatePickerLocale, DatePickerPickerType } from "../DatePicker/types"; import type { IconGlobalType } from "../../types"; export type InputDateDefaultableProps = { @@ -14,4 +14,6 @@ export type InputDateDefaultableProps = { icon?: IconGlobalType | null; }; -export type InputDateProps = InputCommonProps & InputDateDefaultableProps; +export type InputDateProps = InputCommonProps & { + pickerFormat?: Exclude; +} & InputDateDefaultableProps; From 76d199e3e155627cff2e79ba14198a0f4bb5735f Mon Sep 17 00:00:00 2001 From: Atakan Bayrak Date: Fri, 20 Nov 2026 11:47:59 +0300 Subject: [PATCH 2/3] review fix --- .../components/DatePicker/DatePicker.test.tsx | 83 ++++++++++++++++++- .../DateSelector/MonthYear/MonthSelector.tsx | 3 +- .../DatePicker/context/DatePickerProvider.tsx | 9 +- src/lib/components/DatePicker/types.ts | 10 ++- .../InputDate/test/InputDate.test.tsx | 31 +++++++ src/lib/components/InputDate/types.ts | 4 +- 6 files changed, 130 insertions(+), 10 deletions(-) diff --git a/src/lib/components/DatePicker/DatePicker.test.tsx b/src/lib/components/DatePicker/DatePicker.test.tsx index 19bb8f82..c809222e 100644 --- a/src/lib/components/DatePicker/DatePicker.test.tsx +++ b/src/lib/components/DatePicker/DatePicker.test.tsx @@ -273,7 +273,10 @@ describe("DatePicker", () => { runSnapshotDefaultsAndStandardPropsTest( (props: StandardPropsWithRef) => render(), { - assertDefaults: ({ getByText, getByTestId }) => { + assertDefaults: ({ getByText, getByTestId, queryByTestId }) => { + // pickerFormat: day + expect(getByTestId("DatePickerDayContainer")).toBeInTheDocument(); + expect(queryByTestId("DatePickerMonthsContainer")).not.toBeInTheDocument(); // variant: borderless expect(getByTestId("Picker")).toHaveClass("borderless"); // locale: English @@ -433,6 +436,84 @@ describe("DatePicker", () => { expect(getByText(dateToSelect.getFullYear())).toBeInTheDocument(); }); + it("should show the month and year of the new value as selected when the value prop changes", () => { + const { rerender, getByText, queryByText } = render(); + + const newValue = new Date(2021, 7, 20); + rerender(); + + expect(getByText(getDateLocale(t).months[newValue.getMonth()])).toBeInTheDocument(); + expect(getByText(newValue.getFullYear())).toBeInTheDocument(); + expect(queryByText(2018)).not.toBeInTheDocument(); + expect(getByText(newValue.getDate())).toHaveClass("selected"); + }); + + it("should show the new year as selected in the Month Picker when the value prop changes", () => { + const { rerender, getByText } = render(); + + const newValue = new Date(2021, 7, 1); + rerender(); + + expect(getByText(newValue.getFullYear())).toBeInTheDocument(); + expect(getByText(getDateLocale(t).monthsShort[newValue.getMonth()])).toHaveClass("selected"); + }); + + describe("pickerFormat month", () => { + const { monthsShort } = getDateLocale(t); + + it("should select the first day of the clicked month and fire onDateChange", () => { + const onDateChange = jest.fn(); + const { getByText } = render(); + + act(() => getByText(monthsShort[7]).click()); + + expect(onDateChange).toHaveBeenCalledTimes(1); + expect(onDateChange).toHaveBeenCalledWith(new Date(2018, 7, 1)); + expect(getByText(monthsShort[7])).toHaveClass("selected"); + expect(getByText(monthsShort[3])).not.toHaveClass("selected"); + }); + + it("should stay in the month view after a month is selected", () => { + const { getByText, getByTestId, queryByTestId } = render(); + + act(() => getByText(monthsShort[5]).click()); + + expect(getByTestId("DatePickerMonthsContainer")).toBeInTheDocument(); + expect(queryByTestId("DatePickerDayContainer")).not.toBeInTheDocument(); + }); + + it("should highlight the selected month only in the year of the selected date", () => { + const { getByText } = render(); + expect(getByText(monthsShort[3])).toHaveClass("selected"); + + act(() => getByText("arrow_forward").click()); + expect(getByText(2019)).toBeInTheDocument(); + expect(getByText(monthsShort[3])).not.toHaveClass("selected"); + + act(() => getByText("arrow_back").click()); + expect(getByText(monthsShort[3])).toHaveClass("selected"); + }); + + it("should not fire onDateChange when the already selected month is clicked again", () => { + const onDateChange = jest.fn(); + const { getByText } = render(); + + act(() => getByText(monthsShort[3]).click()); + expect(onDateChange).not.toHaveBeenCalled(); + expect(getByText(monthsShort[3])).toHaveClass("selected"); + }); + + it("should fire onDateChange when the same month is clicked in a different year", () => { + const onDateChange = jest.fn(); + const { getByText } = render(); + + act(() => getByText("arrow_forward").click()); + act(() => getByText(monthsShort[3]).click()); + + expect(onDateChange).toHaveBeenCalledWith(new Date(2019, 3, 1)); + }); + }); + it("should switch the view when the pickerFormat prop is changed", () => { const { rerender, getByTestId, queryByTestId } = render(); expect(getByTestId("DatePickerDayContainer")).toBeInTheDocument(); diff --git a/src/lib/components/DatePicker/components/DateSelector/MonthYear/MonthSelector.tsx b/src/lib/components/DatePicker/components/DateSelector/MonthYear/MonthSelector.tsx index db8d9367..4407decd 100644 --- a/src/lib/components/DatePicker/components/DateSelector/MonthYear/MonthSelector.tsx +++ b/src/lib/components/DatePicker/components/DateSelector/MonthYear/MonthSelector.tsx @@ -7,9 +7,10 @@ import Header from "@/components/DatePicker/components/Header/Header"; import MonthYearButton from "./MonthYearButton"; const MonthSelector = () => { - const { pickerDate, setPickerDate, setPicker, locale, onPickerChange, size, isMonthOnly, selectedDate, setSelectedDate, onDateChange } = + const { pickerDate, setPickerDate, setPicker, locale, onPickerChange, size, pickerFormat, selectedDate, setSelectedDate, onDateChange } = useContext(DatePickerContext); const [activeDate] = useState(pickerDate); + const isMonthOnly = pickerFormat === "month"; const prevClickHandler = useCallback(() => { setPickerDate(new Date(pickerDate.getFullYear() - 1, pickerDate.getMonth())); diff --git a/src/lib/components/DatePicker/context/DatePickerProvider.tsx b/src/lib/components/DatePicker/context/DatePickerProvider.tsx index 9a1aa5c5..7232e409 100644 --- a/src/lib/components/DatePicker/context/DatePickerProvider.tsx +++ b/src/lib/components/DatePicker/context/DatePickerProvider.tsx @@ -15,7 +15,12 @@ export const DatePickerProvider = (props: PropsWithChildren(DateUtils.getDateTimeless(value) ?? DateUtils.getTodayTimeless()); const today = useMemo(() => DateUtils.getTodayTimeless(), []); - useEffect(() => setSelectedDate(value), [value]); + const valueTime = value?.getTime(); + useEffect(() => { + const date = valueTime !== undefined ? DateUtils.getDateTimeless(new Date(valueTime)) : undefined; + setSelectedDate(date); + if (date) setPickerDate(date); + }, [valueTime]); useEffect(() => setPicker(pickerFormat), [pickerFormat]); const onYearMonthTabSelected = useCallback( @@ -39,7 +44,7 @@ export const DatePickerProvider = (props: PropsWithChildren ({ size, - isMonthOnly: pickerFormat === "month", + pickerFormat, pickerDate, selectedDate, previousTab, diff --git a/src/lib/components/DatePicker/types.ts b/src/lib/components/DatePicker/types.ts index 973dc035..6567014e 100644 --- a/src/lib/components/DatePicker/types.ts +++ b/src/lib/components/DatePicker/types.ts @@ -8,7 +8,7 @@ export type DatePickerProps = { onClearClick?: () => void; onDateChange?: (date?: Date) => void; onPickerChange?: (picker: DatePickerPickerType) => void; - pickerFormat?: Exclude; + pickerFormat?: DatePickerFormat; } & DatePickerDefaultableProps; export type DatePickerDefaultableProps = { @@ -41,14 +41,16 @@ export type DatePickerProviderProps = { onPickerChange?: (picker: DatePickerPickerType) => void; onClearClick?: () => void; firstDayOfWeek: DaysOfWeek; - pickerFormat?: Exclude; + pickerFormat?: DatePickerFormat; }; export type DatePickerPickerType = "day" | "month" | "year"; +export type DatePickerFormat = "day" | "month"; + export type DatePickerContextProps = { size: Size4SM; - isMonthOnly: boolean; + pickerFormat: DatePickerFormat; firstDayOfWeek: DaysOfWeek; locale: DatePickerLocale; pickerDate: Date; // The date that is used to hold the values when choosing temporary values like months or years. @@ -74,7 +76,7 @@ export type DatePickerLocale = { export const datePickerContextDefaultValues: DatePickerContextProps = { size: "md", - isMonthOnly: false, + pickerFormat: "day", pickerDate: DateUtils.getTodayTimeless(), picker: "day", previousTab: "day", diff --git a/src/lib/components/InputDate/test/InputDate.test.tsx b/src/lib/components/InputDate/test/InputDate.test.tsx index 3942e4b4..e81f67bd 100644 --- a/src/lib/components/InputDate/test/InputDate.test.tsx +++ b/src/lib/components/InputDate/test/InputDate.test.tsx @@ -242,6 +242,37 @@ describe("InputDate", () => { expect(onChange).toHaveBeenCalledTimes(5); }); + describe("pickerFormat month", () => { + it("should display MM/YYYY as the placeholder", () => { + const { getInput } = renderExt(); + expect(getInput()).toHaveAttribute("placeholder", "MM/YYYY"); + }); + + it("should open the picker in the month view and display MM/YYYY in the input when a month is selected", async () => { + const onChange = jest.fn(); + const { getInput } = renderExt(); + + await userEvent.click(getInput()); + expect(screen.getByTestId("DatePickerMonthsContainer")).toBeInTheDocument(); + expect(screen.queryByTestId("DatePickerDayContainer")).not.toBeInTheDocument(); + + await userEvent.click(screen.getByText(getDateLocale(t).monthsShort[4])); + expect(getInput()).toHaveValue("05/2025"); + expect(onChange).toHaveBeenLastCalledWith(new Date(2025, 4, 1)); + }); + + it("should call onChange with the first day of the month when MM/YYYY is typed, and treat DD/MM/YYYY as invalid", () => { + const onChange = jest.fn(); + const { getInput } = renderExt(); + + fireEvent.change(getInput(), { target: { value: "03/2026" } }); + expect(onChange).toHaveBeenNthCalledWith(1, new Date(2026, 2, 1)); + + fireEvent.change(getInput(), { target: { value: "05/03/2026" } }); + expect(onChange).toHaveBeenNthCalledWith(2, undefined); + }); + }); + it("should be rendered as success variant when success prop is true", () => { const { getByTestId } = render(); expect(getByTestId("inputItem")).toHaveClass("success"); diff --git a/src/lib/components/InputDate/types.ts b/src/lib/components/InputDate/types.ts index 51f23496..03814a22 100644 --- a/src/lib/components/InputDate/types.ts +++ b/src/lib/components/InputDate/types.ts @@ -1,6 +1,6 @@ import { InputCommonProps, InputSize } from "../Form/types"; import { DateFormat, DaysOfWeek } from "../Motif/Pickers/types"; -import { DatePickerLocale, DatePickerPickerType } from "../DatePicker/types"; +import { DatePickerFormat, DatePickerLocale } from "../DatePicker/types"; import type { IconGlobalType } from "../../types"; export type InputDateDefaultableProps = { @@ -15,5 +15,5 @@ export type InputDateDefaultableProps = { }; export type InputDateProps = InputCommonProps & { - pickerFormat?: Exclude; + pickerFormat?: DatePickerFormat; } & InputDateDefaultableProps; From 50742911a770cd21771154fcab921a805a0f071d Mon Sep 17 00:00:00 2001 From: Atakan Bayrak Date: Tue, 24 Nov 2026 15:36:07 +0300 Subject: [PATCH 3/3] month-yearpicker --- .../DatePicker/DatePicker.stories.tsx | 1 - .../components/DatePicker/DatePicker.test.tsx | 95 +----------- src/lib/components/DatePicker/DatePicker.tsx | 2 - .../DateSelector/MonthYear/MonthGrid.tsx | 22 +++ .../DateSelector/MonthYear/MonthSelector.tsx | 31 +--- .../DateSelector/MonthYear/YearGrid.tsx | 22 +++ .../DateSelector/MonthYear/YearSelector.tsx | 35 +---- .../DateSelector/MonthYear/useYearPage.ts | 26 ++++ .../DatePicker/context/DatePickerProvider.tsx | 14 +- src/lib/components/DatePicker/types.ts | 6 - .../InputDate/InputDate.stories.tsx | 1 - src/lib/components/InputDate/InputDate.tsx | 17 +-- src/lib/components/InputDate/helper.ts | 2 +- .../InputDate/test/InputDate.test.tsx | 31 ---- src/lib/components/InputDate/types.ts | 6 +- .../MonthPicker/MonthPicker.stories.tsx | 30 ++++ .../MonthPicker/MonthPicker.test.tsx | 67 +++++++++ .../components/MonthPicker/MonthPicker.tsx | 46 ++++++ .../__snapshots__/MonthPicker.test.tsx.snap | 80 ++++++++++ src/lib/components/MonthPicker/index.ts | 1 + src/lib/components/MonthPicker/types.ts | 21 +++ .../YearPicker/YearPicker.stories.tsx | 30 ++++ .../components/YearPicker/YearPicker.test.tsx | 81 ++++++++++ src/lib/components/YearPicker/YearPicker.tsx | 49 ++++++ .../__snapshots__/YearPicker.test.tsx.snap | 142 ++++++++++++++++++ src/lib/components/YearPicker/index.ts | 1 + src/lib/components/YearPicker/types.ts | 10 ++ src/lib/index.ts | 2 + .../motif/types/componentDefaultableProps.ts | 4 + 29 files changed, 656 insertions(+), 219 deletions(-) create mode 100644 src/lib/components/DatePicker/components/DateSelector/MonthYear/MonthGrid.tsx create mode 100644 src/lib/components/DatePicker/components/DateSelector/MonthYear/YearGrid.tsx create mode 100644 src/lib/components/DatePicker/components/DateSelector/MonthYear/useYearPage.ts create mode 100644 src/lib/components/MonthPicker/MonthPicker.stories.tsx create mode 100644 src/lib/components/MonthPicker/MonthPicker.test.tsx create mode 100644 src/lib/components/MonthPicker/MonthPicker.tsx create mode 100644 src/lib/components/MonthPicker/__snapshots__/MonthPicker.test.tsx.snap create mode 100644 src/lib/components/MonthPicker/index.ts create mode 100644 src/lib/components/MonthPicker/types.ts create mode 100644 src/lib/components/YearPicker/YearPicker.stories.tsx create mode 100644 src/lib/components/YearPicker/YearPicker.test.tsx create mode 100644 src/lib/components/YearPicker/YearPicker.tsx create mode 100644 src/lib/components/YearPicker/__snapshots__/YearPicker.test.tsx.snap create mode 100644 src/lib/components/YearPicker/index.ts create mode 100644 src/lib/components/YearPicker/types.ts diff --git a/src/lib/components/DatePicker/DatePicker.stories.tsx b/src/lib/components/DatePicker/DatePicker.stories.tsx index c2075369..72d6195e 100644 --- a/src/lib/components/DatePicker/DatePicker.stories.tsx +++ b/src/lib/components/DatePicker/DatePicker.stories.tsx @@ -11,7 +11,6 @@ const meta: Meta = { variant: { table: { defaultValue: { summary: "borderless" } } }, size: { table: { defaultValue: { summary: "md" } } }, firstDayOfWeek: { table: { defaultValue: { summary: "1" } } }, - pickerFormat: { control: { type: "radio" }, options: ["day", "month"], table: { defaultValue: { summary: "day" } } }, }, args: { variant: "shadow", diff --git a/src/lib/components/DatePicker/DatePicker.test.tsx b/src/lib/components/DatePicker/DatePicker.test.tsx index c809222e..9241c36f 100644 --- a/src/lib/components/DatePicker/DatePicker.test.tsx +++ b/src/lib/components/DatePicker/DatePicker.test.tsx @@ -273,10 +273,7 @@ describe("DatePicker", () => { runSnapshotDefaultsAndStandardPropsTest( (props: StandardPropsWithRef) => render(), { - assertDefaults: ({ getByText, getByTestId, queryByTestId }) => { - // pickerFormat: day - expect(getByTestId("DatePickerDayContainer")).toBeInTheDocument(); - expect(queryByTestId("DatePickerMonthsContainer")).not.toBeInTheDocument(); + assertDefaults: ({ getByText, getByTestId }) => { // variant: borderless expect(getByTestId("Picker")).toHaveClass("borderless"); // locale: English @@ -435,94 +432,4 @@ describe("DatePicker", () => { expect(getByText(getDateLocale(t).months[dateToSelect.getMonth()])).toBeInTheDocument(); expect(getByText(dateToSelect.getFullYear())).toBeInTheDocument(); }); - - it("should show the month and year of the new value as selected when the value prop changes", () => { - const { rerender, getByText, queryByText } = render(); - - const newValue = new Date(2021, 7, 20); - rerender(); - - expect(getByText(getDateLocale(t).months[newValue.getMonth()])).toBeInTheDocument(); - expect(getByText(newValue.getFullYear())).toBeInTheDocument(); - expect(queryByText(2018)).not.toBeInTheDocument(); - expect(getByText(newValue.getDate())).toHaveClass("selected"); - }); - - it("should show the new year as selected in the Month Picker when the value prop changes", () => { - const { rerender, getByText } = render(); - - const newValue = new Date(2021, 7, 1); - rerender(); - - expect(getByText(newValue.getFullYear())).toBeInTheDocument(); - expect(getByText(getDateLocale(t).monthsShort[newValue.getMonth()])).toHaveClass("selected"); - }); - - describe("pickerFormat month", () => { - const { monthsShort } = getDateLocale(t); - - it("should select the first day of the clicked month and fire onDateChange", () => { - const onDateChange = jest.fn(); - const { getByText } = render(); - - act(() => getByText(monthsShort[7]).click()); - - expect(onDateChange).toHaveBeenCalledTimes(1); - expect(onDateChange).toHaveBeenCalledWith(new Date(2018, 7, 1)); - expect(getByText(monthsShort[7])).toHaveClass("selected"); - expect(getByText(monthsShort[3])).not.toHaveClass("selected"); - }); - - it("should stay in the month view after a month is selected", () => { - const { getByText, getByTestId, queryByTestId } = render(); - - act(() => getByText(monthsShort[5]).click()); - - expect(getByTestId("DatePickerMonthsContainer")).toBeInTheDocument(); - expect(queryByTestId("DatePickerDayContainer")).not.toBeInTheDocument(); - }); - - it("should highlight the selected month only in the year of the selected date", () => { - const { getByText } = render(); - expect(getByText(monthsShort[3])).toHaveClass("selected"); - - act(() => getByText("arrow_forward").click()); - expect(getByText(2019)).toBeInTheDocument(); - expect(getByText(monthsShort[3])).not.toHaveClass("selected"); - - act(() => getByText("arrow_back").click()); - expect(getByText(monthsShort[3])).toHaveClass("selected"); - }); - - it("should not fire onDateChange when the already selected month is clicked again", () => { - const onDateChange = jest.fn(); - const { getByText } = render(); - - act(() => getByText(monthsShort[3]).click()); - expect(onDateChange).not.toHaveBeenCalled(); - expect(getByText(monthsShort[3])).toHaveClass("selected"); - }); - - it("should fire onDateChange when the same month is clicked in a different year", () => { - const onDateChange = jest.fn(); - const { getByText } = render(); - - act(() => getByText("arrow_forward").click()); - act(() => getByText(monthsShort[3]).click()); - - expect(onDateChange).toHaveBeenCalledWith(new Date(2019, 3, 1)); - }); - }); - - it("should switch the view when the pickerFormat prop is changed", () => { - const { rerender, getByTestId, queryByTestId } = render(); - expect(getByTestId("DatePickerDayContainer")).toBeInTheDocument(); - - rerender(); - expect(getByTestId("DatePickerMonthsContainer")).toBeInTheDocument(); - expect(queryByTestId("DatePickerDayContainer")).not.toBeInTheDocument(); - - rerender(); - expect(getByTestId("DatePickerDayContainer")).toBeInTheDocument(); - }); }); diff --git a/src/lib/components/DatePicker/DatePicker.tsx b/src/lib/components/DatePicker/DatePicker.tsx index 8c0eb5f7..4027b808 100644 --- a/src/lib/components/DatePicker/DatePicker.tsx +++ b/src/lib/components/DatePicker/DatePicker.tsx @@ -19,7 +19,6 @@ export const DatePicker = (props: PropsWithRef) onClearClick, removeActionButtons, firstDayOfWeek = 1, - pickerFormat = "day", className, style, ref, @@ -32,7 +31,6 @@ export const DatePicker = (props: PropsWithRef) value={value} locale={locale} firstDayOfWeek={firstDayOfWeek} - pickerFormat={pickerFormat} fluid={!!fluid} onDateChange={onDateChange} onPickerChange={onPickerChange} diff --git a/src/lib/components/DatePicker/components/DateSelector/MonthYear/MonthGrid.tsx b/src/lib/components/DatePicker/components/DateSelector/MonthYear/MonthGrid.tsx new file mode 100644 index 00000000..6dfc6995 --- /dev/null +++ b/src/lib/components/DatePicker/components/DateSelector/MonthYear/MonthGrid.tsx @@ -0,0 +1,22 @@ +import styles from "@/components/DatePicker/DatePicker.module.scss"; +import MonthYearButton from "./MonthYearButton"; + +type Props = { + monthsShort: string[]; + selectedMonth?: number; + onMonthClick: (month: number) => void; +}; + +const MonthGrid = (props: Props) => { + const { monthsShort, selectedMonth, onMonthClick } = props; + + return ( +
+ {monthsShort.map((label, month) => ( + onMonthClick(month)} selected={month === selectedMonth} /> + ))} +
+ ); +}; + +export default MonthGrid; diff --git a/src/lib/components/DatePicker/components/DateSelector/MonthYear/MonthSelector.tsx b/src/lib/components/DatePicker/components/DateSelector/MonthYear/MonthSelector.tsx index 4407decd..a42850c4 100644 --- a/src/lib/components/DatePicker/components/DateSelector/MonthYear/MonthSelector.tsx +++ b/src/lib/components/DatePicker/components/DateSelector/MonthYear/MonthSelector.tsx @@ -4,13 +4,11 @@ import styles from "../../../DatePicker.module.scss"; import { useCallback, useContext, useState } from "react"; import { DatePickerContext } from "@/components/DatePicker/context/DatePickerProvider"; import Header from "@/components/DatePicker/components/Header/Header"; -import MonthYearButton from "./MonthYearButton"; +import MonthGrid from "./MonthGrid"; const MonthSelector = () => { - const { pickerDate, setPickerDate, setPicker, locale, onPickerChange, size, pickerFormat, selectedDate, setSelectedDate, onDateChange } = - useContext(DatePickerContext); + const { pickerDate, setPickerDate, setPicker, locale, onPickerChange, size } = useContext(DatePickerContext); const [activeDate] = useState(pickerDate); - const isMonthOnly = pickerFormat === "month"; const prevClickHandler = useCallback(() => { setPickerDate(new Date(pickerDate.getFullYear() - 1, pickerDate.getMonth())); @@ -21,12 +19,6 @@ const MonthSelector = () => { }, [pickerDate, setPickerDate]); const handleMonthClick = (month: number) => { - if (isMonthOnly) { - const date = new Date(pickerDate.getFullYear(), month, 1); - selectedDate?.getTime() !== date.getTime() && onDateChange?.(date); - setSelectedDate(date); - return; - } const newPickerDate = new Date(pickerDate.getTime()); newPickerDate.setMonth(month); setPickerDate(newPickerDate); @@ -38,20 +30,11 @@ const MonthSelector = () => { <>

-
- {locale.months.map((_, mIndex) => ( - handleMonthClick(mIndex)} - selected={ - isMonthOnly - ? selectedDate?.getMonth() === mIndex && selectedDate.getFullYear() === pickerDate.getFullYear() - : pickerDate.getMonth() === mIndex && pickerDate.getFullYear() === activeDate.getFullYear() - } - /> - ))} -
+ ); }; diff --git a/src/lib/components/DatePicker/components/DateSelector/MonthYear/YearGrid.tsx b/src/lib/components/DatePicker/components/DateSelector/MonthYear/YearGrid.tsx new file mode 100644 index 00000000..e541d842 --- /dev/null +++ b/src/lib/components/DatePicker/components/DateSelector/MonthYear/YearGrid.tsx @@ -0,0 +1,22 @@ +import styles from "@/components/DatePicker/DatePicker.module.scss"; +import MonthYearButton from "./MonthYearButton"; + +type Props = { + years: number[]; + selectedYear?: number; + onYearClick: (year: number) => void; +}; + +const YearGrid = (props: Props) => { + const { years, selectedYear, onYearClick } = props; + + return ( +
+ {years.map(year => ( + onYearClick(year)} selected={year === selectedYear} /> + ))} +
+ ); +}; + +export default YearGrid; diff --git a/src/lib/components/DatePicker/components/DateSelector/MonthYear/YearSelector.tsx b/src/lib/components/DatePicker/components/DateSelector/MonthYear/YearSelector.tsx index a6fb3e0c..403137c8 100644 --- a/src/lib/components/DatePicker/components/DateSelector/MonthYear/YearSelector.tsx +++ b/src/lib/components/DatePicker/components/DateSelector/MonthYear/YearSelector.tsx @@ -1,28 +1,15 @@ "use client"; -import { useCallback, useContext, useEffect, useState } from "react"; +import { useContext } from "react"; import { DatePickerContext } from "@/components/DatePicker/context/DatePickerProvider"; -import { calculateYears } from "@/components/DatePicker/components/helper"; import Header from "@/components/DatePicker/components/Header/Header"; -import MonthYearButton from "./MonthYearButton"; +import YearGrid from "./YearGrid"; +import useYearPage from "./useYearPage"; import styles from "@/components/DatePicker/DatePicker.module.scss"; const YearSelector = () => { const { pickerDate, setPickerDate, setPicker, previousTab, onPickerChange, size } = useContext(DatePickerContext); - const [years, setYears] = useState([]); - const displayedYearsCount = 16; - - useEffect(() => { - setYears(calculateYears(pickerDate.getFullYear() - displayedYearsCount / 2, pickerDate.getFullYear() + displayedYearsCount / 2)); - }, [pickerDate]); - - const prevClickHandler = useCallback(() => { - setYears(calculateYears(years[0] - displayedYearsCount, years[0])); - }, [years]); - - const nextClickHandler = useCallback(() => { - setYears(calculateYears(years[years.length - 1] + 1, years[years.length - 1] + displayedYearsCount + 1)); - }, [years]); + const { years, showPrevPage, showNextPage } = useYearPage(pickerDate.getFullYear()); const handleYearClick = (year: number) => { const newPickerDate = new Date(pickerDate.getTime()); @@ -34,19 +21,9 @@ const YearSelector = () => { return ( <> -
+

-
- {years.map(year => ( - handleYearClick(year)} selected={year === pickerDate.getFullYear()} key={year} /> - ))} -
+ ); }; diff --git a/src/lib/components/DatePicker/components/DateSelector/MonthYear/useYearPage.ts b/src/lib/components/DatePicker/components/DateSelector/MonthYear/useYearPage.ts new file mode 100644 index 00000000..3f7e3ecd --- /dev/null +++ b/src/lib/components/DatePicker/components/DateSelector/MonthYear/useYearPage.ts @@ -0,0 +1,26 @@ +import { useCallback, useEffect, useMemo, useState } from "react"; +import { calculateYears } from "@/components/DatePicker/components/helper"; + +const displayedYearsCount = 16; + +const getFirstYear = (year: number) => year - displayedYearsCount / 2; + +/** + * Holds the page of years shown in a year grid. The page is centered on the given year, and moves to it again only when the + * year leaves the page, so picking a year on the current page does not shift the grid. + */ +const useYearPage = (year: number) => { + const [firstYear, setFirstYear] = useState(() => getFirstYear(year)); + + useEffect(() => { + setFirstYear(first => (year >= first && year < first + displayedYearsCount ? first : getFirstYear(year))); + }, [year]); + + const years = useMemo(() => calculateYears(firstYear, firstYear + displayedYearsCount), [firstYear]); + const showPrevPage = useCallback(() => setFirstYear(first => first - displayedYearsCount), []); + const showNextPage = useCallback(() => setFirstYear(first => first + displayedYearsCount), []); + + return { years, showPrevPage, showNextPage }; +}; + +export default useYearPage; diff --git a/src/lib/components/DatePicker/context/DatePickerProvider.tsx b/src/lib/components/DatePicker/context/DatePickerProvider.tsx index 7232e409..f8d311da 100644 --- a/src/lib/components/DatePicker/context/DatePickerProvider.tsx +++ b/src/lib/components/DatePicker/context/DatePickerProvider.tsx @@ -7,21 +7,15 @@ import { DateUtils } from "../../../../utils/dateUtils"; export const DatePickerContext = createContext(datePickerContextDefaultValues); export const DatePickerProvider = (props: PropsWithChildren) => { - const { children, value, size, locale, fluid, onDateChange, onPickerChange, onClearClick, firstDayOfWeek, pickerFormat = "day" } = props; + const { children, value, size, locale, fluid, onDateChange, onPickerChange, onClearClick, firstDayOfWeek } = props; - const [picker, setPicker] = useState(pickerFormat); + const [picker, setPicker] = useState("day"); const [previousTab, setPreviousTab] = useState<"day" | "month">("day"); const [selectedDate, setSelectedDate] = useState(DateUtils.getDateTimeless(value)); const [pickerDate, setPickerDate] = useState(DateUtils.getDateTimeless(value) ?? DateUtils.getTodayTimeless()); const today = useMemo(() => DateUtils.getTodayTimeless(), []); - const valueTime = value?.getTime(); - useEffect(() => { - const date = valueTime !== undefined ? DateUtils.getDateTimeless(new Date(valueTime)) : undefined; - setSelectedDate(date); - if (date) setPickerDate(date); - }, [valueTime]); - useEffect(() => setPicker(pickerFormat), [pickerFormat]); + useEffect(() => setSelectedDate(value), [value]); const onYearMonthTabSelected = useCallback( (nextTab: "year" | "month", previousTab: "month" | "day") => { @@ -44,7 +38,6 @@ export const DatePickerProvider = (props: PropsWithChildren ({ size, - pickerFormat, pickerDate, selectedDate, previousTab, @@ -75,7 +68,6 @@ export const DatePickerProvider = (props: PropsWithChildren void; onDateChange?: (date?: Date) => void; onPickerChange?: (picker: DatePickerPickerType) => void; - pickerFormat?: DatePickerFormat; } & DatePickerDefaultableProps; export type DatePickerDefaultableProps = { @@ -41,16 +40,12 @@ export type DatePickerProviderProps = { onPickerChange?: (picker: DatePickerPickerType) => void; onClearClick?: () => void; firstDayOfWeek: DaysOfWeek; - pickerFormat?: DatePickerFormat; }; export type DatePickerPickerType = "day" | "month" | "year"; -export type DatePickerFormat = "day" | "month"; - export type DatePickerContextProps = { size: Size4SM; - pickerFormat: DatePickerFormat; firstDayOfWeek: DaysOfWeek; locale: DatePickerLocale; pickerDate: Date; // The date that is used to hold the values when choosing temporary values like months or years. @@ -76,7 +71,6 @@ export type DatePickerLocale = { export const datePickerContextDefaultValues: DatePickerContextProps = { size: "md", - pickerFormat: "day", pickerDate: DateUtils.getTodayTimeless(), picker: "day", previousTab: "day", diff --git a/src/lib/components/InputDate/InputDate.stories.tsx b/src/lib/components/InputDate/InputDate.stories.tsx index b227e98e..954f871c 100644 --- a/src/lib/components/InputDate/InputDate.stories.tsx +++ b/src/lib/components/InputDate/InputDate.stories.tsx @@ -11,7 +11,6 @@ const meta: Meta = { size: { table: { defaultValue: { summary: "md" } } }, placeholder: { table: { defaultValue: { summary: "Reflects format prop" } } }, firstDayOfWeek: { table: { defaultValue: { summary: "1" } } }, - pickerFormat: { control: { type: "radio" }, options: ["day", "month"], table: { defaultValue: { summary: "day" } } }, }, args: {}, }; diff --git a/src/lib/components/InputDate/InputDate.tsx b/src/lib/components/InputDate/InputDate.tsx index c14eb5e7..9a4abfce 100644 --- a/src/lib/components/InputDate/InputDate.tsx +++ b/src/lib/components/InputDate/InputDate.tsx @@ -28,20 +28,8 @@ const pickerSizeMap = { const InputDate = (p: PropsWithRef) => { const props = usePropsWithThemeDefaults("InputDate", p); - const { editable, pill, value, onChange, ref, style, className, locale: propsLocale, firstDayOfWeek, icon, pickerFormat } = props; - const format = useMemo(() => { - const mergedFormat = { ...defaultDateFormat, ...props.format }; - if (pickerFormat !== "month") return mergedFormat; - - const dayIndex = mergedFormat.order.indexOf("day"); - if (dayIndex === -1) return mergedFormat; - - return { - ...mergedFormat, - order: mergedFormat.order.filter((_, index) => index !== dayIndex), - prefix: mergedFormat.prefix?.filter((_, index) => index !== dayIndex), - }; - }, [pickerFormat, props.format]); + const { editable, pill, value, onChange, ref, style, className, locale: propsLocale, firstDayOfWeek, icon } = props; + const format = useMemo(() => ({ ...defaultDateFormat, ...props.format }), [props.format]); const locale = useDateLocale(propsLocale); const placeholder = useMemo( @@ -173,7 +161,6 @@ const InputDate = (p: PropsWithRef) => { className={styles.datePicker} style={pickerStyle} locale={locale} - pickerFormat={pickerFormat} />, document.body, )} diff --git a/src/lib/components/InputDate/helper.ts b/src/lib/components/InputDate/helper.ts index 49136984..e3280110 100644 --- a/src/lib/components/InputDate/helper.ts +++ b/src/lib/components/InputDate/helper.ts @@ -128,7 +128,7 @@ export const parseDate = (dateString: string, format: DateFormat, locale: DatePi const year = parseInt(dateValues.year ?? "", 10); const fullYear = yearFormat === "YY" ? 2000 + year : year; const month = parseMonth(dateValues.month ?? ""); - const day = order.includes("day") ? parseInt(dateValues.day ?? "", 10) : 1; + const day = parseInt(dateValues.day ?? "", 10); // Create and validate date const date = new Date(fullYear, month - 1, day); diff --git a/src/lib/components/InputDate/test/InputDate.test.tsx b/src/lib/components/InputDate/test/InputDate.test.tsx index e81f67bd..3942e4b4 100644 --- a/src/lib/components/InputDate/test/InputDate.test.tsx +++ b/src/lib/components/InputDate/test/InputDate.test.tsx @@ -242,37 +242,6 @@ describe("InputDate", () => { expect(onChange).toHaveBeenCalledTimes(5); }); - describe("pickerFormat month", () => { - it("should display MM/YYYY as the placeholder", () => { - const { getInput } = renderExt(); - expect(getInput()).toHaveAttribute("placeholder", "MM/YYYY"); - }); - - it("should open the picker in the month view and display MM/YYYY in the input when a month is selected", async () => { - const onChange = jest.fn(); - const { getInput } = renderExt(); - - await userEvent.click(getInput()); - expect(screen.getByTestId("DatePickerMonthsContainer")).toBeInTheDocument(); - expect(screen.queryByTestId("DatePickerDayContainer")).not.toBeInTheDocument(); - - await userEvent.click(screen.getByText(getDateLocale(t).monthsShort[4])); - expect(getInput()).toHaveValue("05/2025"); - expect(onChange).toHaveBeenLastCalledWith(new Date(2025, 4, 1)); - }); - - it("should call onChange with the first day of the month when MM/YYYY is typed, and treat DD/MM/YYYY as invalid", () => { - const onChange = jest.fn(); - const { getInput } = renderExt(); - - fireEvent.change(getInput(), { target: { value: "03/2026" } }); - expect(onChange).toHaveBeenNthCalledWith(1, new Date(2026, 2, 1)); - - fireEvent.change(getInput(), { target: { value: "05/03/2026" } }); - expect(onChange).toHaveBeenNthCalledWith(2, undefined); - }); - }); - it("should be rendered as success variant when success prop is true", () => { const { getByTestId } = render(); expect(getByTestId("inputItem")).toHaveClass("success"); diff --git a/src/lib/components/InputDate/types.ts b/src/lib/components/InputDate/types.ts index 03814a22..b6955ea9 100644 --- a/src/lib/components/InputDate/types.ts +++ b/src/lib/components/InputDate/types.ts @@ -1,6 +1,6 @@ import { InputCommonProps, InputSize } from "../Form/types"; import { DateFormat, DaysOfWeek } from "../Motif/Pickers/types"; -import { DatePickerFormat, DatePickerLocale } from "../DatePicker/types"; +import { DatePickerLocale } from "../DatePicker/types"; import type { IconGlobalType } from "../../types"; export type InputDateDefaultableProps = { @@ -14,6 +14,4 @@ export type InputDateDefaultableProps = { icon?: IconGlobalType | null; }; -export type InputDateProps = InputCommonProps & { - pickerFormat?: DatePickerFormat; -} & InputDateDefaultableProps; +export type InputDateProps = InputCommonProps & InputDateDefaultableProps; diff --git a/src/lib/components/MonthPicker/MonthPicker.stories.tsx b/src/lib/components/MonthPicker/MonthPicker.stories.tsx new file mode 100644 index 00000000..282601c2 --- /dev/null +++ b/src/lib/components/MonthPicker/MonthPicker.stories.tsx @@ -0,0 +1,30 @@ +import type { Meta, StoryObj } from "@storybook/nextjs"; + +import MonthPicker from "./MonthPicker"; + +const meta: Meta = { + title: "Components/MonthPicker", + component: MonthPicker, + argTypes: { + value: { control: { type: "number", min: 0, max: 11 } }, + variant: { table: { defaultValue: { summary: "borderless" } } }, + size: { table: { defaultValue: { summary: "md" } } }, + }, + args: { + variant: "shadow", + }, +}; + +export default meta; +type Story = StoryObj; + +export const Primary: Story = { + parameters: { + chromatic: { disableSnapshot: true }, + }, +}; + +export const PrimaryStaticForChromatic: Story = { + tags: ["!autodocs", "!dev"], + args: { value: 1 }, +}; diff --git a/src/lib/components/MonthPicker/MonthPicker.test.tsx b/src/lib/components/MonthPicker/MonthPicker.test.tsx new file mode 100644 index 00000000..046db525 --- /dev/null +++ b/src/lib/components/MonthPicker/MonthPicker.test.tsx @@ -0,0 +1,67 @@ +import { act, render } from "@testing-library/react"; +import MonthPicker from "./MonthPicker"; +import { t, runSnapshotDefaultsAndStandardPropsTest } from "../../../utils/testUtils"; +import { StandardPropsWithRef } from "../../types"; +import { getDateLocale } from "src/i18n/helper.ts"; + +describe("MonthPicker", () => { + const { monthsShort } = getDateLocale(t); + + runSnapshotDefaultsAndStandardPropsTest((props: StandardPropsWithRef) => render(), { + assertDefaults: ({ getByTestId, getByText }) => { + // variant: borderless + expect(getByTestId("Picker")).toHaveClass("borderless"); + // size: md + expect(getByTestId("Picker")).toHaveClass("md"); + // locale: English + expect(getByText(monthsShort[0])).toBeInTheDocument(); + }, + }); + + it("should show only the twelve months", () => { + const { getByTestId, queryByText } = render(); + + expect(getByTestId("DatePickerMonthsContainer").children).toHaveLength(12); + expect(queryByText("arrow_back")).not.toBeInTheDocument(); + expect(queryByText("arrow_forward")).not.toBeInTheDocument(); + }); + + it("should show the month in the value prop as selected", () => { + const { getByText } = render(); + expect(getByText(monthsShort[3])).toHaveClass("selected"); + }); + + it("should call onMonthChange with the index of the clicked month and select it", () => { + const onMonthChange = jest.fn(); + const { getByText } = render(); + + act(() => getByText(monthsShort[0]).click()); + + expect(onMonthChange).toHaveBeenCalledTimes(1); + expect(onMonthChange).toHaveBeenCalledWith(0); + expect(getByText(monthsShort[0])).toHaveClass("selected"); + expect(getByText(monthsShort[3])).not.toHaveClass("selected"); + }); + + it("should not call onMonthChange when the selected month is clicked again", () => { + const onMonthChange = jest.fn(); + const { getByText } = render(); + + act(() => getByText(monthsShort[3]).click()); + expect(onMonthChange).not.toHaveBeenCalled(); + }); + + it("should select the new month when the value prop changes", () => { + const { rerender, getByText } = render(); + + rerender(); + expect(getByText(monthsShort[7])).toHaveClass("selected"); + expect(getByText(monthsShort[3])).not.toHaveClass("selected"); + }); + + it("should show the month names in the locale prop", () => { + const locale = { monthsShort: ["M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8", "M9", "M10", "M11", "M12"] }; + const { getByText } = render(); + expect(getByText("M12")).toBeInTheDocument(); + }); +}); diff --git a/src/lib/components/MonthPicker/MonthPicker.tsx b/src/lib/components/MonthPicker/MonthPicker.tsx new file mode 100644 index 00000000..c75a8133 --- /dev/null +++ b/src/lib/components/MonthPicker/MonthPicker.tsx @@ -0,0 +1,46 @@ +"use client"; + +import { useEffect, useState } from "react"; +import Picker from "../Motif/Pickers/Picker"; +import MonthGrid from "@/components/DatePicker/components/DateSelector/MonthYear/MonthGrid"; +import styles from "@/components/DatePicker/DatePicker.module.scss"; +import { MonthPickerProps } from "./types"; +import { PropsWithRef } from "../../types"; +import usePropsWithThemeDefaults from "../../motif/hooks/usePropsWithThemeDefaults"; +import { sanitizeModuleClasses } from "../../../utils/cssUtils"; +import { useDateLocale } from "src/i18n/useDateLocale.ts"; + +const MonthPicker = (props: PropsWithRef) => { + const { + size = "md", + variant = "borderless", + fluid, + value, + onMonthChange, + locale: propsLocale, + className, + style, + ref, + } = usePropsWithThemeDefaults("MonthPicker", props); + const locale = useDateLocale(propsLocale); + const [selectedMonth, setSelectedMonth] = useState(value); + + useEffect(() => setSelectedMonth(value), [value]); + + const handleMonthClick = (month: number) => { + if (month === selectedMonth) return; + setSelectedMonth(month); + onMonthChange?.(month); + }; + + return ( + +
+ +
+
+ ); +}; + +MonthPicker.displayName = "MonthPicker"; +export default MonthPicker; diff --git a/src/lib/components/MonthPicker/__snapshots__/MonthPicker.test.tsx.snap b/src/lib/components/MonthPicker/__snapshots__/MonthPicker.test.tsx.snap new file mode 100644 index 00000000..0ff6925a --- /dev/null +++ b/src/lib/components/MonthPicker/__snapshots__/MonthPicker.test.tsx.snap @@ -0,0 +1,80 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`MonthPicker should render with only required props, have default prop values stated here, and have standard props; className, style and ref props working as expected 1`] = ` +
+
+
+
+ + + + + + + + + + + + +
+
+
+
+`; diff --git a/src/lib/components/MonthPicker/index.ts b/src/lib/components/MonthPicker/index.ts new file mode 100644 index 00000000..d2a8619c --- /dev/null +++ b/src/lib/components/MonthPicker/index.ts @@ -0,0 +1 @@ +export { default } from "./MonthPicker"; diff --git a/src/lib/components/MonthPicker/types.ts b/src/lib/components/MonthPicker/types.ts new file mode 100644 index 00000000..0ca331fc --- /dev/null +++ b/src/lib/components/MonthPicker/types.ts @@ -0,0 +1,21 @@ +import { PickerPropsCommon } from "../Motif/Pickers/types"; +import { DatePickerLocale } from "../DatePicker/types"; + +export type MonthPickerProps = { + /** Index of the selected month: 0 for January, 11 for December. */ + value?: number; + /** Called with the index of the clicked month (0 for January) when it differs from the selected one. */ + onMonthChange?: (month: number) => void; +} & MonthPickerDefaultableProps; + +export type MonthPickerDefaultableProps = { + /** + * ``` + * { + * // 3 letters + * monthsShort: string[]; + * } + * ``` + */ + locale?: Pick; +} & PickerPropsCommon; diff --git a/src/lib/components/YearPicker/YearPicker.stories.tsx b/src/lib/components/YearPicker/YearPicker.stories.tsx new file mode 100644 index 00000000..05dab653 --- /dev/null +++ b/src/lib/components/YearPicker/YearPicker.stories.tsx @@ -0,0 +1,30 @@ +import type { Meta, StoryObj } from "@storybook/nextjs"; + +import YearPicker from "./YearPicker"; + +const meta: Meta = { + title: "Components/YearPicker", + component: YearPicker, + argTypes: { + value: { control: { type: "number" } }, + variant: { table: { defaultValue: { summary: "borderless" } } }, + size: { table: { defaultValue: { summary: "md" } } }, + }, + args: { + variant: "shadow", + }, +}; + +export default meta; +type Story = StoryObj; + +export const Primary: Story = { + parameters: { + chromatic: { disableSnapshot: true }, + }, +}; + +export const PrimaryStaticForChromatic: Story = { + tags: ["!autodocs", "!dev"], + args: { value: 2013 }, +}; diff --git a/src/lib/components/YearPicker/YearPicker.test.tsx b/src/lib/components/YearPicker/YearPicker.test.tsx new file mode 100644 index 00000000..b794f0ff --- /dev/null +++ b/src/lib/components/YearPicker/YearPicker.test.tsx @@ -0,0 +1,81 @@ +import { act, render } from "@testing-library/react"; +import YearPicker from "./YearPicker"; +import { runSnapshotDefaultsAndStandardPropsTest } from "../../../utils/testUtils"; +import { StandardPropsWithRef } from "../../types"; + +describe("YearPicker", () => { + const year = 2013; + + runSnapshotDefaultsAndStandardPropsTest((props: StandardPropsWithRef) => render(), { + assertDefaults: ({ getByTestId }) => { + // variant: borderless + expect(getByTestId("Picker")).toHaveClass("borderless"); + // size: md + expect(getByTestId("Picker")).toHaveClass("md"); + }, + }); + + const getYears = (container: HTMLElement) => [...container.children].map(button => Number(button.textContent)); + const yearsFrom = (first: number) => Array.from({ length: 16 }, (_, index) => first + index); + + it("should show the year in the value prop in the header and sixteen years around it, with the year selected", () => { + const { getByTestId, getAllByText } = render(); + + expect(getYears(getByTestId("DatePickerYearsContainer"))).toEqual(yearsFrom(2005)); + const [headerYear, gridYear] = getAllByText(year); + expect(headerYear.closest("div")).toHaveClass("headerMonthYearButtonContainer"); + expect(gridYear).toHaveClass("selected"); + }); + + it("should show the current year in the header and the years around it, with nothing selected, when there is no value", () => { + const currentYear = new Date().getFullYear(); + const { getByTestId, getAllByText } = render(); + + expect(getYears(getByTestId("DatePickerYearsContainer"))).toEqual(yearsFrom(currentYear - 8)); + expect(getAllByText(currentYear)).toHaveLength(2); + expect(getByTestId("DatePickerYearsContainer").querySelector(".selected")).toBeNull(); + }); + + it("should call onYearChange with the clicked year, show it in the header and keep the years on the page", () => { + const onYearChange = jest.fn(); + const { getByTestId, getAllByText } = render(); + + act(() => getAllByText(2018)[0].click()); + + expect(onYearChange).toHaveBeenCalledTimes(1); + expect(onYearChange).toHaveBeenCalledWith(2018); + const [headerYear, gridYear] = getAllByText(2018); + expect(headerYear.closest("div")).toHaveClass("headerMonthYearButtonContainer"); + expect(gridYear).toHaveClass("selected"); + expect(getYears(getByTestId("DatePickerYearsContainer"))).toEqual(yearsFrom(2005)); + }); + + it("should not call onYearChange when the selected year is clicked again", () => { + const onYearChange = jest.fn(); + const { getAllByText } = render(); + + act(() => getAllByText(year)[1].click()); + expect(onYearChange).not.toHaveBeenCalled(); + }); + + it("should show the previous and next sixteen years when the arrow buttons are clicked, keeping the selected year in the header", () => { + const { getByTestId, getByText } = render(); + + act(() => getByText("arrow_forward").click()); + expect(getYears(getByTestId("DatePickerYearsContainer"))).toEqual(yearsFrom(2021)); + expect(getByText(year)).toBeInTheDocument(); + + act(() => getByText("arrow_back").click()); + act(() => getByText("arrow_back").click()); + expect(getYears(getByTestId("DatePickerYearsContainer"))).toEqual(yearsFrom(1989)); + expect(getByText(year)).toBeInTheDocument(); + }); + + it("should show the page of the new year when the value prop changes to a year outside the page", () => { + const { rerender, getByTestId, getAllByText } = render(); + + rerender(); + expect(getYears(getByTestId("DatePickerYearsContainer"))).toEqual(yearsFrom(2042)); + expect(getAllByText(2050)[1]).toHaveClass("selected"); + }); +}); diff --git a/src/lib/components/YearPicker/YearPicker.tsx b/src/lib/components/YearPicker/YearPicker.tsx new file mode 100644 index 00000000..a96b1203 --- /dev/null +++ b/src/lib/components/YearPicker/YearPicker.tsx @@ -0,0 +1,49 @@ +"use client"; + +import { useEffect, useState } from "react"; +import Picker from "../Motif/Pickers/Picker"; +import Header from "@/components/DatePicker/components/Header/Header"; +import YearGrid from "@/components/DatePicker/components/DateSelector/MonthYear/YearGrid"; +import useYearPage from "@/components/DatePicker/components/DateSelector/MonthYear/useYearPage"; +import styles from "@/components/DatePicker/DatePicker.module.scss"; +import { YearPickerProps } from "./types"; +import { PropsWithRef } from "../../types"; +import usePropsWithThemeDefaults from "../../motif/hooks/usePropsWithThemeDefaults"; +import { sanitizeModuleClasses } from "../../../utils/cssUtils"; + +const YearPicker = (props: PropsWithRef) => { + const { + size = "md", + variant = "borderless", + fluid, + value, + onYearChange, + className, + style, + ref, + } = usePropsWithThemeDefaults("YearPicker", props); + const [selectedYear, setSelectedYear] = useState(value); + const displayedYear = selectedYear ?? new Date().getFullYear(); + const { years, showPrevPage, showNextPage } = useYearPage(displayedYear); + + useEffect(() => setSelectedYear(value), [value]); + + const handleYearClick = (year: number) => { + if (year === selectedYear) return; + setSelectedYear(year); + onYearChange?.(year); + }; + + return ( + +
+
+
+ +
+
+ ); +}; + +YearPicker.displayName = "YearPicker"; +export default YearPicker; diff --git a/src/lib/components/YearPicker/__snapshots__/YearPicker.test.tsx.snap b/src/lib/components/YearPicker/__snapshots__/YearPicker.test.tsx.snap new file mode 100644 index 00000000..8df8ed67 --- /dev/null +++ b/src/lib/components/YearPicker/__snapshots__/YearPicker.test.tsx.snap @@ -0,0 +1,142 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`YearPicker should render with only required props, have default prop values stated here, and have standard props; className, style and ref props working as expected 1`] = ` +
+
+
+
+ +
+ +
+ +
+
+
+ + + + + + + + + + + + + + + + +
+
+
+
+`; diff --git a/src/lib/components/YearPicker/index.ts b/src/lib/components/YearPicker/index.ts new file mode 100644 index 00000000..89d4b7ad --- /dev/null +++ b/src/lib/components/YearPicker/index.ts @@ -0,0 +1 @@ +export { default } from "./YearPicker"; diff --git a/src/lib/components/YearPicker/types.ts b/src/lib/components/YearPicker/types.ts new file mode 100644 index 00000000..fc642c14 --- /dev/null +++ b/src/lib/components/YearPicker/types.ts @@ -0,0 +1,10 @@ +import { PickerPropsCommon } from "../Motif/Pickers/types"; + +export type YearPickerProps = { + /** The selected year, e.g. 2025. */ + value?: number; + /** Called with the clicked year when it differs from the selected one. */ + onYearChange?: (year: number) => void; +} & YearPickerDefaultableProps; + +export type YearPickerDefaultableProps = PickerPropsCommon; diff --git a/src/lib/index.ts b/src/lib/index.ts index b4d00ba6..719f7521 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -36,6 +36,7 @@ export { default as Link } from "./components/Link"; export { default as ListView } from "./components/ListView"; export { default as MenuList } from "./components/MenuList"; export { default as Modal } from "./components/Modal/Modal"; +export { default as MonthPicker } from "./components/MonthPicker"; export { default as NavBar } from "./components/NavBar"; export { default as Pagination } from "./components/Pagination"; export { default as Panel } from "./components/Panel"; @@ -62,3 +63,4 @@ export { default as Tooltip } from "./components/Tooltip"; export { default as UploadList } from "./components/Upload/UploadList"; export { default as UploadDragger } from "./components/Upload/UploadDragger"; export { default as UploadInput } from "./components/Upload/UploadInput"; +export { default as YearPicker } from "./components/YearPicker"; diff --git a/src/lib/motif/types/componentDefaultableProps.ts b/src/lib/motif/types/componentDefaultableProps.ts index 0b599397..a2da9cf5 100644 --- a/src/lib/motif/types/componentDefaultableProps.ts +++ b/src/lib/motif/types/componentDefaultableProps.ts @@ -34,6 +34,7 @@ import { LinkDefaultableProps } from "../../components/Link/types"; import { ListViewDefaultableProps } from "../../components/ListView/types"; import { MenuListDefaultableProps } from "../../components/MenuList/types"; import { ModalDefaultableProps } from "../../components/Modal/types"; +import { MonthPickerDefaultableProps } from "../../components/MonthPicker/types"; import { NavBarDefaultableProps } from "../../components/NavBar/types"; import { PaginationDefaultableProps } from "../../components/Pagination/types"; import { PanelDefaultableProps } from "../../components/Panel/types"; @@ -60,6 +61,7 @@ import { UploadListDefaultableProps } from "../../components/Upload/UploadList/t import { UploadDefaultableProps } from "../../components/Upload/types"; import { UploadInputDefaultableProps } from "../../components/Upload/UploadInput/types"; import { ImageUploadDefaultableProps } from "../../components/Upload/ImageUpload/types"; +import { YearPickerDefaultableProps } from "../../components/YearPicker/types"; import { ContainerDefaultableProps } from "../../components/Container/types"; export type ComponentDefaultableProps = { @@ -102,6 +104,7 @@ export type ComponentDefaultableProps = { ListView?: ListViewDefaultableProps; MenuList?: MenuListDefaultableProps; Modal?: ModalDefaultableProps; + MonthPicker?: MonthPickerDefaultableProps; NavBar?: NavBarDefaultableProps; Pagination?: PaginationDefaultableProps; Panel?: PanelDefaultableProps; @@ -127,4 +130,5 @@ export type ComponentDefaultableProps = { UploadList?: UploadListDefaultableProps; UploadDragger?: UploadDefaultableProps; UploadInput?: UploadInputDefaultableProps; + YearPicker?: YearPickerDefaultableProps; };