UNPKG

@zenithui/day-picker

Version:

A ZenithUi Time Picker is React component enables users to select a date or date range from calendar.

66 lines (65 loc) 2.54 kB
import * as __WEBPACK_EXTERNAL_MODULE_date_fns_925206bc__ from "date-fns"; function getInitialDate(selected) { if (selected instanceof Date) return selected; if (selected && "from" in selected && selected.from) return selected.from; return new Date(); } function getInitialRange(selected) { if (!selected) return { from: null, to: null }; if (selected instanceof Date) return { from: selected, to: selected }; if ("from" in selected && "to" in selected) return { from: selected.from, to: selected.to ?? null }; return { from: null, to: null }; } function isBetweenRange(day, range, focus) { if (!range.from || !focus) return false; if ((0, __WEBPACK_EXTERNAL_MODULE_date_fns_925206bc__.isAfter)(focus, range.from)) return (0, __WEBPACK_EXTERNAL_MODULE_date_fns_925206bc__.isWithinInterval)(day, { start: range.from, end: focus }); if ((0, __WEBPACK_EXTERNAL_MODULE_date_fns_925206bc__.isBefore)(focus, range.from)) return (0, __WEBPACK_EXTERNAL_MODULE_date_fns_925206bc__.isWithinInterval)(day, { start: focus, end: range.from }); return false; } function parseDate(date) { return "string" == typeof date ? new Date(date) : date; } function getDisabled(current, disable) { if (!disable) return false; if (disable.before && (0, __WEBPACK_EXTERNAL_MODULE_date_fns_925206bc__.isBefore)(current, parseDate(disable.before))) return true; if (disable.after && (0, __WEBPACK_EXTERNAL_MODULE_date_fns_925206bc__.isAfter)(current, parseDate(disable.after))) return true; if (disable.date && (0, __WEBPACK_EXTERNAL_MODULE_date_fns_925206bc__.isSameDay)(current, parseDate(disable.date))) return true; if (disable?.dates?.some((date)=>__WEBPACK_EXTERNAL_MODULE_date_fns_925206bc__.isSameDay(current, parseDate(date)))) return true; if (disable.days) { const currentDay = current.getDay(); const dayNames = [ "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" ]; if (disable.days.some((day)=>{ if ("number" == typeof day) return currentDay === day; return dayNames.indexOf(day.toLowerCase()) === currentDay; })) return true; } if (disable.modifier?.(current)) return true; return false; } export { getDisabled, getInitialDate, getInitialRange, isBetweenRange, parseDate };