UNPKG

@renderlesskit/react

Version:

Collection of headless components/hooks that are accessible, composable, customizable from low level to build your own UI & Design System powered by Reakit

226 lines (188 loc) 10 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports.useCalendarState = useCalendarState; var React = _interopRequireWildcard(require("react")); var _reakit = require("reakit"); var _hooks = require("@chakra-ui/hooks"); var _i18n = require("@react-aria/i18n"); var _utils = require("../utils"); var _LiveAnnouncer = require("../utils/LiveAnnouncer"); var _helpers = require("./helpers"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function useCalendarState() { var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var initialValue = props.value, _props$defaultValue = props.defaultValue, defaultValue = _props$defaultValue === void 0 ? (0, _utils.toUTCString)(new Date()) : _props$defaultValue, onChange = props.onChange, minValue = props.minValue, maxValue = props.maxValue, _props$isDisabled = props.isDisabled, isDisabled = _props$isDisabled === void 0 ? false : _props$isDisabled, _props$isReadOnly = props.isReadOnly, isReadOnly = _props$isReadOnly === void 0 ? false : _props$isReadOnly, _props$autoFocus = props.autoFocus, autoFocus = _props$autoFocus === void 0 ? false : _props$autoFocus; var _useControllableState = (0, _utils.useControllableState)({ value: initialValue, defaultValue: defaultValue, onChange: onChange }), _useControllableState2 = _slicedToArray(_useControllableState, 2), value = _useControllableState2[0], setValue = _useControllableState2[1]; var date = React.useMemo(function () { return new Date(value); }, [value]); var minDateValue = React.useMemo(function () { return minValue ? new Date(minValue) : new Date(-864e13); }, [minValue]); var maxDateValue = React.useMemo(function () { return maxValue ? new Date(maxValue) : new Date(864e13); }, [maxValue]); var _React$useState = React.useState(date), _React$useState2 = _slicedToArray(_React$useState, 2), currentMonth = _React$useState2[0], setCurrentMonth = _React$useState2[1]; var _React$useState3 = React.useState(date), _React$useState4 = _slicedToArray(_React$useState3, 2), focusedDate = _React$useState4[0], setFocusedDate = _React$useState4[1]; var _React$useState5 = React.useState(autoFocus), _React$useState6 = _slicedToArray(_React$useState5, 2), isFocused = _React$useState6[0], setFocused = _React$useState6[1]; var month = currentMonth.getMonth(); var year = currentMonth.getFullYear(); var weekStart = (0, _helpers.useWeekStart)(); var weekDays = (0, _helpers.useWeekDays)(weekStart); var monthStartsAt = ((0, _utils.startOfMonth)(currentMonth).getDay() - weekStart) % 7; if (monthStartsAt < 0) { monthStartsAt += 7; } var days = (0, _utils.getDaysInMonth)(currentMonth); var weeksInMonth = Math.ceil((monthStartsAt + days) / 7); // Get 2D Date arrays in 7 days a week format var daysInMonth = React.useMemo(function () { return (0, _helpers.generateDaysInMonthArray)(month, monthStartsAt, weeksInMonth, year); }, [month, monthStartsAt, weeksInMonth, year]); function isInvalidDateRange(value) { var min = new Date(minDateValue); var max = new Date(maxDateValue); return value < min || value > max; } // Sets focus to a specific cell date function focusCell(date) { if (isInvalidDateRange(date)) return; if (!(0, _utils.isSameMonth)(date, currentMonth)) { setCurrentMonth((0, _utils.startOfMonth)(date)); } setFocusedDate(date); } var dateFormatter = (0, _i18n.useDateFormatter)({ dateStyle: "full" }); var announceSelectedDate = React.useCallback(function (value) { if (!value) return; (0, _LiveAnnouncer.announce)("Selected Date: ".concat(dateFormatter.format(value))); }, [dateFormatter]); var setDate = React.useCallback(function (value) { if (!isDisabled && !isReadOnly) { setValue((0, _utils.toUTCString)(value)); announceSelectedDate(value); } }, [announceSelectedDate, isDisabled, isReadOnly, setValue]); // TODO // This runs only once when the component is mounted // Controlled state doesn't change the claender position // React.useEffect(() => { // const clampedDate = clamp(date, { // start: minDateValue, // end: maxDateValue, // }); // setDate(clampedDate); // setCurrentMonth(clampedDate); // setFocusedDate(clampedDate); // }, [date, maxDateValue, minDateValue, setDate]); var monthFormatter = (0, _i18n.useDateFormatter)({ month: "long", year: "numeric" }); // Announce when the current month changes (0, _hooks.useUpdateEffect)(function () { // announce the new month with a change from the Previous or Next button if (!isFocused) { (0, _LiveAnnouncer.announce)(monthFormatter.format(currentMonth)); } // handle an update to the current month from the Previous or Next button // rather than move focus, we announce the new month value }, [currentMonth]); var _useId = (0, _reakit.unstable_useId)({ id: props.id, baseId: "calendar" }), calendarId = _useId.id; return { dateValue: date, setDateValue: setDate, calendarId: calendarId, month: month, year: year, weekStart: weekStart, weekDays: weekDays, daysInMonth: daysInMonth, isDisabled: isDisabled, isFocused: isFocused, isReadOnly: isReadOnly, setFocused: setFocused, currentMonth: currentMonth, setCurrentMonth: setCurrentMonth, focusedDate: focusedDate, focusCell: focusCell, setFocusedDate: setFocusedDate, focusNextDay: function focusNextDay() { focusCell((0, _utils.addDays)(focusedDate, 1)); }, focusPreviousDay: function focusPreviousDay() { focusCell((0, _utils.subDays)(focusedDate, 1)); }, focusNextWeek: function focusNextWeek() { focusCell((0, _utils.addWeeks)(focusedDate, 1)); }, focusPreviousWeek: function focusPreviousWeek() { focusCell((0, _utils.subWeeks)(focusedDate, 1)); }, focusNextMonth: function focusNextMonth() { focusCell((0, _utils.addMonths)(focusedDate, 1)); }, focusPreviousMonth: function focusPreviousMonth() { focusCell((0, _utils.subMonths)(focusedDate, 1)); }, focusStartOfMonth: function focusStartOfMonth() { focusCell((0, _utils.startOfMonth)(focusedDate)); }, focusEndOfMonth: function focusEndOfMonth() { focusCell((0, _utils.endOfMonth)((0, _utils.startOfDay)(focusedDate))); }, focusNextYear: function focusNextYear() { focusCell((0, _utils.addYears)(focusedDate, 1)); }, focusPreviousYear: function focusPreviousYear() { focusCell((0, _utils.subYears)(focusedDate, 1)); }, selectFocusedDate: function selectFocusedDate() { setDate(focusedDate); }, selectDate: function selectDate(date) { setDate(date); }, isInvalidDateRange: isInvalidDateRange, isRangeCalendar: false }; } //# sourceMappingURL=CalendarState.js.map