UNPKG

@focuson/form_components

Version:

Components that can be used by @focuson/forms

73 lines (72 loc) 4.41 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LabelAndDateInput = exports.legacyParseDate = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const utils_1 = require("@focuson/utils"); const state_1 = require("@focuson/state"); const label_1 = require("./label"); const react_datepicker_1 = __importDefault(require("react-datepicker")); const date_fns_1 = require("date-fns"); const makeButtons_1 = require("./makeButtons"); const legacyParseDate = (format) => (date) => { return (0, date_fns_1.parse)(date.replace(/\//g, '-'), format.replace(/\//g, '-'), new Date()); }; exports.legacyParseDate = legacyParseDate; function LabelAndDateInput(props) { const { state, ariaLabel, id, mode, label, name, buttons, readonly, datesExcluded, fieldNameInHolidays, workingDaysInPast, workingDaysInFuture, includeWeekends, dateFormat } = props; const dateFormatL = dateFormat ? dateFormat : 'yyyy/MM/dd'; const datesToExclude = datesExcluded === null || datesExcluded === void 0 ? void 0 : datesExcluded.optJsonOr([]).map(d => d[fieldNameInHolidays ? fieldNameInHolidays : 'holiday']); const datesToExcludeAsDateType = (0, utils_1.safeArray)(datesToExclude).map(d => (0, exports.legacyParseDate)(dateFormatL)(d)); const isHoliday = (date, datesList) => { return datesList.find((d) => new Date(d).toDateString() === date.toDateString()); }; const isExcluded = (date, datesList) => { return includeWeekends ? !isHoliday(date, datesList) : (isWeekday(date) && !isHoliday(date, datesList)); }; const onChange = (date) => { if (date) state.setJson((0, date_fns_1.format)(date, dateFormatL), (0, state_1.reasonFor)('LabelAndDate', 'onChange', id)); }; const isWeekday = (date) => { const day = date.getDay(); return day !== 0 && day !== 6; }; const addDays = (date, datesList, n) => { let count = 0; let newDate = date; while (count < n) { newDate = new Date(date.setDate(date.getDate() + 1)); if (isExcluded(newDate, datesList)) count++; } return newDate; }; const subDays = (date, datesList, n) => { let count = 0; let newDate = date; while (count < n) { newDate = new Date(date.setDate(date.getDate() - 1)); if (isExcluded(newDate, datesList)) count++; } return newDate; }; const minDate = addDays(new Date(), (0, utils_1.safeArray)(datesToExclude), workingDaysInFuture ? workingDaysInFuture : 0); const selectedDateString = state.optJson(); const selectedDate = selectedDateString ? (0, exports.legacyParseDate)(dateFormatL)(selectedDateString) : undefined; let error = false; if (!(0, date_fns_1.isValid)(selectedDate)) { error = true; } const handleChangeRaw = (e) => { const date = e.currentTarget.value; state.setJson(date, (0, state_1.reasonFor)('LabelAndDate', 'onChange', id)); }; return ((0, jsx_runtime_1.jsxs)("div", Object.assign({ className: `labelAndDate ${props.labelPosition == 'Horizontal' ? 'd-flex-inline' : ''}` }, { children: [(0, jsx_runtime_1.jsx)(label_1.Label, { state: state, htmlFor: name, label: label }), (0, jsx_runtime_1.jsxs)("div", Object.assign({ className: `${props.buttons && props.buttons.length > 0 ? 'inputAndButtons' : ''}` }, { children: [(0, jsx_runtime_1.jsx)(react_datepicker_1.default, { id: id, dateFormat: dateFormatL, todayButton: "Select Today", selected: error ? null : selectedDate, onChange: (date) => onChange(date), filterDate: includeWeekends ? undefined : isWeekday, excludeDates: datesToExcludeAsDateType, minDate: minDate, highlightDates: datesToExcludeAsDateType, readOnly: mode === 'view' || readonly, // className={error ? "red-border" : ""} closeOnScroll: true, onChangeRaw: (e) => handleChangeRaw(e), placeholderText: "Select a date" }), (0, makeButtons_1.makeButtons)(props), error && (0, jsx_runtime_1.jsxs)("div", Object.assign({ className: "custom-error" }, { children: ["Invalid Date: ", state.optJsonOr('')] }))] }))] }))); } exports.LabelAndDateInput = LabelAndDateInput;