| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.DatePicker = exports.yyyyMMddFormat = void 0; |
| 4 |
const tslib_1 = require("tslib"); |
| 5 |
const jsx_runtime_1 = require("react/jsx-runtime"); |
| 6 |
const react_1 = require("react"); |
| 7 |
const react_styles_1 = require("@patternfly/react-styles"); |
| 8 |
const date_picker_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/DatePicker/date-picker")); |
| 9 |
const calendar_month_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/CalendarMonth/calendar-month")); |
| 10 |
const TextInput_1 = require("../TextInput/TextInput"); |
| 11 |
const Button_1 = require("../Button"); |
| 12 |
const Popover_1 = require("../Popover/Popover"); |
| 13 |
const InputGroup_1 = require("../InputGroup"); |
| 14 |
const rh_ui_calendar_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/rh-ui-calendar-icon')); |
| 15 |
const CalendarMonth_1 = require("../CalendarMonth"); |
| 16 |
const helpers_1 = require("../../helpers"); |
| 17 |
const datetimeUtils_1 = require("../../helpers/datetimeUtils"); |
| 18 |
const HelperText_1 = require("../HelperText"); |
| 19 |
const c_date_picker__input_c_form_control_width_chars_1 = tslib_1.__importDefault(require('@patternfly/react-tokens/dist/js/c_date_picker__input_c_form_control_width_chars')); |
| 20 |
const yyyyMMddFormat = (date) => `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date |
| 21 |
.getDate() |
| 22 |
.toString() |
| 23 |
.padStart(2, '0')}`; |
| 24 |
exports.yyyyMMddFormat = yyyyMMddFormat; |
| 25 |
const DatePickerBase = (_a, ref) => { |
| 26 |
var { className, locale = undefined, dateFormat = exports.yyyyMMddFormat, dateParse = (val) => (val.split('-').length === 3 ? new Date(`${val}T00:00:00`) : new Date(undefined)), isDisabled = false, placeholder = 'YYYY-MM-DD', value: valueProp = '', 'aria-label': ariaLabel = 'Date picker', buttonAriaLabel = 'Toggle date picker', onChange = () => undefined, onBlur = () => undefined, invalidFormatText = 'Invalid date', requiredDateOptions, helperText, appendTo = 'inline', popoverProps, monthFormat, weekdayFormat, longWeekdayFormat, dayFormat, weekStart, validators = [], rangeStart, style: styleProps = {}, inputProps = {} } = _a, props = tslib_1.__rest(_a, ["className", "locale", "dateFormat", "dateParse", "isDisabled", "placeholder", "value", 'aria-label', "buttonAriaLabel", "onChange", "onBlur", "invalidFormatText", "requiredDateOptions", "helperText", "appendTo", "popoverProps", "monthFormat", "weekdayFormat", "longWeekdayFormat", "dayFormat", "weekStart", "validators", "rangeStart", "style", "inputProps"]); |
| 27 |
const [value, setValue] = (0, react_1.useState)(valueProp); |
| 28 |
const [valueDate, setValueDate] = (0, react_1.useState)(dateParse(value)); |
| 29 |
const [errorText, setErrorText] = (0, react_1.useState)(''); |
| 30 |
const [popoverOpen, setPopoverOpen] = (0, react_1.useState)(false); |
| 31 |
const [selectOpen, setSelectOpen] = (0, react_1.useState)(false); |
| 32 |
const [pristine, setPristine] = (0, react_1.useState)(true); |
| 33 |
const [textInputFocused, setTextInputFocused] = (0, react_1.useState)(false); |
| 34 |
const widthChars = (0, react_1.useMemo)(() => Math.max(dateFormat(new Date()).length, placeholder.length), [dateFormat]); |
| 35 |
const style = Object.assign({ [c_date_picker__input_c_form_control_width_chars_1.default.name]: widthChars }, styleProps); |
| 36 |
const buttonRef = (0, react_1.useRef)(undefined); |
| 37 |
const datePickerWrapperRef = (0, react_1.useRef)(undefined); |
| 38 |
const triggerRef = (0, react_1.useRef)(undefined); |
| 39 |
const dateIsRequired = (requiredDateOptions === null || requiredDateOptions === void 0 ? void 0 : requiredDateOptions.isRequired) || false; |
| 40 |
const emptyDateText = (requiredDateOptions === null || requiredDateOptions === void 0 ? void 0 : requiredDateOptions.emptyDateText) || 'Date cannot be blank'; |
| 41 |
(0, react_1.useEffect)(() => { |
| 42 |
setValue(valueProp); |
| 43 |
setValueDate(dateParse(valueProp)); |
| 44 |
}, [valueProp]); |
| 45 |
(0, react_1.useEffect)(() => { |
| 46 |
if ((0, datetimeUtils_1.isValidDate)(valueDate)) { |
| 47 |
applyValidators(valueDate); |
| 48 |
} |
| 49 |
}, [validators]); |
| 50 |
(0, react_1.useEffect)(() => { |
| 51 |
setPristine(!value); |
| 52 |
const newValueDate = dateParse(value); |
| 53 |
if (errorText && (0, datetimeUtils_1.isValidDate)(newValueDate)) { |
| 54 |
applyValidators(newValueDate); |
| 55 |
} |
| 56 |
if (value === '' && !pristine && !textInputFocused) { |
| 57 |
dateIsRequired ? setErrorText(emptyDateText) : setErrorText(''); |
| 58 |
} |
| 59 |
}, [value]); |
| 60 |
const applyValidators = (date) => { |
| 61 |
const validatorResults = validators.map((validator) => validator(date)); |
| 62 |
const validatorsToApply = validatorResults.filter((validator) => validator !== ''); |
| 63 |
setErrorText(validatorsToApply.join('\n') || ''); |
| 64 |
}; |
| 65 |
const onTextInput = (event, value) => { |
| 66 |
setValue(value); |
| 67 |
setErrorText(''); |
| 68 |
const newValueDate = dateParse(value); |
| 69 |
setValueDate(newValueDate); |
| 70 |
if ((0, datetimeUtils_1.isValidDate)(newValueDate)) { |
| 71 |
onChange(event, value, new Date(newValueDate)); |
| 72 |
} |
| 73 |
else { |
| 74 |
onChange(event, value); |
| 75 |
} |
| 76 |
}; |
| 77 |
const onInputBlur = (event) => { |
| 78 |
setTextInputFocused(false); |
| 79 |
const newValueDate = dateParse(value); |
| 80 |
const dateIsValid = (0, datetimeUtils_1.isValidDate)(newValueDate); |
| 81 |
const onBlurDateArg = dateIsValid ? new Date(newValueDate) : undefined; |
| 82 |
onBlur(event, value, onBlurDateArg); |
| 83 |
if (dateIsValid) { |
| 84 |
applyValidators(newValueDate); |
| 85 |
} |
| 86 |
if (!dateIsValid && !pristine) { |
| 87 |
setErrorText(invalidFormatText); |
| 88 |
} |
| 89 |
if (!dateIsValid && pristine && (requiredDateOptions === null || requiredDateOptions === void 0 ? void 0 : requiredDateOptions.isRequired)) { |
| 90 |
setErrorText(emptyDateText); |
| 91 |
} |
| 92 |
}; |
| 93 |
const onDateClick = (_event, newValueDate) => { |
| 94 |
const newValue = dateFormat(newValueDate); |
| 95 |
setValue(newValue); |
| 96 |
setValueDate(newValueDate); |
| 97 |
applyValidators(newValueDate); |
| 98 |
setPopoverOpen(false); |
| 99 |
onChange(null, newValue, new Date(newValueDate)); |
| 100 |
}; |
| 101 |
const onKeyPress = (ev) => { |
| 102 |
if (ev.key === 'Enter' && value) { |
| 103 |
if ((0, datetimeUtils_1.isValidDate)(valueDate)) { |
| 104 |
applyValidators(valueDate); |
| 105 |
} |
| 106 |
else { |
| 107 |
setErrorText(invalidFormatText); |
| 108 |
} |
| 109 |
} |
| 110 |
}; |
| 111 |
(0, react_1.useImperativeHandle)(ref, () => ({ |
| 112 |
setCalendarOpen: (isOpen) => setPopoverOpen(isOpen), |
| 113 |
toggleCalendar: (setOpen) => { |
| 114 |
setPopoverOpen((prev) => (setOpen !== undefined ? setOpen : !prev)); |
| 115 |
}, |
| 116 |
isCalendarOpen: popoverOpen |
| 117 |
}), [setPopoverOpen, popoverOpen, selectOpen]); |
| 118 |
const createFocusSelectorString = (modifierClass) => `.${calendar_month_1.default.calendarMonthDatesCell}.${modifierClass} .${calendar_month_1.default.calendarMonthDate}`; |
| 119 |
const focusSelectorForSelectedDate = createFocusSelectorString(calendar_month_1.default.modifiers.selected); |
| 120 |
const focusSelectorForSelectedEndRangeDate = createFocusSelectorString(`${calendar_month_1.default.modifiers.selected}.${calendar_month_1.default.modifiers.endRange}`); |
| 121 |
const focusSelectorForUnselectedDate = createFocusSelectorString(calendar_month_1.default.modifiers.current); |
| 122 |
/** |
| 123 |
* Returns a CSS selector for a date button element which will receive initial focus after opening calendar popover. |
| 124 |
* In case of a range picker it returns the end date, if it is selected, start date otherwise. |
| 125 |
* In case of a normal datepicker it returns the selected date, if present, today otherwise. |
| 126 |
*/ |
| 127 |
const getElementSelectorToFocus = () => { |
| 128 |
if ((0, datetimeUtils_1.isValidDate)(valueDate) && (0, datetimeUtils_1.isValidDate)(rangeStart)) { |
| 129 |
return focusSelectorForSelectedEndRangeDate; |
| 130 |
} |
| 131 |
if ((0, datetimeUtils_1.isValidDate)(valueDate) || (0, datetimeUtils_1.isValidDate)(rangeStart)) { |
| 132 |
return focusSelectorForSelectedDate; |
| 133 |
} |
| 134 |
return focusSelectorForUnselectedDate; |
| 135 |
}; |
| 136 |
return ((0, jsx_runtime_1.jsxs)("div", Object.assign({ className: (0, react_styles_1.css)(date_picker_1.default.datePicker, className), ref: datePickerWrapperRef, style: style }, props, { children: [(0, jsx_runtime_1.jsx)(Popover_1.Popover, Object.assign({ elementToFocus: getElementSelectorToFocus(), position: "bottom", bodyContent: (0, jsx_runtime_1.jsx)(CalendarMonth_1.CalendarMonth, { date: valueDate, onChange: onDateClick, locale: locale, |
| 137 |
// Use truthy values of strings |
| 138 |
validators: validators.map((validator) => (date) => !validator(date)), onSelectToggle: (open) => setSelectOpen(open), monthFormat: monthFormat, weekdayFormat: weekdayFormat, longWeekdayFormat: longWeekdayFormat, dayFormat: dayFormat, weekStart: weekStart, rangeStart: rangeStart }), showClose: false, isVisible: popoverOpen, shouldClose: (event, hideFunction) => { |
| 139 |
event = event; |
| 140 |
if (event.key === helpers_1.KeyTypes.Escape && selectOpen) { |
| 141 |
event.stopPropagation(); |
| 142 |
setSelectOpen(false); |
| 143 |
return false; |
| 144 |
} |
| 145 |
// Let our button handle toggling |
| 146 |
if (buttonRef.current && buttonRef.current.contains(event.target)) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
if (popoverOpen) { |
| 150 |
event.stopPropagation(); |
| 151 |
setPopoverOpen(false); |
| 152 |
hideFunction(); |
| 153 |
// If datepicker is required and the popover is opened without the text input |
| 154 |
// first receiving focus, we want to validate that the text input is not blank upon |
| 155 |
// closing the popover |
| 156 |
(requiredDateOptions === null || requiredDateOptions === void 0 ? void 0 : requiredDateOptions.isRequired) && !value && setErrorText(emptyDateText); |
| 157 |
} |
| 158 |
if (event.key === helpers_1.KeyTypes.Escape && popoverOpen) { |
| 159 |
event.stopPropagation(); |
| 160 |
} |
| 161 |
return true; |
| 162 |
}, withFocusTrap: true, hasNoPadding: true, hasAutoWidth: true, appendTo: appendTo, triggerRef: triggerRef }, popoverProps, { children: (0, jsx_runtime_1.jsx)("div", { className: date_picker_1.default.datePickerInput, ref: triggerRef, children: (0, jsx_runtime_1.jsxs)(InputGroup_1.InputGroup, { children: [(0, jsx_runtime_1.jsx)(InputGroup_1.InputGroupItem, { children: (0, jsx_runtime_1.jsx)(TextInput_1.TextInput, Object.assign({ isDisabled: isDisabled, isRequired: requiredDateOptions === null || requiredDateOptions === void 0 ? void 0 : requiredDateOptions.isRequired, "aria-label": ariaLabel, placeholder: placeholder, validated: errorText.trim() ? 'error' : 'default', value: value, onChange: onTextInput, onBlur: onInputBlur, onFocus: () => setTextInputFocused(true), onKeyPress: onKeyPress }, inputProps)) }), (0, jsx_runtime_1.jsx)(InputGroup_1.InputGroupItem, { children: (0, jsx_runtime_1.jsx)(Button_1.Button, { ref: buttonRef, variant: "control", "aria-label": buttonAriaLabel, onClick: () => setPopoverOpen(!popoverOpen), isDisabled: isDisabled, icon: (0, jsx_runtime_1.jsx)(rh_ui_calendar_icon_1.default, {}), "aria-haspopup": "dialog" }) })] }) }) })), (errorText || helperText) && ((0, jsx_runtime_1.jsx)("div", { className: date_picker_1.default.datePickerHelperText, children: errorText ? ((0, jsx_runtime_1.jsx)(HelperText_1.HelperText, { children: (0, jsx_runtime_1.jsx)(HelperText_1.HelperTextItem, { variant: "error", children: errorText }) })) : (helperText) }))] }))); |
| 163 |
}; |
| 164 |
exports.DatePicker = (0, react_1.forwardRef)(DatePickerBase); |
| 165 |
exports.DatePicker.displayName = 'DatePicker'; |
| 166 |
//# sourceMappingURL=DatePicker.js.map |