UNPKG

@harvest-profit/npk

Version:
84 lines 4.42 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const DateInput_1 = __importDefault(require("./DateInput")); const Calendar_1 = __importDefault(require("../Calendar")); const Menu_1 = __importDefault(require("../Menu")); const Button_1 = __importDefault(require("../Button")); const regular_1 = require("@harvest-profit/npk/icons/regular"); const Group_1 = __importDefault(require("../Input/Group")); const utils_1 = require("../Calendar/utils"); const DateRangeInput = ({ picker, visibleMonths = 2, value: externalValue, onChange: onExternalChange, granularity = 'day', output = 'ISO', includeYear = true, monthAsName = false, ...props }) => { let fromFormat = v => v; let toFormat = v => v; switch (output) { case 'ISO': fromFormat = utils_1.fromISO; toFormat = utils_1.toISO; break; case 'timestamp': fromFormat = utils_1.fromTimestamp; toFormat = utils_1.toTimestamp; break; default: break; } // Ensure the value is in the correct format let value = { start: fromFormat(externalValue?.start), end: fromFormat(externalValue?.end) }; if (!value.start && !value.end) value = null; const ref = (0, react_1.useRef)(null); const [isFocused, setIsFocused] = (0, react_1.useState)(false); const onFocus = () => { setTimeout(() => { if (ref.current && !isFocused && ref.current.contains(document.activeElement)) { setIsFocused(true); if (props.onFocus) props.onFocus({ target: ref.current }); } }, 10); }; const onBlur = () => { setTimeout(() => { if (ref.current && isFocused && !ref.current.contains(document.activeElement)) { setIsFocused(false); if (props.onFocus) props.onBlur({ target: ref.current }); } }, 10); }; (0, react_1.useEffect)(() => { if (!isFocused && value?.start && value?.end && value.start > value.end) { if (onExternalChange) onExternalChange({ start: toFormat(value.end), end: toFormat(value.start) }); } }, [isFocused, value?.start, value?.end]); const onChangeStart = (newValue) => { if (onExternalChange) { onExternalChange({ start: toFormat(newValue), end: toFormat(value?.end), }); } }; const onChangeEnd = (newValue) => { if (onExternalChange) { onExternalChange({ start: toFormat(value?.start), end: toFormat(newValue), }); } }; // If you provide a "picker" prop, we will show a calendar const extraProps = {}; if (picker) { extraProps.trailingVisual = ((0, jsx_runtime_1.jsxs)(Menu_1.default, { arrow: true, placement: "bottom", autoDismiss: false, children: [(0, jsx_runtime_1.jsx)(Button_1.default, { invisible: true, icon: regular_1.CalendarIcon, "aria-label": "Pick a date range", tabIndex: -1 }), (0, jsx_runtime_1.jsx)(Menu_1.default.Overlay, { children: (0, jsx_runtime_1.jsx)(Calendar_1.default, { visibleMonths: visibleMonths, range: true, value: value, onChange: onExternalChange, output: "date", ...props }) })] })); } return ((0, jsx_runtime_1.jsxs)(Group_1.default, { containsSegments: true, contentsRef: ref, ...props, ...extraProps, children: [(0, jsx_runtime_1.jsx)(DateInput_1.default, { output: "date", excludeGroup: true, onChange: onChangeStart, value: value?.start, granularity: granularity, variant: "plain", onFocus: onFocus, onBlur: onBlur, includeYear: includeYear, monthAsName: monthAsName }), (0, jsx_runtime_1.jsx)("span", { style: { padding: '0 10px' }, children: "\u2013" }), (0, jsx_runtime_1.jsx)(DateInput_1.default, { output: "date", excludeGroup: true, onChange: onChangeEnd, value: value?.end, granularity: granularity, variant: "plain", onFocus: onFocus, onBlur: onBlur, includeYear: includeYear, monthAsName: monthAsName })] })); }; exports.default = DateRangeInput; //# sourceMappingURL=DateRangeInput.js.map