@react-stately/calendar 3.0.0-alpha.1 → 3.0.0-alpha.4

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.
package/dist/module.js CHANGED
@@ -1,410 +1,411 @@
1
- import _babelRuntimeHelpersEsmObjectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
2
- import { useEffect, useMemo, useRef, useState } from "react";
3
- import { useDateFormatter } from "@react-aria/i18n";
4
- import { useControlledState } from "@react-stately/utils";
5
- import { maxDate, minDate, startOfMonth, startOfWeek, startOfYear, toCalendarDate, GregorianCalendar, isSameDay, toCalendar, today } from "@internationalized/date";
6
- import _babelRuntimeHelpersEsmExtends from "@babel/runtime/helpers/esm/extends";
1
+ import {DateFormatter as $keQhS$DateFormatter, toCalendar as $keQhS$toCalendar, toCalendarDate as $keQhS$toCalendarDate, today as $keQhS$today, GregorianCalendar as $keQhS$GregorianCalendar, isSameDay as $keQhS$isSameDay, startOfYear as $keQhS$startOfYear, startOfMonth as $keQhS$startOfMonth, startOfWeek as $keQhS$startOfWeek, maxDate as $keQhS$maxDate, minDate as $keQhS$minDate, isEqualDay as $keQhS$isEqualDay} from "@internationalized/date";
2
+ import {useControlledState as $keQhS$useControlledState} from "@react-stately/utils";
3
+ import {useMemo as $keQhS$useMemo, useState as $keQhS$useState, useRef as $keQhS$useRef} from "react";
7
4
 
