UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

179 lines (178 loc) 6.22 kB
"use client"; import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { convertStringToDate } from "../DatePickerCalc.js"; import { addMonths, isSameDay } from 'date-fns'; export default function useDates(dateProps, { dateFormat, isRange = false }) { const [previousDateProps, setPreviousDateProps] = useState(dateProps); const [dates, setDates] = useState({ ...mapDates(dateProps, { dateFormat, isRange }) }); const datesRef = useRef(dates); useEffect(() => { datesRef.current = dates; }, [dates]); 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 => ({ ...currentDates, ...derivedDates })); setPreviousDateProps(dateProps); } const updateDates = 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 : convertStringToDate(dateProps === null || dateProps === void 0 ? void 0 : dateProps.endDate, { dateFormat }) || undefined; const startMonth = (_ref = (_convertStringToDate = 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 = convertStringToDate(dateProps.endMonth, { dateFormat: dateFormat })) !== null && _convertStringToDate2 !== void 0 ? _convertStringToDate2 : isRange ? hasExplicitStartMonth ? addMonths(startMonth, 1) : endDate !== null && endDate !== void 0 ? endDate : addMonths(startMonth, 1) : startMonth; const minDate = convertStringToDate(dateProps.minDate, { dateFormat }); const maxDate = 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 = 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 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 : 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