UNPKG

tdesign-react

Version:
314 lines (310 loc) 12.3 kB
/** * tdesign v1.15.1 * (c) 2025 tdesign * @license MIT */ import { _ as _defineProperty } from '../_chunks/dep-cb0a3966.js'; import { _ as _slicedToArray } from '../_chunks/dep-48805ab8.js'; import React, { useState, useEffect } from 'react'; import classNames from 'classnames'; import dayjs from 'dayjs'; import { TimeIcon } from 'tdesign-icons-react'; import { isArray } from 'lodash-es'; import { T as TIME_PICKER_EMPTY, v as validateInputValue, f as formatInputValue } from '../_chunks/dep-f7770f5d.js'; import noop from '../_util/noop.js'; import useControlled from '../hooks/useControlled.js'; import useConfig from '../hooks/useConfig.js'; import useGlobalIcon from '../hooks/useGlobalIcon.js'; import { RangeInputPopup } from '../range-input/index.js'; import TimePickerPanel from './panel/TimePickerPanel.js'; import { useTimePickerTextConfig } from './hooks/useTimePickerTextConfig.js'; import { timeRangePickerDefaultProps } from './defaultProps.js'; import useDefaultProps from '../hooks/useDefaultProps.js'; import '../_chunks/dep-eca3a3de.js'; import '../_chunks/dep-026a4c6b.js'; import '../_chunks/dep-dbf3be07.js'; import '../_chunks/dep-e29214cb.js'; import '../_chunks/dep-3538034b.js'; import '../hooks/useDebounce.js'; import '../hooks/usePersistFn.js'; import '../hooks/usePropsRef.js'; import '../config-provider/ConfigContext.js'; import '../locale/zh_CN.js'; import '../_chunks/dep-3c9ab31a.js'; import '../range-input/RangeInput.js'; import '../_chunks/dep-6b660ef0.js'; 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 '../input/InputGroup.js'; import '../locale/LocalReceiver.js'; import '../config-provider/index.js'; import '../config-provider/ConfigProvider.js'; import '../config-provider/type.js'; import '../input/defaultProps.js'; import '../_util/parseTNode.js'; import '../_chunks/dep-f53c91cd.js'; import '../_chunks/dep-b908e1fe.js'; import '../input/useLengthLimit.js'; import '../_chunks/dep-1630b9b4.js'; import '../_chunks/dep-87d110df.js'; import '../input/style/index.js'; import '../input/type.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-a74cc5e4.js'; import '../_util/react-render.js'; import '../common/PluginContainer.js'; import '../popup/style/index.js'; import '../popup/type.js'; import '../select-input/useOverlayInnerStyle.js'; import '../range-input/style/index.js'; import '../range-input/type.js'; import '../button/index.js'; import '../button/Button.js'; import '../hooks/useDomRefCallback.js'; import '../hooks/useRipple.js'; import '../_chunks/dep-c48e2ca1.js'; import '../loading/index.js'; import '../loading/Loading.js'; import '../loading/gradient.js'; import '../loading/defaultProps.js'; import '../loading/plugin.js'; import '../loading/style/index.js'; import '../loading/type.js'; import '../button/defaultProps.js'; import '../button/style/index.js'; import '../button/type.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; } function handlePositionTrans(income) { return income === "first" ? "start" : "end"; } var TimeRangePicker = function TimeRangePicker(originalProps) { var props = useDefaultProps(originalProps, timeRangePickerDefaultProps); var TEXT_CONFIG = useTimePickerTextConfig(); var allowInput = props.allowInput, borderless = props.borderless, clearable = props.clearable, disabled = props.disabled, format = props.format, hideDisabledTime = props.hideDisabledTime, _props$placeholder = props.placeholder, placeholder = _props$placeholder === void 0 ? TEXT_CONFIG.rangePlaceholder : _props$placeholder, size = props.size, steps = props.steps, disableTime = props.disableTime, _props$onBlur = props.onBlur, onBlur = _props$onBlur === void 0 ? noop : _props$onBlur, _props$onFocus = props.onFocus, onFocus = _props$onFocus === void 0 ? noop : _props$onFocus, _props$onInput = props.onInput, onInput = _props$onInput === void 0 ? noop : _props$onInput, style = props.style, className = props.className, presets = props.presets, label = props.label; var _useControlled = useControlled(props, "value", props.onChange), _useControlled2 = _slicedToArray(_useControlled, 2), value = _useControlled2[0], onChange = _useControlled2[1]; var _useConfig = useConfig(), classPrefix = _useConfig.classPrefix; var _useGlobalIcon = useGlobalIcon({ TimeIcon: TimeIcon }), TimeIcon$1 = _useGlobalIcon.TimeIcon; var _useState = useState(false), _useState2 = _slicedToArray(_useState, 2), isPanelShowed = _useState2[0], setPanelShow = _useState2[1]; var _useState3 = useState(void 0), _useState4 = _slicedToArray(_useState3, 2), currentPanelIdx = _useState4[0], setCurrentPanelIdx = _useState4[1]; var _useState5 = useState(["", ""]), _useState6 = _slicedToArray(_useState5, 2), currentValue = _useState6[0], setCurrentValue = _useState6[1]; var name = "".concat(classPrefix, "-time-picker"); var inputClasses = classNames("".concat(name, "__group"), _defineProperty({}, "".concat(classPrefix, "-is-focused"), isPanelShowed)); var handleShowPopup = function handleShowPopup(visible, _ref) { var trigger = _ref.trigger; if (trigger === "trigger-element-click") { setPanelShow(true); return; } setPanelShow(visible); }; function handlePickerValue(pickValue, currentValue2) { var _currentValue2$, _currentValue2$2; if (Array.isArray(pickValue)) return pickValue; return currentPanelIdx === 0 ? [pickValue, (_currentValue2$ = currentValue2[1]) !== null && _currentValue2$ !== void 0 ? _currentValue2$ : pickValue] : [(_currentValue2$2 = currentValue2[0]) !== null && _currentValue2$2 !== void 0 ? _currentValue2$2 : pickValue, pickValue]; } var handleOnPick = function handleOnPick(pickValue, e) { var _props$onPick; var context; if (isArray(pickValue)) { context = { e: e }; } else if (currentPanelIdx.value === 0) { context = { e: e, position: "start" }; } else { context = { e: e, position: "end" }; } (_props$onPick = props.onPick) === null || _props$onPick === void 0 || _props$onPick.call(props, pickValue, context); }; var handleClear = function handleClear(context) { var e = context.e; e.stopPropagation(); onChange(void 0); setCurrentValue(TIME_PICKER_EMPTY); }; var handleClick = function handleClick(_ref2) { var position = _ref2.position; setCurrentPanelIdx(position === "first" ? 0 : 1); }; var handleTimeChange = function handleTimeChange(newValue, context) { var nextCurrentValue = handlePickerValue(newValue, currentValue); setCurrentValue(nextCurrentValue); handleOnPick(nextCurrentValue, context); }; var autoSwapTime = function autoSwapTime(valueBeforeConfirm) { var _valueBeforeConfirm = _slicedToArray(valueBeforeConfirm, 2), startTime = _valueBeforeConfirm[0], endTime = _valueBeforeConfirm[1]; var startDayjs = dayjs(startTime, props.format); var endDayjs = dayjs(endTime, props.format); if (startDayjs.isAfter(endDayjs, "second")) return [endTime, startTime]; return [startTime, endTime]; }; var handleInputBlur = function handleInputBlur(value2, _ref3) { var e = _ref3.e; if (allowInput) { var isValidTime = validateInputValue(currentValue[currentPanelIdx], format); if (isValidTime) { var _currentValue$, _currentValue$2; var formattedVal = formatInputValue(currentValue[currentPanelIdx], format); currentPanelIdx === 0 ? setCurrentValue([formattedVal, (_currentValue$ = currentValue[1]) !== null && _currentValue$ !== void 0 ? _currentValue$ : formattedVal]) : setCurrentValue([(_currentValue$2 = currentValue[0]) !== null && _currentValue$2 !== void 0 ? _currentValue$2 : formattedVal, formattedVal]); } } onBlur({ value: value2, e: e }); }; var handleInputChange = function handleInputChange(inputVal, _ref4) { var e = _ref4.e, position = _ref4.position; setCurrentValue(inputVal); onInput({ value: value, e: e, position: handlePositionTrans(position) }); }; var handleClickConfirm = function handleClickConfirm() { var isValidTime = !currentValue.find(function (v) { return !validateInputValue(v, format); }); if (isValidTime) onChange(props.autoSwap ? autoSwapTime(currentValue) : currentValue); setPanelShow(false); }; var handleFocus = function handleFocus(value2, _ref5) { var e = _ref5.e, position = _ref5.position; onFocus({ value: value2, e: e, position: handlePositionTrans(position) }); }; useEffect(function () { setCurrentValue(isPanelShowed ? value !== null && value !== void 0 ? value : TIME_PICKER_EMPTY : TIME_PICKER_EMPTY); if (!isPanelShowed) setCurrentPanelIdx(void 0); }, [isPanelShowed]); return /* @__PURE__ */React.createElement("div", { className: classNames(name, className), style: style }, /* @__PURE__ */React.createElement(RangeInputPopup, { style: style, disabled: disabled, popupVisible: isPanelShowed, onPopupVisibleChange: handleShowPopup, popupProps: _objectSpread({ overlayInnerStyle: { width: "auto", padding: 0 } }, props.popupProps), onInputChange: handleInputChange, inputValue: isPanelShowed ? currentValue : value !== null && value !== void 0 ? value : TIME_PICKER_EMPTY, rangeInputProps: _objectSpread({ size: size, borderless: borderless, clearable: clearable, className: inputClasses, value: isPanelShowed ? currentValue : value !== null && value !== void 0 ? value : void 0, placeholder: placeholder, suffixIcon: /* @__PURE__ */React.createElement(TimeIcon$1, null), onClear: handleClear, onClick: handleClick, onFocus: handleFocus, onBlur: handleInputBlur, readonly: !allowInput, activeIndex: currentPanelIdx, label: label }, props.rangeInputProps), tips: props.tips, status: props.status, panel: /* @__PURE__ */React.createElement(TimePickerPanel, { steps: steps, format: format, disableTime: disableTime, isShowPanel: isPanelShowed, hideDisabledTime: hideDisabledTime, isFooterDisplay: true, value: currentValue[currentPanelIdx || 0], onChange: handleTimeChange, handleConfirmClick: handleClickConfirm, position: currentPanelIdx === 0 ? "start" : "end", activeIndex: currentPanelIdx, presets: presets }) })); }; TimeRangePicker.displayName = "TimeRangePicker"; export { TimeRangePicker as default }; //# sourceMappingURL=TimeRangePicker.js.map