@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
90 lines • 4.66 kB
JavaScript
;
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;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.MonthButton = void 0;
const date_fns_1 = require("date-fns");
const react_1 = __importStar(require("react"));
const Theme_1 = require("../../../theme/Theme");
const date_utils_1 = require("../../date-utils");
const MonthPicker_context_1 = require("../MonthPicker.context");
const MonthPicker_util_1 = require("../MonthPicker.util");
const disableMonth = (month, fromDate, toDate) => {
if (fromDate && toDate) {
return (((0, date_fns_1.compareAsc)(month, fromDate) === -1 && !(0, date_fns_1.isSameMonth)(month, fromDate)) ||
((0, date_fns_1.compareDesc)(month, toDate) === -1 && !(0, date_fns_1.isSameMonth)(month, toDate)));
}
if (fromDate) {
return (0, date_fns_1.compareAsc)(month, fromDate) === -1 && !(0, date_fns_1.isSameMonth)(month, fromDate);
}
if (toDate) {
return (0, date_fns_1.compareDesc)(month, toDate) === -1 && !(0, date_fns_1.isSameMonth)(month, toDate);
}
return false;
};
const MonthButton = ({ month, months, focus, setFocus, tabRoot, setTabRoot, }) => {
const ref = (0, react_1.useRef)(null);
const { cn } = (0, Theme_1.useRenameCSS)();
const { fromDate, toDate, locale, selected, disabled, year, onYearChange, onMonthSelect, caption, } = (0, MonthPicker_context_1.useMonthPickerContext)();
const isSelected = selected && (0, date_fns_1.isSameMonth)(month, selected);
(0, react_1.useEffect)(() => {
if (focus) {
(0, date_fns_1.isSameMonth)(month, focus) && ref.current && ref.current.focus();
setFocus();
}
}, [focus, month, setFocus]);
const isDisabled = (0, date_utils_1.isMatch)((0, date_fns_1.setYear)(month, year.getFullYear()), disabled) ||
disableMonth(month, fromDate, toDate);
const isThisMonth = (0, date_utils_1.dateIsInCurrentMonth)(month, year);
return (react_1.default.createElement("button", { ref: ref, type: "button", onClick: () => onMonthSelect === null || onMonthSelect === void 0 ? void 0 : onMonthSelect(isSelected ? undefined : month), disabled: isDisabled, "aria-pressed": !!isSelected, "data-current-month": isThisMonth, className: cn("navds-date__month-button", {
"rdp-day_today": isThisMonth,
"rdp-day_selected": isSelected,
"rdp-day_disabled": isDisabled,
}), tabIndex: tabRoot && (0, date_fns_1.isSameMonth)(month, (0, date_fns_1.setYear)(tabRoot, year.getFullYear()))
? 0
: -1, onKeyDown: (e) => {
const next = (0, MonthPicker_util_1.nextEnabled)(months, e.key, disabled, month, onYearChange, year, caption === "dropdown", fromDate, toDate);
setFocus(next);
setTabRoot(next);
}, onFocus: () => {
setTabRoot(focus);
} },
react_1.default.createElement("span", { "aria-hidden": "true" }, (0, date_fns_1.format)(new Date(month), "LLL", { locale })
.replace(".", "")
.substring(0, 3)),
react_1.default.createElement("span", { className: cn("navds-sr-only") }, (0, date_fns_1.format)(new Date(month), "LLLL", { locale }))));
};
exports.MonthButton = MonthButton;
exports.default = exports.MonthButton;
//# sourceMappingURL=MonthPicker.Button.js.map