+
>
);
};
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/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 cd2e2917..16768984 100644
--- a/src/lib/index.ts
+++ b/src/lib/index.ts
@@ -37,6 +37,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";
@@ -63,3 +64,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 6da97f6c..271d7978 100644
--- a/src/lib/motif/types/componentDefaultableProps.ts
+++ b/src/lib/motif/types/componentDefaultableProps.ts
@@ -35,6 +35,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";
@@ -61,6 +62,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 = {
@@ -104,6 +106,7 @@ export type ComponentDefaultableProps = {
ListView?: ListViewDefaultableProps;
MenuList?: MenuListDefaultableProps;
Modal?: ModalDefaultableProps;
+ MonthPicker?: MonthPickerDefaultableProps;
NavBar?: NavBarDefaultableProps;
Pagination?: PaginationDefaultableProps;
Panel?: PanelDefaultableProps;
@@ -129,4 +132,5 @@ export type ComponentDefaultableProps = {
UploadList?: UploadListDefaultableProps;
UploadDragger?: UploadDefaultableProps;
UploadInput?: UploadInputDefaultableProps;
+ YearPicker?: YearPickerDefaultableProps;
};