UNPKG

yanzhen-design-vue

Version:

85 lines (84 loc) 2.98 kB
import { ref, computed, unref, useAttrs, useSlots, mergeProps } from "vue"; import dayjs from "dayjs"; import { isEmptyValue, isEmptyArray } from "@yanzhen-libs/utils"; import { useDynamicProps, useModel, useProxyProps } from "@yanzhen-libs/utils-vue"; import { pick } from "lodash-es"; import { antRangePickerProps } from "./range-time.mjs"; function useRangeTime(props, emit, context) { const _ref = ref(null); const updatePropsKeys = useDynamicProps({ update: true }); const value = useModel(props, "value", emit); const beginTime = useModel(props, "beginTime", emit); const endTime = useModel(props, "endTime", emit); const showTimeRef = computed(() => { const { showTime } = props; const defaultValue = [dayjs("00:00:00", "HH:mm:ss"), dayjs("23:59:59", "HH:mm:ss")]; return showTime === true ? { format: "HH:mm:ss", defaultValue } : isEmptyValue(showTime) ? false : showTime; }); const formatRef = computed(() => { const format = props.format; const showTime = unref(showTimeRef); if (showTime === false) { return isEmptyValue(format) ? "YYYY-MM-DD" : format; } else { return isEmptyValue(format) ? "YYYY-MM-DD HH:mm:ss" : format; } }); function handleUpdateValue(_value) { const format = unref(formatRef); let [begin, end] = !isEmptyArray(_value) ? _value : []; begin = isEmptyValue(begin) ? void 0 : dayjs(begin).format(format); end = isEmptyValue(end) ? void 0 : dayjs(end).format(format); beginTime.value = begin; endTime.value = end; value.value = isEmptyValue(end) ? void 0 : [begin, end]; console.log(begin, end, beginTime.value, format, "begin, endbegin, end"); } const valueRef = computed(() => { if (updatePropsKeys.includes("beginTime") && updatePropsKeys.includes("endTime")) { const begin2 = isEmptyValue(unref(beginTime)) ? void 0 : dayjs(unref(beginTime)); const end2 = isEmptyValue(unref(endTime)) ? void 0 : dayjs(unref(endTime)); return [begin2, end2]; } const [begin, end] = !isEmptyArray(unref(value)) ? unref(value) : []; return [ isEmptyValue(begin) ? void 0 : dayjs(begin), isEmptyValue(end) ? void 0 : dayjs(end) ]; }); const attrs = useAttrs(); const slots = useSlots(); const slotKeys = Object.keys(slots); const config = computed(() => pick(props, ...Object.keys(antRangePickerProps.props))); const proxyProps = useProxyProps(config, antRangePickerProps.emits, { emit, exclude: ["value", "showTime", "format"], filter: (key) => { switch (key) { case "onUpdate:value": case "onUpdate:beginTime": case "onUpdate:endTime": return false; } return true; } }); const propsRef = computed(() => { return mergeProps(unref(attrs), unref(proxyProps)); }); return { _ref, formatRef, showTimeRef, propsRef, slotKeys, valueRef, handleUpdateValue }; } export { useRangeTime };