UNPKG

@opensig/opendesign

Version:

58 lines (57 loc) 2.15 kB
import dayjs from "dayjs"; import customParseFormat from "dayjs/plugin/customParseFormat.js"; import { isNil } from "../_utils/is.mjs"; function computeRangeState({ cell, unit, rangeStart, rangeEnd, anchorDate, hoverDate }) { if (rangeStart && rangeEnd) { return { isRangeStart: cell.isSame(rangeStart, unit), isRangeEnd: cell.isSame(rangeEnd, unit), isInRange: cell.isAfter(rangeStart, unit) && cell.isBefore(rangeEnd, unit) }; } if (anchorDate) { if (hoverDate) { const [effStart, effEnd] = hoverDate.isBefore(anchorDate, unit) ? [hoverDate, anchorDate] : [anchorDate, hoverDate]; return { isRangeStart: cell.isSame(effStart, unit), isRangeEnd: cell.isSame(effEnd, unit), isInRange: cell.isAfter(effStart, unit) && cell.isBefore(effEnd, unit) }; } return { isRangeStart: cell.isSame(anchorDate, unit), isRangeEnd: false, isInRange: false }; } return { isRangeStart: false, isRangeEnd: false, isInRange: false }; } dayjs.extend(customParseFormat); const YEAR_VIEW_STEP = 10; function parseValue(value) { if (isNil(value) || value === "") return null; if (typeof value === "number" || value instanceof Date) { const _d = dayjs(value); return _d.isValid() ? _d : null; } const d = dayjs(value); return d.isValid() ? d : null; } function isMonthDisabled(year, month, options) { const { disabledMonth, minDate, maxDate } = options; const d = dayjs().year(year).month(month); if (minDate && d.endOf("month").isBefore(minDate, "day")) return true; if (maxDate && d.startOf("month").isAfter(maxDate, "day")) return true; if (disabledMonth && disabledMonth({ date: d.toDate(), year, month })) return true; return false; } function isYearDisabled(year, options) { const { disabledYear, minDate, maxDate } = options; if (minDate && year < minDate.year()) return true; if (maxDate && year > maxDate.year()) return true; if (disabledYear && disabledYear({ date: dayjs().year(year).toDate(), year })) return true; return false; } export { YEAR_VIEW_STEP, computeRangeState, isMonthDisabled, isYearDisabled, parseValue };