UNPKG

antd-mobile

Version:
237 lines (236 loc) 11.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Context = exports.CalendarPickerView = void 0; var _classnames = _interopRequireDefault(require("classnames")); var _dayjs = _interopRequireDefault(require("dayjs")); var _isSameOrBefore = _interopRequireDefault(require("dayjs/plugin/isSameOrBefore")); var _isoWeek = _interopRequireDefault(require("dayjs/plugin/isoWeek")); var _react = _interopRequireWildcard(require("react")); var _nativeProps = require("../../utils/native-props"); var _usePropsValue = require("../../utils/use-props-value"); var _withDefaultProps = require("../../utils/with-default-props"); var _configProvider = require("../config-provider"); var _convert = require("./convert"); var _useSyncScroll = _interopRequireDefault(require("./useSyncScroll")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } _dayjs.default.extend(_isoWeek.default); _dayjs.default.extend(_isSameOrBefore.default); const classPrefix = 'adm-calendar-picker-view'; const Context = _react.default.createContext({ visible: false }); exports.Context = Context; const defaultProps = { weekStartsOn: 'Sunday', defaultValue: null, allowClear: true, usePopup: true, selectionMode: 'single' }; const CalendarPickerView = (0, _react.forwardRef)((p, ref) => { var _a; const bodyRef = (0, _react.useRef)(null); const today = (0, _dayjs.default)(); const props = (0, _withDefaultProps.mergeProps)(defaultProps, p); const { locale } = (0, _configProvider.useConfig)(); const markItems = [...locale.Calendar.markItems]; if (props.weekStartsOn === 'Sunday') { const item = markItems.pop(); if (item) markItems.unshift(item); } const [dateRange, setDateRange] = (0, _usePropsValue.usePropsValue)({ value: props.value === undefined ? undefined : (0, _convert.convertValueToRange)(props.selectionMode, props.value), defaultValue: (0, _convert.convertValueToRange)(props.selectionMode, props.defaultValue), onChange: v => { var _a, _b; if (props.selectionMode === 'single') { (_a = props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, v ? v[0] : null); } else if (props.selectionMode === 'range') { (_b = props.onChange) === null || _b === void 0 ? void 0 : _b.call(props, v); } } }); const [intermediate, setIntermediate] = (0, _react.useState)(false); const [current, setCurrent] = (0, _react.useState)(() => (0, _dayjs.default)(dateRange ? dateRange[0] : today).date(1)); // =============================== Scroll =============================== const context = (0, _react.useContext)(Context); const scrollTo = (0, _useSyncScroll.default)(current, context.visible, bodyRef); // ============================== Boundary ============================== const maxDay = (0, _react.useMemo)(() => props.max ? (0, _dayjs.default)(props.max) : current.add(6, 'month'), [props.max, current]); const minDay = (0, _react.useMemo)(() => props.min ? (0, _dayjs.default)(props.min) : current, [props.min, current]); // ================================ Refs ================================ (0, _react.useImperativeHandle)(ref, () => ({ jumpTo: pageOrPageGenerator => { let page; if (typeof pageOrPageGenerator === 'function') { page = pageOrPageGenerator({ year: current.year(), month: current.month() + 1 }); } else { page = pageOrPageGenerator; } const next = (0, _convert.convertPageToDayjs)(page); setCurrent(next); scrollTo(next); }, jumpToToday: () => { const next = (0, _dayjs.default)().date(1); setCurrent(next); scrollTo(next); }, getDateRange: () => dateRange })); // =============================== Render =============================== const header = _react.default.createElement("div", { className: `${classPrefix}-header` }, _react.default.createElement("div", { className: `${classPrefix}-title` }, (_a = props.title) !== null && _a !== void 0 ? _a : locale.Calendar.title)); function renderBody() { var _a; const cells = []; let monthIterator = minDay; // 遍历月份 while (monthIterator.isSameOrBefore(maxDay, 'month')) { const year = monthIterator.year(); const month = monthIterator.month() + 1; const renderMap = { year, month }; const yearMonth = `${year}-${month}`; // 获取需要预先填充的空格,如果是 7 天则不需要填充 const presetEmptyCellCount = props.weekStartsOn === 'Monday' ? monthIterator.date(1).isoWeekday() - 1 : monthIterator.date(1).isoWeekday(); const presetEmptyCells = presetEmptyCellCount == 7 ? null : Array(presetEmptyCellCount).fill(null).map((_, index) => _react.default.createElement("div", { key: index, className: `${classPrefix}-cell` })); cells.push(_react.default.createElement("div", { key: yearMonth, "data-year-month": yearMonth }, _react.default.createElement("div", { className: `${classPrefix}-title` }, (_a = locale.Calendar.yearAndMonth) === null || _a === void 0 ? void 0 : _a.replace(/\${(.*?)}/g, (_, variable) => { var _a; return (_a = renderMap[variable]) === null || _a === void 0 ? void 0 : _a.toString(); })), _react.default.createElement("div", { className: `${classPrefix}-cells` }, presetEmptyCells, Array(monthIterator.daysInMonth()).fill(null).map((_, index) => { var _a; const d = monthIterator.date(index + 1); let isSelect = false; let isBegin = false; let isEnd = false; let isSelectRowBegin = false; let isSelectRowEnd = false; if (dateRange) { const [begin, end] = dateRange; isBegin = d.isSame(begin, 'day'); isEnd = d.isSame(end, 'day'); isSelect = isBegin || isEnd || d.isAfter(begin, 'day') && d.isBefore(end, 'day'); if (isSelect) { isSelectRowBegin = (cells.length % 7 === 0 || d.isSame(d.startOf('month'), 'day')) && !isBegin; isSelectRowEnd = (cells.length % 7 === 6 || d.isSame(d.endOf('month'), 'day')) && !isEnd; } } const disabled = props.shouldDisableDate ? props.shouldDisableDate(d.toDate()) : maxDay && d.isAfter(maxDay, 'day') || minDay && d.isBefore(minDay, 'day'); const renderTop = () => { var _a; const top = (_a = props.renderTop) === null || _a === void 0 ? void 0 : _a.call(props, d.toDate()); if (top) { return top; } if (props.selectionMode === 'range') { if (isBegin) { return locale.Calendar.start; } if (isEnd) { return locale.Calendar.end; } } if (d.isSame(today, 'day') && !isSelect) { return locale.Calendar.today; } }; return _react.default.createElement("div", { key: d.valueOf(), className: (0, _classnames.default)(`${classPrefix}-cell`, { [`${classPrefix}-cell-today`]: d.isSame(today, 'day'), [`${classPrefix}-cell-selected`]: isSelect, [`${classPrefix}-cell-selected-begin`]: isBegin, [`${classPrefix}-cell-selected-end`]: isEnd, [`${classPrefix}-cell-selected-row-begin`]: isSelectRowBegin, [`${classPrefix}-cell-selected-row-end`]: isSelectRowEnd, [`${classPrefix}-cell-disabled`]: !!disabled }), onClick: () => { if (!props.selectionMode) return; if (disabled) return; const date = d.toDate(); function shouldClear() { if (!props.allowClear) return false; if (!dateRange) return false; const [begin, end] = dateRange; return d.isSame(begin, 'date') && d.isSame(end, 'day'); } if (props.selectionMode === 'single') { if (props.allowClear && shouldClear()) { setDateRange(null); return; } setDateRange([date, date]); } else if (props.selectionMode === 'range') { if (!dateRange) { setDateRange([date, date]); setIntermediate(true); return; } if (shouldClear()) { setDateRange(null); setIntermediate(false); return; } if (intermediate) { const another = dateRange[0]; setDateRange(another > date ? [date, another] : [another, date]); setIntermediate(false); } else { setDateRange([date, date]); setIntermediate(true); } } } }, _react.default.createElement("div", { className: `${classPrefix}-cell-top` }, renderTop()), _react.default.createElement("div", { className: `${classPrefix}-cell-date` }, props.renderDate ? props.renderDate(d.toDate()) : d.date()), _react.default.createElement("div", { className: `${classPrefix}-cell-bottom` }, (_a = props.renderBottom) === null || _a === void 0 ? void 0 : _a.call(props, d.toDate()))); })))); monthIterator = monthIterator.add(1, 'month'); } return cells; } const body = _react.default.createElement("div", { className: `${classPrefix}-body`, ref: bodyRef }, renderBody()); const mark = _react.default.createElement("div", { className: `${classPrefix}-mark` }, markItems.map((item, index) => _react.default.createElement("div", { key: index, className: `${classPrefix}-mark-cell` }, item))); return (0, _nativeProps.withNativeProps)(props, _react.default.createElement("div", { className: classPrefix }, header, mark, body)); }); exports.CalendarPickerView = CalendarPickerView;