UNPKG

tdesign-vue

Version:
236 lines (232 loc) 9.46 kB
/** * tdesign v1.14.1 * (c) 2025 tdesign * @license MIT */ import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { ref, computed, watch } from '@vue/composition-api'; import { usePrefixClass } from '../../hooks/useConfig.js'; import { getDefaultFormat, formatDate, isValidDate, parseToDayjs } from '../../_common/js/date-picker/format.js'; import useRangeValue from './useRangeValue.js'; import { useConfig } from '../../config-provider/useConfig.js'; import '../../config-provider/context.js'; import 'lodash-es'; import '../../_common/js/global-config/default-config.js'; import '../../_common/js/global-config/locale/zh_CN.js'; import '../../_chunks/dep-ba613a02.js'; import '@babel/runtime/helpers/typeof'; import '../../_chunks/dep-fdb1b253.js'; import 'dayjs'; import '../../_common/js/global-config/t.js'; import '@babel/runtime/helpers/slicedToArray'; import '../../_chunks/dep-655d809c.js'; import '../../_common/js/log/log.js'; import '../../hooks/useVModel.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, _ref) { var emit = _ref.emit; var COMPONENT_NAME = usePrefixClass("date-range-picker"); var _useConfig = useConfig("datePicker"), global = _useConfig.global; var isMountedRef = ref(false); var inputRef = ref(); var _useRangeValue = useRangeValue(props), value = _useRangeValue.value, onChange = _useRangeValue.onChange, time = _useRangeValue.time, month = _useRangeValue.month, year = _useRangeValue.year, cacheValue = _useRangeValue.cacheValue, isFirstValueSelected = _useRangeValue.isFirstValueSelected; var formatRef = computed(function () { return getDefaultFormat({ mode: props.mode, format: props.format, valueType: props.valueType, enableTimePicker: props.enableTimePicker }); }); var popupVisible = ref(false); var isHoverCell = ref(false); var activeIndex = ref(0); var inputValue = ref(formatDate(value.value, { format: formatRef.value.format })); var rangeInputProps = computed(function () { return _objectSpread(_objectSpread({}, props.rangeInputProps), {}, { ref: inputRef, borderless: props.borderless, size: props.size, clearable: props.clearable, prefixIcon: props.prefixIcon, readonly: !props.allowInput || props.readonly, separator: props.separator || global.value.rangeSeparator, placeholder: props.placeholder || global.value.placeholder[props.mode], activeIndex: popupVisible.value ? activeIndex.value : void 0, "class": _defineProperty({}, "".concat(COMPONENT_NAME.value, "__input--placeholder"), isHoverCell.value), onClick: function onClick(_ref2) { var position = _ref2.position; activeIndex.value = position === "first" ? 0 : 1; }, onClear: function onClear(_ref3) { var e = _ref3.e; e.stopPropagation(); popupVisible.value = false; onChange === null || onChange === void 0 || onChange([], { dayjsValue: [], trigger: "clear" }); emit("clear", [], { dayjsValue: [], trigger: "clear" }); }, onBlur: function onBlur(newVal, _ref4) { var _props$onBlur; var e = _ref4.e, position = _ref4.position; (_props$onBlur = props.onBlur) === null || _props$onBlur === void 0 || _props$onBlur.call(props, { value: newVal, partial: PARTIAL_MAP[position], e: e }); emit("blur", { value: newVal, partial: PARTIAL_MAP[position], e: e }); }, onFocus: function onFocus(newVal, _ref5) { var _props$onFocus; var e = _ref5.e, position = _ref5.position; (_props$onFocus = props.onFocus) === null || _props$onFocus === void 0 || _props$onFocus.call(props, { value: newVal, partial: PARTIAL_MAP[position], e: e }); emit("focus", { value: newVal, partial: PARTIAL_MAP[position], e: e }); activeIndex.value = position === "first" ? 0 : 1; }, onChange: function onChange(newVal) { inputValue.value = newVal; if (!isValidDate(newVal, formatRef.value.format)) return; cacheValue.value = newVal; var newYear = []; var newMonth = []; var newTime = []; newVal.forEach(function (v) { newYear.push(parseToDayjs(v, formatRef.value.format).year()); newMonth.push(parseToDayjs(v, formatRef.value.format).month()); newTime.push(parseToDayjs(v, formatRef.value.format).format(formatRef.value.timeFormat)); }); year.value = newYear; month.value = newMonth; time.value = newTime; }, onEnter: function onEnter(newVal) { if (!isValidDate(newVal, formatRef.value.format) && !isValidDate(value.value, formatRef.value.format)) return; popupVisible.value = false; if (isValidDate(newVal, formatRef.value.format)) { onChange === null || onChange === void 0 || onChange(formatDate(newVal, { format: formatRef.value.format, targetFormat: formatRef.value.valueType, autoSwap: true }), { dayjsValue: newVal.map(function (v) { return parseToDayjs(v, formatRef.value.format); }), trigger: "enter" }); } else if (isValidDate(value.value, formatRef.value.format)) { inputValue.value = formatDate(value.value, { format: formatRef.value.format }); } else { inputValue.value = []; } } }); }); var popupProps = computed(function () { var _props$popupProps$ove, _props$popupProps, _props$popupProps2; return _objectSpread(_objectSpread({ expandAnimation: true }, props.popupProps), {}, { overlayInnerStyle: (_props$popupProps$ove = (_props$popupProps = props.popupProps) === null || _props$popupProps === void 0 ? void 0 : _props$popupProps.overlayInnerStyle) !== null && _props$popupProps$ove !== void 0 ? _props$popupProps$ove : { width: "auto" }, overlayClassName: [(_props$popupProps2 = props.popupProps) === null || _props$popupProps2 === void 0 ? void 0 : _props$popupProps2.overlayClassName, "".concat(COMPONENT_NAME.value, "__panel-container")], onVisibleChange: function onVisibleChange(visible, context) { var _props$popupProps3, _props$popupProps3$on; (_props$popupProps3 = props.popupProps) === null || _props$popupProps3 === void 0 || (_props$popupProps3$on = _props$popupProps3.onVisibleChange) === null || _props$popupProps3$on === void 0 || _props$popupProps3$on.call(_props$popupProps3, visible, context); if (context.trigger === "trigger-element-click") { var _inputRef$value, _inputRef$value$focus; var indexMap = { 0: "first", 1: "second" }; (_inputRef$value = inputRef.value) === null || _inputRef$value === void 0 || (_inputRef$value$focus = _inputRef$value.focus) === null || _inputRef$value$focus === void 0 || _inputRef$value$focus.call(_inputRef$value, { position: indexMap[activeIndex.value] }); popupVisible.value = true; return; } popupVisible.value = visible; } }); }); watch(value, function (value2) { if (!value2) { inputValue.value = []; return; } if (!isValidDate(value2, formatRef.value.format)) return; inputValue.value = formatDate(value2, { format: formatRef.value.format }); }, { immediate: true }); watch(activeIndex, function (index) { var _inputRef$value2, _inputRef$value2$focu; if (!isMountedRef.value) { isMountedRef.value = true; return; } if (!popupVisible.value) return; var indexMap = { 0: "first", 1: "second" }; (_inputRef$value2 = inputRef.value) === null || _inputRef$value2 === void 0 || (_inputRef$value2$focu = _inputRef$value2.focus) === null || _inputRef$value2$focu === void 0 || _inputRef$value2$focu.call(_inputRef$value2, { position: indexMap[index] }); }); return { year: year, month: month, value: value, time: time, inputValue: inputValue, popupVisible: popupVisible, rangeInputProps: rangeInputProps, popupProps: popupProps, isHoverCell: isHoverCell, activeIndex: activeIndex, isFirstValueSelected: isFirstValueSelected, cacheValue: cacheValue, onChange: onChange }; } export { PARTIAL_MAP, useRange as default }; //# sourceMappingURL=useRange.js.map