UNPKG

@wordpress/components

Version:
151 lines (150 loc) • 6.51 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let date_fns = require("date-fns"); let _wordpress_element = require("@wordpress/element"); // packages/components/src/date-time/date-picker/use-lilius/index.ts /** * This source is a local copy of the use-lilius library, since the original * library is not actively maintained. * @see https://github.com/WordPress/gutenberg/discussions/64968 * * use-lilius@2.0.5 * https://github.com/Avarios/use-lilius * * The MIT License (MIT) * * Copyright (c) 2021-Present Danny Tatom * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ let Month = /* @__PURE__ */ function(Month) { Month[Month["JANUARY"] = 0] = "JANUARY"; Month[Month["FEBRUARY"] = 1] = "FEBRUARY"; Month[Month["MARCH"] = 2] = "MARCH"; Month[Month["APRIL"] = 3] = "APRIL"; Month[Month["MAY"] = 4] = "MAY"; Month[Month["JUNE"] = 5] = "JUNE"; Month[Month["JULY"] = 6] = "JULY"; Month[Month["AUGUST"] = 7] = "AUGUST"; Month[Month["SEPTEMBER"] = 8] = "SEPTEMBER"; Month[Month["OCTOBER"] = 9] = "OCTOBER"; Month[Month["NOVEMBER"] = 10] = "NOVEMBER"; Month[Month["DECEMBER"] = 11] = "DECEMBER"; return Month; }({}); let Day = /* @__PURE__ */ function(Day) { Day[Day["SUNDAY"] = 0] = "SUNDAY"; Day[Day["MONDAY"] = 1] = "MONDAY"; Day[Day["TUESDAY"] = 2] = "TUESDAY"; Day[Day["WEDNESDAY"] = 3] = "WEDNESDAY"; Day[Day["THURSDAY"] = 4] = "THURSDAY"; Day[Day["FRIDAY"] = 5] = "FRIDAY"; Day[Day["SATURDAY"] = 6] = "SATURDAY"; return Day; }({}); const inRange = (date, min, max) => ((0, date_fns.isEqual)(date, min) || (0, date_fns.isAfter)(date, min)) && ((0, date_fns.isEqual)(date, max) || (0, date_fns.isBefore)(date, max)); const clearTime = (date) => (0, date_fns.set)(date, { hours: 0, minutes: 0, seconds: 0, milliseconds: 0 }); const useLilius = ({ weekStartsOn = 0, viewing: initialViewing = /* @__PURE__ */ new Date(), selected: initialSelected = [], numberOfMonths = 1 } = {}) => { const [viewing, setViewing] = (0, _wordpress_element.useState)(initialViewing); const viewToday = (0, _wordpress_element.useCallback)(() => setViewing((0, date_fns.startOfToday)()), [setViewing]); const viewMonth = (0, _wordpress_element.useCallback)((month) => setViewing((v) => (0, date_fns.setMonth)(v, month)), []); const viewPreviousMonth = (0, _wordpress_element.useCallback)(() => setViewing((v) => (0, date_fns.subMonths)(v, 1)), []); const viewNextMonth = (0, _wordpress_element.useCallback)(() => setViewing((v) => (0, date_fns.addMonths)(v, 1)), []); const viewYear = (0, _wordpress_element.useCallback)((year) => setViewing((v) => (0, date_fns.setYear)(v, year)), []); const viewPreviousYear = (0, _wordpress_element.useCallback)(() => setViewing((v) => (0, date_fns.subYears)(v, 1)), []); const viewNextYear = (0, _wordpress_element.useCallback)(() => setViewing((v) => (0, date_fns.addYears)(v, 1)), []); const [selected, setSelected] = (0, _wordpress_element.useState)(initialSelected.map(clearTime)); const clearSelected = () => setSelected([]); const isSelected = (0, _wordpress_element.useCallback)((date) => selected.findIndex((s) => (0, date_fns.isEqual)(s, date)) > -1, [selected]); const select = (0, _wordpress_element.useCallback)((date, replaceExisting) => { if (replaceExisting) setSelected(Array.isArray(date) ? date : [date]); else setSelected((selectedItems) => selectedItems.concat(Array.isArray(date) ? date : [date])); }, []); const deselect = (0, _wordpress_element.useCallback)((date) => setSelected((selectedItems) => Array.isArray(date) ? selectedItems.filter((s) => !date.map((d) => d.getTime()).includes(s.getTime())) : selectedItems.filter((s) => !(0, date_fns.isEqual)(s, date))), []); const toggle = (0, _wordpress_element.useCallback)((date, replaceExisting) => isSelected(date) ? deselect(date) : select(date, replaceExisting), [ deselect, isSelected, select ]); const selectRange = (0, _wordpress_element.useCallback)((start, end, replaceExisting) => { if (replaceExisting) setSelected((0, date_fns.eachDayOfInterval)({ start, end })); else setSelected((selectedItems) => selectedItems.concat((0, date_fns.eachDayOfInterval)({ start, end }))); }, []); const deselectRange = (0, _wordpress_element.useCallback)((start, end) => { setSelected((selectedItems) => selectedItems.filter((s) => !(0, date_fns.eachDayOfInterval)({ start, end }).map((d) => d.getTime()).includes(s.getTime()))); }, []); const calendar = (0, _wordpress_element.useMemo)(() => (0, date_fns.eachMonthOfInterval)({ start: (0, date_fns.startOfMonth)(viewing), end: (0, date_fns.endOfMonth)((0, date_fns.addMonths)(viewing, numberOfMonths - 1)) }).map((month) => (0, date_fns.eachWeekOfInterval)({ start: (0, date_fns.startOfMonth)(month), end: (0, date_fns.endOfMonth)(month) }, { weekStartsOn }).map((week) => (0, date_fns.eachDayOfInterval)({ start: (0, date_fns.startOfWeek)(week, { weekStartsOn }), end: (0, date_fns.endOfWeek)(week, { weekStartsOn }) }))), [ viewing, weekStartsOn, numberOfMonths ]); return { clearTime, inRange, viewing, setViewing, viewToday, viewMonth, viewPreviousMonth, viewNextMonth, viewYear, viewPreviousYear, viewNextYear, selected, setSelected, clearSelected, isSelected, select, deselect, toggle, selectRange, deselectRange, calendar }; }; exports.Day = Day; exports.Month = Month; exports.useLilius = useLilius; //# sourceMappingURL=index.cjs.map