UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

87 lines (86 loc) 2.61 kB
"use client"; import _JSON$parse from "core-js-pure/stable/json/parse.js"; import React, { useCallback, useContext, useMemo } from 'react'; import { convertStringToDate } from "./DatePickerCalc.js"; import Button from "../button/Button.js"; import DatePickerContext from "./DatePickerContext.js"; import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; function DatePickerAddon(props) { const { updateDates, callOnChangeHandler, hidePicker, startDate, endDate } = useContext(DatePickerContext); const { shortcuts, renderElement } = props; const currentDates = useMemo(() => ({ date: startDate, startDate: startDate, endDate: endDate }), [startDate, endDate]); const callOnChange = useCallback(({ startDate, endDate, event = null } = {}) => { updateDates({ startDate, endDate }); callOnChangeHandler({ startDate, endDate: endDate || startDate, event }); }, [updateDates, callOnChangeHandler]); const setDate = useCallback(({ shortcut, event }) => { const usedStartDate = shortcut.date || shortcut.startDate; const usedEndDate = shortcut.endDate; const startDate = typeof usedStartDate === 'function' ? usedStartDate(currentDates) : usedStartDate ? convertStringToDate(usedStartDate) : null; const endDate = typeof usedEndDate === 'function' ? usedEndDate(currentDates) : usedEndDate ? convertStringToDate(usedEndDate) : null; callOnChange({ startDate, endDate: endDate || startDate, event }); if (shortcut.closeOnSelect) { hidePicker({ ...event, focusOnClose: true }); } }, [callOnChange, currentDates, hidePicker]); const shortcutsArray = useMemo(() => shortcuts ? typeof shortcuts === 'string' ? _JSON$parse(shortcuts) : shortcuts : [], [shortcuts]); const hasShortcuts = shortcutsArray && shortcutsArray.length > 0; const shortcutElements = useMemo(() => hasShortcuts && _jsx(_Fragment, { children: shortcutsArray.map(({ title, ...shortcut }, i) => { return _jsx(Button, { text: title, variant: "secondary", onClick: event => setDate({ shortcut, event }) }, i); }) }), [hasShortcuts, shortcutsArray, setDate]); if (!hasShortcuts && !renderElement) { return null; } return _jsxs("div", { className: "dnb-date-picker__addon", children: [renderElement, shortcutElements] }); } export default DatePickerAddon; //# sourceMappingURL=DatePickerAddon.js.map