UNPKG

@orca-fe/pocket

Version:

UI components by orca-team

270 lines (268 loc) 15.6 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } Object.defineProperty(exports, "__esModule", { value: true }); exports.useCalendarRef = exports.default = void 0; var _icons = require("@ant-design/icons"); var _ahooks = require("ahooks"); var _moment = _interopRequireDefault(require("moment")); var _classnames = _interopRequireDefault(require("classnames")); var _react = _interopRequireWildcard(require("react")); var _reactWindow = require("react-window"); var _Context = _interopRequireDefault(require("./Context")); var _Week = _interopRequireDefault(require("./Week")); var _VirtualCalendar = _interopRequireDefault(require("./VirtualCalendar.style")); var _jsxRuntime = require("react/jsx-runtime"); var _excluded = ["className", "rowHeight", "startDate", "endDate", "defaultCurrentMonth", "today", "checked", "monthFormat", "weekHeaderFormat", "checkable", "onDateClick", "onCurrentMonthChange", "monthChangeDebounce", "arrowDown", "arrowUp", "children"]; function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } 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(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } var ef = function ef() { return undefined; }; var VirtualCalendar = /*#__PURE__*/_react.default.forwardRef(function (props, ref) { var _useState = (0, _react.useState)(function () { return { defaultStartDate: (0, _moment.default)('2010-01-01 00:00:00'), defaultEndDate: (0, _moment.default)('2030-01-01 00:00:00') }; }), _useState2 = _slicedToArray(_useState, 1), _useState2$ = _useState2[0], defaultEndDate = _useState2$.defaultEndDate, defaultStartDate = _useState2$.defaultStartDate; var styles = (0, _VirtualCalendar.default)(); var _useState3 = (0, _react.useState)(function () { return (0, _moment.default)(); }), _useState4 = _slicedToArray(_useState3, 1), now = _useState4[0]; var _props$className = props.className, className = _props$className === void 0 ? '' : _props$className, _props$rowHeight = props.rowHeight, _rowHeight = _props$rowHeight === void 0 ? 'auto' : _props$rowHeight, _props$startDate = props.startDate, _startDate = _props$startDate === void 0 ? defaultStartDate : _props$startDate, _props$endDate = props.endDate, _endDate = _props$endDate === void 0 ? defaultEndDate : _props$endDate, _props$defaultCurrent = props.defaultCurrentMonth, defaultCurrentMonth = _props$defaultCurrent === void 0 ? now : _props$defaultCurrent, _today = props.today, _checked = props.checked, _props$monthFormat = props.monthFormat, monthFormat = _props$monthFormat === void 0 ? 'YYYY-MM' : _props$monthFormat, _props$weekHeaderForm = props.weekHeaderFormat, weekHeaderFormat = _props$weekHeaderForm === void 0 ? 'ddd' : _props$weekHeaderForm, _props$checkable = props.checkable, checkable = _props$checkable === void 0 ? true : _props$checkable, _props$onDateClick = props.onDateClick, onDateClick = _props$onDateClick === void 0 ? ef : _props$onDateClick, _props$onCurrentMonth = props.onCurrentMonthChange, onCurrentMonthChange = _props$onCurrentMonth === void 0 ? ef : _props$onCurrentMonth, _props$monthChangeDeb = props.monthChangeDebounce, monthChangeDebounce = _props$monthChangeDeb === void 0 ? 300 : _props$monthChangeDeb, _props$arrowDown = props.arrowDown, arrowDown = _props$arrowDown === void 0 ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.CaretDownOutlined, {}) : _props$arrowDown, _props$arrowUp = props.arrowUp, arrowUp = _props$arrowUp === void 0 ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.CaretUpOutlined, {}) : _props$arrowUp, _props$children = props.children, children = _props$children === void 0 ? ef : _props$children, otherProps = _objectWithoutProperties(props, _excluded); var showToday = _today !== false; var listRef = (0, _react.useRef)(null); var outerRef = (0, _react.useRef)(null); var bodyRef = (0, _react.useRef)(null); var initRef = (0, _react.useRef)(false); var _ref = (0, _ahooks.useSize)(bodyRef) || { width: 0, height: 0 }, width = _ref.width, height = _ref.height; var rowHeight = _rowHeight === 'auto' ? height / 6 : _rowHeight; if (height > 0 && rowHeight > 0 && !initRef.current) initRef.current = true; var weekHeader = (0, _react.useMemo)(function () { return Array.from(Array(7).keys()).map(function (_, index) { return (0, _moment.default)().weekday(index).format(weekHeaderFormat); }); }, [weekHeaderFormat]); var _useState5 = (0, _react.useState)(0), _useState6 = _slicedToArray(_useState5, 2), centerTop = _useState6[0], setCenterTop = _useState6[1]; var _useMemo = (0, _react.useMemo)(function () { var startDate = _startDate.clone().weekday(0); var endDate = _endDate.clone().weekday(6); return { startDate: startDate, endDate: endDate }; }, [_startDate, _endDate]), endDate = _useMemo.endDate, startDate = _useMemo.startDate; var allWeekList = (0, _react.useMemo)(function () { return Array.from(Array(Math.ceil(endDate.diff(startDate, 'week'))).keys()); }, [startDate, endDate]); // 计算出当前的月份 var currentMonth = (0, _react.useMemo)(function () { var centerWeek = Math.floor(centerTop / rowHeight); var centerMonth = startDate.clone().add(centerWeek, 'week'); return centerMonth; }, [rowHeight, centerTop, startDate]); var currentMonthStr = (0, _react.useMemo)(function () { return currentMonth.format('YYYY-MM'); }, [currentMonth]); var monthFormatStr = (0, _react.useMemo)(function () { return currentMonth.format(monthFormat); }, [currentMonth, monthFormat]); var onCurrentMonthChangeDebounce = (0, _ahooks.useDebounceFn)(onCurrentMonthChange, { wait: monthChangeDebounce }); (0, _react.useEffect)(function () { onCurrentMonthChangeDebounce.run(currentMonthStr); }, [currentMonthStr]); var today = (0, _react.useMemo)(function () { if (typeof _today === 'boolean') { return now; } return (0, _moment.default)(_today); }, [_today]); var todayStr = (0, _react.useMemo)(function () { return today.format('YYYY-MM-DD'); }, [today]); var checked = (0, _react.useMemo)(function () { return _checked ? (0, _moment.default)(_checked) : undefined; }, [_checked]); var debounceCallback = (0, _ahooks.useDebounceFn)(function (callback) { callback(); }, { wait: 30 }); var scrollTo = (0, _ahooks.useMemoizedFn)(function (month) { var anim = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; onCurrentMonthChangeDebounce.cancel(); debounceCallback.cancel(); var target = (0, _moment.default)(month); var week = target.diff(startDate, 'week'); if (outerRef.current) { var scrollTop = week * rowHeight; if (anim) { outerRef.current.scrollTo({ top: scrollTop, behavior: 'smooth' }); } else { outerRef.current.scrollTop = scrollTop; debounceCallback.run(function () { // check scrollHeight if (outerRef.current && Math.trunc(outerRef.current.scrollTop) !== Math.trunc(scrollTop)) { scrollTo(month, anim); } }); } } else if (listRef.current) { listRef.current.scrollTo(week * rowHeight); } }); (0, _react.useImperativeHandle)(ref, function () { return { scrollTo: scrollTo }; }); (0, _react.useEffect)(function () { if (initRef.current && defaultCurrentMonth) { scrollTo((0, _moment.default)(defaultCurrentMonth).date(1), false); } }, [initRef.current]); var handleDateClick = (0, _ahooks.useMemoizedFn)(onDateClick); var customDateRender = (0, _ahooks.useMemoizedFn)(children); return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Context.default.Provider, { value: (0, _react.useMemo)(function () { return { currentMonth: currentMonthStr, startDate: startDate, today: todayStr, rowHeight: rowHeight, checked: checked, onDateClick: handleDateClick, customDateRender: customDateRender }; }, [currentMonthStr, startDate, todayStr, rowHeight, checked]), children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", _objectSpread(_objectSpread({ className: "".concat((0, _classnames.default)(styles.root, _defineProperty(_defineProperty({}, styles.showToday, showToday), styles.checkable, checkable)), " ").concat(className) }, otherProps), {}, { children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: styles.backgroundMonth, children: monthFormatStr }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", { className: styles.toolbar, children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: styles.parentValue, children: monthFormatStr }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { style: { flex: 1 } }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: styles.button, onClick: function onClick() { scrollTo(currentMonth.clone().add(-1, 'month').date(1)); }, children: arrowUp }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: styles.button, onClick: function onClick() { scrollTo(currentMonth.clone().add(1, 'month').date(1)); }, children: arrowDown })] }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: styles.header, children: weekHeader.map(function (str) { return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: styles.headerItem, children: str }, str); }) }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { ref: bodyRef, className: styles.body, children: height && rowHeight && /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactWindow.FixedSizeList, { ref: listRef, outerRef: outerRef, className: styles.virtualList, width: width, height: height, itemSize: rowHeight, itemCount: allWeekList.length, onScroll: function onScroll(options) { var scrollOffset = options.scrollOffset; var targetCenterTop = scrollOffset + 0.5 * height; if (Math.abs(targetCenterTop - centerTop) > rowHeight) { setCenterTop(targetCenterTop); } }, children: _Week.default }) })] })) }); }); var useCalendarRef = exports.useCalendarRef = function useCalendarRef() { return (0, _react.useRef)(null); }; var _default = exports.default = VirtualCalendar;