UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

218 lines (217 loc) 7.2 kB
"use client"; import { useCallback, useMemo, useState } from 'react'; import { convertStringToDate, isDisabled } from "../DatePickerCalc.js"; import { addMonths, isSameDay } from 'date-fns'; export default function useDates(dateProps, { dateFormat, isRange = false, shouldCorrectDate = false }) { const [previousDateProps, setPreviousDateProps] = useState(dateProps); const [dates, setDates] = useState({ ...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 => ({ ...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 { ...currentDates, ...newDates, ...months, ...correctedDates }; }); callback === null || callback === void 0 || callback({ ...dates, ...newDates, ...months, ...correctedDates }); }, [dates, shouldCorrectDate, isRange]); return { dates, updateDates, previousDateProps }; } function mapDates(dateProps, { dateFormat, isRange, shouldCorrectDate }) { 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 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 = { date, startDate, endDate, startMonth, endMonth, minDate, maxDate, ...correctedDates }; 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 correctDates({ startDate, endDate, minDate, maxDate, isRange }) { 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({ 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