UNPKG

@orca-fe/pocket

Version:

UI components by orca-team

235 lines (234 loc) 12.7 kB
var _excluded = ["className", "rowHeight", "startDate", "endDate", "defaultCurrentMonth", "today", "checked", "monthFormat", "weekHeaderFormat", "checkable", "onDateClick", "onCurrentMonthChange", "monthChangeDebounce", "arrowDown", "arrowUp", "children"]; 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; } import { CaretDownOutlined, CaretUpOutlined } from '@ant-design/icons'; import { useDebounceFn, useMemoizedFn, useSize } from 'ahooks'; import moment from 'moment'; import cn from 'classnames'; import React, { useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react'; import { FixedSizeList } from 'react-window'; import VirtualCalendarContext from "./Context"; import Week from "./Week"; import useStyles from "./VirtualCalendar.style"; import { jsx as _jsx } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; var ef = () => undefined; var VirtualCalendar = /*#__PURE__*/React.forwardRef((props, ref) => { var _useState = useState(() => ({ defaultStartDate: moment('2010-01-01 00:00:00'), defaultEndDate: moment('2030-01-01 00:00:00') })), _useState2 = _slicedToArray(_useState, 1), _useState2$ = _useState2[0], defaultEndDate = _useState2$.defaultEndDate, defaultStartDate = _useState2$.defaultStartDate; var styles = useStyles(); var _useState3 = useState(() => moment()), _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__*/_jsx(CaretDownOutlined, {}) : _props$arrowDown, _props$arrowUp = props.arrowUp, arrowUp = _props$arrowUp === void 0 ? /*#__PURE__*/_jsx(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 = useRef(null); var outerRef = useRef(null); var bodyRef = useRef(null); var initRef = useRef(false); var _ref = 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 = useMemo(() => Array.from(Array(7).keys()).map((_, index) => moment().weekday(index).format(weekHeaderFormat)), [weekHeaderFormat]); var _useState5 = useState(0), _useState6 = _slicedToArray(_useState5, 2), centerTop = _useState6[0], setCenterTop = _useState6[1]; var _useMemo = useMemo(() => { var startDate = _startDate.clone().weekday(0); var endDate = _endDate.clone().weekday(6); return { startDate, endDate }; }, [_startDate, _endDate]), endDate = _useMemo.endDate, startDate = _useMemo.startDate; var allWeekList = useMemo(() => Array.from(Array(Math.ceil(endDate.diff(startDate, 'week'))).keys()), [startDate, endDate]); // 计算出当前的月份 var currentMonth = useMemo(() => { var centerWeek = Math.floor(centerTop / rowHeight); var centerMonth = startDate.clone().add(centerWeek, 'week'); return centerMonth; }, [rowHeight, centerTop, startDate]); var currentMonthStr = useMemo(() => currentMonth.format('YYYY-MM'), [currentMonth]); var monthFormatStr = useMemo(() => currentMonth.format(monthFormat), [currentMonth, monthFormat]); var onCurrentMonthChangeDebounce = useDebounceFn(onCurrentMonthChange, { wait: monthChangeDebounce }); useEffect(() => { onCurrentMonthChangeDebounce.run(currentMonthStr); }, [currentMonthStr]); var today = useMemo(() => { if (typeof _today === 'boolean') { return now; } return moment(_today); }, [_today]); var todayStr = useMemo(() => today.format('YYYY-MM-DD'), [today]); var checked = useMemo(() => _checked ? moment(_checked) : undefined, [_checked]); var debounceCallback = useDebounceFn(callback => { callback(); }, { wait: 30 }); var scrollTo = useMemoizedFn((month, anim = true) => { onCurrentMonthChangeDebounce.cancel(); debounceCallback.cancel(); var target = moment(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(() => { // 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); } }); useImperativeHandle(ref, () => ({ scrollTo })); useEffect(() => { if (initRef.current && defaultCurrentMonth) { scrollTo(moment(defaultCurrentMonth).date(1), false); } }, [initRef.current]); var handleDateClick = useMemoizedFn(onDateClick); var customDateRender = useMemoizedFn(children); return /*#__PURE__*/_jsx(VirtualCalendarContext.Provider, { value: useMemo(() => ({ currentMonth: currentMonthStr, startDate, today: todayStr, rowHeight, checked, onDateClick: handleDateClick, customDateRender }), [currentMonthStr, startDate, todayStr, rowHeight, checked]), children: /*#__PURE__*/_jsxs("div", _objectSpread(_objectSpread({ className: `${cn(styles.root, { [styles.showToday]: showToday, [styles.checkable]: checkable })} ${className}` }, otherProps), {}, { children: [/*#__PURE__*/_jsx("div", { className: styles.backgroundMonth, children: monthFormatStr }), /*#__PURE__*/_jsxs("div", { className: styles.toolbar, children: [/*#__PURE__*/_jsx("div", { className: styles.parentValue, children: monthFormatStr }), /*#__PURE__*/_jsx("div", { style: { flex: 1 } }), /*#__PURE__*/_jsx("div", { className: styles.button, onClick: () => { scrollTo(currentMonth.clone().add(-1, 'month').date(1)); }, children: arrowUp }), /*#__PURE__*/_jsx("div", { className: styles.button, onClick: () => { scrollTo(currentMonth.clone().add(1, 'month').date(1)); }, children: arrowDown })] }), /*#__PURE__*/_jsx("div", { className: styles.header, children: weekHeader.map(str => /*#__PURE__*/_jsx("div", { className: styles.headerItem, children: str }, str)) }), /*#__PURE__*/_jsx("div", { ref: bodyRef, className: styles.body, children: height && rowHeight && /*#__PURE__*/_jsx(FixedSizeList, { ref: listRef, outerRef: outerRef, className: styles.virtualList, width: width, height: height, itemSize: rowHeight, itemCount: allWeekList.length, onScroll: options => { var scrollOffset = options.scrollOffset; var targetCenterTop = scrollOffset + 0.5 * height; if (Math.abs(targetCenterTop - centerTop) > rowHeight) { setCenterTop(targetCenterTop); } }, children: Week }) })] })) }); }); export var useCalendarRef = () => useRef(null); export default VirtualCalendar;