8
- function $e9245e22e7d2e30e959129f8619bae$export$isInvalid(date, minValue, maxValue) {
9
- return minValue != null && date.compare(minValue) < 0 || maxValue != null && date.compare(maxValue) > 0;
5
+ function $parcel$export(e, n, v, s) {
6
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
10
7
  }
8
+ var $77b8da46fac7b53e$exports = {};
11
9
 
12
- function $e9245e22e7d2e30e959129f8619bae$export$alignCenter(date, duration, locale, minValue, maxValue) {
13
- let halfDuration = {};
14
10
 
15
- for (let key in duration) {
16
- halfDuration[key] = Math.floor(duration[key] / 2);
11
+ var $131cf43a05231e1e$exports = {};
17
12
 
18
- if (halfDuration[key] > 0 && duration[key] % 2 === 0) {
19
- halfDuration[key]--;
20
- }
21
- }
13
+ $parcel$export($131cf43a05231e1e$exports, "useCalendarState", () => $131cf43a05231e1e$export$6d095e787d2b5e1f);
22
14
 
23
- let aligned = $e9245e22e7d2e30e959129f8619bae$export$alignStart(date, duration, locale).subtract(halfDuration);
24
- return $e9245e22e7d2e30e959129f8619bae$export$constrainStart(date, aligned, duration, locale, minValue, maxValue);
15
+ function $f62d864046160412$export$eac50920cf2fd59a(date, minValue, maxValue) {
16
+ return minValue != null && date.compare(minValue) < 0 || maxValue != null && date.compare(maxValue) > 0;
25
17
  }
26
-
27
- function $e9245e22e7d2e30e959129f8619bae$export$alignStart(date, duration, locale, minValue, maxValue) {
28
- // align to the start of the largest unit
29
- let aligned = date;
30
-
31
- if (duration.years) {
32
- aligned = startOfYear(date);
33
- } else if (duration.months) {
34
- aligned = startOfMonth(date);
35
- } else if (duration.weeks) {
36
- aligned = startOfWeek(date, locale);
37
- }
38
-
39
- return $e9245e22e7d2e30e959129f8619bae$export$constrainStart(date, aligned, duration, locale, minValue, maxValue);
18
+ function $f62d864046160412$export$f4a51ff076cc9a09(date, duration, locale, minValue, maxValue) {
19
+ let halfDuration = {
20
+ };
21
+ for(let key in duration){
22
+ halfDuration[key] = Math.floor(duration[key] / 2);
23
+ if (halfDuration[key] > 0 && duration[key] % 2 === 0) halfDuration[key]--;
24
+ }
25
+ let aligned = $f62d864046160412$export$144a00ba6044eb9(date, duration, locale).subtract(halfDuration);
26
+ return $f62d864046160412$export$5bb865b12696a77d(date, aligned, duration, locale, minValue, maxValue);
40
27
  }
41
-
42
- function $e9245e22e7d2e30e959129f8619bae$export$alignEnd(date, duration, locale, minValue, maxValue) {
43
- let d = _babelRuntimeHelpersEsmExtends({}, duration); // subtract 1 from the smallest unit
44
-
45
-
46
- if (duration.days) {
47
- d.days--;
48
- } else if (duration.weeks) {
49
- d.weeks--;
50
- } else if (duration.months) {
51
- d.months--;
52
- } else if (duration.years) {
53
- d.years--;
54
- }
55
-
56
- let aligned = $e9245e22e7d2e30e959129f8619bae$export$alignStart(date, duration, locale).subtract(d);
57
- return $e9245e22e7d2e30e959129f8619bae$export$constrainStart(date, aligned, duration, locale, minValue, maxValue);
28
+ function $f62d864046160412$export$144a00ba6044eb9(date, duration, locale, minValue, maxValue) {
29
+ // align to the start of the largest unit
30
+ let aligned = date;
31
+ if (duration.years) aligned = $keQhS$startOfYear(date);
32
+ else if (duration.months) aligned = $keQhS$startOfMonth(date);
33
+ else if (duration.weeks) aligned = $keQhS$startOfWeek(date, locale);
34
+ return $f62d864046160412$export$5bb865b12696a77d(date, aligned, duration, locale, minValue, maxValue);
58
35
  }
59
-
60
- function $e9245e22e7d2e30e959129f8619bae$export$constrainStart(date, aligned, duration, locale, minValue, maxValue) {
61
- if (minValue && date.compare(minValue) >= 0) {
62
- aligned = maxDate(aligned, $e9245e22e7d2e30e959129f8619bae$export$alignStart(toCalendarDate(minValue), duration, locale));
63
- }
64
-
65
- if (maxValue && date.compare(maxValue) <= 0) {
66
- aligned = minDate(aligned, $e9245e22e7d2e30e959129f8619bae$export$alignEnd(toCalendarDate(maxValue), duration, locale));
67
- }
68
-
69
- return aligned;
36
+ function $f62d864046160412$export$530edbfc915b2b04(date, duration, locale, minValue, maxValue) {
37
+ let d = {
38
+ ...duration
39
+ };
40
+ // subtract 1 from the smallest unit
41
+ if (duration.days) d.days--;
42
+ else if (duration.weeks) d.weeks--;
43
+ else if (duration.months) d.months--;
44
+ else if (duration.years) d.years--;
45
+ let aligned = $f62d864046160412$export$144a00ba6044eb9(date, duration, locale).subtract(d);
46
+ return $f62d864046160412$export$5bb865b12696a77d(date, aligned, duration, locale, minValue, maxValue);
70
47
  }
71
-
72
- function $e9245e22e7d2e30e959129f8619bae$export$constrainValue(date, minValue, maxValue) {
73
- if (minValue) {
74
- date = maxDate(date, toCalendarDate(minValue));
75
- }
76
-
77
- if (maxValue) {
78
- date = minDate(date, toCalendarDate(maxValue));
79
- }
80
-
81
- return date;
48
+ function $f62d864046160412$export$5bb865b12696a77d(date, aligned, duration, locale, minValue, maxValue) {
49
+ if (minValue && date.compare(minValue) >= 0) aligned = $keQhS$maxDate(aligned, $f62d864046160412$export$144a00ba6044eb9($keQhS$toCalendarDate(minValue), duration, locale));
50
+ if (maxValue && date.compare(maxValue) <= 0) aligned = $keQhS$minDate(aligned, $f62d864046160412$export$530edbfc915b2b04($keQhS$toCalendarDate(maxValue), duration, locale));
51
+ return aligned;
52
+ }
53
+ function $f62d864046160412$export$4f5203c0d889109e(date, minValue, maxValue) {
54
+ if (minValue) date = $keQhS$maxDate(date, $keQhS$toCalendarDate(minValue));
55
+ if (maxValue) date = $keQhS$minDate(date, $keQhS$toCalendarDate(maxValue));
56
+ return date;
82
57
  }
83
58
 
84
- export function useCalendarState(props) {
85
- let defaultFormatter = useDateFormatter();
86
- let resolvedOptions = useMemo(() => defaultFormatter.resolvedOptions(), [defaultFormatter]);
87
- let {
88
- locale,
89
- createCalendar,
90
- timeZone = resolvedOptions.timeZone,
91
- visibleDuration = {
92
- months: 1
93
- },
94
- minValue,
95
- maxValue,
96
- selectionAlignment
97
- } = props;
98
- let calendar = useMemo(() => createCalendar(resolvedOptions.calendar), [createCalendar, resolvedOptions.calendar]);
99
- let [value, setControlledValue] = useControlledState(props.value, props.defaultValue, props.onChange);
100
- let calendarDateValue = useMemo(() => value ? toCalendar(toCalendarDate(value), calendar) : null, [value, calendar]);
101
- let defaultDate = useMemo(() => calendarDateValue || $e9245e22e7d2e30e959129f8619bae$export$constrainValue(toCalendar(today(timeZone), calendar), minValue, maxValue), [calendarDateValue, timeZone, calendar, minValue, maxValue]);
102
- let [startDate, setStartDate] = useState(() => {
103
- switch (selectionAlignment) {
104
- case 'start':
105
- return $e9245e22e7d2e30e959129f8619bae$export$alignStart(defaultDate, visibleDuration, locale, minValue, maxValue);
106
59
 
107
- case 'end':
108
- return $e9245e22e7d2e30e959129f8619bae$export$alignEnd(defaultDate, visibleDuration, locale, minValue, maxValue);
109
60
 
110
- case 'center':
111
- default:
112
- return $e9245e22e7d2e30e959129f8619bae$export$alignCenter(defaultDate, visibleDuration, locale, minValue, maxValue);
113
- }
114
- });
115
- let [focusedDate, setFocusedDate] = useState(defaultDate);
116
- let [isFocused, setFocused] = useState(props.autoFocus || false);
117
- let endDate = useMemo(() => startDate.add(visibleDuration).subtract({
118
- days: 1
119
- }), [startDate, visibleDuration]); // Reset focused date and visible range when calendar changes.
120
61
 
121
- let lastCalendarIdentifier = useRef(calendar.identifier);
122
- useEffect(() => {
62
+
63
+ function $131cf43a05231e1e$export$6d095e787d2b5e1f(props) {
64
+ let defaultFormatter = $keQhS$useMemo(()=>new $keQhS$DateFormatter(props.locale)
65
+ , [
66
+ props.locale
67
+ ]);
68
+ let resolvedOptions = $keQhS$useMemo(()=>defaultFormatter.resolvedOptions()
69
+ , [
70
+ defaultFormatter
71
+ ]);
72
+ let { locale: locale , createCalendar: createCalendar , visibleDuration: visibleDuration = {
73
+ months: 1
74
+ } , minValue: minValue , maxValue: maxValue , selectionAlignment: selectionAlignment } = props;
75
+ let calendar = $keQhS$useMemo(()=>createCalendar(resolvedOptions.calendar)
76
+ , [
77
+ createCalendar,
78
+ resolvedOptions.calendar
79
+ ]);
80
+ let [value, setControlledValue] = $keQhS$useControlledState(props.value, props.defaultValue, props.onChange);
81
+ let calendarDateValue = $keQhS$useMemo(()=>value ? $keQhS$toCalendar($keQhS$toCalendarDate(value), calendar) : null
82
+ , [
83
+ value,
84
+ calendar
85
+ ]);
86
+ let timeZone = $keQhS$useMemo(()=>value && 'timeZone' in value ? value.timeZone : resolvedOptions.timeZone
87
+ , [
88
+ value,
89
+ resolvedOptions.timeZone
90
+ ]);
91
+ let focusedCalendarDate = $keQhS$useMemo(()=>props.focusedValue ? $f62d864046160412$export$4f5203c0d889109e($keQhS$toCalendar($keQhS$toCalendarDate(props.focusedValue), calendar), minValue, maxValue) : undefined
92
+ , [
93
+ props.focusedValue,
94
+ calendar,
95
+ minValue,
96
+ maxValue
97
+ ]);
98
+ let defaultFocusedCalendarDate = $keQhS$useMemo(()=>$f62d864046160412$export$4f5203c0d889109e(props.defaultFocusedValue ? $keQhS$toCalendar($keQhS$toCalendarDate(props.defaultFocusedValue), calendar) : calendarDateValue || $keQhS$toCalendar($keQhS$today(timeZone), calendar), minValue, maxValue)
99
+ , [
100
+ props.defaultFocusedValue,
101
+ calendarDateValue,
102
+ timeZone,
103
+ calendar,
104
+ minValue,
105
+ maxValue
106
+ ]);
107
+ let [focusedDate, setFocusedDate] = $keQhS$useControlledState(focusedCalendarDate, defaultFocusedCalendarDate, props.onFocusChange);
108
+ let [startDate, setStartDate] = $keQhS$useState(()=>{
109
+ switch(selectionAlignment){
110
+ case 'start':
111
+ return $f62d864046160412$export$144a00ba6044eb9(focusedDate, visibleDuration, locale, minValue, maxValue);
112
+ case 'end':
113
+ return $f62d864046160412$export$530edbfc915b2b04(focusedDate, visibleDuration, locale, minValue, maxValue);
114
+ case 'center':
115
+ default:
116
+ return $f62d864046160412$export$f4a51ff076cc9a09(focusedDate, visibleDuration, locale, minValue, maxValue);
117
+ }
118
+ });
119
+ let [isFocused, setFocused] = $keQhS$useState(props.autoFocus || false);
120
+ let endDate = $keQhS$useMemo(()=>startDate.add(visibleDuration).subtract({
121
+ days: 1
122
+ })
123
+ , [
124
+ startDate,
125
+ visibleDuration
126
+ ]);
127
+ // Reset focused date and visible range when calendar changes.
128
+ let lastCalendarIdentifier = $keQhS$useRef(calendar.identifier);
123
129
  if (calendar.identifier !== lastCalendarIdentifier.current) {
124
- let newFocusedDate = toCalendar(focusedDate, calendar);
125
- setStartDate($e9245e22e7d2e30e959129f8619bae$export$alignCenter(newFocusedDate, visibleDuration, locale, minValue, maxValue));
126
- setFocusedDate(newFocusedDate);
127
- lastCalendarIdentifier.current = calendar.identifier;
130
+ let newFocusedDate = $keQhS$toCalendar(focusedDate, calendar);
131
+ setStartDate($f62d864046160412$export$f4a51ff076cc9a09(newFocusedDate, visibleDuration, locale, minValue, maxValue));
132
+ setFocusedDate(newFocusedDate);
133
+ lastCalendarIdentifier.current = calendar.identifier;
128
134
  }
129
- }, [calendar, focusedDate, visibleDuration, locale, minValue, maxValue]); // Sets focus to a specific cell date
130
-
131
- function focusCell(date) {
132
- // date = constrain(focusedDate, date, visibleDuration, locale, minValue, maxValue);
133
- date = $e9245e22e7d2e30e959129f8619bae$export$constrainValue(date, minValue, maxValue);
134
- let next = startDate.add(visibleDuration);
135
-
136
- if (date.compare(startDate) < 0) {
137
- setStartDate($e9245e22e7d2e30e959129f8619bae$export$alignEnd(date, visibleDuration, locale, minValue, maxValue));
138
- } else if (date.compare(next) >= 0) {
139
- setStartDate($e9245e22e7d2e30e959129f8619bae$export$alignStart(date, visibleDuration, locale, minValue, maxValue));
135
+ if ($f62d864046160412$export$eac50920cf2fd59a(focusedDate, minValue, maxValue)) // If the focused date was moved to an invalid value, it can't be focused, so constrain it.
136
+ setFocusedDate($f62d864046160412$export$4f5203c0d889109e(focusedDate, minValue, maxValue));
137
+ else if (focusedDate.compare(startDate) < 0) setStartDate($f62d864046160412$export$530edbfc915b2b04(focusedDate, visibleDuration, locale, minValue, maxValue));
138
+ else if (focusedDate.compare(startDate.add(visibleDuration)) >= 0) setStartDate($f62d864046160412$export$144a00ba6044eb9(focusedDate, visibleDuration, locale, minValue, maxValue));
139
+ // Sets focus to a specific cell date
140
+ function focusCell(date) {
141
+ date = $f62d864046160412$export$4f5203c0d889109e(date, minValue, maxValue);
142
+ setFocusedDate(date);
140
143
  }
141
-
142
- setFocusedDate(date);
143
- }
144
-
145
- function setValue(newValue) {
146
- if (!props.isDisabled && !props.isReadOnly) {
147
- // The display calendar should not have any effect on the emitted value.
148
- // Emit dates in the same calendar as the original value, if any, otherwise gregorian.
149
- newValue = toCalendar(newValue, (value == null ? void 0 : value.calendar) || new GregorianCalendar()); // Preserve time if the input value had one.
150
-
151
- if (value && 'hour' in value) {
152
- setControlledValue(value.set(newValue));
153
- } else {
154
- setControlledValue(newValue);
155
- }
144
+ function setValue(newValue) {
145
+ if (!props.isDisabled && !props.isReadOnly) {
146
+ // The display calendar should not have any effect on the emitted value.
147
+ // Emit dates in the same calendar as the original value, if any, otherwise gregorian.
148
+ newValue = $keQhS$toCalendar(newValue, (value === null || value === void 0 ? void 0 : value.calendar) || new $keQhS$GregorianCalendar());
149
+ // Preserve time if the input value had one.
150
+ if (value && 'hour' in value) setControlledValue(value.set(newValue));
151
+ else setControlledValue(newValue);
152
+ }
156
153
  }
157
- }
158
-
159
- return {
160
- isDisabled: props.isDisabled,
161
- isReadOnly: props.isReadOnly,
162
- value: calendarDateValue,
163
- setValue,
164
- visibleRange: {
165
- start: startDate,
166
- end: endDate
167
- },
168
- focusedDate,
169
- timeZone,
170
-
171
- setFocusedDate(date) {
172
- setFocusedDate(date);
173
- setFocused(true);
174
- },
175
-
176
- focusNextDay() {
177
- focusCell(focusedDate.add({
178
- days: 1
179
- }));
180
- },
181
-
182
- focusPreviousDay() {
183
- focusCell(focusedDate.subtract({
184
- days: 1
185
- }));
186
- },
187
-
188
- focusNextRow() {
189
- if (visibleDuration.days) {
190
- this.focusNextPage();
191
- } else if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) {
192
- focusCell(focusedDate.add({
193
- weeks: 1
194
- }));
195
- }
196
- },
197
-
198
- focusPreviousRow() {
199
- if (visibleDuration.days) {
200
- this.focusPreviousPage();
201
- } else if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) {
202
- focusCell(focusedDate.subtract({
203
- weeks: 1
204
- }));
205
- }
206
- },
207
-
208
- focusNextPage() {
209
- let start = startDate.add(visibleDuration);
210
- setStartDate($e9245e22e7d2e30e959129f8619bae$export$constrainStart(focusedDate, start, visibleDuration, locale, minValue, maxValue));
211
- setFocusedDate($e9245e22e7d2e30e959129f8619bae$export$constrainValue(focusedDate.add(visibleDuration), minValue, maxValue));
212
- },
213
-
214
- focusPreviousPage() {
215
- let start = startDate.subtract(visibleDuration);
216
- setStartDate($e9245e22e7d2e30e959129f8619bae$export$constrainStart(focusedDate, start, visibleDuration, locale, minValue, maxValue));
217
- setFocusedDate($e9245e22e7d2e30e959129f8619bae$export$constrainValue(focusedDate.subtract(visibleDuration), minValue, maxValue));
218
- },
219
-
220
- focusPageStart() {
221
- focusCell(startDate);
222
- },
223
-
224
- focusPageEnd() {
225
- focusCell(endDate);
226
- },
227
-
228
- focusNextSection() {
229
- if (visibleDuration.days) {
230
- this.focusNextPage();
231
- } else if (visibleDuration.weeks) {
232
- focusCell(focusedDate.add({
233
- months: 1
234
- }));
235
- } else if (visibleDuration.months || visibleDuration.years) {
236
- focusCell(focusedDate.add({
237
- years: 1
238
- }));
239
- }
240
- },
241
-
242
- focusPreviousSection() {
243
- if (visibleDuration.days) {
244
- this.focusPreviousPage();
245
- } else if (visibleDuration.weeks) {
246
- focusCell(focusedDate.subtract({
247
- months: 1
248
- }));
249
- } else if (visibleDuration.months || visibleDuration.years) {
250
- focusCell(focusedDate.subtract({
251
- years: 1
252
- }));
253
- }
254
- },
255
-
256
- selectFocusedDate() {
257
- setValue(focusedDate);
258
- },
154
+ return {
155
+ isDisabled: props.isDisabled,
156
+ isReadOnly: props.isReadOnly,
157
+ value: calendarDateValue,
158
+ setValue: setValue,
159
+ visibleRange: {
160
+ start: startDate,
161
+ end: endDate
162
+ },
163
+ focusedDate: focusedDate,
164
+ timeZone: timeZone,
165
+ setFocusedDate (date) {
166
+ focusCell(date);
167
+ setFocused(true);
168
+ },
169
+ focusNextDay () {
170
+ focusCell(focusedDate.add({
171
+ days: 1
172
+ }));
173
+ },
174
+ focusPreviousDay () {
175
+ focusCell(focusedDate.subtract({
176
+ days: 1
177
+ }));
178
+ },
179
+ focusNextRow () {
180
+ if (visibleDuration.days) this.focusNextPage();
181
+ else if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) focusCell(focusedDate.add({
182
+ weeks: 1
183
+ }));
184
+ },
185
+ focusPreviousRow () {
186
+ if (visibleDuration.days) this.focusPreviousPage();
187
+ else if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) focusCell(focusedDate.subtract({
188
+ weeks: 1
189
+ }));
190
+ },
191
+ focusNextPage () {
192
+ let start = startDate.add(visibleDuration);
193
+ setFocusedDate($f62d864046160412$export$4f5203c0d889109e(focusedDate.add(visibleDuration), minValue, maxValue));
194
+ setStartDate($f62d864046160412$export$5bb865b12696a77d(focusedDate, start, visibleDuration, locale, minValue, maxValue));
195
+ },
196
+ focusPreviousPage () {
197
+ let start = startDate.subtract(visibleDuration);
198
+ setFocusedDate($f62d864046160412$export$4f5203c0d889109e(focusedDate.subtract(visibleDuration), minValue, maxValue));
199
+ setStartDate($f62d864046160412$export$5bb865b12696a77d(focusedDate, start, visibleDuration, locale, minValue, maxValue));
200
+ },
201
+ focusPageStart () {
202
+ focusCell(startDate);
203
+ },
204
+ focusPageEnd () {
205
+ focusCell(endDate);
206
+ },
207
+ focusNextSection () {
208
+ if (visibleDuration.days) this.focusNextPage();
209
+ else if (visibleDuration.weeks) focusCell(focusedDate.add({
210
+ months: 1
211
+ }));
212
+ else if (visibleDuration.months || visibleDuration.years) focusCell(focusedDate.add({
213
+ years: 1
214
+ }));
215
+ },
216
+ focusPreviousSection () {
217
+ if (visibleDuration.days) this.focusPreviousPage();
218
+ else if (visibleDuration.weeks) focusCell(focusedDate.subtract({
219
+ months: 1
220
+ }));
221
+ else if (visibleDuration.months || visibleDuration.years) focusCell(focusedDate.subtract({
222
+ years: 1
223
+ }));
224
+ },
225
+ selectFocusedDate () {
226
+ setValue(focusedDate);
227
+ },
228
+ selectDate (date) {
229
+ setValue(date);
230
+ },
231
+ isFocused: isFocused,
232
+ setFocused: setFocused,
233
+ isInvalid (date) {
234
+ return $f62d864046160412$export$eac50920cf2fd59a(date, minValue, maxValue);
235
+ },
236
+ isSelected (date) {
237
+ return calendarDateValue != null && $keQhS$isSameDay(date, calendarDateValue) && !this.isCellDisabled(date) && !this.isCellUnavailable(date);
238
+ },
239
+ isCellFocused (date) {
240
+ return isFocused && focusedDate && $keQhS$isSameDay(date, focusedDate);
241
+ },
242
+ isCellDisabled (date) {
243
+ return props.isDisabled || date.compare(startDate) < 0 || date.compare(endDate) > 0 || this.isInvalid(date, minValue, maxValue);
244
+ },
245
+ isCellUnavailable (date) {
246
+ return props.isDateUnavailable && props.isDateUnavailable(date);
247
+ },
248
+ isPreviousVisibleRangeInvalid () {
249
+ return this.isInvalid(startDate.subtract({
250
+ days: 1
251
+ }), minValue, maxValue);
252
+ },
253
+ isNextVisibleRangeInvalid () {
254
+ return this.isInvalid(endDate.add({
255
+ days: 1
256
+ }), minValue, maxValue);
257
+ }
258
+ };
259
+ }
259
260
 
260
- selectDate(date) {
261
- setValue(date);
262
- },
263
261
 
264
- isFocused,
265
- setFocused,
262
+ var $9a36b6ba2fb1a7c5$exports = {};
266
263
 
267
- isInvalid(date) {
268
- return $e9245e22e7d2e30e959129f8619bae$export$isInvalid(date, minValue, maxValue);
269
- },
264
+ $parcel$export($9a36b6ba2fb1a7c5$exports, "useRangeCalendarState", () => $9a36b6ba2fb1a7c5$export$9a987164d97ecc90);
270
265
 
271
- isSelected(date) {
272
- return calendarDateValue != null && isSameDay(date, calendarDateValue);
273
- },
274
266
 
275
- isCellFocused(date) {
276
- return isFocused && focusedDate && isSameDay(date, focusedDate);
277
- },
278
267
 
279
- isCellDisabled(date) {
280
- return props.isDisabled || date.compare(startDate) < 0 || date.compare(endDate) > 0 || $e9245e22e7d2e30e959129f8619bae$export$isInvalid(date, minValue, maxValue);
281
- },
282
268
 
283
- isPreviousVisibleRangeInvalid() {
284
- return $e9245e22e7d2e30e959129f8619bae$export$isInvalid(startDate.subtract({
285
- days: 1
286
- }), minValue, maxValue);
287
- },
288
269
 
289
- isNextVisibleRangeInvalid() {
290
- return $e9245e22e7d2e30e959129f8619bae$export$isInvalid(endDate.add({
291
- days: 1
292
- }), minValue, maxValue);
270
+ function $9a36b6ba2fb1a7c5$export$9a987164d97ecc90(props) {
271
+ let { value: valueProp , defaultValue: defaultValue , onChange: onChange , createCalendar: createCalendar , locale: locale , visibleDuration: visibleDuration = {
272
+ months: 1
273
+ } , minValue: minValue , maxValue: maxValue , ...calendarProps } = props;
274
+ let [value, setValue] = $keQhS$useControlledState(valueProp, defaultValue || null, onChange);
275
+ let [anchorDate, setAnchorDateState] = $keQhS$useState(null);
276
+ let alignment = 'center';
277
+ if (value && value.start && value.end) {
278
+ let start = $f62d864046160412$export$f4a51ff076cc9a09($keQhS$toCalendarDate(value.start), visibleDuration, locale, minValue, maxValue);
279
+ let end = start.add(visibleDuration).subtract({
280
+ days: 1
281
+ });
282
+ if (value.end.compare(end) > 0) alignment = 'start';
293
283
  }
294
-
295
- };
296
- }
297
- export function useRangeCalendarState(props) {
298
- let {
299
- value: valueProp,
300
- defaultValue,
301
- onChange,
302
- createCalendar,
303
- locale,
304
- visibleDuration = {
305
- months: 1
306
- },
307
- minValue,
308
- maxValue
309
- } = props,
310
- calendarProps = _babelRuntimeHelpersEsmObjectWithoutPropertiesLoose(props, ["value", "defaultValue", "onChange", "createCalendar", "locale", "visibleDuration", "minValue", "maxValue"]);
311
-
312
- let [value, setValue] = useControlledState(valueProp, defaultValue, onChange);
313
- let [anchorDate, setAnchorDate] = useState(null);
314
- let alignment = 'center';
315
-
316
- if (value && value.start && value.end) {
317
- let start = $e9245e22e7d2e30e959129f8619bae$export$alignCenter(toCalendarDate(value.start), visibleDuration, locale, minValue, maxValue);
318
- let end = start.add(visibleDuration).subtract({
319
- days: 1
284
+ // Available range must be stored in a ref so we have access to the updated version immediately in `isInvalid`.
285
+ let availableRangeRef = $keQhS$useRef(null);
286
+ let [availableRange, setAvailableRange] = $keQhS$useState(null);
287
+ let min = $keQhS$useMemo(()=>{
288
+ return $keQhS$maxDate(minValue, availableRange === null || availableRange === void 0 ? void 0 : availableRange.start);
289
+ }, [
290
+ minValue,
291
+ availableRange
292
+ ]);
293
+ let max = $keQhS$useMemo(()=>{
294
+ return $keQhS$minDate(maxValue, availableRange === null || availableRange === void 0 ? void 0 : availableRange.end);
295
+ }, [
296
+ maxValue,
297
+ availableRange
298
+ ]);
299
+ let calendar = $131cf43a05231e1e$export$6d095e787d2b5e1f({
300
+ ...calendarProps,
301
+ value: value && value.start,
302
+ createCalendar: createCalendar,
303
+ locale: locale,
304
+ visibleDuration: visibleDuration,
305
+ minValue: min,
306
+ maxValue: max,
307
+ selectionAlignment: alignment
320
308
  });
321
-
322
- if (value.end.compare(end) > 0) {
323
- alignment = 'start';
324
- }
325
- }
326
-
327
- let calendar = useCalendarState(_babelRuntimeHelpersEsmExtends({}, calendarProps, {
328
- value: value && value.start,
329
- createCalendar,
330
- locale,
331
- visibleDuration,
332
- minValue,
333
- maxValue,
334
- selectionAlignment: alignment
335
- }));
336
- let highlightedRange = anchorDate ? $b99e30b96646887f54a48af37fe3b03$var$makeRange(anchorDate, calendar.focusedDate) : value && $b99e30b96646887f54a48af37fe3b03$var$makeRange(value.start, value.end);
337
-
338
- let selectDate = date => {
339
- if (props.isReadOnly) {
340
- return;
341
- }
342
-
343
- if (!anchorDate) {
344
- setAnchorDate(date);
345
- } else {
346
- let range = $b99e30b96646887f54a48af37fe3b03$var$makeRange(anchorDate, date);
347
- setValue({
348
- start: $b99e30b96646887f54a48af37fe3b03$var$convertValue(range.start, value == null ? void 0 : value.start),
349
- end: $b99e30b96646887f54a48af37fe3b03$var$convertValue(range.end, value == null ? void 0 : value.end)
350
- });
351
- setAnchorDate(null);
309
+ let updateAvailableRange = (date)=>{
310
+ if (date && props.isDateUnavailable && !props.allowsNonContiguousRanges) {
311
+ availableRangeRef.current = {
312
+ start: $9a36b6ba2fb1a7c5$var$nextUnavailableDate(date, calendar, -1),
313
+ end: $9a36b6ba2fb1a7c5$var$nextUnavailableDate(date, calendar, 1)
314
+ };
315
+ setAvailableRange(availableRangeRef.current);
316
+ } else {
317
+ availableRangeRef.current = null;
318
+ setAvailableRange(null);
319
+ }
320
+ };
321
+ // If the visible range changes, we need to update the available range.
322
+ let lastVisibleRange = $keQhS$useRef(calendar.visibleRange);
323
+ if (!$keQhS$isEqualDay(calendar.visibleRange.start, lastVisibleRange.current.start) || !$keQhS$isEqualDay(calendar.visibleRange.end, lastVisibleRange.current.end)) {
324
+ updateAvailableRange(anchorDate);
325
+ lastVisibleRange.current = calendar.visibleRange;
352
326
  }
353
- };
354
-
355
- let [isDragging, setDragging] = useState(false);
356
- return _babelRuntimeHelpersEsmExtends({}, calendar, {
357
- value,
358
- setValue,
359
- anchorDate,
360
- setAnchorDate,
361
- highlightedRange,
362
-
363
- selectFocusedDate() {
364
- selectDate(calendar.focusedDate);
365
- },
366
-
367
- selectDate,
368
-
369
- highlightDate(date) {
370
- if (anchorDate) {
371
- calendar.setFocusedDate(date);
372
- }
373
- },
374
-
375
- isSelected(date) {
376
- return highlightedRange && date.compare(highlightedRange.start) >= 0 && date.compare(highlightedRange.end) <= 0;
377
- },
378
-
379
- isDragging,
380
- setDragging
381
- });
327
+ let setAnchorDate = (date)=>{
328
+ if (date) {
329
+ setAnchorDateState(date);
330
+ updateAvailableRange(date);
331
+ } else {
332
+ setAnchorDateState(null);
333
+ updateAvailableRange(null);
334
+ }
335
+ };
336
+ let highlightedRange = anchorDate ? $9a36b6ba2fb1a7c5$var$makeRange(anchorDate, calendar.focusedDate) : value && $9a36b6ba2fb1a7c5$var$makeRange(value.start, value.end);
337
+ let selectDate = (date)=>{
338
+ if (props.isReadOnly) return;
339
+ if (!anchorDate) setAnchorDate(date);
340
+ else {
341
+ let range = $9a36b6ba2fb1a7c5$var$makeRange(anchorDate, date);
342
+ setValue({
343
+ start: $9a36b6ba2fb1a7c5$var$convertValue(range.start, value === null || value === void 0 ? void 0 : value.start),
344
+ end: $9a36b6ba2fb1a7c5$var$convertValue(range.end, value === null || value === void 0 ? void 0 : value.end)
345
+ });
346
+ setAnchorDate(null);
347
+ }
348
+ };
349
+ let [isDragging, setDragging] = $keQhS$useState(false);
350
+ return {
351
+ ...calendar,
352
+ value: value,
353
+ setValue: setValue,
354
+ anchorDate: anchorDate,
355
+ setAnchorDate: setAnchorDate,
356
+ highlightedRange: highlightedRange,
357
+ selectFocusedDate () {
358
+ selectDate(calendar.focusedDate);
359
+ },
360
+ selectDate: selectDate,
361
+ highlightDate (date) {
362
+ if (anchorDate) calendar.setFocusedDate(date);
363
+ },
364
+ isSelected (date) {
365
+ return highlightedRange && date.compare(highlightedRange.start) >= 0 && date.compare(highlightedRange.end) <= 0 && !calendar.isCellDisabled(date) && !calendar.isCellUnavailable(date);
366
+ },
367
+ isInvalid (date) {
368
+ var ref, ref1;
369
+ return calendar.isInvalid(date) || $f62d864046160412$export$eac50920cf2fd59a(date, (ref = availableRangeRef.current) === null || ref === void 0 ? void 0 : ref.start, (ref1 = availableRangeRef.current) === null || ref1 === void 0 ? void 0 : ref1.end);
370
+ },
371
+ isDragging: isDragging,
372
+ setDragging: setDragging
373
+ };
382
374
  }
383
-
384
- function $b99e30b96646887f54a48af37fe3b03$var$makeRange(start, end) {
385
- if (!start || !end) {
375
+ function $9a36b6ba2fb1a7c5$var$makeRange(start, end) {
376
+ if (!start || !end) return null;
377
+ if (end.compare(start) < 0) [start, end] = [
378
+ end,
379
+ start
380
+ ];
381
+ return {
382
+ start: $keQhS$toCalendarDate(start),
383
+ end: $keQhS$toCalendarDate(end)
384
+ };
385
+ }
386
+ function $9a36b6ba2fb1a7c5$var$convertValue(newValue, oldValue) {
387
+ // The display calendar should not have any effect on the emitted value.
388
+ // Emit dates in the same calendar as the original value, if any, otherwise gregorian.
389
+ newValue = $keQhS$toCalendar(newValue, (oldValue === null || oldValue === void 0 ? void 0 : oldValue.calendar) || new $keQhS$GregorianCalendar());
390
+ // Preserve time if the input value had one.
391
+ if (oldValue && 'hour' in oldValue) return oldValue.set(newValue);
392
+ return newValue;
393
+ }
394
+ function $9a36b6ba2fb1a7c5$var$nextUnavailableDate(anchorDate, state, dir) {
395
+ let nextDate = anchorDate.add({
396
+ days: dir
397
+ });
398
+ while((dir < 0 ? nextDate.compare(state.visibleRange.start) >= 0 : nextDate.compare(state.visibleRange.end) <= 0) && !state.isCellUnavailable(nextDate))nextDate = nextDate.add({
399
+ days: dir
400
+ });
401
+ if (state.isCellUnavailable(nextDate)) return nextDate.add({
402
+ days: -dir
403
+ });
386
404
  return null;
387
- }
388
-
389
- if (end.compare(start) < 0) {
390
- [start, end] = [end, start];
391
- }
392
-
393
- return {
394
- start: toCalendarDate(start),
395
- end: toCalendarDate(end)
396
- };
397
405
  }
398
406
 
399
- function $b99e30b96646887f54a48af37fe3b03$var$convertValue(newValue, oldValue) {
400
- // The display calendar should not have any effect on the emitted value.
401
- // Emit dates in the same calendar as the original value, if any, otherwise gregorian.
402
- newValue = toCalendar(newValue, (oldValue == null ? void 0 : oldValue.calendar) || new GregorianCalendar()); // Preserve time if the input value had one.
403
407
 
404
- if (oldValue && 'hour' in oldValue) {
405
- return oldValue.set(newValue);
406
- }
407
408
 
408
- return newValue;
409
- }
409
+
410
+ export {$131cf43a05231e1e$export$6d095e787d2b5e1f as useCalendarState, $9a36b6ba2fb1a7c5$export$9a987164d97ecc90 as useRangeCalendarState};
410
411
  //# sourceMappingURL=module.js.map