UNPKG

tdesign-react

Version:
552 lines (548 loc) 20.9 kB
/** * tdesign v1.15.1 * (c) 2025 tdesign * @license MIT */ import { _ as _defineProperty } from '../_chunks/dep-cb0a3966.js'; import { _ as _toConsumableArray } from '../_chunks/dep-87d110df.js'; import { _ as _slicedToArray } from '../_chunks/dep-48805ab8.js'; import React, { forwardRef, useState, useEffect } from 'react'; import classNames from 'classnames'; import dayjs from 'dayjs'; import { g as getDefaultFormat, f as formatDate, a as formatTime, i as initYearMonthTime, p as parseToDayjs, b as isValidDate } from '../_chunks/dep-2c0a85a4.js'; import { s as subtractMonth, a as addMonth, e as extractTimeObj } from '../_chunks/dep-b1c8a9c9.js'; import { l as log } from '../_chunks/dep-b908e1fe.js'; import useConfig from '../hooks/useConfig.js'; import useDefaultProps from '../hooks/useDefaultProps.js'; import { RangeInputPopup } from '../range-input/index.js'; import { dateRangePickerDefaultProps } from './defaultProps.js'; import useRange from './hooks/useRange.js'; import RangePanel from './panel/RangePanel.js'; import { dateCorrection } from './utils.js'; import '../_chunks/dep-eca3a3de.js'; import '../_chunks/dep-026a4c6b.js'; import 'lodash-es'; import '../_chunks/dep-9dbbf468.js'; import '../_chunks/dep-f9b15bc8.js'; import '../_chunks/dep-8ef7b882.js'; import '../config-provider/ConfigContext.js'; import '../_chunks/dep-f97636ce.js'; import '../range-input/RangeInput.js'; import '../_chunks/dep-6b660ef0.js'; import 'tdesign-icons-react'; import '../input/index.js'; import '../input/Input.js'; import '../hooks/useLayoutEffect.js'; import '../_chunks/dep-3a09424a.js'; import '../_util/forwardRefWithStatics.js'; import 'hoist-non-react-statics'; import '../hooks/useGlobalIcon.js'; import '../input/InputGroup.js'; import '../hooks/useControlled.js'; import '../_util/noop.js'; import '../locale/LocalReceiver.js'; import '../config-provider/ConfigProvider.js'; import '../input/defaultProps.js'; import '../_util/parseTNode.js'; import '../input/useLengthLimit.js'; import '../_chunks/dep-6af6bc60.js'; import '../range-input/defaultProps.js'; import '../range-input/RangeInputPopup.js'; import '../popup/index.js'; import '../popup/Popup.js'; import 'react-transition-group'; import '../_util/ref.js'; import 'react-is'; import '../_util/isFragment.js'; import '../common/Portal.js'; import 'react-dom'; import '../hooks/useAnimation.js'; import '../hooks/useAttach.js'; import '../hooks/useMutationObserver.js'; import '../hooks/useLatest.js'; import '../hooks/usePopper.js'; import '@popperjs/core'; import 'react-fast-compare'; import '../hooks/useWindowSize.js'; import '../popup/defaultProps.js'; import '../popup/hooks/useTrigger.js'; import '../_util/composeRefs.js'; import '../_util/listener.js'; import '../popup/utils/transition.js'; import '../popup/PopupPlugin.js'; import '../_chunks/dep-52ff3837.js'; import '../_util/react-render.js'; import '../common/PluginContainer.js'; import '../select-input/useOverlayInnerStyle.js'; import './hooks/useRangeValue.js'; import './panel/PanelContent.js'; import './base/Header.js'; import '../select/index.js'; import '../select/base/Select.js'; import '../_util/helper.js'; import '../common/FakeArrow.js'; import '../loading/index.js'; import '../loading/Loading.js'; import '../loading/gradient.js'; import '../_chunks/dep-c48e2ca1.js'; import '../hooks/useDomRefCallback.js'; import '../loading/defaultProps.js'; import '../loading/plugin.js'; import '../select-input/index.js'; import '../select-input/SelectInput.js'; import '../select-input/useSingle.js'; import '../select-input/useMultiple.js'; import '../tag-input/index.js'; import '../tag-input/TagInput.js'; import '../hooks/useDragSorter.js'; import '../hooks/useEventCallback.js'; import '../tag-input/defaultProps.js'; import '../tag-input/useHover.js'; import '../tag-input/useTagList.js'; import '../tag/index.js'; import '../tag/Tag.js'; import '../_chunks/dep-3c3d29db.js'; import '../tag/defaultProps.js'; import '../tag/CheckTag.js'; import '../_chunks/dep-dbcf7b24.js'; import '../tag/CheckTagGroup.js'; import '../tag-input/useTagScroll.js'; import '../select-input/defaultProps.js'; import '../select/defaultProps.js'; import '../_chunks/dep-72472aa7.js'; import '../select/base/OptionGroup.js'; import '../select/base/Option.js'; import '../hooks/useRipple.js'; import '../select/base/PopupContent.js'; import '../select/hooks/usePanelVirtualScroll.js'; import '../hooks/useVirtualScroll.js'; import '../pagination/index.js'; import '../pagination/Pagination.js'; import '../input-number/index.js'; import '../input-number/InputNumber.js'; import '../button/index.js'; import '../button/Button.js'; import '../button/defaultProps.js'; import '../input-number/useInputNumber.js'; import '../_chunks/dep-76b39920.js'; import '../hooks/useCommonClassName.js'; import '../input-number/defaultProps.js'; import '../input-adornment/InputAdornment.js'; import '../pagination/hooks/useBoundaryJumper.js'; import '../pagination/hooks/usePrevNextJumper.js'; import '../pagination/hooks/usePageNumber.js'; import '../pagination/hooks/useTotal.js'; import '../pagination/validators.js'; import '../pagination/defaultProps.js'; import '../pagination/PaginationMini.js'; import './base/Table.js'; import './base/Cell.js'; import '../time-picker/index.js'; import '../time-picker/TimePicker.js'; import '../_chunks/dep-2990bb49.js'; import '../time-picker/hooks/useTimePickerTextConfig.js'; import '../hooks/useDebounce.js'; import '../hooks/usePersistFn.js'; import '../hooks/usePropsRef.js'; import '../time-picker/TimeRangePicker.js'; import '../time-picker/panel/TimePickerPanel.js'; import '../time-picker/defaultProps.js'; import './panel/ExtraContent.js'; import './base/Footer.js'; import './hooks/useTableData.js'; import './hooks/useDisableDate.js'; 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; } var DateRangePicker = /*#__PURE__*/forwardRef(function (originalProps, ref) { var _useConfig = useConfig(), classPrefix = _useConfig.classPrefix, globalDatePickerConfig = _useConfig.datePicker; var props = useDefaultProps(originalProps, dateRangePickerDefaultProps); var className = props.className, style = props.style, disabled = props.disabled, mode = props.mode, enableTimePicker = props.enableTimePicker, disableDate = props.disableDate, _props$firstDayOfWeek = props.firstDayOfWeek, firstDayOfWeek = _props$firstDayOfWeek === void 0 ? globalDatePickerConfig.firstDayOfWeek : _props$firstDayOfWeek, presets = props.presets, timePickerProps = props.timePickerProps, presetsPlacement = props.presetsPlacement, panelPreselection = props.panelPreselection, cancelRangeSelectLimit = props.cancelRangeSelectLimit, onPick = props.onPick, disableTime = props.disableTime; var _useRange = useRange(props), inputValue = _useRange.inputValue, popupVisible = _useRange.popupVisible, rangeInputProps = _useRange.rangeInputProps, popupProps = _useRange.popupProps, value = _useRange.value, year = _useRange.year, month = _useRange.month, time = _useRange.time, activeIndex = _useRange.activeIndex, isHoverCell = _useRange.isHoverCell, setActiveIndex = _useRange.setActiveIndex, onChange = _useRange.onChange, setIsHoverCell = _useRange.setIsHoverCell, setInputValue = _useRange.setInputValue, setPopupVisible = _useRange.setPopupVisible, setTime = _useRange.setTime, setYear = _useRange.setYear, setMonth = _useRange.setMonth, isFirstValueSelected = _useRange.isFirstValueSelected, setIsFirstValueSelected = _useRange.setIsFirstValueSelected, cacheValue = _useRange.cacheValue, setCacheValue = _useRange.setCacheValue; var _getDefaultFormat = getDefaultFormat({ mode: mode, enableTimePicker: enableTimePicker, format: props.format, valueType: props.valueType }), format = _getDefaultFormat.format, timeFormat = _getDefaultFormat.timeFormat, valueType = _getDefaultFormat.valueType; var _useState = useState(false), _useState2 = _slicedToArray(_useState, 2), isSelected = _useState2[0], setIsSelected = _useState2[1]; var handlePopupInvisible = function handlePopupInvisible() { var _props$popupProps, _props$popupProps$onV; setPopupVisible(false); (_props$popupProps = props.popupProps) === null || _props$popupProps === void 0 || (_props$popupProps$onV = _props$popupProps.onVisibleChange) === null || _props$popupProps$onV === void 0 || _props$popupProps$onV.call(_props$popupProps, false, {}); }; useEffect(function () { if (popupVisible) { setIsSelected(false); setCacheValue(formatDate(value || [], { format: format })); setTime(formatTime(value || [dayjs().format(timeFormat), dayjs().format(timeFormat)], format, timeFormat, props.defaultTime)); if (!value.length) { var _initYearMonthTime = initYearMonthTime({ value: value, mode: mode, format: format, enableTimePicker: enableTimePicker }), defaultYear = _initYearMonthTime.year, defaultMonth = _initYearMonthTime.month; setYear(defaultYear); setMonth(defaultMonth); } else if (value.length === 2 && !enableTimePicker) { var nextMonth = value.map(function (v) { return parseToDayjs(v, format).month(); }); if (year[0] === year[1] && nextMonth[0] === nextMonth[1]) { nextMonth[0] === 11 ? nextMonth[0] -= 1 : nextMonth[1] += 1; } setYear(value.map(function (v) { return parseToDayjs(v, format).year(); })); setMonth(nextMonth); } else { setYear(value.map(function (v) { return parseToDayjs(v, format).year(); })); setMonth(value.map(function (v) { return parseToDayjs(v, format).month(); })); } } else { setActiveIndex(0); setIsHoverCell(false); setIsFirstValueSelected(false); setInputValue(formatDate(value || [], { format: format })); } }, [popupVisible]); function onCellMouseEnter(date) { setIsHoverCell(true); var nextValue = _toConsumableArray(inputValue); nextValue[activeIndex] = formatDate(date, { format: format }); setInputValue(nextValue); } function onCellMouseLeave() { setIsHoverCell(false); setInputValue(cacheValue); } function onCellClick(date, _ref) { var e = _ref.e; onPick === null || onPick === void 0 || onPick(date, { e: e, partial: activeIndex ? "end" : "start" }); setIsHoverCell(false); setIsSelected(true); var nextValue = _toConsumableArray(inputValue); nextValue[activeIndex] = formatDate(date, { format: format }); setCacheValue(nextValue); setInputValue(nextValue); if (enableTimePicker) return; var notValidIndex = nextValue.findIndex(function (v) { return !v || !isValidDate(v, format); }); if (notValidIndex === -1 && nextValue.length === 2) { if (!isFirstValueSelected && parseToDayjs(nextValue[0], format).isAfter(parseToDayjs(nextValue[1], format))) { nextValue[activeIndex ? 0 : 1] = ""; setCacheValue(nextValue); setInputValue(nextValue); } else { onChange(formatDate(nextValue, { format: format, targetFormat: valueType, autoSwap: true }), { dayjsValue: nextValue.map(function (v) { return parseToDayjs(v, format); }), trigger: "pick" }); } } if (!isFirstValueSelected || !activeIndex) { var nextIndex = notValidIndex; if (nextIndex === -1) nextIndex = activeIndex ? 0 : 1; setActiveIndex(nextIndex); setIsFirstValueSelected(!!nextValue[0]); } else { handlePopupInvisible(); } } function onJumperClick(_ref2) { var trigger = _ref2.trigger, partial = _ref2.partial; var partialIndex = partial === "start" ? 0 : 1; var monthCountMap = { date: 1, week: 1, month: 12, quarter: 12, year: 120 }; var monthCount = monthCountMap[mode] || 0; var current = new Date(year[partialIndex], month[partialIndex]); var next = null; if (trigger === "prev") { next = subtractMonth(current, monthCount); } else if (trigger === "current") { next = new Date(); } else if (trigger === "next") { next = addMonth(current, monthCount); } var nextYear = _toConsumableArray(year); nextYear[partialIndex] = next.getFullYear(); var nextMonth = _toConsumableArray(month); nextMonth[partialIndex] = next.getMonth(); var onlyYearSelect = ["year", "quarter", "month"].includes(mode); var correctedDate = dateCorrection(partialIndex, nextYear, nextMonth, onlyYearSelect); nextYear = correctedDate.nextYear; nextMonth = correctedDate.nextMonth; setYear(nextYear); setMonth(nextMonth); } function onTimePickerChange(val) { var _extractTimeObj = extractTimeObj(val), hours = _extractTimeObj.hours, minutes = _extractTimeObj.minutes, seconds = _extractTimeObj.seconds, milliseconds = _extractTimeObj.milliseconds, meridiem = _extractTimeObj.meridiem; var nextInputValue = _toConsumableArray(inputValue); var changedInputValue = inputValue[activeIndex]; var currentDate = !dayjs(changedInputValue, format).isValid() ? dayjs().year(year[activeIndex]).month(month[activeIndex]) : dayjs(changedInputValue, format); var nextHours = hours; if (/am/i.test(meridiem) && nextHours === 12) nextHours -= 12; if (/pm/i.test(meridiem) && nextHours < 12) nextHours += 12; var nextDate = currentDate.hour(nextHours).minute(minutes).second(seconds).millisecond(milliseconds).toDate(); nextInputValue[activeIndex] = nextDate; var nextTime = _toConsumableArray(time); nextTime[activeIndex] = val; setTime(nextTime); setIsSelected(true); setInputValue(formatDate(nextInputValue, { format: format })); setCacheValue(formatDate(nextInputValue, { format: format })); } function onConfirmClick(_ref3) { var _props$onConfirm; var e = _ref3.e; var nextValue = _toConsumableArray(inputValue); props === null || props === void 0 || (_props$onConfirm = props.onConfirm) === null || _props$onConfirm === void 0 || _props$onConfirm.call(props, { e: e, date: nextValue, partial: activeIndex ? "end" : "start" }); var notValidIndex = nextValue.findIndex(function (v) { return !v || !isValidDate(v, format); }); if (notValidIndex === -1 && nextValue.length === 2) { if (!isFirstValueSelected && parseToDayjs(nextValue[0], format).isAfter(parseToDayjs(nextValue[1], format))) { nextValue[activeIndex ? 0 : 1] = ""; setCacheValue(nextValue); setInputValue(nextValue); } else { onChange(formatDate(nextValue, { format: format, targetFormat: valueType, autoSwap: true }), { dayjsValue: nextValue.map(function (v) { return parseToDayjs(v, format); }), trigger: "confirm" }); } } if (!isFirstValueSelected || !activeIndex) { var nextIndex = notValidIndex; if (nextIndex === -1) nextIndex = activeIndex ? 0 : 1; setActiveIndex(nextIndex); setIsFirstValueSelected(!!nextValue[0]); } else if (nextValue.length === 2) { handlePopupInvisible(); } } function onPresetClick(preset, context) { var presetValue = preset; if (typeof preset === "function") { presetValue = preset(); } if (!Array.isArray(presetValue)) { log.error("DateRangePicker", "preset: ".concat(preset, " must be Array!")); } else { var _props$onPresetClick; var formattedPreset = formatDate(presetValue, { format: format }); setInputValue(formattedPreset); setCacheValue(formattedPreset); setTime(formatTime(formattedPreset, format, timeFormat, props.defaultTime)); var newYear = formattedPreset.map(function (v) { return parseToDayjs(v, format).year(); }); var newMonth = formattedPreset.map(function (v) { return parseToDayjs(v, format).month(); }); setYear(newYear); setMonth(newMonth); setIsSelected(true); setIsFirstValueSelected(true); handlePopupInvisible(); onChange(formattedPreset, { dayjsValue: formattedPreset.map(function (p) { return parseToDayjs(p, format); }), trigger: "preset" }); (_props$onPresetClick = props.onPresetClick) === null || _props$onPresetClick === void 0 || _props$onPresetClick.call(props, context); } } function onYearChange(nextVal, _ref4) { var partial = _ref4.partial; var partialIndex = partial === "start" ? 0 : 1; if (enableTimePicker) partialIndex = activeIndex; var nextYear = _toConsumableArray(year); var nextMonth = _toConsumableArray(month); nextYear[partialIndex] = nextVal; var onlyYearSelect = ["year", "quarter", "month"].includes(mode); var correctedDate = dateCorrection(partialIndex, nextYear, nextMonth, onlyYearSelect); nextYear = correctedDate.nextYear; nextMonth = correctedDate.nextMonth; setYear(nextYear); !onlyYearSelect && setMonth(nextMonth); } function onMonthChange(nextVal, _ref5) { var partial = _ref5.partial; var partialIndex = partial === "start" ? 0 : 1; if (enableTimePicker) partialIndex = activeIndex; setMonth(function (currentMonth) { var nextMonth = _toConsumableArray(currentMonth); nextMonth[partialIndex] = nextVal; if (year[0] === year[1]) { if (partialIndex === 0) { if (nextMonth[1] <= nextMonth[0]) { nextMonth[1] = nextMonth[0] + 1; if (nextMonth[1] === 12) { nextMonth[1] = 0; setYear(function (currentYear) { return [currentYear[0], currentYear[1] + 1]; }); } } } if (partialIndex === 1) { nextMonth[0] = Math.min(nextMonth[0], nextMonth[1]); if (nextMonth[0] >= nextMonth[1]) { nextMonth[0] -= 1; if (nextMonth[0] === -1) { nextMonth[0] = 11; setYear(function (currentYear) { return [currentYear[0] - 1, currentYear[1]]; }); } } } } return nextMonth; }); } var panelProps = { hoverValue: isHoverCell ? inputValue : [], value: isSelected ? cacheValue : value, isFirstValueSelected: isFirstValueSelected, panelPreselection: panelPreselection, year: year, month: month, mode: mode, format: format, presets: presets, time: time, disableDate: disableDate, firstDayOfWeek: firstDayOfWeek, timePickerProps: timePickerProps, enableTimePicker: enableTimePicker, presetsPlacement: presetsPlacement, activeIndex: activeIndex, popupVisible: popupVisible, cancelRangeSelectLimit: cancelRangeSelectLimit, onCellClick: onCellClick, onCellMouseEnter: onCellMouseEnter, onCellMouseLeave: onCellMouseLeave, onJumperClick: onJumperClick, onConfirmClick: onConfirmClick, onPresetClick: onPresetClick, onYearChange: onYearChange, onMonthChange: onMonthChange, onTimePickerChange: onTimePickerChange, disableTime: disableTime }; return /* @__PURE__ */React.createElement("div", { className: classNames("".concat(classPrefix, "-date-range-picker"), className), style: style, ref: ref }, /* @__PURE__ */React.createElement(RangeInputPopup, { disabled: disabled, status: props.status, tips: props.tips, inputValue: inputValue, popupProps: popupProps, rangeInputProps: rangeInputProps, popupVisible: popupVisible, panel: /* @__PURE__ */React.createElement(RangePanel, _objectSpread({}, panelProps)) })); }); DateRangePicker.displayName = "DateRangePicker"; export { DateRangePicker as default }; //# sourceMappingURL=DateRangePicker.js.map