UNPKG

@navikt/ds-react

Version:

React components from the Norwegian Labour and Welfare Administration.

89 lines 4.31 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MonthPickerTable = void 0; const date_fns_1 = require("date-fns"); const react_1 = __importStar(require("react")); const typography_1 = require("../../../typography"); const date_utils_1 = require("../../date-utils"); const MonthPicker_context_1 = require("../MonthPicker.context"); const MonthPicker_Button_1 = __importDefault(require("./MonthPicker.Button")); const getAllMonths = () => { const months = []; const date = (0, date_fns_1.startOfMonth)(new Date()); for (let month = 0; month <= 11; month++) { months.push((0, date_fns_1.setMonth)(date, month)); } return months; }; const MonthPickerTable = () => { const [focus, setFocus] = (0, react_1.useState)(); const { selected, year, disabled } = (0, MonthPicker_context_1.useMonthPickerContext)(); const months = getAllMonths(); const hasSelected = selected && months.some((m) => (0, date_fns_1.isSameMonth)((0, date_fns_1.setYear)(m, year.getFullYear()), selected)); const getRootFallback = () => { const today = (0, date_fns_1.startOfMonth)(new Date()); if ((year === null || year === void 0 ? void 0 : year.getFullYear()) === today.getFullYear() && !(0, date_utils_1.isMatch)(today, disabled)) { return today; } for (let i = 0; i < months.length; i++) { const m = months[i]; if (!(0, date_utils_1.isMatch)((0, date_fns_1.setYear)(m, year.getFullYear()), disabled)) { return (0, date_fns_1.setYear)(m, year.getFullYear()); } } }; const [tabRoot, setTabRoot] = (0, react_1.useState)(hasSelected ? selected : getRootFallback()); if ((tabRoot === null || tabRoot === void 0 ? void 0 : tabRoot.getFullYear()) !== year.getFullYear()) { setTabRoot(hasSelected ? selected : getRootFallback()); } const tableMonths = [ months.slice(0, 4), months.slice(4, 8), months.slice(8, 12), ]; return (react_1.default.createElement(typography_1.BodyShort, { as: "table", className: "rdp-table" }, react_1.default.createElement("tbody", { className: "rdp-tbody" }, tableMonths.map((tableRow, i) => (react_1.default.createElement("tr", { className: "rdp-row", key: i }, tableRow.map((month) => { return (react_1.default.createElement("td", { key: month.toDateString(), className: "rdp-cell" }, react_1.default.createElement(MonthPicker_Button_1.default, { month: (0, date_fns_1.setYear)(month, year.getFullYear()), months: months, focus: focus, setFocus: setFocus, tabRoot: tabRoot, setTabRoot: setTabRoot }))); }))))))); }; exports.MonthPickerTable = MonthPickerTable; //# sourceMappingURL=MonthPicker.Table.js.map