@react-stately/calendar 3.9.2 → 3.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +0,0 @@
1
- {"mappings":";;;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;;AAoDM,SAAS,0CAAkD,KAA8B;IAC9F,IAAI,mBAAmB,CAAA,GAAA,oBAAM,EAAE,IAAM,IAAI,CAAA,GAAA,0CAAY,EAAE,MAAM,MAAM,GAAG;QAAC,MAAM,MAAM;KAAC;IACpF,IAAI,kBAAkB,CAAA,GAAA,oBAAM,EAAE,IAAM,iBAAiB,eAAe,IAAI;QAAC;KAAiB;IAC1F,IAAI,UACF,MAAM,kBACN,cAAc,mBACd,kBAAkB;QAAC,QAAQ;IAAC,aAC5B,QAAQ,YACR,QAAQ,sBACR,kBAAkB,qBAClB,iBAAiB,gBACjB,eAAe,2BACf,cAAc,EACf,GAAG;IACJ,IAAI,WAAW,CAAA,GAAA,oBAAM,EAAE,IAAM,eAAe,gBAAgB,QAAQ,GAAyB;QAAC;QAAgB,gBAAgB,QAAQ;KAAC;QAE9B;IAAzG,IAAI,CAAC,OAAO,mBAAmB,GAAG,CAAA,GAAA,2CAAiB,EAAwC,MAAM,KAAK,EAAG,CAAA,sBAAA,MAAM,YAAY,cAAlB,iCAAA,sBAAsB,MAAO,MAAM,QAAQ;IACpJ,IAAI,oBAAoB,CAAA,GAAA,oBAAM,EAAE,IAAM,QAAQ,CAAA,GAAA,uCAAS,EAAE,CAAA,GAAA,2CAAa,EAAE,QAAQ,YAAY,MAAM;QAAC;QAAO;KAAS;IACnH,IAAI,WAAW,CAAA,GAAA,oBAAM,EAAE,IAAM,SAAS,cAAc,QAAQ,MAAM,QAAQ,GAAG,gBAAgB,QAAQ,EAAE;QAAC;QAAO,gBAAgB,QAAQ;KAAC;IACxI,IAAI,sBAAsB,CAAA,GAAA,oBAAM,EAAE,IAChC,MAAM,YAAY,GACd,CAAA,GAAA,wCAA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AAO,SAAS;YACxC,IAAI,OAAO,KAAK,GAAG,CAAC;gBAAC,OAAO;YAAS;YACrC,IAAI,QAAiC,EAAE;YAEvC,OAAO,CAAA,GAAA,wCAAU,EAAE,MAAM,QAAQ;YAEjC,mFAAmF;YACnF,sEAAsE;YACtE,IAAI,YAAY,CAAA,GAAA,yCAAW,EAAE,MAAM,QAAQ;YAC3C,IAAK,IAAI,IAAI,GAAG,IAAI,WAAW,IAC7B,MAAM,IAAI,CAAC;YAGb,MAAO,MAAM,MAAM,GAAG,EAAG;gBACvB,MAAM,IAAI,CAAC;gBACX,IAAI,WAAW,KAAK,GAAG,CAAC;oBAAC,MAAM;gBAAC;gBAChC,IAAI,CAAA,GAAA,sCAAQ,EAAE,MAAM,WAElB;gBAEF,OAAO;YACT;YAEA,8DAA8D;YAC9D,MAAO,MAAM,MAAM,GAAG,EACpB,MAAM,IAAI,CAAC;YAGb,OAAO;QACT;IACF;AACF;AAEA,SAAS,mCAAa,QAAsB;IAC1C,IAAI,OAAO;QAAC,GAAG,QAAQ;IAAA;IACvB,IAAK,IAAI,OAAO,SACd,IAAI,CAAC,IAAI,GAAG;IAEd,OAAO;AACT","sources":["packages/@react-stately/calendar/src/useCalendarState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {alignCenter, alignEnd, alignStart, constrainStart, constrainValue, isInvalid, previousAvailableDate} from './utils';\nimport {\n Calendar,\n CalendarDate,\n CalendarIdentifier,\n DateDuration,\n DateFormatter,\n endOfMonth,\n endOfWeek,\n getDayOfWeek,\n GregorianCalendar,\n isEqualCalendar,\n isSameDay,\n startOfMonth,\n startOfWeek,\n toCalendar,\n toCalendarDate,\n today\n} from '@internationalized/date';\nimport {CalendarProps, DateValue, MappedDateValue} from '@react-types/calendar';\nimport {CalendarState} from './types';\nimport {useControlledState} from '@react-stately/utils';\nimport {useMemo, useState} from 'react';\nimport {ValidationState} from '@react-types/shared';\n\nexport interface CalendarStateOptions<T extends DateValue = DateValue> extends CalendarProps<T> {\n /** The locale to display and edit the value according to. */\n locale: string,\n /**\n * A function that creates a [Calendar](../internationalized/date/Calendar.html)\n * object for a given calendar identifier. Such a function may be imported from the\n * `@internationalized/date` package, or manually implemented to include support for\n * only certain calendars.\n */\n createCalendar: (name: CalendarIdentifier) => Calendar,\n /**\n * The amount of days that will be displayed at once. This affects how pagination works.\n * @default {months: 1}\n */\n visibleDuration?: DateDuration,\n /** \n * Determines the alignment of the visible months on initial render based on the current selection or current date if there is no selection. \n * @default 'center'\n */\n selectionAlignment?: 'start' | 'center' | 'end'\n}\n/**\n * Provides state management for a calendar component.\n * A calendar displays one or more date grids and allows users to select a single date.\n */\nexport function useCalendarState<T extends DateValue = DateValue>(props: CalendarStateOptions<T>): CalendarState {\n let defaultFormatter = useMemo(() => new DateFormatter(props.locale), [props.locale]);\n let resolvedOptions = useMemo(() => defaultFormatter.resolvedOptions(), [defaultFormatter]);\n let {\n locale,\n createCalendar,\n visibleDuration = {months: 1},\n minValue,\n maxValue,\n selectionAlignment,\n isDateUnavailable,\n pageBehavior = 'visible',\n firstDayOfWeek\n } = props;\n let calendar = useMemo(() => createCalendar(resolvedOptions.calendar as CalendarIdentifier), [createCalendar, resolvedOptions.calendar]);\n\n let [value, setControlledValue] = useControlledState<DateValue | null, MappedDateValue<T>>(props.value!, props.defaultValue ?? null!, props.onChange);\n let calendarDateValue = useMemo(() => value ? toCalendar(toCalendarDate(value), calendar) : null, [value, calendar]);\n let timeZone = useMemo(() => value && 'timeZone' in value ? value.timeZone : resolvedOptions.timeZone, [value, resolvedOptions.timeZone]);\n let focusedCalendarDate = useMemo(() => (\n props.focusedValue\n ? constrainValue(toCalendar(toCalendarDate(props.focusedValue), calendar), minValue, maxValue)\n : undefined\n ), [props.focusedValue, calendar, minValue, maxValue]);\n let defaultFocusedCalendarDate = useMemo(() => (\n constrainValue(\n props.defaultFocusedValue\n ? toCalendar(toCalendarDate(props.defaultFocusedValue), calendar)\n : calendarDateValue || toCalendar(today(timeZone), calendar),\n minValue,\n maxValue\n )\n ), [props.defaultFocusedValue, calendarDateValue, timeZone, calendar, minValue, maxValue]);\n let [focusedDate, setFocusedDate] = useControlledState(focusedCalendarDate, defaultFocusedCalendarDate, props.onFocusChange);\n let [startDate, setStartDate] = useState(() => {\n switch (selectionAlignment) {\n case 'start':\n return alignStart(focusedDate, visibleDuration, locale, minValue, maxValue);\n case 'end':\n return alignEnd(focusedDate, visibleDuration, locale, minValue, maxValue);\n case 'center':\n default:\n return alignCenter(focusedDate, visibleDuration, locale, minValue, maxValue);\n }\n });\n let [isFocused, setFocused] = useState(props.autoFocus || false);\n\n let endDate = useMemo(() => {\n let duration = {...visibleDuration};\n if (duration.days) {\n duration.days--;\n } else {\n duration.days = -1;\n }\n return startDate.add(duration);\n }, [startDate, visibleDuration]);\n\n // Reset focused date and visible range when calendar changes.\n let [lastCalendar, setLastCalendar] = useState(calendar);\n if (!isEqualCalendar(calendar, lastCalendar)) {\n let newFocusedDate = toCalendar(focusedDate, calendar);\n setStartDate(alignCenter(newFocusedDate, visibleDuration, locale, minValue, maxValue));\n setFocusedDate(newFocusedDate);\n setLastCalendar(calendar);\n }\n\n if (isInvalid(focusedDate, minValue, maxValue)) {\n // If the focused date was moved to an invalid value, it can't be focused, so constrain it.\n setFocusedDate(constrainValue(focusedDate, minValue, maxValue));\n } else if (focusedDate.compare(startDate) < 0) {\n setStartDate(alignEnd(focusedDate, visibleDuration, locale, minValue, maxValue));\n } else if (focusedDate.compare(endDate) > 0) {\n setStartDate(alignStart(focusedDate, visibleDuration, locale, minValue, maxValue));\n }\n\n // Sets focus to a specific cell date\n function focusCell(date: CalendarDate) {\n date = constrainValue(date, minValue, maxValue);\n setFocusedDate(date);\n }\n\n function setValue(newValue: CalendarDate | null) {\n if (!props.isDisabled && !props.isReadOnly) {\n let localValue = newValue;\n if (localValue === null) {\n setControlledValue(null);\n return;\n }\n localValue = constrainValue(localValue, minValue, maxValue);\n localValue = previousAvailableDate(localValue, startDate, isDateUnavailable);\n if (!localValue) {\n return;\n }\n\n // The display calendar should not have any effect on the emitted value.\n // Emit dates in the same calendar as the original value, if any, otherwise gregorian.\n localValue = toCalendar(localValue, value?.calendar || new GregorianCalendar());\n\n // Preserve time if the input value had one.\n if (value && 'hour' in value) {\n setControlledValue(value.set(localValue));\n } else {\n setControlledValue(localValue);\n }\n }\n }\n\n let isUnavailable = useMemo(() => {\n if (!calendarDateValue) {\n return false;\n }\n\n if (isDateUnavailable && isDateUnavailable(calendarDateValue)) {\n return true;\n }\n\n return isInvalid(calendarDateValue, minValue, maxValue);\n }, [calendarDateValue, isDateUnavailable, minValue, maxValue]);\n let isValueInvalid = props.isInvalid || props.validationState === 'invalid' || isUnavailable;\n let validationState: ValidationState | null = isValueInvalid ? 'invalid' : null;\n\n let pageDuration = useMemo(() => {\n if (pageBehavior === 'visible') {\n return visibleDuration;\n }\n\n return unitDuration(visibleDuration);\n }, [pageBehavior, visibleDuration]);\n\n return {\n isDisabled: props.isDisabled ?? false,\n isReadOnly: props.isReadOnly ?? false,\n value: calendarDateValue,\n setValue,\n visibleRange: {\n start: startDate,\n end: endDate\n },\n minValue,\n maxValue,\n focusedDate,\n timeZone,\n validationState,\n isValueInvalid,\n setFocusedDate(date) {\n focusCell(date);\n setFocused(true);\n },\n focusNextDay() {\n focusCell(focusedDate.add({days: 1}));\n },\n focusPreviousDay() {\n focusCell(focusedDate.subtract({days: 1}));\n },\n focusNextRow() {\n if (visibleDuration.days) {\n this.focusNextPage();\n } else if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) {\n focusCell(focusedDate.add({weeks: 1}));\n }\n },\n focusPreviousRow() {\n if (visibleDuration.days) {\n this.focusPreviousPage();\n } else if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) {\n focusCell(focusedDate.subtract({weeks: 1}));\n }\n },\n focusNextPage() {\n let start = startDate.add(pageDuration);\n setFocusedDate(constrainValue(focusedDate.add(pageDuration), minValue, maxValue));\n setStartDate(\n alignStart(\n constrainStart(focusedDate, start, pageDuration, locale, minValue, maxValue),\n pageDuration,\n locale\n )\n );\n },\n focusPreviousPage() {\n let start = startDate.subtract(pageDuration);\n setFocusedDate(constrainValue(focusedDate.subtract(pageDuration), minValue, maxValue));\n setStartDate(\n alignStart(\n constrainStart(focusedDate, start, pageDuration, locale, minValue, maxValue),\n pageDuration,\n locale\n )\n );\n },\n focusSectionStart() {\n if (visibleDuration.days) {\n focusCell(startDate);\n } else if (visibleDuration.weeks) {\n focusCell(startOfWeek(focusedDate, locale));\n } else if (visibleDuration.months || visibleDuration.years) {\n focusCell(startOfMonth(focusedDate));\n }\n },\n focusSectionEnd() {\n if (visibleDuration.days) {\n focusCell(endDate);\n } else if (visibleDuration.weeks) {\n focusCell(endOfWeek(focusedDate, locale));\n } else if (visibleDuration.months || visibleDuration.years) {\n focusCell(endOfMonth(focusedDate));\n }\n },\n focusNextSection(larger) {\n if (!larger && !visibleDuration.days) {\n focusCell(focusedDate.add(unitDuration(visibleDuration)));\n return;\n }\n\n if (visibleDuration.days) {\n this.focusNextPage();\n } else if (visibleDuration.weeks) {\n focusCell(focusedDate.add({months: 1}));\n } else if (visibleDuration.months || visibleDuration.years) {\n focusCell(focusedDate.add({years: 1}));\n }\n },\n focusPreviousSection(larger) {\n if (!larger && !visibleDuration.days) {\n focusCell(focusedDate.subtract(unitDuration(visibleDuration)));\n return;\n }\n\n if (visibleDuration.days) {\n this.focusPreviousPage();\n } else if (visibleDuration.weeks) {\n focusCell(focusedDate.subtract({months: 1}));\n } else if (visibleDuration.months || visibleDuration.years) {\n focusCell(focusedDate.subtract({years: 1}));\n }\n },\n selectFocusedDate() {\n if (!(isDateUnavailable && isDateUnavailable(focusedDate))) {\n setValue(focusedDate);\n }\n },\n selectDate(date) {\n setValue(date);\n },\n isFocused,\n setFocused,\n isInvalid(date) {\n return isInvalid(date, minValue, maxValue);\n },\n isSelected(date) {\n return calendarDateValue != null && isSameDay(date, calendarDateValue) && !this.isCellDisabled(date) && !this.isCellUnavailable(date);\n },\n isCellFocused(date) {\n return isFocused && focusedDate && isSameDay(date, focusedDate);\n },\n isCellDisabled(date) {\n return props.isDisabled || date.compare(startDate) < 0 || date.compare(endDate) > 0 || this.isInvalid(date);\n },\n isCellUnavailable(date) {\n return props.isDateUnavailable ? props.isDateUnavailable(date) : false;\n },\n isPreviousVisibleRangeInvalid() {\n let prev = startDate.subtract({days: 1});\n return isSameDay(prev, startDate) || this.isInvalid(prev);\n },\n isNextVisibleRangeInvalid() {\n // Adding may return the same date if we reached the end of time\n // according to the calendar system (e.g. 9999-12-31).\n let next = endDate.add({days: 1});\n return isSameDay(next, endDate) || this.isInvalid(next);\n },\n getDatesInWeek(weekIndex, from = startDate) {\n let date = from.add({weeks: weekIndex});\n let dates: (CalendarDate | null)[] = [];\n\n date = startOfWeek(date, locale, firstDayOfWeek);\n \n // startOfWeek will clamp dates within the calendar system's valid range, which may\n // start in the middle of a week. In this case, add null placeholders.\n let dayOfWeek = getDayOfWeek(date, locale, firstDayOfWeek);\n for (let i = 0; i < dayOfWeek; i++) {\n dates.push(null);\n }\n\n while (dates.length < 7) {\n dates.push(date);\n let nextDate = date.add({days: 1});\n if (isSameDay(date, nextDate)) {\n // If the next day is the same, we have hit the end of the calendar system.\n break;\n }\n date = nextDate;\n }\n\n // Add null placeholders if at the end of the calendar system.\n while (dates.length < 7) {\n dates.push(null);\n }\n\n return dates;\n }\n };\n}\n\nfunction unitDuration(duration: DateDuration) {\n let unit = {...duration};\n for (let key in duration) {\n unit[key] = 1;\n }\n return unit;\n}\n"],"names":[],"version":3,"file":"useCalendarState.main.js.map"}
@@ -1,295 +0,0 @@
1
- import {alignCenter as $f62d864046160412$export$f4a51ff076cc9a09, alignEnd as $f62d864046160412$export$530edbfc915b2b04, alignStart as $f62d864046160412$export$144a00ba6044eb9, constrainStart as $f62d864046160412$export$5bb865b12696a77d, constrainValue as $f62d864046160412$export$4f5203c0d889109e, isInvalid as $f62d864046160412$export$eac50920cf2fd59a, previousAvailableDate as $f62d864046160412$export$a1d3911297b952d7} from "./utils.mjs";
2
- import {DateFormatter as $7G4ZY$DateFormatter, toCalendar as $7G4ZY$toCalendar, toCalendarDate as $7G4ZY$toCalendarDate, today as $7G4ZY$today, isEqualCalendar as $7G4ZY$isEqualCalendar, GregorianCalendar as $7G4ZY$GregorianCalendar, startOfWeek as $7G4ZY$startOfWeek, startOfMonth as $7G4ZY$startOfMonth, endOfWeek as $7G4ZY$endOfWeek, endOfMonth as $7G4ZY$endOfMonth, isSameDay as $7G4ZY$isSameDay, getDayOfWeek as $7G4ZY$getDayOfWeek} from "@internationalized/date";
3
- import {useControlledState as $7G4ZY$useControlledState} from "@react-stately/utils";
4
- import {useMemo as $7G4ZY$useMemo, useState as $7G4ZY$useState} from "react";
5
-
6
- /*
7
- * Copyright 2020 Adobe. All rights reserved.
8
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
9
- * you may not use this file except in compliance with the License. You may obtain a copy
10
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
11
- *
12
- * Unless required by applicable law or agreed to in writing, software distributed under
13
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
14
- * OF ANY KIND, either express or implied. See the License for the specific language
15
- * governing permissions and limitations under the License.
16
- */
17
-
18
-
19
-
20
- function $131cf43a05231e1e$export$6d095e787d2b5e1f(props) {
21
- let defaultFormatter = (0, $7G4ZY$useMemo)(()=>new (0, $7G4ZY$DateFormatter)(props.locale), [
22
- props.locale
23
- ]);
24
- let resolvedOptions = (0, $7G4ZY$useMemo)(()=>defaultFormatter.resolvedOptions(), [
25
- defaultFormatter
26
- ]);
27
- let { locale: locale, createCalendar: createCalendar, visibleDuration: visibleDuration = {
28
- months: 1
29
- }, minValue: minValue, maxValue: maxValue, selectionAlignment: selectionAlignment, isDateUnavailable: isDateUnavailable, pageBehavior: pageBehavior = 'visible', firstDayOfWeek: firstDayOfWeek } = props;
30
- let calendar = (0, $7G4ZY$useMemo)(()=>createCalendar(resolvedOptions.calendar), [
31
- createCalendar,
32
- resolvedOptions.calendar
33
- ]);
34
- var _props_defaultValue;
35
- let [value, setControlledValue] = (0, $7G4ZY$useControlledState)(props.value, (_props_defaultValue = props.defaultValue) !== null && _props_defaultValue !== void 0 ? _props_defaultValue : null, props.onChange);
36
- let calendarDateValue = (0, $7G4ZY$useMemo)(()=>value ? (0, $7G4ZY$toCalendar)((0, $7G4ZY$toCalendarDate)(value), calendar) : null, [
37
- value,
38
- calendar
39
- ]);
40
- let timeZone = (0, $7G4ZY$useMemo)(()=>value && 'timeZone' in value ? value.timeZone : resolvedOptions.timeZone, [
41
- value,
42
- resolvedOptions.timeZone
43
- ]);
44
- let focusedCalendarDate = (0, $7G4ZY$useMemo)(()=>props.focusedValue ? (0, $f62d864046160412$export$4f5203c0d889109e)((0, $7G4ZY$toCalendar)((0, $7G4ZY$toCalendarDate)(props.focusedValue), calendar), minValue, maxValue) : undefined, [
45
- props.focusedValue,
46
- calendar,
47
- minValue,
48
- maxValue
49
- ]);
50
- let defaultFocusedCalendarDate = (0, $7G4ZY$useMemo)(()=>(0, $f62d864046160412$export$4f5203c0d889109e)(props.defaultFocusedValue ? (0, $7G4ZY$toCalendar)((0, $7G4ZY$toCalendarDate)(props.defaultFocusedValue), calendar) : calendarDateValue || (0, $7G4ZY$toCalendar)((0, $7G4ZY$today)(timeZone), calendar), minValue, maxValue), [
51
- props.defaultFocusedValue,
52
- calendarDateValue,
53
- timeZone,
54
- calendar,
55
- minValue,
56
- maxValue
57
- ]);
58
- let [focusedDate, setFocusedDate] = (0, $7G4ZY$useControlledState)(focusedCalendarDate, defaultFocusedCalendarDate, props.onFocusChange);
59
- let [startDate, setStartDate] = (0, $7G4ZY$useState)(()=>{
60
- switch(selectionAlignment){
61
- case 'start':
62
- return (0, $f62d864046160412$export$144a00ba6044eb9)(focusedDate, visibleDuration, locale, minValue, maxValue);
63
- case 'end':
64
- return (0, $f62d864046160412$export$530edbfc915b2b04)(focusedDate, visibleDuration, locale, minValue, maxValue);
65
- case 'center':
66
- default:
67
- return (0, $f62d864046160412$export$f4a51ff076cc9a09)(focusedDate, visibleDuration, locale, minValue, maxValue);
68
- }
69
- });
70
- let [isFocused, setFocused] = (0, $7G4ZY$useState)(props.autoFocus || false);
71
- let endDate = (0, $7G4ZY$useMemo)(()=>{
72
- let duration = {
73
- ...visibleDuration
74
- };
75
- if (duration.days) duration.days--;
76
- else duration.days = -1;
77
- return startDate.add(duration);
78
- }, [
79
- startDate,
80
- visibleDuration
81
- ]);
82
- // Reset focused date and visible range when calendar changes.
83
- let [lastCalendar, setLastCalendar] = (0, $7G4ZY$useState)(calendar);
84
- if (!(0, $7G4ZY$isEqualCalendar)(calendar, lastCalendar)) {
85
- let newFocusedDate = (0, $7G4ZY$toCalendar)(focusedDate, calendar);
86
- setStartDate((0, $f62d864046160412$export$f4a51ff076cc9a09)(newFocusedDate, visibleDuration, locale, minValue, maxValue));
87
- setFocusedDate(newFocusedDate);
88
- setLastCalendar(calendar);
89
- }
90
- if ((0, $f62d864046160412$export$eac50920cf2fd59a)(focusedDate, minValue, maxValue)) // If the focused date was moved to an invalid value, it can't be focused, so constrain it.
91
- setFocusedDate((0, $f62d864046160412$export$4f5203c0d889109e)(focusedDate, minValue, maxValue));
92
- else if (focusedDate.compare(startDate) < 0) setStartDate((0, $f62d864046160412$export$530edbfc915b2b04)(focusedDate, visibleDuration, locale, minValue, maxValue));
93
- else if (focusedDate.compare(endDate) > 0) setStartDate((0, $f62d864046160412$export$144a00ba6044eb9)(focusedDate, visibleDuration, locale, minValue, maxValue));
94
- // Sets focus to a specific cell date
95
- function focusCell(date) {
96
- date = (0, $f62d864046160412$export$4f5203c0d889109e)(date, minValue, maxValue);
97
- setFocusedDate(date);
98
- }
99
- function setValue(newValue) {
100
- if (!props.isDisabled && !props.isReadOnly) {
101
- let localValue = newValue;
102
- if (localValue === null) {
103
- setControlledValue(null);
104
- return;
105
- }
106
- localValue = (0, $f62d864046160412$export$4f5203c0d889109e)(localValue, minValue, maxValue);
107
- localValue = (0, $f62d864046160412$export$a1d3911297b952d7)(localValue, startDate, isDateUnavailable);
108
- if (!localValue) return;
109
- // The display calendar should not have any effect on the emitted value.
110
- // Emit dates in the same calendar as the original value, if any, otherwise gregorian.
111
- localValue = (0, $7G4ZY$toCalendar)(localValue, (value === null || value === void 0 ? void 0 : value.calendar) || new (0, $7G4ZY$GregorianCalendar)());
112
- // Preserve time if the input value had one.
113
- if (value && 'hour' in value) setControlledValue(value.set(localValue));
114
- else setControlledValue(localValue);
115
- }
116
- }
117
- let isUnavailable = (0, $7G4ZY$useMemo)(()=>{
118
- if (!calendarDateValue) return false;
119
- if (isDateUnavailable && isDateUnavailable(calendarDateValue)) return true;
120
- return (0, $f62d864046160412$export$eac50920cf2fd59a)(calendarDateValue, minValue, maxValue);
121
- }, [
122
- calendarDateValue,
123
- isDateUnavailable,
124
- minValue,
125
- maxValue
126
- ]);
127
- let isValueInvalid = props.isInvalid || props.validationState === 'invalid' || isUnavailable;
128
- let validationState = isValueInvalid ? 'invalid' : null;
129
- let pageDuration = (0, $7G4ZY$useMemo)(()=>{
130
- if (pageBehavior === 'visible') return visibleDuration;
131
- return $131cf43a05231e1e$var$unitDuration(visibleDuration);
132
- }, [
133
- pageBehavior,
134
- visibleDuration
135
- ]);
136
- var _props_isDisabled, _props_isReadOnly;
137
- return {
138
- isDisabled: (_props_isDisabled = props.isDisabled) !== null && _props_isDisabled !== void 0 ? _props_isDisabled : false,
139
- isReadOnly: (_props_isReadOnly = props.isReadOnly) !== null && _props_isReadOnly !== void 0 ? _props_isReadOnly : false,
140
- value: calendarDateValue,
141
- setValue: setValue,
142
- visibleRange: {
143
- start: startDate,
144
- end: endDate
145
- },
146
- minValue: minValue,
147
- maxValue: maxValue,
148
- focusedDate: focusedDate,
149
- timeZone: timeZone,
150
- validationState: validationState,
151
- isValueInvalid: isValueInvalid,
152
- setFocusedDate (date) {
153
- focusCell(date);
154
- setFocused(true);
155
- },
156
- focusNextDay () {
157
- focusCell(focusedDate.add({
158
- days: 1
159
- }));
160
- },
161
- focusPreviousDay () {
162
- focusCell(focusedDate.subtract({
163
- days: 1
164
- }));
165
- },
166
- focusNextRow () {
167
- if (visibleDuration.days) this.focusNextPage();
168
- else if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) focusCell(focusedDate.add({
169
- weeks: 1
170
- }));
171
- },
172
- focusPreviousRow () {
173
- if (visibleDuration.days) this.focusPreviousPage();
174
- else if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) focusCell(focusedDate.subtract({
175
- weeks: 1
176
- }));
177
- },
178
- focusNextPage () {
179
- let start = startDate.add(pageDuration);
180
- setFocusedDate((0, $f62d864046160412$export$4f5203c0d889109e)(focusedDate.add(pageDuration), minValue, maxValue));
181
- setStartDate((0, $f62d864046160412$export$144a00ba6044eb9)((0, $f62d864046160412$export$5bb865b12696a77d)(focusedDate, start, pageDuration, locale, minValue, maxValue), pageDuration, locale));
182
- },
183
- focusPreviousPage () {
184
- let start = startDate.subtract(pageDuration);
185
- setFocusedDate((0, $f62d864046160412$export$4f5203c0d889109e)(focusedDate.subtract(pageDuration), minValue, maxValue));
186
- setStartDate((0, $f62d864046160412$export$144a00ba6044eb9)((0, $f62d864046160412$export$5bb865b12696a77d)(focusedDate, start, pageDuration, locale, minValue, maxValue), pageDuration, locale));
187
- },
188
- focusSectionStart () {
189
- if (visibleDuration.days) focusCell(startDate);
190
- else if (visibleDuration.weeks) focusCell((0, $7G4ZY$startOfWeek)(focusedDate, locale));
191
- else if (visibleDuration.months || visibleDuration.years) focusCell((0, $7G4ZY$startOfMonth)(focusedDate));
192
- },
193
- focusSectionEnd () {
194
- if (visibleDuration.days) focusCell(endDate);
195
- else if (visibleDuration.weeks) focusCell((0, $7G4ZY$endOfWeek)(focusedDate, locale));
196
- else if (visibleDuration.months || visibleDuration.years) focusCell((0, $7G4ZY$endOfMonth)(focusedDate));
197
- },
198
- focusNextSection (larger) {
199
- if (!larger && !visibleDuration.days) {
200
- focusCell(focusedDate.add($131cf43a05231e1e$var$unitDuration(visibleDuration)));
201
- return;
202
- }
203
- if (visibleDuration.days) this.focusNextPage();
204
- else if (visibleDuration.weeks) focusCell(focusedDate.add({
205
- months: 1
206
- }));
207
- else if (visibleDuration.months || visibleDuration.years) focusCell(focusedDate.add({
208
- years: 1
209
- }));
210
- },
211
- focusPreviousSection (larger) {
212
- if (!larger && !visibleDuration.days) {
213
- focusCell(focusedDate.subtract($131cf43a05231e1e$var$unitDuration(visibleDuration)));
214
- return;
215
- }
216
- if (visibleDuration.days) this.focusPreviousPage();
217
- else if (visibleDuration.weeks) focusCell(focusedDate.subtract({
218
- months: 1
219
- }));
220
- else if (visibleDuration.months || visibleDuration.years) focusCell(focusedDate.subtract({
221
- years: 1
222
- }));
223
- },
224
- selectFocusedDate () {
225
- if (!(isDateUnavailable && isDateUnavailable(focusedDate))) setValue(focusedDate);
226
- },
227
- selectDate (date) {
228
- setValue(date);
229
- },
230
- isFocused: isFocused,
231
- setFocused: setFocused,
232
- isInvalid (date) {
233
- return (0, $f62d864046160412$export$eac50920cf2fd59a)(date, minValue, maxValue);
234
- },
235
- isSelected (date) {
236
- return calendarDateValue != null && (0, $7G4ZY$isSameDay)(date, calendarDateValue) && !this.isCellDisabled(date) && !this.isCellUnavailable(date);
237
- },
238
- isCellFocused (date) {
239
- return isFocused && focusedDate && (0, $7G4ZY$isSameDay)(date, focusedDate);
240
- },
241
- isCellDisabled (date) {
242
- return props.isDisabled || date.compare(startDate) < 0 || date.compare(endDate) > 0 || this.isInvalid(date);
243
- },
244
- isCellUnavailable (date) {
245
- return props.isDateUnavailable ? props.isDateUnavailable(date) : false;
246
- },
247
- isPreviousVisibleRangeInvalid () {
248
- let prev = startDate.subtract({
249
- days: 1
250
- });
251
- return (0, $7G4ZY$isSameDay)(prev, startDate) || this.isInvalid(prev);
252
- },
253
- isNextVisibleRangeInvalid () {
254
- // Adding may return the same date if we reached the end of time
255
- // according to the calendar system (e.g. 9999-12-31).
256
- let next = endDate.add({
257
- days: 1
258
- });
259
- return (0, $7G4ZY$isSameDay)(next, endDate) || this.isInvalid(next);
260
- },
261
- getDatesInWeek (weekIndex, from = startDate) {
262
- let date = from.add({
263
- weeks: weekIndex
264
- });
265
- let dates = [];
266
- date = (0, $7G4ZY$startOfWeek)(date, locale, firstDayOfWeek);
267
- // startOfWeek will clamp dates within the calendar system's valid range, which may
268
- // start in the middle of a week. In this case, add null placeholders.
269
- let dayOfWeek = (0, $7G4ZY$getDayOfWeek)(date, locale, firstDayOfWeek);
270
- for(let i = 0; i < dayOfWeek; i++)dates.push(null);
271
- while(dates.length < 7){
272
- dates.push(date);
273
- let nextDate = date.add({
274
- days: 1
275
- });
276
- if ((0, $7G4ZY$isSameDay)(date, nextDate)) break;
277
- date = nextDate;
278
- }
279
- // Add null placeholders if at the end of the calendar system.
280
- while(dates.length < 7)dates.push(null);
281
- return dates;
282
- }
283
- };
284
- }
285
- function $131cf43a05231e1e$var$unitDuration(duration) {
286
- let unit = {
287
- ...duration
288
- };
289
- for(let key in duration)unit[key] = 1;
290
- return unit;
291
- }
292
-
293
-
294
- export {$131cf43a05231e1e$export$6d095e787d2b5e1f as useCalendarState};
295
- //# sourceMappingURL=useCalendarState.module.js.map
@@ -1,295 +0,0 @@
1
- import {alignCenter as $f62d864046160412$export$f4a51ff076cc9a09, alignEnd as $f62d864046160412$export$530edbfc915b2b04, alignStart as $f62d864046160412$export$144a00ba6044eb9, constrainStart as $f62d864046160412$export$5bb865b12696a77d, constrainValue as $f62d864046160412$export$4f5203c0d889109e, isInvalid as $f62d864046160412$export$eac50920cf2fd59a, previousAvailableDate as $f62d864046160412$export$a1d3911297b952d7} from "./utils.module.js";
2
- import {DateFormatter as $7G4ZY$DateFormatter, toCalendar as $7G4ZY$toCalendar, toCalendarDate as $7G4ZY$toCalendarDate, today as $7G4ZY$today, isEqualCalendar as $7G4ZY$isEqualCalendar, GregorianCalendar as $7G4ZY$GregorianCalendar, startOfWeek as $7G4ZY$startOfWeek, startOfMonth as $7G4ZY$startOfMonth, endOfWeek as $7G4ZY$endOfWeek, endOfMonth as $7G4ZY$endOfMonth, isSameDay as $7G4ZY$isSameDay, getDayOfWeek as $7G4ZY$getDayOfWeek} from "@internationalized/date";
3
- import {useControlledState as $7G4ZY$useControlledState} from "@react-stately/utils";
4
- import {useMemo as $7G4ZY$useMemo, useState as $7G4ZY$useState} from "react";
5
-
6
- /*
7
- * Copyright 2020 Adobe. All rights reserved.
8
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
9
- * you may not use this file except in compliance with the License. You may obtain a copy
10
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
11
- *
12
- * Unless required by applicable law or agreed to in writing, software distributed under
13
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
14
- * OF ANY KIND, either express or implied. See the License for the specific language
15
- * governing permissions and limitations under the License.
16
- */
17
-
18
-
19
-
20
- function $131cf43a05231e1e$export$6d095e787d2b5e1f(props) {
21
- let defaultFormatter = (0, $7G4ZY$useMemo)(()=>new (0, $7G4ZY$DateFormatter)(props.locale), [
22
- props.locale
23
- ]);
24
- let resolvedOptions = (0, $7G4ZY$useMemo)(()=>defaultFormatter.resolvedOptions(), [
25
- defaultFormatter
26
- ]);
27
- let { locale: locale, createCalendar: createCalendar, visibleDuration: visibleDuration = {
28
- months: 1
29
- }, minValue: minValue, maxValue: maxValue, selectionAlignment: selectionAlignment, isDateUnavailable: isDateUnavailable, pageBehavior: pageBehavior = 'visible', firstDayOfWeek: firstDayOfWeek } = props;
30
- let calendar = (0, $7G4ZY$useMemo)(()=>createCalendar(resolvedOptions.calendar), [
31
- createCalendar,
32
- resolvedOptions.calendar
33
- ]);
34
- var _props_defaultValue;
35
- let [value, setControlledValue] = (0, $7G4ZY$useControlledState)(props.value, (_props_defaultValue = props.defaultValue) !== null && _props_defaultValue !== void 0 ? _props_defaultValue : null, props.onChange);
36
- let calendarDateValue = (0, $7G4ZY$useMemo)(()=>value ? (0, $7G4ZY$toCalendar)((0, $7G4ZY$toCalendarDate)(value), calendar) : null, [
37
- value,
38
- calendar
39
- ]);
40
- let timeZone = (0, $7G4ZY$useMemo)(()=>value && 'timeZone' in value ? value.timeZone : resolvedOptions.timeZone, [
41
- value,
42
- resolvedOptions.timeZone
43
- ]);
44
- let focusedCalendarDate = (0, $7G4ZY$useMemo)(()=>props.focusedValue ? (0, $f62d864046160412$export$4f5203c0d889109e)((0, $7G4ZY$toCalendar)((0, $7G4ZY$toCalendarDate)(props.focusedValue), calendar), minValue, maxValue) : undefined, [
45
- props.focusedValue,
46
- calendar,
47
- minValue,
48
- maxValue
49
- ]);
50
- let defaultFocusedCalendarDate = (0, $7G4ZY$useMemo)(()=>(0, $f62d864046160412$export$4f5203c0d889109e)(props.defaultFocusedValue ? (0, $7G4ZY$toCalendar)((0, $7G4ZY$toCalendarDate)(props.defaultFocusedValue), calendar) : calendarDateValue || (0, $7G4ZY$toCalendar)((0, $7G4ZY$today)(timeZone), calendar), minValue, maxValue), [
51
- props.defaultFocusedValue,
52
- calendarDateValue,
53
- timeZone,
54
- calendar,
55
- minValue,
56
- maxValue
57
- ]);
58
- let [focusedDate, setFocusedDate] = (0, $7G4ZY$useControlledState)(focusedCalendarDate, defaultFocusedCalendarDate, props.onFocusChange);
59
- let [startDate, setStartDate] = (0, $7G4ZY$useState)(()=>{
60
- switch(selectionAlignment){
61
- case 'start':
62
- return (0, $f62d864046160412$export$144a00ba6044eb9)(focusedDate, visibleDuration, locale, minValue, maxValue);
63
- case 'end':
64
- return (0, $f62d864046160412$export$530edbfc915b2b04)(focusedDate, visibleDuration, locale, minValue, maxValue);
65
- case 'center':
66
- default:
67
- return (0, $f62d864046160412$export$f4a51ff076cc9a09)(focusedDate, visibleDuration, locale, minValue, maxValue);
68
- }
69
- });
70
- let [isFocused, setFocused] = (0, $7G4ZY$useState)(props.autoFocus || false);
71
- let endDate = (0, $7G4ZY$useMemo)(()=>{
72
- let duration = {
73
- ...visibleDuration
74
- };
75
- if (duration.days) duration.days--;
76
- else duration.days = -1;
77
- return startDate.add(duration);
78
- }, [
79
- startDate,
80
- visibleDuration
81
- ]);
82
- // Reset focused date and visible range when calendar changes.
83
- let [lastCalendar, setLastCalendar] = (0, $7G4ZY$useState)(calendar);
84
- if (!(0, $7G4ZY$isEqualCalendar)(calendar, lastCalendar)) {
85
- let newFocusedDate = (0, $7G4ZY$toCalendar)(focusedDate, calendar);
86
- setStartDate((0, $f62d864046160412$export$f4a51ff076cc9a09)(newFocusedDate, visibleDuration, locale, minValue, maxValue));
87
- setFocusedDate(newFocusedDate);
88
- setLastCalendar(calendar);
89
- }
90
- if ((0, $f62d864046160412$export$eac50920cf2fd59a)(focusedDate, minValue, maxValue)) // If the focused date was moved to an invalid value, it can't be focused, so constrain it.
91
- setFocusedDate((0, $f62d864046160412$export$4f5203c0d889109e)(focusedDate, minValue, maxValue));
92
- else if (focusedDate.compare(startDate) < 0) setStartDate((0, $f62d864046160412$export$530edbfc915b2b04)(focusedDate, visibleDuration, locale, minValue, maxValue));
93
- else if (focusedDate.compare(endDate) > 0) setStartDate((0, $f62d864046160412$export$144a00ba6044eb9)(focusedDate, visibleDuration, locale, minValue, maxValue));
94
- // Sets focus to a specific cell date
95
- function focusCell(date) {
96
- date = (0, $f62d864046160412$export$4f5203c0d889109e)(date, minValue, maxValue);
97
- setFocusedDate(date);
98
- }
99
- function setValue(newValue) {
100
- if (!props.isDisabled && !props.isReadOnly) {
101
- let localValue = newValue;
102
- if (localValue === null) {
103
- setControlledValue(null);
104
- return;
105
- }
106
- localValue = (0, $f62d864046160412$export$4f5203c0d889109e)(localValue, minValue, maxValue);
107
- localValue = (0, $f62d864046160412$export$a1d3911297b952d7)(localValue, startDate, isDateUnavailable);
108
- if (!localValue) return;
109
- // The display calendar should not have any effect on the emitted value.
110
- // Emit dates in the same calendar as the original value, if any, otherwise gregorian.
111
- localValue = (0, $7G4ZY$toCalendar)(localValue, (value === null || value === void 0 ? void 0 : value.calendar) || new (0, $7G4ZY$GregorianCalendar)());
112
- // Preserve time if the input value had one.
113
- if (value && 'hour' in value) setControlledValue(value.set(localValue));
114
- else setControlledValue(localValue);
115
- }
116
- }
117
- let isUnavailable = (0, $7G4ZY$useMemo)(()=>{
118
- if (!calendarDateValue) return false;
119
- if (isDateUnavailable && isDateUnavailable(calendarDateValue)) return true;
120
- return (0, $f62d864046160412$export$eac50920cf2fd59a)(calendarDateValue, minValue, maxValue);
121
- }, [
122
- calendarDateValue,
123
- isDateUnavailable,
124
- minValue,
125
- maxValue
126
- ]);
127
- let isValueInvalid = props.isInvalid || props.validationState === 'invalid' || isUnavailable;
128
- let validationState = isValueInvalid ? 'invalid' : null;
129
- let pageDuration = (0, $7G4ZY$useMemo)(()=>{
130
- if (pageBehavior === 'visible') return visibleDuration;
131
- return $131cf43a05231e1e$var$unitDuration(visibleDuration);
132
- }, [
133
- pageBehavior,
134
- visibleDuration
135
- ]);
136
- var _props_isDisabled, _props_isReadOnly;
137
- return {
138
- isDisabled: (_props_isDisabled = props.isDisabled) !== null && _props_isDisabled !== void 0 ? _props_isDisabled : false,
139
- isReadOnly: (_props_isReadOnly = props.isReadOnly) !== null && _props_isReadOnly !== void 0 ? _props_isReadOnly : false,
140
- value: calendarDateValue,
141
- setValue: setValue,
142
- visibleRange: {
143
- start: startDate,
144
- end: endDate
145
- },
146
- minValue: minValue,
147
- maxValue: maxValue,
148
- focusedDate: focusedDate,
149
- timeZone: timeZone,
150
- validationState: validationState,
151
- isValueInvalid: isValueInvalid,
152
- setFocusedDate (date) {
153
- focusCell(date);
154
- setFocused(true);
155
- },
156
- focusNextDay () {
157
- focusCell(focusedDate.add({
158
- days: 1
159
- }));
160
- },
161
- focusPreviousDay () {
162
- focusCell(focusedDate.subtract({
163
- days: 1
164
- }));
165
- },
166
- focusNextRow () {
167
- if (visibleDuration.days) this.focusNextPage();
168
- else if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) focusCell(focusedDate.add({
169
- weeks: 1
170
- }));
171
- },
172
- focusPreviousRow () {
173
- if (visibleDuration.days) this.focusPreviousPage();
174
- else if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) focusCell(focusedDate.subtract({
175
- weeks: 1
176
- }));
177
- },
178
- focusNextPage () {
179
- let start = startDate.add(pageDuration);
180
- setFocusedDate((0, $f62d864046160412$export$4f5203c0d889109e)(focusedDate.add(pageDuration), minValue, maxValue));
181
- setStartDate((0, $f62d864046160412$export$144a00ba6044eb9)((0, $f62d864046160412$export$5bb865b12696a77d)(focusedDate, start, pageDuration, locale, minValue, maxValue), pageDuration, locale));
182
- },
183
- focusPreviousPage () {
184
- let start = startDate.subtract(pageDuration);
185
- setFocusedDate((0, $f62d864046160412$export$4f5203c0d889109e)(focusedDate.subtract(pageDuration), minValue, maxValue));
186
- setStartDate((0, $f62d864046160412$export$144a00ba6044eb9)((0, $f62d864046160412$export$5bb865b12696a77d)(focusedDate, start, pageDuration, locale, minValue, maxValue), pageDuration, locale));
187
- },
188
- focusSectionStart () {
189
- if (visibleDuration.days) focusCell(startDate);
190
- else if (visibleDuration.weeks) focusCell((0, $7G4ZY$startOfWeek)(focusedDate, locale));
191
- else if (visibleDuration.months || visibleDuration.years) focusCell((0, $7G4ZY$startOfMonth)(focusedDate));
192
- },
193
- focusSectionEnd () {
194
- if (visibleDuration.days) focusCell(endDate);
195
- else if (visibleDuration.weeks) focusCell((0, $7G4ZY$endOfWeek)(focusedDate, locale));
196
- else if (visibleDuration.months || visibleDuration.years) focusCell((0, $7G4ZY$endOfMonth)(focusedDate));
197
- },
198
- focusNextSection (larger) {
199
- if (!larger && !visibleDuration.days) {
200
- focusCell(focusedDate.add($131cf43a05231e1e$var$unitDuration(visibleDuration)));
201
- return;
202
- }
203
- if (visibleDuration.days) this.focusNextPage();
204
- else if (visibleDuration.weeks) focusCell(focusedDate.add({
205
- months: 1
206
- }));
207
- else if (visibleDuration.months || visibleDuration.years) focusCell(focusedDate.add({
208
- years: 1
209
- }));
210
- },
211
- focusPreviousSection (larger) {
212
- if (!larger && !visibleDuration.days) {
213
- focusCell(focusedDate.subtract($131cf43a05231e1e$var$unitDuration(visibleDuration)));
214
- return;
215
- }
216
- if (visibleDuration.days) this.focusPreviousPage();
217
- else if (visibleDuration.weeks) focusCell(focusedDate.subtract({
218
- months: 1
219
- }));
220
- else if (visibleDuration.months || visibleDuration.years) focusCell(focusedDate.subtract({
221
- years: 1
222
- }));
223
- },
224
- selectFocusedDate () {
225
- if (!(isDateUnavailable && isDateUnavailable(focusedDate))) setValue(focusedDate);
226
- },
227
- selectDate (date) {
228
- setValue(date);
229
- },
230
- isFocused: isFocused,
231
- setFocused: setFocused,
232
- isInvalid (date) {
233
- return (0, $f62d864046160412$export$eac50920cf2fd59a)(date, minValue, maxValue);
234
- },
235
- isSelected (date) {
236
- return calendarDateValue != null && (0, $7G4ZY$isSameDay)(date, calendarDateValue) && !this.isCellDisabled(date) && !this.isCellUnavailable(date);
237
- },
238
- isCellFocused (date) {
239
- return isFocused && focusedDate && (0, $7G4ZY$isSameDay)(date, focusedDate);
240
- },
241
- isCellDisabled (date) {
242
- return props.isDisabled || date.compare(startDate) < 0 || date.compare(endDate) > 0 || this.isInvalid(date);
243
- },
244
- isCellUnavailable (date) {
245
- return props.isDateUnavailable ? props.isDateUnavailable(date) : false;
246
- },
247
- isPreviousVisibleRangeInvalid () {
248
- let prev = startDate.subtract({
249
- days: 1
250
- });
251
- return (0, $7G4ZY$isSameDay)(prev, startDate) || this.isInvalid(prev);
252
- },
253
- isNextVisibleRangeInvalid () {
254
- // Adding may return the same date if we reached the end of time
255
- // according to the calendar system (e.g. 9999-12-31).
256
- let next = endDate.add({
257
- days: 1
258
- });
259
- return (0, $7G4ZY$isSameDay)(next, endDate) || this.isInvalid(next);
260
- },
261
- getDatesInWeek (weekIndex, from = startDate) {
262
- let date = from.add({
263
- weeks: weekIndex
264
- });
265
- let dates = [];
266
- date = (0, $7G4ZY$startOfWeek)(date, locale, firstDayOfWeek);
267
- // startOfWeek will clamp dates within the calendar system's valid range, which may
268
- // start in the middle of a week. In this case, add null placeholders.
269
- let dayOfWeek = (0, $7G4ZY$getDayOfWeek)(date, locale, firstDayOfWeek);
270
- for(let i = 0; i < dayOfWeek; i++)dates.push(null);
271
- while(dates.length < 7){
272
- dates.push(date);
273
- let nextDate = date.add({
274
- days: 1
275
- });
276
- if ((0, $7G4ZY$isSameDay)(date, nextDate)) break;
277
- date = nextDate;
278
- }
279
- // Add null placeholders if at the end of the calendar system.
280
- while(dates.length < 7)dates.push(null);
281
- return dates;
282
- }
283
- };
284
- }
285
- function $131cf43a05231e1e$var$unitDuration(duration) {
286
- let unit = {
287
- ...duration
288
- };
289
- for(let key in duration)unit[key] = 1;
290
- return unit;
291
- }
292
-
293
-
294
- export {$131cf43a05231e1e$export$6d095e787d2b5e1f as useCalendarState};
295
- //# sourceMappingURL=useCalendarState.module.js.map