UNPKG

@navikt/ds-react

Version:

React components from the Norwegian Labour and Welfare Administration.

63 lines 4.96 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { format, getMonth, getYear, setMonth, setYear, startOfMonth, } from "date-fns"; import React, { useCallback } from "react"; import { useDayPicker } from "react-day-picker"; import { ArrowLeftIcon, ArrowRightIcon } from "@navikt/aksel-icons"; import { Button } from "../../../button/index.js"; import { Select } from "../../../form/select/index.js"; import { BodyShort } from "../../../typography/index.js"; import { omit } from "../../../utils-external/index.js"; import { useDateTranslationContext } from "../../Date.locale.js"; import { calendarRange, getMonthOptions, getYearOptions, } from "../../date-utils/index.js"; import { DatePickerWeekRow } from "./DatePicker.WeekRow.js"; const DatePickerMonths = (_a) => { var { children, calendarMonth, locale, onWeekNumberClick } = _a, rest = __rest(_a, ["children", "calendarMonth", "locale", "onWeekNumberClick"]); const { dayPickerProps, goToMonth, previousMonth, nextMonth } = useDayPicker(); const { captionLayout } = dayPickerProps; const translate = useDateTranslationContext().translate; const handleMonthChange = useCallback((date, e) => { const selectedMonth = Number(e.target.value); const newMonth = setMonth(startOfMonth(date), selectedMonth); goToMonth(newMonth); }, [goToMonth]); const handleYearChange = useCallback((date, e) => { const selectedYear = Number(e.target.value); const newMonth = setYear(startOfMonth(date), selectedYear); goToMonth(newMonth); }, [goToMonth]); const [navStart, navEnd] = calendarRange({ captionLayout: captionLayout === "dropdown" ? "dropdown" : "label", startMonth: dayPickerProps.startMonth, endMonth: dayPickerProps.endMonth, today: dayPickerProps.today, }); const months = getMonthOptions({ displayMonth: calendarMonth.date, navStart, navEnd, locale, }); const dropdownYears = getYearOptions({ navStart, navEnd, locale }); return (React.createElement("div", Object.assign({}, omit(rest, ["displayIndex"])), React.createElement("div", { className: "aksel-date__caption" }, (captionLayout === null || captionLayout === void 0 ? void 0 : captionLayout.startsWith("dropdown")) && (React.createElement("span", { "aria-live": "polite", "aria-atomic": "true", className: "aksel-sr-only" }, format(calendarMonth.date, "LLLL y", { locale }))), React.createElement(Button, { variant: "tertiary-neutral", disabled: !previousMonth, onClick: () => previousMonth && goToMonth(previousMonth), icon: React.createElement(ArrowLeftIcon, { title: translate("goToPreviousMonth") }), className: "aksel-date__caption-button", type: "button" }), (captionLayout === null || captionLayout === void 0 ? void 0 : captionLayout.startsWith("dropdown")) ? (React.createElement("div", { className: "aksel-date__caption" }, React.createElement(Select, { label: translate("month"), hideLabel: true, className: "aksel-date__caption__month", onChange: (event) => handleMonthChange(calendarMonth.date, event), value: getMonth(calendarMonth.date) }, months === null || months === void 0 ? void 0 : months.map(({ value, label, disabled }) => (React.createElement("option", { key: value, value: value, disabled: disabled }, label)))), React.createElement(Select, { label: translate("year"), hideLabel: true, className: "aksel-date__caption__year", onChange: (event) => handleYearChange(calendarMonth.date, event), value: getYear(calendarMonth.date) }, dropdownYears === null || dropdownYears === void 0 ? void 0 : dropdownYears.map(({ value, label, disabled }) => (React.createElement("option", { key: value, value: value, disabled: disabled }, label)))))) : (React.createElement(BodyShort, { weight: "semibold", as: "span", "aria-live": "polite", role: "status", className: "aksel-date__caption-label" }, format(calendarMonth.date, "LLLL y", { locale }))), React.createElement(Button, { variant: "tertiary-neutral", icon: React.createElement(ArrowRightIcon, { title: translate("goToNextMonth") }), onClick: () => nextMonth && goToMonth(nextMonth), disabled: !nextMonth, className: "aksel-date__caption-button", type: "button" })), React.createElement(DatePickerWeekRow, { weeks: calendarMonth.weeks, onWeekNumberClick: onWeekNumberClick }), children)); }; export { DatePickerMonths }; //# sourceMappingURL=DatePicker.Months.js.map