UNPKG

zarm

Version:

基于 React 的移动端UI库

280 lines (247 loc) 9.71 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; 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; } import { createBEM } from '@zarm-design/bem'; import dayjs from 'dayjs'; import throttle from 'lodash/throttle'; import React, { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'; import { Transition } from 'react-transition-group'; import Carousel from '../carousel'; import { ConfigContext } from '../config-provider'; import useScroll from '../use-scroll'; import Header from './Header'; import CalendarMonthView from './Month'; import parseState from './utils/parseState'; import Week from './Week'; var Calendar = /*#__PURE__*/React.forwardRef(function (props, ref) { var className = props.className, dateRender = props.dateRender, disabledDate = props.disabledDate, onChange = props.onChange, onSelect = props.onSelect, mode = props.mode, maxDate = props.max, minDate = props.min, direction = props.direction, header = props.header; var container = ref || /*#__PURE__*/React.createRef(); var _useContext = useContext(ConfigContext), prefixCls = _useContext.prefixCls; var carouselRef = useRef(null); var bem = createBEM('calendar', { prefixCls: prefixCls }); var cls = bem([className]); var _useState = useState(function () { return _objectSpread(_objectSpread({}, parseState(props)), {}, { step: 0 }); }), _useState2 = _slicedToArray(_useState, 2), state = _useState2[0], setState = _useState2[1]; var _useMemo = useMemo(function () { var minDay = minDate ? dayjs(minDate).toDate() : dayjs().toDate(); var maxDay = maxDate ? dayjs(maxDate).toDate() : dayjs().add(1, 'year').toDate(); var duration = [minDay, maxDay].sort(function (item1, item2) { return +item1 - +item2; }); return duration; }, [maxDate, minDate]), _useMemo2 = _slicedToArray(_useMemo, 2), min = _useMemo2[0], max = _useMemo2[1]; var steps = useMemo(function () { return mode === 'range' ? 2 : 1; }, [mode]); var value = state.value; var nodes = useRef({}); var scrollBodyRef = /*#__PURE__*/React.createRef(); var weekRef = React.useRef(); var _useState3 = useState(), _useState4 = _slicedToArray(_useState3, 2), scrollDate = _useState4[0], setScrollDate = _useState4[1]; var _useState5 = useState(false), _useState6 = _slicedToArray(_useState5, 2), scrolling = _useState6[0], setScrolling = _useState6[1]; var isHorizontal = useMemo(function () { return direction === 'horizontal'; }, [direction]); var months = useMemo(function () { var month = []; var dateMax = dayjs(max); var dateMin = dayjs(min); var len = (dateMax.year() - dateMin.year()) * 12 + dateMax.month() - dateMin.month(); var i = 0; do { month.push(dayjs(min).add(i, 'month').toDate()); i += 1; } while (i <= len); return month; }, [max, min]); var currentMonthIndex = useMemo(function () { var currentTime = dayjs(value[0] || new Date()); return months.findIndex(function (current) { return dayjs(current).isSame(currentTime, 'month'); }); }, [value]); var _useState7 = useState(currentMonthIndex), _useState8 = _slicedToArray(_useState7, 2), currentMonth = _useState8[0], setCurrentMonth = _useState8[1]; // 月历定位 var scrollIntoView = useRef(false); var anchor = function anchor() { var _node$el, _node$el$scrollIntoVi; var target = value[0] || new Date(); var key = "".concat(target.getFullYear(), "-").concat(target.getMonth() + 1); var node = nodes.current[key]; node === null || node === void 0 ? void 0 : (_node$el = node.el()) === null || _node$el === void 0 ? void 0 : (_node$el$scrollIntoVi = _node$el.scrollIntoView) === null || _node$el$scrollIntoVi === void 0 ? void 0 : _node$el$scrollIntoVi.call(_node$el); }; var handleDateClick = useCallback(function (date) { var step = state.step; var currentStep = step + 1; var idx = value.map(Number).indexOf(Number(date)); if (currentStep === 1 && mode !== 'multiple') { value.splice(0, value.length); } if (mode === 'range') { value[currentStep - 1] = date; } else if (idx > -1) { value.splice(idx, 1); } else if (mode === 'multiple') { value.push(date); } else { value[currentStep - 1] = date; } value.sort(function (item1, item2) { return +item1 - +item2; }); setState(function (prevState) { return _objectSpread(_objectSpread({}, prevState), {}, { value: value, step: currentStep === steps ? 0 : currentStep }); }); if ((currentStep >= steps || mode === 'multiple') && typeof onChange === 'function') { onChange(value); } if (typeof onSelect === 'function') { onSelect(value); } }, [mode, state, value, onChange]); var renderMonth = useCallback(function (dateMonth) { var key = "".concat(dateMonth.getFullYear(), "-").concat(dateMonth.getMonth() + 1); return /*#__PURE__*/React.createElement(CalendarMonthView, { key: key, min: min, max: max, mode: mode, value: value, dateMonth: dateMonth, dateRender: dateRender, disabledDate: disabledDate, onDateClick: handleDateClick, ref: function ref(n) { nodes.current[key] = n; } }); }, [min, max, mode, value, dateRender, disabledDate, handleDateClick, nodes]); var content = useMemo(function () { return months.map(function (item) { return renderMonth(item); }); }, [renderMonth]); var showHeader = useMemo(function () { return direction === 'horizontal' && header; }, [direction, header]); var bodyScroll = throttle(function () { var weekNode = weekRef === null || weekRef === void 0 ? void 0 : weekRef.current; var body = scrollBodyRef.current; if (!body) { return false; } var _ref = body, scrollTop = _ref.scrollTop; var keys = Object.keys(nodes === null || nodes === void 0 ? void 0 : nodes.current); keys = keys.sort(function (a, b) { return new Date(a).getTime() - new Date(b).getTime(); }); for (var i = 0; i < keys.length; i++) { var _nodes$current$keys$i; var el = nodes === null || nodes === void 0 ? void 0 : (_nodes$current$keys$i = nodes.current[keys[i]]) === null || _nodes$current$keys$i === void 0 ? void 0 : _nodes$current$keys$i.el(); if ((el === null || el === void 0 ? void 0 : el.offsetTop) + (el === null || el === void 0 ? void 0 : el.clientHeight) - (weekNode === null || weekNode === void 0 ? void 0 : weekNode.clientHeight) > scrollTop) { setScrollDate(el.getAttribute('title')); return; } } }, 150); var monthsContent = useMemo(function () { if (isHorizontal) { return /*#__PURE__*/React.createElement(Carousel, { className: bem('body'), showPagination: false, activeIndex: currentMonth, onChange: setCurrentMonth, ref: carouselRef }, content); } return /*#__PURE__*/React.createElement("div", { className: bem('body'), ref: scrollBodyRef, onScroll: bodyScroll }, content, /*#__PURE__*/React.createElement(Transition, { in: scrolling, timeout: 500 }, function (tState) { return /*#__PURE__*/React.createElement("div", { className: bem('scroll-month', [_defineProperty({}, tState, true)]) }, scrollDate); })); }, [mode, currentMonth, content, isHorizontal, scrollDate, scrolling]); useEffect(function () { !isHorizontal && anchor(); }, [direction, minDate, maxDate]); var timer = useRef(); useScroll({ container: scrollBodyRef, onScroll: function onScroll() { !scrollIntoView.current && setScrolling(true); if (timer.current) { clearTimeout(timer.current); } timer.current = setTimeout(function () { setScrolling(false); scrollIntoView.current = false; }, 250); } }); return /*#__PURE__*/React.createElement("div", { className: cls, ref: container }, showHeader && /*#__PURE__*/React.createElement(Header, { changeMonth: function changeMonth(idx) { var _carouselRef$current; // @ts-ignore carouselRef === null || carouselRef === void 0 ? void 0 : (_carouselRef$current = carouselRef.current) === null || _carouselRef$current === void 0 ? void 0 : _carouselRef$current.onSlideTo(idx); }, months: months, currentMonth: currentMonth }), /*#__PURE__*/React.createElement(Week, { ref: weekRef }), monthsContent); }); Calendar.defaultProps = { mode: 'single', dateRender: function dateRender(date) { return date.getDate(); }, disabledDate: function disabledDate() { return false; }, direction: 'vertical', header: false }; export default Calendar;