UNPKG

antd-mobile

Version:
226 lines 9.26 kB
import classNames from 'classnames'; import dayjs from 'dayjs'; import isSameOrBefore from 'dayjs/plugin/isSameOrBefore'; import isoWeek from 'dayjs/plugin/isoWeek'; import React, { forwardRef, useContext, useImperativeHandle, useMemo, useRef, useState } from 'react'; import { withNativeProps } from '../../utils/native-props'; import { usePropsValue } from '../../utils/use-props-value'; import { mergeProps } from '../../utils/with-default-props'; import { useConfig } from '../config-provider'; import { convertPageToDayjs, convertValueToRange } from './convert'; import useSyncScroll from './useSyncScroll'; dayjs.extend(isoWeek); dayjs.extend(isSameOrBefore); const classPrefix = 'adm-calendar-picker-view'; export const Context = React.createContext({ visible: false }); const defaultProps = { weekStartsOn: 'Sunday', defaultValue: null, allowClear: true, usePopup: true, selectionMode: 'single' }; export const CalendarPickerView = forwardRef((p, ref) => { var _a; const bodyRef = useRef(null); const today = dayjs(); const props = mergeProps(defaultProps, p); const { locale } = useConfig(); const markItems = [...locale.Calendar.markItems]; if (props.weekStartsOn === 'Sunday') { const item = markItems.pop(); if (item) markItems.unshift(item); } const [dateRange, setDateRange] = usePropsValue({ value: props.value === undefined ? undefined : convertValueToRange(props.selectionMode, props.value), defaultValue: 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] = useState(false); const [current, setCurrent] = useState(() => dayjs(dateRange ? dateRange[0] : today).date(1)); // =============================== Scroll =============================== const context = useContext(Context); const scrollTo = useSyncScroll(current, context.visible, bodyRef); // ============================== Boundary ============================== const maxDay = useMemo(() => props.max ? dayjs(props.max) : current.add(6, 'month'), [props.max, current]); const minDay = useMemo(() => props.min ? dayjs(props.min) : current, [props.min, current]); // ================================ Refs ================================ useImperativeHandle(ref, () => ({ jumpTo: pageOrPageGenerator => { let page; if (typeof pageOrPageGenerator === 'function') { page = pageOrPageGenerator({ year: current.year(), month: current.month() + 1 }); } else { page = pageOrPageGenerator; } const next = convertPageToDayjs(page); setCurrent(next); scrollTo(next); }, jumpToToday: () => { const next = dayjs().date(1); setCurrent(next); scrollTo(next); }, getDateRange: () => dateRange })); // =============================== Render =============================== const header = React.createElement("div", { className: `${classPrefix}-header` }, React.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.createElement("div", { key: index, className: `${classPrefix}-cell` })); cells.push(React.createElement("div", { key: yearMonth, "data-year-month": yearMonth }, React.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.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.createElement("div", { key: d.valueOf(), className: classNames(`${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.createElement("div", { className: `${classPrefix}-cell-top` }, renderTop()), React.createElement("div", { className: `${classPrefix}-cell-date` }, props.renderDate ? props.renderDate(d.toDate()) : d.date()), React.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.createElement("div", { className: `${classPrefix}-body`, ref: bodyRef }, renderBody()); const mark = React.createElement("div", { className: `${classPrefix}-mark` }, markItems.map((item, index) => React.createElement("div", { key: index, className: `${classPrefix}-mark-cell` }, item))); return withNativeProps(props, React.createElement("div", { className: classPrefix }, header, mark, body)); });