@react-stately/calendar
Advanced tools
Comparing version 3.0.0-alpha.1 to 3.0.0-alpha.2
736
dist/main.js
@@ -1,444 +0,368 @@ | ||
var _babelRuntimeHelpersObjectWithoutPropertiesLoose = $parcel$interopDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose")); | ||
var $8fZ0X$internationalizeddate = require("@internationalized/date"); | ||
var $8fZ0X$reactstatelyutils = require("@react-stately/utils"); | ||
var $8fZ0X$reactariai18n = require("@react-aria/i18n"); | ||
var $8fZ0X$react = require("react"); | ||
var { | ||
useEffect, | ||
useMemo, | ||
useRef, | ||
useState | ||
} = require("react"); | ||
function $parcel$exportWildcard(dest, source) { | ||
Object.keys(source).forEach(function(key) { | ||
if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) { | ||
return; | ||
} | ||
var { | ||
useDateFormatter | ||
} = require("@react-aria/i18n"); | ||
Object.defineProperty(dest, key, { | ||
enumerable: true, | ||
get: function get() { | ||
return source[key]; | ||
} | ||
}); | ||
}); | ||
var { | ||
useControlledState | ||
} = require("@react-stately/utils"); | ||
var { | ||
maxDate, | ||
minDate, | ||
startOfMonth, | ||
startOfWeek, | ||
startOfYear, | ||
toCalendarDate, | ||
GregorianCalendar, | ||
isSameDay, | ||
toCalendar, | ||
today | ||
} = require("@internationalized/date"); | ||
var _babelRuntimeHelpersExtends = $parcel$interopDefault(require("@babel/runtime/helpers/extends")); | ||
function $parcel$interopDefault(a) { | ||
return a && a.__esModule ? a.default : a; | ||
return dest; | ||
} | ||
function $cb3c71934523b9f11dab70388c912b98$export$isInvalid(date, minValue, maxValue) { | ||
return minValue != null && date.compare(minValue) < 0 || maxValue != null && date.compare(maxValue) > 0; | ||
function $parcel$export(e, n, v, s) { | ||
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true}); | ||
} | ||
var $b6932658840cac06$exports = {}; | ||
function $cb3c71934523b9f11dab70388c912b98$export$alignCenter(date, duration, locale, minValue, maxValue) { | ||
let halfDuration = {}; | ||
for (let key in duration) { | ||
halfDuration[key] = Math.floor(duration[key] / 2); | ||
var $f61c34523ac023ce$exports = {}; | ||
if (halfDuration[key] > 0 && duration[key] % 2 === 0) { | ||
halfDuration[key]--; | ||
} | ||
} | ||
$parcel$export($f61c34523ac023ce$exports, "useCalendarState", () => $f61c34523ac023ce$export$6d095e787d2b5e1f); | ||
let aligned = $cb3c71934523b9f11dab70388c912b98$export$alignStart(date, duration, locale).subtract(halfDuration); | ||
return $cb3c71934523b9f11dab70388c912b98$export$constrainStart(date, aligned, duration, locale, minValue, maxValue); | ||
function $7e122b6ea38c4913$export$eac50920cf2fd59a(date, minValue, maxValue) { | ||
return minValue != null && date.compare(minValue) < 0 || maxValue != null && date.compare(maxValue) > 0; | ||
} | ||
function $cb3c71934523b9f11dab70388c912b98$export$alignStart(date, duration, locale, minValue, maxValue) { | ||
// align to the start of the largest unit | ||
let aligned = date; | ||
if (duration.years) { | ||
aligned = startOfYear(date); | ||
} else if (duration.months) { | ||
aligned = startOfMonth(date); | ||
} else if (duration.weeks) { | ||
aligned = startOfWeek(date, locale); | ||
} | ||
return $cb3c71934523b9f11dab70388c912b98$export$constrainStart(date, aligned, duration, locale, minValue, maxValue); | ||
function $7e122b6ea38c4913$export$f4a51ff076cc9a09(date, duration, locale, minValue, maxValue) { | ||
let halfDuration = { | ||
}; | ||
for(let key in duration){ | ||
halfDuration[key] = Math.floor(duration[key] / 2); | ||
if (halfDuration[key] > 0 && duration[key] % 2 === 0) halfDuration[key]--; | ||
} | ||
let aligned = $7e122b6ea38c4913$export$144a00ba6044eb9(date, duration, locale).subtract(halfDuration); | ||
return $7e122b6ea38c4913$export$5bb865b12696a77d(date, aligned, duration, locale, minValue, maxValue); | ||
} | ||
function $cb3c71934523b9f11dab70388c912b98$export$alignEnd(date, duration, locale, minValue, maxValue) { | ||
let d = _babelRuntimeHelpersExtends({}, duration); // subtract 1 from the smallest unit | ||
if (duration.days) { | ||
d.days--; | ||
} else if (duration.weeks) { | ||
d.weeks--; | ||
} else if (duration.months) { | ||
d.months--; | ||
} else if (duration.years) { | ||
d.years--; | ||
} | ||
let aligned = $cb3c71934523b9f11dab70388c912b98$export$alignStart(date, duration, locale).subtract(d); | ||
return $cb3c71934523b9f11dab70388c912b98$export$constrainStart(date, aligned, duration, locale, minValue, maxValue); | ||
function $7e122b6ea38c4913$export$144a00ba6044eb9(date, duration, locale, minValue, maxValue) { | ||
// align to the start of the largest unit | ||
let aligned = date; | ||
if (duration.years) aligned = $8fZ0X$internationalizeddate.startOfYear(date); | ||
else if (duration.months) aligned = $8fZ0X$internationalizeddate.startOfMonth(date); | ||
else if (duration.weeks) aligned = $8fZ0X$internationalizeddate.startOfWeek(date, locale); | ||
return $7e122b6ea38c4913$export$5bb865b12696a77d(date, aligned, duration, locale, minValue, maxValue); | ||
} | ||
function $cb3c71934523b9f11dab70388c912b98$export$constrainStart(date, aligned, duration, locale, minValue, maxValue) { | ||
if (minValue && date.compare(minValue) >= 0) { | ||
aligned = maxDate(aligned, $cb3c71934523b9f11dab70388c912b98$export$alignStart(toCalendarDate(minValue), duration, locale)); | ||
} | ||
if (maxValue && date.compare(maxValue) <= 0) { | ||
aligned = minDate(aligned, $cb3c71934523b9f11dab70388c912b98$export$alignEnd(toCalendarDate(maxValue), duration, locale)); | ||
} | ||
return aligned; | ||
function $7e122b6ea38c4913$export$530edbfc915b2b04(date, duration, locale, minValue, maxValue) { | ||
let d = { | ||
...duration | ||
}; | ||
// subtract 1 from the smallest unit | ||
if (duration.days) d.days--; | ||
else if (duration.weeks) d.weeks--; | ||
else if (duration.months) d.months--; | ||
else if (duration.years) d.years--; | ||
let aligned = $7e122b6ea38c4913$export$144a00ba6044eb9(date, duration, locale).subtract(d); | ||
return $7e122b6ea38c4913$export$5bb865b12696a77d(date, aligned, duration, locale, minValue, maxValue); | ||
} | ||
function $cb3c71934523b9f11dab70388c912b98$export$constrainValue(date, minValue, maxValue) { | ||
if (minValue) { | ||
date = maxDate(date, toCalendarDate(minValue)); | ||
} | ||
if (maxValue) { | ||
date = minDate(date, toCalendarDate(maxValue)); | ||
} | ||
return date; | ||
function $7e122b6ea38c4913$export$5bb865b12696a77d(date, aligned, duration, locale, minValue, maxValue) { | ||
if (minValue && date.compare(minValue) >= 0) aligned = $8fZ0X$internationalizeddate.maxDate(aligned, $7e122b6ea38c4913$export$144a00ba6044eb9($8fZ0X$internationalizeddate.toCalendarDate(minValue), duration, locale)); | ||
if (maxValue && date.compare(maxValue) <= 0) aligned = $8fZ0X$internationalizeddate.minDate(aligned, $7e122b6ea38c4913$export$530edbfc915b2b04($8fZ0X$internationalizeddate.toCalendarDate(maxValue), duration, locale)); | ||
return aligned; | ||
} | ||
function $7e122b6ea38c4913$export$4f5203c0d889109e(date, minValue, maxValue) { | ||
if (minValue) date = $8fZ0X$internationalizeddate.maxDate(date, $8fZ0X$internationalizeddate.toCalendarDate(minValue)); | ||
if (maxValue) date = $8fZ0X$internationalizeddate.minDate(date, $8fZ0X$internationalizeddate.toCalendarDate(maxValue)); | ||
return date; | ||
} | ||
function useCalendarState(props) { | ||
let defaultFormatter = useDateFormatter(); | ||
let resolvedOptions = useMemo(() => defaultFormatter.resolvedOptions(), [defaultFormatter]); | ||
let { | ||
locale, | ||
createCalendar, | ||
timeZone = resolvedOptions.timeZone, | ||
visibleDuration = { | ||
months: 1 | ||
}, | ||
minValue, | ||
maxValue, | ||
selectionAlignment | ||
} = props; | ||
let calendar = useMemo(() => createCalendar(resolvedOptions.calendar), [createCalendar, resolvedOptions.calendar]); | ||
let [value, setControlledValue] = useControlledState(props.value, props.defaultValue, props.onChange); | ||
let calendarDateValue = useMemo(() => value ? toCalendar(toCalendarDate(value), calendar) : null, [value, calendar]); | ||
let defaultDate = useMemo(() => calendarDateValue || $cb3c71934523b9f11dab70388c912b98$export$constrainValue(toCalendar(today(timeZone), calendar), minValue, maxValue), [calendarDateValue, timeZone, calendar, minValue, maxValue]); | ||
let [startDate, setStartDate] = useState(() => { | ||
switch (selectionAlignment) { | ||
case 'start': | ||
return $cb3c71934523b9f11dab70388c912b98$export$alignStart(defaultDate, visibleDuration, locale, minValue, maxValue); | ||
case 'end': | ||
return $cb3c71934523b9f11dab70388c912b98$export$alignEnd(defaultDate, visibleDuration, locale, minValue, maxValue); | ||
case 'center': | ||
default: | ||
return $cb3c71934523b9f11dab70388c912b98$export$alignCenter(defaultDate, visibleDuration, locale, minValue, maxValue); | ||
} | ||
}); | ||
let [focusedDate, setFocusedDate] = useState(defaultDate); | ||
let [isFocused, setFocused] = useState(props.autoFocus || false); | ||
let endDate = useMemo(() => startDate.add(visibleDuration).subtract({ | ||
days: 1 | ||
}), [startDate, visibleDuration]); // Reset focused date and visible range when calendar changes. | ||
let lastCalendarIdentifier = useRef(calendar.identifier); | ||
useEffect(() => { | ||
if (calendar.identifier !== lastCalendarIdentifier.current) { | ||
let newFocusedDate = toCalendar(focusedDate, calendar); | ||
setStartDate($cb3c71934523b9f11dab70388c912b98$export$alignCenter(newFocusedDate, visibleDuration, locale, minValue, maxValue)); | ||
setFocusedDate(newFocusedDate); | ||
lastCalendarIdentifier.current = calendar.identifier; | ||
} | ||
}, [calendar, focusedDate, visibleDuration, locale, minValue, maxValue]); // Sets focus to a specific cell date | ||
function focusCell(date) { | ||
// date = constrain(focusedDate, date, visibleDuration, locale, minValue, maxValue); | ||
date = $cb3c71934523b9f11dab70388c912b98$export$constrainValue(date, minValue, maxValue); | ||
let next = startDate.add(visibleDuration); | ||
if (date.compare(startDate) < 0) { | ||
setStartDate($cb3c71934523b9f11dab70388c912b98$export$alignEnd(date, visibleDuration, locale, minValue, maxValue)); | ||
} else if (date.compare(next) >= 0) { | ||
setStartDate($cb3c71934523b9f11dab70388c912b98$export$alignStart(date, visibleDuration, locale, minValue, maxValue)); | ||
function $f61c34523ac023ce$export$6d095e787d2b5e1f(props) { | ||
let defaultFormatter = $8fZ0X$reactariai18n.useDateFormatter(); | ||
let resolvedOptions = $8fZ0X$react.useMemo(()=>defaultFormatter.resolvedOptions() | ||
, [ | ||
defaultFormatter | ||
]); | ||
let { locale: locale , createCalendar: createCalendar , visibleDuration: visibleDuration = { | ||
months: 1 | ||
} , minValue: minValue , maxValue: maxValue , selectionAlignment: selectionAlignment } = props; | ||
let calendar = $8fZ0X$react.useMemo(()=>createCalendar(resolvedOptions.calendar) | ||
, [ | ||
createCalendar, | ||
resolvedOptions.calendar | ||
]); | ||
let [value, setControlledValue] = $8fZ0X$reactstatelyutils.useControlledState(props.value, props.defaultValue, props.onChange); | ||
let calendarDateValue = $8fZ0X$react.useMemo(()=>value ? $8fZ0X$internationalizeddate.toCalendar($8fZ0X$internationalizeddate.toCalendarDate(value), calendar) : null | ||
, [ | ||
value, | ||
calendar | ||
]); | ||
let timeZone = $8fZ0X$react.useMemo(()=>value && 'timeZone' in value ? value.timeZone : resolvedOptions.timeZone | ||
, [ | ||
value, | ||
resolvedOptions.timeZone | ||
]); | ||
let defaultDate = $8fZ0X$react.useMemo(()=>calendarDateValue || $7e122b6ea38c4913$export$4f5203c0d889109e($8fZ0X$internationalizeddate.toCalendar($8fZ0X$internationalizeddate.today(timeZone), calendar), minValue, maxValue) | ||
, [ | ||
calendarDateValue, | ||
timeZone, | ||
calendar, | ||
minValue, | ||
maxValue | ||
]); | ||
let [startDate, setStartDate] = $8fZ0X$react.useState(()=>{ | ||
switch(selectionAlignment){ | ||
case 'start': | ||
return $7e122b6ea38c4913$export$144a00ba6044eb9(defaultDate, visibleDuration, locale, minValue, maxValue); | ||
case 'end': | ||
return $7e122b6ea38c4913$export$530edbfc915b2b04(defaultDate, visibleDuration, locale, minValue, maxValue); | ||
case 'center': | ||
default: | ||
return $7e122b6ea38c4913$export$f4a51ff076cc9a09(defaultDate, visibleDuration, locale, minValue, maxValue); | ||
} | ||
}); | ||
let [focusedDate, setFocusedDate] = $8fZ0X$react.useState(defaultDate); | ||
let [isFocused, setFocused] = $8fZ0X$react.useState(props.autoFocus || false); | ||
let endDate = $8fZ0X$react.useMemo(()=>startDate.add(visibleDuration).subtract({ | ||
days: 1 | ||
}) | ||
, [ | ||
startDate, | ||
visibleDuration | ||
]); | ||
// Reset focused date and visible range when calendar changes. | ||
let lastCalendarIdentifier = $8fZ0X$react.useRef(calendar.identifier); | ||
$8fZ0X$react.useEffect(()=>{ | ||
if (calendar.identifier !== lastCalendarIdentifier.current) { | ||
let newFocusedDate = $8fZ0X$internationalizeddate.toCalendar(focusedDate, calendar); | ||
setStartDate($7e122b6ea38c4913$export$f4a51ff076cc9a09(newFocusedDate, visibleDuration, locale, minValue, maxValue)); | ||
setFocusedDate(newFocusedDate); | ||
lastCalendarIdentifier.current = calendar.identifier; | ||
} | ||
}, [ | ||
calendar, | ||
focusedDate, | ||
visibleDuration, | ||
locale, | ||
minValue, | ||
maxValue | ||
]); | ||
// Sets focus to a specific cell date | ||
function focusCell(date) { | ||
// date = constrain(focusedDate, date, visibleDuration, locale, minValue, maxValue); | ||
date = $7e122b6ea38c4913$export$4f5203c0d889109e(date, minValue, maxValue); | ||
let next = startDate.add(visibleDuration); | ||
if (date.compare(startDate) < 0) setStartDate($7e122b6ea38c4913$export$530edbfc915b2b04(date, visibleDuration, locale, minValue, maxValue)); | ||
else if (date.compare(next) >= 0) setStartDate($7e122b6ea38c4913$export$144a00ba6044eb9(date, visibleDuration, locale, minValue, maxValue)); | ||
setFocusedDate(date); | ||
} | ||
setFocusedDate(date); | ||
} | ||
function setValue(newValue) { | ||
if (!props.isDisabled && !props.isReadOnly) { | ||
// The display calendar should not have any effect on the emitted value. | ||
// Emit dates in the same calendar as the original value, if any, otherwise gregorian. | ||
newValue = toCalendar(newValue, (value == null ? void 0 : value.calendar) || new GregorianCalendar()); // Preserve time if the input value had one. | ||
if (value && 'hour' in value) { | ||
setControlledValue(value.set(newValue)); | ||
} else { | ||
setControlledValue(newValue); | ||
} | ||
function setValue(newValue) { | ||
if (!props.isDisabled && !props.isReadOnly) { | ||
// The display calendar should not have any effect on the emitted value. | ||
// Emit dates in the same calendar as the original value, if any, otherwise gregorian. | ||
newValue = $8fZ0X$internationalizeddate.toCalendar(newValue, value?.calendar || new $8fZ0X$internationalizeddate.GregorianCalendar()); | ||
// Preserve time if the input value had one. | ||
if (value && 'hour' in value) setControlledValue(value.set(newValue)); | ||
else setControlledValue(newValue); | ||
} | ||
} | ||
} | ||
return { | ||
isDisabled: props.isDisabled, | ||
isReadOnly: props.isReadOnly, | ||
value: calendarDateValue, | ||
setValue: setValue, | ||
visibleRange: { | ||
start: startDate, | ||
end: endDate | ||
}, | ||
focusedDate: focusedDate, | ||
timeZone: timeZone, | ||
setFocusedDate (date) { | ||
setFocusedDate(date); | ||
setFocused(true); | ||
}, | ||
focusNextDay () { | ||
focusCell(focusedDate.add({ | ||
days: 1 | ||
})); | ||
}, | ||
focusPreviousDay () { | ||
focusCell(focusedDate.subtract({ | ||
days: 1 | ||
})); | ||
}, | ||
focusNextRow () { | ||
if (visibleDuration.days) this.focusNextPage(); | ||
else if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) focusCell(focusedDate.add({ | ||
weeks: 1 | ||
})); | ||
}, | ||
focusPreviousRow () { | ||
if (visibleDuration.days) this.focusPreviousPage(); | ||
else if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) focusCell(focusedDate.subtract({ | ||
weeks: 1 | ||
})); | ||
}, | ||
focusNextPage () { | ||
let start = startDate.add(visibleDuration); | ||
setStartDate($7e122b6ea38c4913$export$5bb865b12696a77d(focusedDate, start, visibleDuration, locale, minValue, maxValue)); | ||
setFocusedDate($7e122b6ea38c4913$export$4f5203c0d889109e(focusedDate.add(visibleDuration), minValue, maxValue)); | ||
}, | ||
focusPreviousPage () { | ||
let start = startDate.subtract(visibleDuration); | ||
setStartDate($7e122b6ea38c4913$export$5bb865b12696a77d(focusedDate, start, visibleDuration, locale, minValue, maxValue)); | ||
setFocusedDate($7e122b6ea38c4913$export$4f5203c0d889109e(focusedDate.subtract(visibleDuration), minValue, maxValue)); | ||
}, | ||
focusPageStart () { | ||
focusCell(startDate); | ||
}, | ||
focusPageEnd () { | ||
focusCell(endDate); | ||
}, | ||
focusNextSection () { | ||
if (visibleDuration.days) this.focusNextPage(); | ||
else if (visibleDuration.weeks) focusCell(focusedDate.add({ | ||
months: 1 | ||
})); | ||
else if (visibleDuration.months || visibleDuration.years) focusCell(focusedDate.add({ | ||
years: 1 | ||
})); | ||
}, | ||
focusPreviousSection () { | ||
if (visibleDuration.days) this.focusPreviousPage(); | ||
else if (visibleDuration.weeks) focusCell(focusedDate.subtract({ | ||
months: 1 | ||
})); | ||
else if (visibleDuration.months || visibleDuration.years) focusCell(focusedDate.subtract({ | ||
years: 1 | ||
})); | ||
}, | ||
selectFocusedDate () { | ||
setValue(focusedDate); | ||
}, | ||
selectDate (date) { | ||
setValue(date); | ||
}, | ||
isFocused: isFocused, | ||
setFocused: setFocused, | ||
isInvalid (date) { | ||
return $7e122b6ea38c4913$export$eac50920cf2fd59a(date, minValue, maxValue); | ||
}, | ||
isSelected (date) { | ||
return calendarDateValue != null && $8fZ0X$internationalizeddate.isSameDay(date, calendarDateValue); | ||
}, | ||
isCellFocused (date) { | ||
return isFocused && focusedDate && $8fZ0X$internationalizeddate.isSameDay(date, focusedDate); | ||
}, | ||
isCellDisabled (date) { | ||
return props.isDisabled || date.compare(startDate) < 0 || date.compare(endDate) > 0 || $7e122b6ea38c4913$export$eac50920cf2fd59a(date, minValue, maxValue); | ||
}, | ||
isPreviousVisibleRangeInvalid () { | ||
return $7e122b6ea38c4913$export$eac50920cf2fd59a(startDate.subtract({ | ||
days: 1 | ||
}), minValue, maxValue); | ||
}, | ||
isNextVisibleRangeInvalid () { | ||
return $7e122b6ea38c4913$export$eac50920cf2fd59a(endDate.add({ | ||
days: 1 | ||
}), minValue, maxValue); | ||
} | ||
}; | ||
} | ||
return { | ||
isDisabled: props.isDisabled, | ||
isReadOnly: props.isReadOnly, | ||
value: calendarDateValue, | ||
setValue, | ||
visibleRange: { | ||
start: startDate, | ||
end: endDate | ||
}, | ||
focusedDate, | ||
timeZone, | ||
setFocusedDate(date) { | ||
setFocusedDate(date); | ||
setFocused(true); | ||
}, | ||
var $794e8b1784854410$exports = {}; | ||
focusNextDay() { | ||
focusCell(focusedDate.add({ | ||
days: 1 | ||
})); | ||
}, | ||
$parcel$export($794e8b1784854410$exports, "useRangeCalendarState", () => $794e8b1784854410$export$9a987164d97ecc90); | ||
focusPreviousDay() { | ||
focusCell(focusedDate.subtract({ | ||
days: 1 | ||
})); | ||
}, | ||
focusNextRow() { | ||
if (visibleDuration.days) { | ||
this.focusNextPage(); | ||
} else if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) { | ||
focusCell(focusedDate.add({ | ||
weeks: 1 | ||
})); | ||
} | ||
}, | ||
focusPreviousRow() { | ||
if (visibleDuration.days) { | ||
this.focusPreviousPage(); | ||
} else if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) { | ||
focusCell(focusedDate.subtract({ | ||
weeks: 1 | ||
})); | ||
} | ||
}, | ||
focusNextPage() { | ||
let start = startDate.add(visibleDuration); | ||
setStartDate($cb3c71934523b9f11dab70388c912b98$export$constrainStart(focusedDate, start, visibleDuration, locale, minValue, maxValue)); | ||
setFocusedDate($cb3c71934523b9f11dab70388c912b98$export$constrainValue(focusedDate.add(visibleDuration), minValue, maxValue)); | ||
}, | ||
focusPreviousPage() { | ||
let start = startDate.subtract(visibleDuration); | ||
setStartDate($cb3c71934523b9f11dab70388c912b98$export$constrainStart(focusedDate, start, visibleDuration, locale, minValue, maxValue)); | ||
setFocusedDate($cb3c71934523b9f11dab70388c912b98$export$constrainValue(focusedDate.subtract(visibleDuration), minValue, maxValue)); | ||
}, | ||
focusPageStart() { | ||
focusCell(startDate); | ||
}, | ||
focusPageEnd() { | ||
focusCell(endDate); | ||
}, | ||
focusNextSection() { | ||
if (visibleDuration.days) { | ||
this.focusNextPage(); | ||
} else if (visibleDuration.weeks) { | ||
focusCell(focusedDate.add({ | ||
months: 1 | ||
})); | ||
} else if (visibleDuration.months || visibleDuration.years) { | ||
focusCell(focusedDate.add({ | ||
years: 1 | ||
})); | ||
} | ||
}, | ||
focusPreviousSection() { | ||
if (visibleDuration.days) { | ||
this.focusPreviousPage(); | ||
} else if (visibleDuration.weeks) { | ||
focusCell(focusedDate.subtract({ | ||
months: 1 | ||
})); | ||
} else if (visibleDuration.months || visibleDuration.years) { | ||
focusCell(focusedDate.subtract({ | ||
years: 1 | ||
})); | ||
} | ||
}, | ||
selectFocusedDate() { | ||
setValue(focusedDate); | ||
}, | ||
selectDate(date) { | ||
setValue(date); | ||
}, | ||
isFocused, | ||
setFocused, | ||
isInvalid(date) { | ||
return $cb3c71934523b9f11dab70388c912b98$export$isInvalid(date, minValue, maxValue); | ||
}, | ||
isSelected(date) { | ||
return calendarDateValue != null && isSameDay(date, calendarDateValue); | ||
}, | ||
isCellFocused(date) { | ||
return isFocused && focusedDate && isSameDay(date, focusedDate); | ||
}, | ||
isCellDisabled(date) { | ||
return props.isDisabled || date.compare(startDate) < 0 || date.compare(endDate) > 0 || $cb3c71934523b9f11dab70388c912b98$export$isInvalid(date, minValue, maxValue); | ||
}, | ||
isPreviousVisibleRangeInvalid() { | ||
return $cb3c71934523b9f11dab70388c912b98$export$isInvalid(startDate.subtract({ | ||
days: 1 | ||
}), minValue, maxValue); | ||
}, | ||
isNextVisibleRangeInvalid() { | ||
return $cb3c71934523b9f11dab70388c912b98$export$isInvalid(endDate.add({ | ||
days: 1 | ||
}), minValue, maxValue); | ||
function $794e8b1784854410$export$9a987164d97ecc90(props) { | ||
let { value: valueProp , defaultValue: defaultValue , onChange: onChange , createCalendar: createCalendar , locale: locale , visibleDuration: visibleDuration = { | ||
months: 1 | ||
} , minValue: minValue , maxValue: maxValue , ...calendarProps } = props; | ||
let [value, setValue] = $8fZ0X$reactstatelyutils.useControlledState(valueProp, defaultValue, onChange); | ||
let [anchorDate, setAnchorDate] = $8fZ0X$react.useState(null); | ||
let alignment = 'center'; | ||
if (value && value.start && value.end) { | ||
let start = $7e122b6ea38c4913$export$f4a51ff076cc9a09($8fZ0X$internationalizeddate.toCalendarDate(value.start), visibleDuration, locale, minValue, maxValue); | ||
let end = start.add(visibleDuration).subtract({ | ||
days: 1 | ||
}); | ||
if (value.end.compare(end) > 0) alignment = 'start'; | ||
} | ||
}; | ||
} | ||
exports.useCalendarState = useCalendarState; | ||
function useRangeCalendarState(props) { | ||
let { | ||
value: valueProp, | ||
defaultValue, | ||
onChange, | ||
createCalendar, | ||
locale, | ||
visibleDuration = { | ||
months: 1 | ||
}, | ||
minValue, | ||
maxValue | ||
} = props, | ||
calendarProps = _babelRuntimeHelpersObjectWithoutPropertiesLoose(props, ["value", "defaultValue", "onChange", "createCalendar", "locale", "visibleDuration", "minValue", "maxValue"]); | ||
let [value, setValue] = useControlledState(valueProp, defaultValue, onChange); | ||
let [anchorDate, setAnchorDate] = useState(null); | ||
let alignment = 'center'; | ||
if (value && value.start && value.end) { | ||
let start = $cb3c71934523b9f11dab70388c912b98$export$alignCenter(toCalendarDate(value.start), visibleDuration, locale, minValue, maxValue); | ||
let end = start.add(visibleDuration).subtract({ | ||
days: 1 | ||
let calendar = $f61c34523ac023ce$export$6d095e787d2b5e1f({ | ||
...calendarProps, | ||
value: value && value.start, | ||
createCalendar: createCalendar, | ||
locale: locale, | ||
visibleDuration: visibleDuration, | ||
minValue: minValue, | ||
maxValue: maxValue, | ||
selectionAlignment: alignment | ||
}); | ||
if (value.end.compare(end) > 0) { | ||
alignment = 'start'; | ||
} | ||
} | ||
let calendar = useCalendarState(_babelRuntimeHelpersExtends({}, calendarProps, { | ||
value: value && value.start, | ||
createCalendar, | ||
locale, | ||
visibleDuration, | ||
minValue, | ||
maxValue, | ||
selectionAlignment: alignment | ||
})); | ||
let highlightedRange = anchorDate ? $baa4c168da5893215a2941ab7b17cc3d$var$makeRange(anchorDate, calendar.focusedDate) : value && $baa4c168da5893215a2941ab7b17cc3d$var$makeRange(value.start, value.end); | ||
let selectDate = date => { | ||
if (props.isReadOnly) { | ||
return; | ||
} | ||
if (!anchorDate) { | ||
setAnchorDate(date); | ||
} else { | ||
let range = $baa4c168da5893215a2941ab7b17cc3d$var$makeRange(anchorDate, date); | ||
setValue({ | ||
start: $baa4c168da5893215a2941ab7b17cc3d$var$convertValue(range.start, value == null ? void 0 : value.start), | ||
end: $baa4c168da5893215a2941ab7b17cc3d$var$convertValue(range.end, value == null ? void 0 : value.end) | ||
}); | ||
setAnchorDate(null); | ||
} | ||
}; | ||
let [isDragging, setDragging] = useState(false); | ||
return _babelRuntimeHelpersExtends({}, calendar, { | ||
value, | ||
setValue, | ||
anchorDate, | ||
setAnchorDate, | ||
highlightedRange, | ||
selectFocusedDate() { | ||
selectDate(calendar.focusedDate); | ||
}, | ||
selectDate, | ||
highlightDate(date) { | ||
if (anchorDate) { | ||
calendar.setFocusedDate(date); | ||
} | ||
}, | ||
isSelected(date) { | ||
return highlightedRange && date.compare(highlightedRange.start) >= 0 && date.compare(highlightedRange.end) <= 0; | ||
}, | ||
isDragging, | ||
setDragging | ||
}); | ||
let highlightedRange = anchorDate ? $794e8b1784854410$var$makeRange(anchorDate, calendar.focusedDate) : value && $794e8b1784854410$var$makeRange(value.start, value.end); | ||
let selectDate = (date)=>{ | ||
if (props.isReadOnly) return; | ||
if (!anchorDate) setAnchorDate(date); | ||
else { | ||
let range = $794e8b1784854410$var$makeRange(anchorDate, date); | ||
setValue({ | ||
start: $794e8b1784854410$var$convertValue(range.start, value?.start), | ||
end: $794e8b1784854410$var$convertValue(range.end, value?.end) | ||
}); | ||
setAnchorDate(null); | ||
} | ||
}; | ||
let [isDragging, setDragging] = $8fZ0X$react.useState(false); | ||
return { | ||
...calendar, | ||
value: value, | ||
setValue: setValue, | ||
anchorDate: anchorDate, | ||
setAnchorDate: setAnchorDate, | ||
highlightedRange: highlightedRange, | ||
selectFocusedDate () { | ||
selectDate(calendar.focusedDate); | ||
}, | ||
selectDate: selectDate, | ||
highlightDate (date) { | ||
if (anchorDate) calendar.setFocusedDate(date); | ||
}, | ||
isSelected (date) { | ||
return highlightedRange && date.compare(highlightedRange.start) >= 0 && date.compare(highlightedRange.end) <= 0; | ||
}, | ||
isDragging: isDragging, | ||
setDragging: setDragging | ||
}; | ||
} | ||
function $794e8b1784854410$var$makeRange(start, end) { | ||
if (!start || !end) return null; | ||
if (end.compare(start) < 0) [start, end] = [ | ||
end, | ||
start | ||
]; | ||
return { | ||
start: $8fZ0X$internationalizeddate.toCalendarDate(start), | ||
end: $8fZ0X$internationalizeddate.toCalendarDate(end) | ||
}; | ||
} | ||
function $794e8b1784854410$var$convertValue(newValue, oldValue) { | ||
// The display calendar should not have any effect on the emitted value. | ||
// Emit dates in the same calendar as the original value, if any, otherwise gregorian. | ||
newValue = $8fZ0X$internationalizeddate.toCalendar(newValue, oldValue?.calendar || new $8fZ0X$internationalizeddate.GregorianCalendar()); | ||
// Preserve time if the input value had one. | ||
if (oldValue && 'hour' in oldValue) return oldValue.set(newValue); | ||
return newValue; | ||
} | ||
exports.useRangeCalendarState = useRangeCalendarState; | ||
function $baa4c168da5893215a2941ab7b17cc3d$var$makeRange(start, end) { | ||
if (!start || !end) { | ||
return null; | ||
} | ||
$parcel$exportWildcard(module.exports, $b6932658840cac06$exports); | ||
$parcel$exportWildcard(module.exports, $f61c34523ac023ce$exports); | ||
$parcel$exportWildcard(module.exports, $794e8b1784854410$exports); | ||
if (end.compare(start) < 0) { | ||
[start, end] = [end, start]; | ||
} | ||
return { | ||
start: toCalendarDate(start), | ||
end: toCalendarDate(end) | ||
}; | ||
} | ||
function $baa4c168da5893215a2941ab7b17cc3d$var$convertValue(newValue, oldValue) { | ||
// The display calendar should not have any effect on the emitted value. | ||
// Emit dates in the same calendar as the original value, if any, otherwise gregorian. | ||
newValue = toCalendar(newValue, (oldValue == null ? void 0 : oldValue.calendar) || new GregorianCalendar()); // Preserve time if the input value had one. | ||
if (oldValue && 'hour' in oldValue) { | ||
return oldValue.set(newValue); | ||
} | ||
return newValue; | ||
} | ||
//# sourceMappingURL=main.js.map |
@@ -1,410 +0,350 @@ | ||
import _babelRuntimeHelpersEsmObjectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; | ||
import { useEffect, useMemo, useRef, useState } from "react"; | ||
import { useDateFormatter } from "@react-aria/i18n"; | ||
import { useControlledState } from "@react-stately/utils"; | ||
import { maxDate, minDate, startOfMonth, startOfWeek, startOfYear, toCalendarDate, GregorianCalendar, isSameDay, toCalendar, today } from "@internationalized/date"; | ||
import _babelRuntimeHelpersEsmExtends from "@babel/runtime/helpers/esm/extends"; | ||
import {toCalendar as $gtpA2$toCalendar, toCalendarDate as $gtpA2$toCalendarDate, today as $gtpA2$today, GregorianCalendar as $gtpA2$GregorianCalendar, isSameDay as $gtpA2$isSameDay, startOfYear as $gtpA2$startOfYear, startOfMonth as $gtpA2$startOfMonth, startOfWeek as $gtpA2$startOfWeek, maxDate as $gtpA2$maxDate, minDate as $gtpA2$minDate} from "@internationalized/date"; | ||
import {useControlledState as $gtpA2$useControlledState} from "@react-stately/utils"; | ||
import {useDateFormatter as $gtpA2$useDateFormatter} from "@react-aria/i18n"; | ||
import {useMemo as $gtpA2$useMemo, useState as $gtpA2$useState, useRef as $gtpA2$useRef, useEffect as $gtpA2$useEffect} from "react"; | ||
function $e9245e22e7d2e30e959129f8619bae$export$isInvalid(date, minValue, maxValue) { | ||
return minValue != null && date.compare(minValue) < 0 || maxValue != null && date.compare(maxValue) > 0; | ||
function $parcel$export(e, n, v, s) { | ||
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true}); | ||
} | ||
var $509a3e380a190741$exports = {}; | ||
function $e9245e22e7d2e30e959129f8619bae$export$alignCenter(date, duration, locale, minValue, maxValue) { | ||
let halfDuration = {}; | ||
for (let key in duration) { | ||
halfDuration[key] = Math.floor(duration[key] / 2); | ||
var $773eaea6455709cc$exports = {}; | ||
if (halfDuration[key] > 0 && duration[key] % 2 === 0) { | ||
halfDuration[key]--; | ||
} | ||
} | ||
$parcel$export($773eaea6455709cc$exports, "useCalendarState", () => $773eaea6455709cc$export$6d095e787d2b5e1f); | ||
let aligned = $e9245e22e7d2e30e959129f8619bae$export$alignStart(date, duration, locale).subtract(halfDuration); | ||
return $e9245e22e7d2e30e959129f8619bae$export$constrainStart(date, aligned, duration, locale, minValue, maxValue); | ||
function $e8a61e762628fc7e$export$eac50920cf2fd59a(date, minValue, maxValue) { | ||
return minValue != null && date.compare(minValue) < 0 || maxValue != null && date.compare(maxValue) > 0; | ||
} | ||
function $e9245e22e7d2e30e959129f8619bae$export$alignStart(date, duration, locale, minValue, maxValue) { | ||
// align to the start of the largest unit | ||
let aligned = date; | ||
if (duration.years) { | ||
aligned = startOfYear(date); | ||
} else if (duration.months) { | ||
aligned = startOfMonth(date); | ||
} else if (duration.weeks) { | ||
aligned = startOfWeek(date, locale); | ||
} | ||
return $e9245e22e7d2e30e959129f8619bae$export$constrainStart(date, aligned, duration, locale, minValue, maxValue); | ||
function $e8a61e762628fc7e$export$f4a51ff076cc9a09(date, duration, locale, minValue, maxValue) { | ||
let halfDuration = { | ||
}; | ||
for(let key in duration){ | ||
halfDuration[key] = Math.floor(duration[key] / 2); | ||
if (halfDuration[key] > 0 && duration[key] % 2 === 0) halfDuration[key]--; | ||
} | ||
let aligned = $e8a61e762628fc7e$export$144a00ba6044eb9(date, duration, locale).subtract(halfDuration); | ||
return $e8a61e762628fc7e$export$5bb865b12696a77d(date, aligned, duration, locale, minValue, maxValue); | ||
} | ||
function $e9245e22e7d2e30e959129f8619bae$export$alignEnd(date, duration, locale, minValue, maxValue) { | ||
let d = _babelRuntimeHelpersEsmExtends({}, duration); // subtract 1 from the smallest unit | ||
if (duration.days) { | ||
d.days--; | ||
} else if (duration.weeks) { | ||
d.weeks--; | ||
} else if (duration.months) { | ||
d.months--; | ||
} else if (duration.years) { | ||
d.years--; | ||
} | ||
let aligned = $e9245e22e7d2e30e959129f8619bae$export$alignStart(date, duration, locale).subtract(d); | ||
return $e9245e22e7d2e30e959129f8619bae$export$constrainStart(date, aligned, duration, locale, minValue, maxValue); | ||
function $e8a61e762628fc7e$export$144a00ba6044eb9(date, duration, locale, minValue, maxValue) { | ||
// align to the start of the largest unit | ||
let aligned = date; | ||
if (duration.years) aligned = $gtpA2$startOfYear(date); | ||
else if (duration.months) aligned = $gtpA2$startOfMonth(date); | ||
else if (duration.weeks) aligned = $gtpA2$startOfWeek(date, locale); | ||
return $e8a61e762628fc7e$export$5bb865b12696a77d(date, aligned, duration, locale, minValue, maxValue); | ||
} | ||
function $e9245e22e7d2e30e959129f8619bae$export$constrainStart(date, aligned, duration, locale, minValue, maxValue) { | ||
if (minValue && date.compare(minValue) >= 0) { | ||
aligned = maxDate(aligned, $e9245e22e7d2e30e959129f8619bae$export$alignStart(toCalendarDate(minValue), duration, locale)); | ||
} | ||
if (maxValue && date.compare(maxValue) <= 0) { | ||
aligned = minDate(aligned, $e9245e22e7d2e30e959129f8619bae$export$alignEnd(toCalendarDate(maxValue), duration, locale)); | ||
} | ||
return aligned; | ||
function $e8a61e762628fc7e$export$530edbfc915b2b04(date, duration, locale, minValue, maxValue) { | ||
let d = { | ||
...duration | ||
}; | ||
// subtract 1 from the smallest unit | ||
if (duration.days) d.days--; | ||
else if (duration.weeks) d.weeks--; | ||
else if (duration.months) d.months--; | ||
else if (duration.years) d.years--; | ||
let aligned = $e8a61e762628fc7e$export$144a00ba6044eb9(date, duration, locale).subtract(d); | ||
return $e8a61e762628fc7e$export$5bb865b12696a77d(date, aligned, duration, locale, minValue, maxValue); | ||
} | ||
function $e9245e22e7d2e30e959129f8619bae$export$constrainValue(date, minValue, maxValue) { | ||
if (minValue) { | ||
date = maxDate(date, toCalendarDate(minValue)); | ||
} | ||
if (maxValue) { | ||
date = minDate(date, toCalendarDate(maxValue)); | ||
} | ||
return date; | ||
function $e8a61e762628fc7e$export$5bb865b12696a77d(date, aligned, duration, locale, minValue, maxValue) { | ||
if (minValue && date.compare(minValue) >= 0) aligned = $gtpA2$maxDate(aligned, $e8a61e762628fc7e$export$144a00ba6044eb9($gtpA2$toCalendarDate(minValue), duration, locale)); | ||
if (maxValue && date.compare(maxValue) <= 0) aligned = $gtpA2$minDate(aligned, $e8a61e762628fc7e$export$530edbfc915b2b04($gtpA2$toCalendarDate(maxValue), duration, locale)); | ||
return aligned; | ||
} | ||
function $e8a61e762628fc7e$export$4f5203c0d889109e(date, minValue, maxValue) { | ||
if (minValue) date = $gtpA2$maxDate(date, $gtpA2$toCalendarDate(minValue)); | ||
if (maxValue) date = $gtpA2$minDate(date, $gtpA2$toCalendarDate(maxValue)); | ||
return date; | ||
} | ||
export function useCalendarState(props) { | ||
let defaultFormatter = useDateFormatter(); | ||
let resolvedOptions = useMemo(() => defaultFormatter.resolvedOptions(), [defaultFormatter]); | ||
let { | ||
locale, | ||
createCalendar, | ||
timeZone = resolvedOptions.timeZone, | ||
visibleDuration = { | ||
months: 1 | ||
}, | ||
minValue, | ||
maxValue, | ||
selectionAlignment | ||
} = props; | ||
let calendar = useMemo(() => createCalendar(resolvedOptions.calendar), [createCalendar, resolvedOptions.calendar]); | ||
let [value, setControlledValue] = useControlledState(props.value, props.defaultValue, props.onChange); | ||
let calendarDateValue = useMemo(() => value ? toCalendar(toCalendarDate(value), calendar) : null, [value, calendar]); | ||
let defaultDate = useMemo(() => calendarDateValue || $e9245e22e7d2e30e959129f8619bae$export$constrainValue(toCalendar(today(timeZone), calendar), minValue, maxValue), [calendarDateValue, timeZone, calendar, minValue, maxValue]); | ||
let [startDate, setStartDate] = useState(() => { | ||
switch (selectionAlignment) { | ||
case 'start': | ||
return $e9245e22e7d2e30e959129f8619bae$export$alignStart(defaultDate, visibleDuration, locale, minValue, maxValue); | ||
case 'end': | ||
return $e9245e22e7d2e30e959129f8619bae$export$alignEnd(defaultDate, visibleDuration, locale, minValue, maxValue); | ||
case 'center': | ||
default: | ||
return $e9245e22e7d2e30e959129f8619bae$export$alignCenter(defaultDate, visibleDuration, locale, minValue, maxValue); | ||
} | ||
}); | ||
let [focusedDate, setFocusedDate] = useState(defaultDate); | ||
let [isFocused, setFocused] = useState(props.autoFocus || false); | ||
let endDate = useMemo(() => startDate.add(visibleDuration).subtract({ | ||
days: 1 | ||
}), [startDate, visibleDuration]); // Reset focused date and visible range when calendar changes. | ||
let lastCalendarIdentifier = useRef(calendar.identifier); | ||
useEffect(() => { | ||
if (calendar.identifier !== lastCalendarIdentifier.current) { | ||
let newFocusedDate = toCalendar(focusedDate, calendar); | ||
setStartDate($e9245e22e7d2e30e959129f8619bae$export$alignCenter(newFocusedDate, visibleDuration, locale, minValue, maxValue)); | ||
setFocusedDate(newFocusedDate); | ||
lastCalendarIdentifier.current = calendar.identifier; | ||
} | ||
}, [calendar, focusedDate, visibleDuration, locale, minValue, maxValue]); // Sets focus to a specific cell date | ||
function focusCell(date) { | ||
// date = constrain(focusedDate, date, visibleDuration, locale, minValue, maxValue); | ||
date = $e9245e22e7d2e30e959129f8619bae$export$constrainValue(date, minValue, maxValue); | ||
let next = startDate.add(visibleDuration); | ||
if (date.compare(startDate) < 0) { | ||
setStartDate($e9245e22e7d2e30e959129f8619bae$export$alignEnd(date, visibleDuration, locale, minValue, maxValue)); | ||
} else if (date.compare(next) >= 0) { | ||
setStartDate($e9245e22e7d2e30e959129f8619bae$export$alignStart(date, visibleDuration, locale, minValue, maxValue)); | ||
function $773eaea6455709cc$export$6d095e787d2b5e1f(props) { | ||
let defaultFormatter = $gtpA2$useDateFormatter(); | ||
let resolvedOptions = $gtpA2$useMemo(()=>defaultFormatter.resolvedOptions() | ||
, [ | ||
defaultFormatter | ||
]); | ||
let { locale: locale , createCalendar: createCalendar , visibleDuration: visibleDuration = { | ||
months: 1 | ||
} , minValue: minValue , maxValue: maxValue , selectionAlignment: selectionAlignment } = props; | ||
let calendar = $gtpA2$useMemo(()=>createCalendar(resolvedOptions.calendar) | ||
, [ | ||
createCalendar, | ||
resolvedOptions.calendar | ||
]); | ||
let [value, setControlledValue] = $gtpA2$useControlledState(props.value, props.defaultValue, props.onChange); | ||
let calendarDateValue = $gtpA2$useMemo(()=>value ? $gtpA2$toCalendar($gtpA2$toCalendarDate(value), calendar) : null | ||
, [ | ||
value, | ||
calendar | ||
]); | ||
let timeZone = $gtpA2$useMemo(()=>value && 'timeZone' in value ? value.timeZone : resolvedOptions.timeZone | ||
, [ | ||
value, | ||
resolvedOptions.timeZone | ||
]); | ||
let defaultDate = $gtpA2$useMemo(()=>calendarDateValue || $e8a61e762628fc7e$export$4f5203c0d889109e($gtpA2$toCalendar($gtpA2$today(timeZone), calendar), minValue, maxValue) | ||
, [ | ||
calendarDateValue, | ||
timeZone, | ||
calendar, | ||
minValue, | ||
maxValue | ||
]); | ||
let [startDate, setStartDate] = $gtpA2$useState(()=>{ | ||
switch(selectionAlignment){ | ||
case 'start': | ||
return $e8a61e762628fc7e$export$144a00ba6044eb9(defaultDate, visibleDuration, locale, minValue, maxValue); | ||
case 'end': | ||
return $e8a61e762628fc7e$export$530edbfc915b2b04(defaultDate, visibleDuration, locale, minValue, maxValue); | ||
case 'center': | ||
default: | ||
return $e8a61e762628fc7e$export$f4a51ff076cc9a09(defaultDate, visibleDuration, locale, minValue, maxValue); | ||
} | ||
}); | ||
let [focusedDate, setFocusedDate] = $gtpA2$useState(defaultDate); | ||
let [isFocused, setFocused] = $gtpA2$useState(props.autoFocus || false); | ||
let endDate = $gtpA2$useMemo(()=>startDate.add(visibleDuration).subtract({ | ||
days: 1 | ||
}) | ||
, [ | ||
startDate, | ||
visibleDuration | ||
]); | ||
// Reset focused date and visible range when calendar changes. | ||
let lastCalendarIdentifier = $gtpA2$useRef(calendar.identifier); | ||
$gtpA2$useEffect(()=>{ | ||
if (calendar.identifier !== lastCalendarIdentifier.current) { | ||
let newFocusedDate = $gtpA2$toCalendar(focusedDate, calendar); | ||
setStartDate($e8a61e762628fc7e$export$f4a51ff076cc9a09(newFocusedDate, visibleDuration, locale, minValue, maxValue)); | ||
setFocusedDate(newFocusedDate); | ||
lastCalendarIdentifier.current = calendar.identifier; | ||
} | ||
}, [ | ||
calendar, | ||
focusedDate, | ||
visibleDuration, | ||
locale, | ||
minValue, | ||
maxValue | ||
]); | ||
// Sets focus to a specific cell date | ||
function focusCell(date) { | ||
// date = constrain(focusedDate, date, visibleDuration, locale, minValue, maxValue); | ||
date = $e8a61e762628fc7e$export$4f5203c0d889109e(date, minValue, maxValue); | ||
let next = startDate.add(visibleDuration); | ||
if (date.compare(startDate) < 0) setStartDate($e8a61e762628fc7e$export$530edbfc915b2b04(date, visibleDuration, locale, minValue, maxValue)); | ||
else if (date.compare(next) >= 0) setStartDate($e8a61e762628fc7e$export$144a00ba6044eb9(date, visibleDuration, locale, minValue, maxValue)); | ||
setFocusedDate(date); | ||
} | ||
setFocusedDate(date); | ||
} | ||
function setValue(newValue) { | ||
if (!props.isDisabled && !props.isReadOnly) { | ||
// The display calendar should not have any effect on the emitted value. | ||
// Emit dates in the same calendar as the original value, if any, otherwise gregorian. | ||
newValue = toCalendar(newValue, (value == null ? void 0 : value.calendar) || new GregorianCalendar()); // Preserve time if the input value had one. | ||
if (value && 'hour' in value) { | ||
setControlledValue(value.set(newValue)); | ||
} else { | ||
setControlledValue(newValue); | ||
} | ||
function setValue(newValue) { | ||
if (!props.isDisabled && !props.isReadOnly) { | ||
// The display calendar should not have any effect on the emitted value. | ||
// Emit dates in the same calendar as the original value, if any, otherwise gregorian. | ||
newValue = $gtpA2$toCalendar(newValue, value?.calendar || new $gtpA2$GregorianCalendar()); | ||
// Preserve time if the input value had one. | ||
if (value && 'hour' in value) setControlledValue(value.set(newValue)); | ||
else setControlledValue(newValue); | ||
} | ||
} | ||
} | ||
return { | ||
isDisabled: props.isDisabled, | ||
isReadOnly: props.isReadOnly, | ||
value: calendarDateValue, | ||
setValue: setValue, | ||
visibleRange: { | ||
start: startDate, | ||
end: endDate | ||
}, | ||
focusedDate: focusedDate, | ||
timeZone: timeZone, | ||
setFocusedDate (date) { | ||
setFocusedDate(date); | ||
setFocused(true); | ||
}, | ||
focusNextDay () { | ||
focusCell(focusedDate.add({ | ||
days: 1 | ||
})); | ||
}, | ||
focusPreviousDay () { | ||
focusCell(focusedDate.subtract({ | ||
days: 1 | ||
})); | ||
}, | ||
focusNextRow () { | ||
if (visibleDuration.days) this.focusNextPage(); | ||
else if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) focusCell(focusedDate.add({ | ||
weeks: 1 | ||
})); | ||
}, | ||
focusPreviousRow () { | ||
if (visibleDuration.days) this.focusPreviousPage(); | ||
else if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) focusCell(focusedDate.subtract({ | ||
weeks: 1 | ||
})); | ||
}, | ||
focusNextPage () { | ||
let start = startDate.add(visibleDuration); | ||
setStartDate($e8a61e762628fc7e$export$5bb865b12696a77d(focusedDate, start, visibleDuration, locale, minValue, maxValue)); | ||
setFocusedDate($e8a61e762628fc7e$export$4f5203c0d889109e(focusedDate.add(visibleDuration), minValue, maxValue)); | ||
}, | ||
focusPreviousPage () { | ||
let start = startDate.subtract(visibleDuration); | ||
setStartDate($e8a61e762628fc7e$export$5bb865b12696a77d(focusedDate, start, visibleDuration, locale, minValue, maxValue)); | ||
setFocusedDate($e8a61e762628fc7e$export$4f5203c0d889109e(focusedDate.subtract(visibleDuration), minValue, maxValue)); | ||
}, | ||
focusPageStart () { | ||
focusCell(startDate); | ||
}, | ||
focusPageEnd () { | ||
focusCell(endDate); | ||
}, | ||
focusNextSection () { | ||
if (visibleDuration.days) this.focusNextPage(); | ||
else if (visibleDuration.weeks) focusCell(focusedDate.add({ | ||
months: 1 | ||
})); | ||
else if (visibleDuration.months || visibleDuration.years) focusCell(focusedDate.add({ | ||
years: 1 | ||
})); | ||
}, | ||
focusPreviousSection () { | ||
if (visibleDuration.days) this.focusPreviousPage(); | ||
else if (visibleDuration.weeks) focusCell(focusedDate.subtract({ | ||
months: 1 | ||
})); | ||
else if (visibleDuration.months || visibleDuration.years) focusCell(focusedDate.subtract({ | ||
years: 1 | ||
})); | ||
}, | ||
selectFocusedDate () { | ||
setValue(focusedDate); | ||
}, | ||
selectDate (date) { | ||
setValue(date); | ||
}, | ||
isFocused: isFocused, | ||
setFocused: setFocused, | ||
isInvalid (date) { | ||
return $e8a61e762628fc7e$export$eac50920cf2fd59a(date, minValue, maxValue); | ||
}, | ||
isSelected (date) { | ||
return calendarDateValue != null && $gtpA2$isSameDay(date, calendarDateValue); | ||
}, | ||
isCellFocused (date) { | ||
return isFocused && focusedDate && $gtpA2$isSameDay(date, focusedDate); | ||
}, | ||
isCellDisabled (date) { | ||
return props.isDisabled || date.compare(startDate) < 0 || date.compare(endDate) > 0 || $e8a61e762628fc7e$export$eac50920cf2fd59a(date, minValue, maxValue); | ||
}, | ||
isPreviousVisibleRangeInvalid () { | ||
return $e8a61e762628fc7e$export$eac50920cf2fd59a(startDate.subtract({ | ||
days: 1 | ||
}), minValue, maxValue); | ||
}, | ||
isNextVisibleRangeInvalid () { | ||
return $e8a61e762628fc7e$export$eac50920cf2fd59a(endDate.add({ | ||
days: 1 | ||
}), minValue, maxValue); | ||
} | ||
}; | ||
} | ||
return { | ||
isDisabled: props.isDisabled, | ||
isReadOnly: props.isReadOnly, | ||
value: calendarDateValue, | ||
setValue, | ||
visibleRange: { | ||
start: startDate, | ||
end: endDate | ||
}, | ||
focusedDate, | ||
timeZone, | ||
setFocusedDate(date) { | ||
setFocusedDate(date); | ||
setFocused(true); | ||
}, | ||
var $47d19290c26a12d7$exports = {}; | ||
focusNextDay() { | ||
focusCell(focusedDate.add({ | ||
days: 1 | ||
})); | ||
}, | ||
$parcel$export($47d19290c26a12d7$exports, "useRangeCalendarState", () => $47d19290c26a12d7$export$9a987164d97ecc90); | ||
focusPreviousDay() { | ||
focusCell(focusedDate.subtract({ | ||
days: 1 | ||
})); | ||
}, | ||
focusNextRow() { | ||
if (visibleDuration.days) { | ||
this.focusNextPage(); | ||
} else if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) { | ||
focusCell(focusedDate.add({ | ||
weeks: 1 | ||
})); | ||
} | ||
}, | ||
focusPreviousRow() { | ||
if (visibleDuration.days) { | ||
this.focusPreviousPage(); | ||
} else if (visibleDuration.weeks || visibleDuration.months || visibleDuration.years) { | ||
focusCell(focusedDate.subtract({ | ||
weeks: 1 | ||
})); | ||
} | ||
}, | ||
focusNextPage() { | ||
let start = startDate.add(visibleDuration); | ||
setStartDate($e9245e22e7d2e30e959129f8619bae$export$constrainStart(focusedDate, start, visibleDuration, locale, minValue, maxValue)); | ||
setFocusedDate($e9245e22e7d2e30e959129f8619bae$export$constrainValue(focusedDate.add(visibleDuration), minValue, maxValue)); | ||
}, | ||
focusPreviousPage() { | ||
let start = startDate.subtract(visibleDuration); | ||
setStartDate($e9245e22e7d2e30e959129f8619bae$export$constrainStart(focusedDate, start, visibleDuration, locale, minValue, maxValue)); | ||
setFocusedDate($e9245e22e7d2e30e959129f8619bae$export$constrainValue(focusedDate.subtract(visibleDuration), minValue, maxValue)); | ||
}, | ||
focusPageStart() { | ||
focusCell(startDate); | ||
}, | ||
focusPageEnd() { | ||
focusCell(endDate); | ||
}, | ||
focusNextSection() { | ||
if (visibleDuration.days) { | ||
this.focusNextPage(); | ||
} else if (visibleDuration.weeks) { | ||
focusCell(focusedDate.add({ | ||
months: 1 | ||
})); | ||
} else if (visibleDuration.months || visibleDuration.years) { | ||
focusCell(focusedDate.add({ | ||
years: 1 | ||
})); | ||
} | ||
}, | ||
focusPreviousSection() { | ||
if (visibleDuration.days) { | ||
this.focusPreviousPage(); | ||
} else if (visibleDuration.weeks) { | ||
focusCell(focusedDate.subtract({ | ||
months: 1 | ||
})); | ||
} else if (visibleDuration.months || visibleDuration.years) { | ||
focusCell(focusedDate.subtract({ | ||
years: 1 | ||
})); | ||
} | ||
}, | ||
selectFocusedDate() { | ||
setValue(focusedDate); | ||
}, | ||
selectDate(date) { | ||
setValue(date); | ||
}, | ||
isFocused, | ||
setFocused, | ||
isInvalid(date) { | ||
return $e9245e22e7d2e30e959129f8619bae$export$isInvalid(date, minValue, maxValue); | ||
}, | ||
isSelected(date) { | ||
return calendarDateValue != null && isSameDay(date, calendarDateValue); | ||
}, | ||
isCellFocused(date) { | ||
return isFocused && focusedDate && isSameDay(date, focusedDate); | ||
}, | ||
isCellDisabled(date) { | ||
return props.isDisabled || date.compare(startDate) < 0 || date.compare(endDate) > 0 || $e9245e22e7d2e30e959129f8619bae$export$isInvalid(date, minValue, maxValue); | ||
}, | ||
isPreviousVisibleRangeInvalid() { | ||
return $e9245e22e7d2e30e959129f8619bae$export$isInvalid(startDate.subtract({ | ||
days: 1 | ||
}), minValue, maxValue); | ||
}, | ||
isNextVisibleRangeInvalid() { | ||
return $e9245e22e7d2e30e959129f8619bae$export$isInvalid(endDate.add({ | ||
days: 1 | ||
}), minValue, maxValue); | ||
function $47d19290c26a12d7$export$9a987164d97ecc90(props) { | ||
let { value: valueProp , defaultValue: defaultValue , onChange: onChange , createCalendar: createCalendar , locale: locale , visibleDuration: visibleDuration = { | ||
months: 1 | ||
} , minValue: minValue , maxValue: maxValue , ...calendarProps } = props; | ||
let [value, setValue] = $gtpA2$useControlledState(valueProp, defaultValue, onChange); | ||
let [anchorDate, setAnchorDate] = $gtpA2$useState(null); | ||
let alignment = 'center'; | ||
if (value && value.start && value.end) { | ||
let start = $e8a61e762628fc7e$export$f4a51ff076cc9a09($gtpA2$toCalendarDate(value.start), visibleDuration, locale, minValue, maxValue); | ||
let end = start.add(visibleDuration).subtract({ | ||
days: 1 | ||
}); | ||
if (value.end.compare(end) > 0) alignment = 'start'; | ||
} | ||
}; | ||
} | ||
export function useRangeCalendarState(props) { | ||
let { | ||
value: valueProp, | ||
defaultValue, | ||
onChange, | ||
createCalendar, | ||
locale, | ||
visibleDuration = { | ||
months: 1 | ||
}, | ||
minValue, | ||
maxValue | ||
} = props, | ||
calendarProps = _babelRuntimeHelpersEsmObjectWithoutPropertiesLoose(props, ["value", "defaultValue", "onChange", "createCalendar", "locale", "visibleDuration", "minValue", "maxValue"]); | ||
let [value, setValue] = useControlledState(valueProp, defaultValue, onChange); | ||
let [anchorDate, setAnchorDate] = useState(null); | ||
let alignment = 'center'; | ||
if (value && value.start && value.end) { | ||
let start = $e9245e22e7d2e30e959129f8619bae$export$alignCenter(toCalendarDate(value.start), visibleDuration, locale, minValue, maxValue); | ||
let end = start.add(visibleDuration).subtract({ | ||
days: 1 | ||
let calendar = $773eaea6455709cc$export$6d095e787d2b5e1f({ | ||
...calendarProps, | ||
value: value && value.start, | ||
createCalendar: createCalendar, | ||
locale: locale, | ||
visibleDuration: visibleDuration, | ||
minValue: minValue, | ||
maxValue: maxValue, | ||
selectionAlignment: alignment | ||
}); | ||
if (value.end.compare(end) > 0) { | ||
alignment = 'start'; | ||
} | ||
} | ||
let calendar = useCalendarState(_babelRuntimeHelpersEsmExtends({}, calendarProps, { | ||
value: value && value.start, | ||
createCalendar, | ||
locale, | ||
visibleDuration, | ||
minValue, | ||
maxValue, | ||
selectionAlignment: alignment | ||
})); | ||
let highlightedRange = anchorDate ? $b99e30b96646887f54a48af37fe3b03$var$makeRange(anchorDate, calendar.focusedDate) : value && $b99e30b96646887f54a48af37fe3b03$var$makeRange(value.start, value.end); | ||
let selectDate = date => { | ||
if (props.isReadOnly) { | ||
return; | ||
} | ||
if (!anchorDate) { | ||
setAnchorDate(date); | ||
} else { | ||
let range = $b99e30b96646887f54a48af37fe3b03$var$makeRange(anchorDate, date); | ||
setValue({ | ||
start: $b99e30b96646887f54a48af37fe3b03$var$convertValue(range.start, value == null ? void 0 : value.start), | ||
end: $b99e30b96646887f54a48af37fe3b03$var$convertValue(range.end, value == null ? void 0 : value.end) | ||
}); | ||
setAnchorDate(null); | ||
} | ||
}; | ||
let [isDragging, setDragging] = useState(false); | ||
return _babelRuntimeHelpersEsmExtends({}, calendar, { | ||
value, | ||
setValue, | ||
anchorDate, | ||
setAnchorDate, | ||
highlightedRange, | ||
selectFocusedDate() { | ||
selectDate(calendar.focusedDate); | ||
}, | ||
selectDate, | ||
highlightDate(date) { | ||
if (anchorDate) { | ||
calendar.setFocusedDate(date); | ||
} | ||
}, | ||
isSelected(date) { | ||
return highlightedRange && date.compare(highlightedRange.start) >= 0 && date.compare(highlightedRange.end) <= 0; | ||
}, | ||
isDragging, | ||
setDragging | ||
}); | ||
let highlightedRange = anchorDate ? $47d19290c26a12d7$var$makeRange(anchorDate, calendar.focusedDate) : value && $47d19290c26a12d7$var$makeRange(value.start, value.end); | ||
let selectDate = (date)=>{ | ||
if (props.isReadOnly) return; | ||
if (!anchorDate) setAnchorDate(date); | ||
else { | ||
let range = $47d19290c26a12d7$var$makeRange(anchorDate, date); | ||
setValue({ | ||
start: $47d19290c26a12d7$var$convertValue(range.start, value?.start), | ||
end: $47d19290c26a12d7$var$convertValue(range.end, value?.end) | ||
}); | ||
setAnchorDate(null); | ||
} | ||
}; | ||
let [isDragging, setDragging] = $gtpA2$useState(false); | ||
return { | ||
...calendar, | ||
value: value, | ||
setValue: setValue, | ||
anchorDate: anchorDate, | ||
setAnchorDate: setAnchorDate, | ||
highlightedRange: highlightedRange, | ||
selectFocusedDate () { | ||
selectDate(calendar.focusedDate); | ||
}, | ||
selectDate: selectDate, | ||
highlightDate (date) { | ||
if (anchorDate) calendar.setFocusedDate(date); | ||
}, | ||
isSelected (date) { | ||
return highlightedRange && date.compare(highlightedRange.start) >= 0 && date.compare(highlightedRange.end) <= 0; | ||
}, | ||
isDragging: isDragging, | ||
setDragging: setDragging | ||
}; | ||
} | ||
function $47d19290c26a12d7$var$makeRange(start, end) { | ||
if (!start || !end) return null; | ||
if (end.compare(start) < 0) [start, end] = [ | ||
end, | ||
start | ||
]; | ||
return { | ||
start: $gtpA2$toCalendarDate(start), | ||
end: $gtpA2$toCalendarDate(end) | ||
}; | ||
} | ||
function $47d19290c26a12d7$var$convertValue(newValue, oldValue) { | ||
// The display calendar should not have any effect on the emitted value. | ||
// Emit dates in the same calendar as the original value, if any, otherwise gregorian. | ||
newValue = $gtpA2$toCalendar(newValue, oldValue?.calendar || new $gtpA2$GregorianCalendar()); | ||
// Preserve time if the input value had one. | ||
if (oldValue && 'hour' in oldValue) return oldValue.set(newValue); | ||
return newValue; | ||
} | ||
function $b99e30b96646887f54a48af37fe3b03$var$makeRange(start, end) { | ||
if (!start || !end) { | ||
return null; | ||
} | ||
if (end.compare(start) < 0) { | ||
[start, end] = [end, start]; | ||
} | ||
return { | ||
start: toCalendarDate(start), | ||
end: toCalendarDate(end) | ||
}; | ||
} | ||
function $b99e30b96646887f54a48af37fe3b03$var$convertValue(newValue, oldValue) { | ||
// The display calendar should not have any effect on the emitted value. | ||
// Emit dates in the same calendar as the original value, if any, otherwise gregorian. | ||
newValue = toCalendar(newValue, (oldValue == null ? void 0 : oldValue.calendar) || new GregorianCalendar()); // Preserve time if the input value had one. | ||
if (oldValue && 'hour' in oldValue) { | ||
return oldValue.set(newValue); | ||
} | ||
return newValue; | ||
} | ||
export {$773eaea6455709cc$export$6d095e787d2b5e1f as useCalendarState, $47d19290c26a12d7$export$9a987164d97ecc90 as useRangeCalendarState}; | ||
//# sourceMappingURL=module.js.map |
{ | ||
"name": "@react-stately/calendar", | ||
"version": "3.0.0-alpha.1", | ||
"version": "3.0.0-alpha.2", | ||
"description": "Spectrum UI components in React", | ||
@@ -21,8 +21,8 @@ "license": "Apache-2.0", | ||
"@babel/runtime": "^7.6.2", | ||
"@internationalized/date": "3.0.0-alpha.1", | ||
"@react-aria/i18n": "^3.3.4", | ||
"@react-stately/utils": "^3.3.0", | ||
"@react-types/calendar": "3.0.0-alpha.1", | ||
"@react-types/datepicker": "3.0.0-alpha.1", | ||
"@react-types/shared": "^3.10.0", | ||
"@internationalized/date": "3.0.0-alpha.2", | ||
"@react-aria/i18n": "^3.3.5", | ||
"@react-stately/utils": "^3.4.0", | ||
"@react-types/calendar": "3.0.0-alpha.2", | ||
"@react-types/datepicker": "3.0.0-alpha.2", | ||
"@react-types/shared": "^3.11.0", | ||
"date-fns": "^1.30.1" | ||
@@ -36,3 +36,3 @@ }, | ||
}, | ||
"gitHead": "16fc29ac722793ffaea083f13760d82ab341cc8d" | ||
"gitHead": "54c2366c4f31bd4bf619126131cd583c12972acc" | ||
} |
@@ -43,3 +43,2 @@ /* | ||
createCalendar, | ||
timeZone = resolvedOptions.timeZone, | ||
visibleDuration = {months: 1}, | ||
@@ -55,2 +54,3 @@ minValue, | ||
let calendarDateValue = useMemo(() => value ? toCalendar(toCalendarDate(value), calendar) : null, [value, calendar]); | ||
let timeZone = useMemo(() => value && 'timeZone' in value ? value.timeZone : resolvedOptions.timeZone, [value, resolvedOptions.timeZone]); | ||
let defaultDate = useMemo(() => calendarDateValue || constrainValue(toCalendar(today(timeZone), calendar), minValue, maxValue), [calendarDateValue, timeZone, calendar, minValue, maxValue]); | ||
@@ -57,0 +57,0 @@ let [startDate, setStartDate] = useState(() => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
127459
1203
+ Added@internationalized/date@3.0.0-alpha.2(transitive)
+ Added@react-types/calendar@3.0.0-alpha.2(transitive)
+ Added@react-types/datepicker@3.0.0-alpha.2(transitive)
- Removed@internationalized/date@3.0.0-alpha.1(transitive)
- Removed@react-types/calendar@3.0.0-alpha.1(transitive)
- Removed@react-types/datepicker@3.0.0-alpha.1(transitive)
Updated@react-aria/i18n@^3.3.5
Updated@react-stately/utils@^3.4.0
Updated@react-types/shared@^3.11.0