UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

184 lines (183 loc) 6.68 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useDates; var _react = require("react"); var _DatePickerCalc = require("../DatePickerCalc.js"); var _dateFns = require("date-fns"); function useDates(dateProps, { dateFormat, isRange = false }) { const [previousDateProps, setPreviousDateProps] = (0, _react.useState)(dateProps); const [dates, setDates] = (0, _react.useState)({ ...mapDates(dateProps, { dateFormat, isRange }) }); const datesRef = (0, _react.useRef)(dates); (0, _react.useEffect)(() => { datesRef.current = dates; }, [dates]); const hasDatePropChanges = (0, _react.useMemo)(() => Object.keys(dateProps).some(date => { const dateProp = dateProps[date]; const previousDate = previousDateProps[date]; const convertedDateProp = (0, _DatePickerCalc.convertStringToDate)(dateProp, { dateFormat }); const convertedPreviousDate = (0, _DatePickerCalc.convertStringToDate)(previousDate, { dateFormat }); if (convertedDateProp instanceof Date && convertedPreviousDate instanceof Date) { return !(0, _dateFns.isSameDay)(convertedDateProp, convertedPreviousDate); } return dateProp !== previousDate; }), [dateProps, previousDateProps, dateFormat]); if (hasDatePropChanges) { const derivedDates = deriveDatesFromProps({ dates, dateProps, previousDateProps, dateFormat, isRange }); setDates(currentDates => ({ ...currentDates, ...derivedDates })); setPreviousDateProps(dateProps); } const updateDates = (0, _react.useCallback)((newDates, callback) => { const currentDates = datesRef.current; const months = updateMonths({ newDates, currentDates }); const nextDates = { ...currentDates, ...newDates, ...months }; datesRef.current = nextDates; setDates(nextDates); callback === null || callback === void 0 || callback(nextDates); }, []); return { dates, updateDates, previousDateProps }; } function mapDates(dateProps, { dateFormat, isRange }) { var _ref, _convertStringToDate, _convertStringToDate2; const date = dateProps.date; const startDate = typeof (dateProps === null || dateProps === void 0 ? void 0 : dateProps.startDate) !== 'undefined' ? getDate(dateProps.startDate, dateFormat) : typeof date !== 'undefined' ? getDate(date, dateFormat) : undefined; const endDate = !isRange ? startDate : (0, _DatePickerCalc.convertStringToDate)(dateProps === null || dateProps === void 0 ? void 0 : dateProps.endDate, { dateFormat }) || undefined; const startMonth = (_ref = (_convertStringToDate = (0, _DatePickerCalc.convertStringToDate)(dateProps.startMonth, { dateFormat })) !== null && _convertStringToDate !== void 0 ? _convertStringToDate : startDate) !== null && _ref !== void 0 ? _ref : new Date(); const hasExplicitStartMonth = typeof dateProps.startMonth !== 'undefined'; const endMonth = (_convertStringToDate2 = (0, _DatePickerCalc.convertStringToDate)(dateProps.endMonth, { dateFormat: dateFormat })) !== null && _convertStringToDate2 !== void 0 ? _convertStringToDate2 : isRange ? hasExplicitStartMonth ? (0, _dateFns.addMonths)(startMonth, 1) : endDate !== null && endDate !== void 0 ? endDate : (0, _dateFns.addMonths)(startMonth, 1) : startMonth; const minDate = (0, _DatePickerCalc.convertStringToDate)(dateProps.minDate, { dateFormat }); const maxDate = (0, _DatePickerCalc.convertStringToDate)(dateProps.maxDate, { dateFormat }); const dates = { date, startDate, endDate, startMonth, endMonth, minDate, maxDate }; return { ...dates }; } function deriveDatesFromProps({ dates, dateProps, previousDateProps, dateFormat, isRange }) { const derivedDates = {}; const startDate = getStartDate(dateProps, previousDateProps); if (typeof startDate !== 'undefined' && startDate !== dates.startDate) { derivedDates.startDate = (0, _DatePickerCalc.convertStringToDate)(startDate, { dateFormat }) || undefined; if (!isRange) { derivedDates.startMonth = (0, _DatePickerCalc.convertStringToDate)(startDate, { dateFormat }) || undefined; derivedDates.endDate = derivedDates.startDate; } } if (isRange && typeof dateProps.endDate !== 'undefined' && dateProps.endDate !== dates.endDate) { derivedDates.endDate = (0, _DatePickerCalc.convertStringToDate)(dateProps.endDate, { dateFormat }) || undefined; } if (typeof dateProps.startMonth !== 'undefined' && dateProps.startMonth !== previousDateProps.startMonth) { derivedDates.startMonth = (0, _DatePickerCalc.convertStringToDate)(dateProps.startMonth, { dateFormat }); } if (typeof dateProps.endMonth !== 'undefined' && dateProps.endMonth !== previousDateProps.endMonth) { derivedDates.endMonth = (0, _DatePickerCalc.convertStringToDate)(dateProps.endMonth, { dateFormat }); } if (typeof dateProps.minDate !== 'undefined' && dateProps.minDate !== previousDateProps.minDate) { derivedDates.minDate = (0, _DatePickerCalc.convertStringToDate)(dateProps.minDate, { dateFormat }); } if (typeof dateProps.maxDate !== 'undefined' && dateProps.maxDate !== previousDateProps.maxDate) { derivedDates.maxDate = (0, _DatePickerCalc.convertStringToDate)(dateProps.maxDate, { dateFormat }); } return derivedDates; } function updateMonths({ newDates, currentDates }) { var _ref2, _newDates$startMonth, _ref3, _newDates$endMonth; const startMonth = (_ref2 = (_newDates$startMonth = newDates.startMonth) !== null && _newDates$startMonth !== void 0 ? _newDates$startMonth : newDates.startDate) !== null && _ref2 !== void 0 ? _ref2 : currentDates.startMonth; const endMonth = (_ref3 = (_newDates$endMonth = newDates.endMonth) !== null && _newDates$endMonth !== void 0 ? _newDates$endMonth : newDates.endDate) !== null && _ref3 !== void 0 ? _ref3 : currentDates.endMonth; return { startMonth, endMonth }; } function getDate(date, dateFormat) { return date instanceof Date ? date : (0, _DatePickerCalc.convertStringToDate)(date !== null && date !== void 0 ? date : '', { dateFormat }); } function getStartDate(dateProps, previousDateProps) { if (typeof dateProps.startDate !== 'undefined' && dateProps.startDate !== previousDateProps.startDate) { return dateProps.startDate; } if (typeof dateProps.date !== 'undefined' && dateProps.date !== previousDateProps.date) { return dateProps.date; } return undefined; } //# sourceMappingURL=useDates.js.map