tdesign-react
Version:
TDesign Component for React
272 lines (268 loc) • 10.7 kB
JavaScript
/**
* 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, { useRef, useState, useEffect } from 'react';
import { CalendarIcon } from 'tdesign-icons-react';
import classNames from 'classnames';
import { g as getDefaultFormat, f as formatDate, b as isValidDate, p as parseToDayjs } from '../../_chunks/dep-9036728c.js';
import useConfig from '../../hooks/useConfig.js';
import useGlobalIcon from '../../hooks/useGlobalIcon.js';
import useRange$1 from './useRangeValue.js';
import '../../_chunks/dep-eca3a3de.js';
import '../../_chunks/dep-026a4c6b.js';
import 'lodash-es';
import 'dayjs';
import '../../_chunks/dep-e29214cb.js';
import '../../_chunks/dep-dbf3be07.js';
import '../../_chunks/dep-f53c91cd.js';
import '../../_chunks/dep-b908e1fe.js';
import '../../config-provider/ConfigContext.js';
import '../../locale/zh_CN.js';
import '../../_chunks/dep-3c9ab31a.js';
import '../../hooks/useControlled.js';
import '../../_util/noop.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 PARTIAL_MAP = {
first: "start",
second: "end"
};
function useRange(props) {
var _props$separator, _props$placeholder, _props$suffixIcon, _props$popupProps$ove, _props$popupProps2, _props$popupProps3;
var _useConfig = useConfig(),
classPrefix = _useConfig.classPrefix,
globalDatePickerConfig = _useConfig.datePicker;
var _useGlobalIcon = useGlobalIcon({
CalendarIcon: CalendarIcon
}),
CalendarIcon$1 = _useGlobalIcon.CalendarIcon;
var name = "".concat(classPrefix, "-date-range-picker");
var isMountedRef = useRef(false);
var inputRef = useRef(null);
var _useRangeValue = useRange$1(props),
value = _useRangeValue.value,
onChange = _useRangeValue.onChange,
time = _useRangeValue.time,
setTime = _useRangeValue.setTime,
month = _useRangeValue.month,
setMonth = _useRangeValue.setMonth,
year = _useRangeValue.year,
setYear = _useRangeValue.setYear,
cacheValue = _useRangeValue.cacheValue,
setCacheValue = _useRangeValue.setCacheValue,
isFirstValueSelected = _useRangeValue.isFirstValueSelected,
setIsFirstValueSelected = _useRangeValue.setIsFirstValueSelected;
var _getDefaultFormat = getDefaultFormat({
mode: props.mode,
format: props.format,
valueType: props.valueType,
enableTimePicker: props.enableTimePicker
}),
format = _getDefaultFormat.format,
timeFormat = _getDefaultFormat.timeFormat,
valueType = _getDefaultFormat.valueType;
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
popupVisible = _useState2[0],
setPopupVisible = _useState2[1];
var _useState3 = useState(false),
_useState4 = _slicedToArray(_useState3, 2),
isHoverCell = _useState4[0],
setIsHoverCell = _useState4[1];
var _useState5 = useState(0),
_useState6 = _slicedToArray(_useState5, 2),
activeIndex = _useState6[0],
setActiveIndex = _useState6[1];
var _useState7 = useState(function () {
return formatDate(value, {
format: format
});
}),
_useState8 = _slicedToArray(_useState7, 2),
inputValue = _useState8[0],
setInputValue = _useState8[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, {});
};
var rangeInputProps = _objectSpread(_objectSpread({}, props.rangeInputProps), {}, {
ref: inputRef,
borderless: props.borderless,
size: props.size,
separator: (_props$separator = props.separator) !== null && _props$separator !== void 0 ? _props$separator : globalDatePickerConfig.rangeSeparator,
clearable: props.clearable,
prefixIcon: props.prefixIcon,
readonly: !props.allowInput,
placeholder: (_props$placeholder = props.placeholder) !== null && _props$placeholder !== void 0 ? _props$placeholder : globalDatePickerConfig.placeholder[props.mode],
activeIndex: popupVisible ? activeIndex : void 0,
suffixIcon: (_props$suffixIcon = props.suffixIcon) !== null && _props$suffixIcon !== void 0 ? _props$suffixIcon : /* @__PURE__ */React.createElement(CalendarIcon$1, null),
label: props.label,
className: classNames(_defineProperty({}, "".concat(name, "__input--placeholder"), isHoverCell)),
onClick: function onClick(_ref) {
var position = _ref.position;
setActiveIndex(position === "first" ? 0 : 1);
},
onClear: function onClear(_ref2) {
var _props$onClear;
var e = _ref2.e;
e.stopPropagation();
handlePopupInvisible();
onChange([], {
dayjsValue: [],
trigger: "clear"
});
(_props$onClear = props.onClear) === null || _props$onClear === void 0 || _props$onClear.call(props, {
e: e
});
},
onBlur: function onBlur(newVal, _ref3) {
var _props$onBlur;
var e = _ref3.e,
position = _ref3.position;
(_props$onBlur = props.onBlur) === null || _props$onBlur === void 0 || _props$onBlur.call(props, {
value: newVal,
partial: PARTIAL_MAP[position],
e: e
});
},
onFocus: function onFocus(newVal, _ref4) {
var _props$onFocus;
var e = _ref4.e,
position = _ref4.position;
(_props$onFocus = props.onFocus) === null || _props$onFocus === void 0 || _props$onFocus.call(props, {
value: newVal,
partial: PARTIAL_MAP[position],
e: e
});
setActiveIndex(position === "first" ? 0 : 1);
},
onChange: function onChange(newVal, _ref5) {
var _props$onInput;
var e = _ref5.e,
position = _ref5.position;
var index = position === "first" ? 0 : 1;
(_props$onInput = props.onInput) === null || _props$onInput === void 0 || _props$onInput.call(props, {
input: newVal[index],
value: value,
partial: PARTIAL_MAP[position],
e: e
});
setInputValue(newVal);
if (!isValidDate(newVal, format)) return;
setCacheValue(newVal);
var newYear = [];
var newMonth = [];
var newTime = [];
newVal.forEach(function (v) {
newYear.push(parseToDayjs(v, format).year());
newMonth.push(parseToDayjs(v, format).month());
newTime.push(parseToDayjs(v, format).format(timeFormat));
});
setYear(newYear);
setMonth(newMonth);
setTime(newTime);
},
onEnter: function onEnter(newVal) {
if (!isValidDate(newVal, format) && !isValidDate(value, format)) return;
handlePopupInvisible();
if (isValidDate(newVal, format)) {
onChange(formatDate(newVal, {
format: format,
targetFormat: valueType,
autoSwap: true
}), {
dayjsValue: newVal.map(function (v) {
return parseToDayjs(v, format);
}),
trigger: "enter"
});
} else if (isValidDate(value, format)) {
setInputValue(formatDate(value, {
format: format
}));
} else {
setInputValue([]);
}
}
});
var popupProps = _objectSpread(_objectSpread({
expandAnimation: true
}, props.popupProps), {}, {
trigger: "mousedown",
overlayInnerStyle: (_props$popupProps$ove = (_props$popupProps2 = props.popupProps) === null || _props$popupProps2 === void 0 ? void 0 : _props$popupProps2.overlayInnerStyle) !== null && _props$popupProps$ove !== void 0 ? _props$popupProps$ove : {
width: "auto"
},
overlayClassName: classNames((_props$popupProps3 = props.popupProps) === null || _props$popupProps3 === void 0 ? void 0 : _props$popupProps3.overlayClassName, "".concat(name, "__panel-container")),
onVisibleChange: function onVisibleChange(visible, context) {
var _props$popupProps4, _props$popupProps4$on;
if (props.disabled) return;
(_props$popupProps4 = props.popupProps) === null || _props$popupProps4 === void 0 || (_props$popupProps4$on = _props$popupProps4.onVisibleChange) === null || _props$popupProps4$on === void 0 || _props$popupProps4$on.call(_props$popupProps4, visible, context);
if (context.trigger === "trigger-element-mousedown") {
var indexMap = {
0: "first",
1: "second"
};
inputRef.current.focus({
position: indexMap[activeIndex]
});
return setPopupVisible(true);
}
setPopupVisible(visible);
}
});
useEffect(function () {
if (!value) {
setInputValue([]);
return;
}
if (!isValidDate(value, format)) return;
setInputValue(formatDate(value, {
format: format
}));
}, [value]);
useEffect(function () {
if (!popupVisible) return;
if (!isMountedRef.current) {
isMountedRef.current = true;
return;
}
var indexMap = {
0: "first",
1: "second"
};
inputRef.current.focus({
position: indexMap[activeIndex]
});
}, [activeIndex]);
return {
year: year,
month: month,
value: value,
time: time,
inputValue: inputValue,
popupVisible: popupVisible,
rangeInputProps: rangeInputProps,
popupProps: popupProps,
isHoverCell: isHoverCell,
onChange: onChange,
setYear: setYear,
setMonth: setMonth,
setTime: setTime,
setIsHoverCell: setIsHoverCell,
setInputValue: setInputValue,
setPopupVisible: setPopupVisible,
activeIndex: activeIndex,
setActiveIndex: setActiveIndex,
isFirstValueSelected: isFirstValueSelected,
setIsFirstValueSelected: setIsFirstValueSelected,
cacheValue: cacheValue,
setCacheValue: setCacheValue
};
}
export { PARTIAL_MAP, useRange as default };
//# sourceMappingURL=useRange.js.map