tdesign-react
Version:
TDesign Component for React
88 lines (84 loc) • 3.14 kB
JavaScript
/**
* tdesign v1.15.1
* (c) 2025 tdesign
* @license MIT
*/
import { _ as _slicedToArray } from '../../_chunks/dep-48805ab8.js';
import { useState, useEffect } from 'react';
import { g as getDefaultFormat, e as extractTimeFormat, a as formatTime, p as parseToDayjs, f as formatDate, b as isValidDate } from '../../_chunks/dep-9036728c.js';
import '../../_chunks/dep-f53c91cd.js';
import useControlled from '../../hooks/useControlled.js';
import { l as log } from '../../_chunks/dep-b908e1fe.js';
import '../../_chunks/dep-026a4c6b.js';
import 'lodash-es';
import 'dayjs';
import '../../_chunks/dep-e29214cb.js';
import '../../_chunks/dep-dbf3be07.js';
import '../../_util/noop.js';
function useSingleValue(props) {
var _useControlled = useControlled(props, "value", props.onChange),
_useControlled2 = _slicedToArray(_useControlled, 2),
value = _useControlled2[0],
onChange = _useControlled2[1];
var _getDefaultFormat = getDefaultFormat({
mode: props.mode,
format: props.format,
enableTimePicker: props.multiple ? false : props.enableTimePicker
}),
format = _getDefaultFormat.format,
timeFormat = _getDefaultFormat.timeFormat;
if (props.enableTimePicker) {
if (!extractTimeFormat(format)) log.error("DatePicker", "format: ".concat(format, " is invalid\uFF0Ctime selection must include time formatting HH:mm:ss"));
}
var _useState = useState(function () {
return formatTime(props.multiple ? value === null || value === void 0 ? void 0 : value[0] : value, format, timeFormat, props.defaultTime);
}),
_useState2 = _slicedToArray(_useState, 2),
time = _useState2[0],
setTime = _useState2[1];
var _useState3 = useState(function () {
return parseToDayjs(props.multiple ? value === null || value === void 0 ? void 0 : value[0] : value, format).month();
}),
_useState4 = _slicedToArray(_useState3, 2),
month = _useState4[0],
setMonth = _useState4[1];
var _useState5 = useState(function () {
return parseToDayjs(props.multiple ? value === null || value === void 0 ? void 0 : value[0] : value, format).year();
}),
_useState6 = _slicedToArray(_useState5, 2),
year = _useState6[0],
setYear = _useState6[1];
var _useState7 = useState(function () {
return formatDate(props.multiple ? value === null || value === void 0 ? void 0 : value[0] : value, {
format: format
});
}),
_useState8 = _slicedToArray(_useState7, 2),
cacheValue = _useState8[0],
setCacheValue = _useState8[1];
useEffect(function () {
if (!value) {
setCacheValue("");
return;
}
if (!isValidDate(value, format)) return;
setCacheValue(formatDate(value, {
format: format
}));
setTime(formatTime(value, format, timeFormat, props.defaultTime));
}, [value]);
return {
year: year,
month: month,
value: value,
time: time,
cacheValue: cacheValue,
onChange: onChange,
setYear: setYear,
setMonth: setMonth,
setTime: setTime,
setCacheValue: setCacheValue
};
}
export { useSingleValue as default };
//# sourceMappingURL=useSingleValue.js.map