UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

209 lines (208 loc) 8.23 kB
"use client"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import "core-js/modules/web.dom-collections.iterator.js"; import { useCallback, useMemo, useState } from 'react'; import { convertStringToDate, isDisabled } from '../DatePickerCalc'; import { addMonths, isSameDay } from 'date-fns'; export default function useDates(dateProps, _ref) { let { dateFormat, isRange = false, shouldCorrectDate = false } = _ref; const [previousDateProps, setPreviousDateProps] = useState(dateProps); const [dates, setDates] = useState(_objectSpread({}, mapDates(dateProps, { dateFormat, isRange, shouldCorrectDate }))); const hasDatePropChanges = useMemo(() => Object.keys(dateProps).some(date => { const dateProp = dateProps[date]; const previousDate = previousDateProps[date]; const convertedDateProp = convertStringToDate(dateProp, { dateFormat }); const convertedPreviousDate = convertStringToDate(previousDate, { dateFormat }); if (convertedDateProp instanceof Date && convertedPreviousDate instanceof Date) { return !isSameDay(convertedDateProp, convertedPreviousDate); } return dateProp !== previousDate; }), [dateProps, previousDateProps, dateFormat]); if (hasDatePropChanges) { const derivedDates = deriveDatesFromProps({ dates, dateProps, previousDateProps, dateFormat, isRange }); setDates(currentDates => _objectSpread(_objectSpread({}, currentDates), derivedDates)); setPreviousDateProps(dateProps); } const updateDates = useCallback((newDates, callback) => { var _newDates$startDate, _newDates$endDate; const correctedDates = shouldCorrectDate ? correctDates({ startDate: (_newDates$startDate = newDates.startDate) !== null && _newDates$startDate !== void 0 ? _newDates$startDate : dates.startDate, endDate: (_newDates$endDate = newDates.endDate) !== null && _newDates$endDate !== void 0 ? _newDates$endDate : dates.endDate, minDate: dates.minDate, maxDate: dates.maxDate, isRange }) : {}; const months = updateMonths({ newDates, currentDates: dates }); setDates(currentDates => { return _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, currentDates), newDates), months), correctedDates); }); callback === null || callback === void 0 ? void 0 : callback(_objectSpread(_objectSpread(_objectSpread(_objectSpread({}, dates), newDates), months), correctedDates)); }, [dates, shouldCorrectDate, isRange]); return { dates, updateDates, previousDateProps }; } function mapDates(dateProps, _ref2) { var _ref3, _convertStringToDate, _convertStringToDate2; let { dateFormat, isRange, shouldCorrectDate } = _ref2; 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 : convertStringToDate(dateProps === null || dateProps === void 0 ? void 0 : dateProps.endDate, { dateFormat }) || undefined; const startMonth = (_ref3 = (_convertStringToDate = convertStringToDate(dateProps.startMonth, { dateFormat })) !== null && _convertStringToDate !== void 0 ? _convertStringToDate : startDate) !== null && _ref3 !== void 0 ? _ref3 : new Date(); const endMonth = (_convertStringToDate2 = convertStringToDate(dateProps.endMonth, { dateFormat: dateFormat })) !== null && _convertStringToDate2 !== void 0 ? _convertStringToDate2 : isRange ? endDate !== null && endDate !== void 0 ? endDate : addMonths(startMonth, 1) : startMonth; const minDate = convertStringToDate(dateProps.minDate, { dateFormat }); const maxDate = convertStringToDate(dateProps.maxDate, { dateFormat }); const correctedDates = shouldCorrectDate ? correctDates({ startDate, endDate, minDate, maxDate, isRange }) : {}; const dates = _objectSpread({ date, startDate, endDate, startMonth, endMonth, minDate, maxDate }, correctedDates); return _objectSpread({}, dates); } function deriveDatesFromProps(_ref4) { let { dates, dateProps, previousDateProps, dateFormat, isRange } = _ref4; const derivedDates = {}; const startDate = getStartDate(dateProps, previousDateProps); if (typeof startDate !== 'undefined' && startDate !== dates.startDate) { derivedDates.startDate = convertStringToDate(startDate, { dateFormat }) || undefined; if (!isRange) { derivedDates.startMonth = convertStringToDate(startDate, { dateFormat }) || undefined; derivedDates.endDate = derivedDates.startDate; } } if (isRange && typeof dateProps.endDate !== 'undefined' && dateProps.endDate !== dates.endDate) { derivedDates.endDate = convertStringToDate(dateProps.endDate, { dateFormat }) || undefined; } if (typeof dateProps.startMonth !== 'undefined' && dateProps.startMonth !== previousDateProps.startMonth) { derivedDates.startMonth = convertStringToDate(dateProps.startMonth, { dateFormat }); } if (typeof dateProps.endMonth !== 'undefined' && dateProps.endMonth !== previousDateProps.endMonth) { derivedDates.endMonth = convertStringToDate(dateProps.endMonth, { dateFormat }); } if (typeof dateProps.minDate !== 'undefined' && dateProps.minDate !== previousDateProps.minDate) { derivedDates.minDate = convertStringToDate(dateProps.minDate, { dateFormat }); } if (typeof dateProps.maxDate !== 'undefined' && dateProps.maxDate !== previousDateProps.maxDate) { derivedDates.maxDate = convertStringToDate(dateProps.maxDate, { dateFormat }); } return derivedDates; } function correctDates(_ref5) { let { startDate, endDate, minDate, maxDate, isRange } = _ref5; const correctedDates = {}; if (isDisabled(startDate, minDate, maxDate)) { correctedDates['startDate'] = minDate; } if (isDisabled(endDate, minDate, maxDate)) { if (!isRange && !minDate) { correctedDates['startDate'] = maxDate; } else { correctedDates['endDate'] = maxDate; } } return correctedDates; } function updateMonths(_ref6) { var _ref7, _newDates$startMonth, _ref8, _newDates$endMonth; let { newDates, currentDates } = _ref6; const startMonth = (_ref7 = (_newDates$startMonth = newDates.startMonth) !== null && _newDates$startMonth !== void 0 ? _newDates$startMonth : newDates.startDate) !== null && _ref7 !== void 0 ? _ref7 : currentDates.startMonth; const endMonth = (_ref8 = (_newDates$endMonth = newDates.endMonth) !== null && _newDates$endMonth !== void 0 ? _newDates$endMonth : newDates.endDate) !== null && _ref8 !== void 0 ? _ref8 : currentDates.endMonth; return { startMonth, endMonth }; } function getDate(date, dateFormat) { return date instanceof Date ? date : 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