yanzhen-design-vue
Version:
171 lines (170 loc) • 5.88 kB
JavaScript
import { defineComponent, useSlots, computed, ref, openBlock, createBlock, unref, mergeProps, createSlots, renderList, withCtx, renderSlot, normalizeProps, guardReactiveProps } from "vue";
import { DatePicker } from "ant-design-vue";
import dayjs from "dayjs";
import { cloneDeep, get } from "lodash-es";
import { timeName, timeProps, timeEmits, timeOptions } from "./time.mjs";
const _sfc_main = /* @__PURE__ */ defineComponent({
...{ name: timeName },
__name: "time",
props: timeProps,
emits: timeEmits,
setup(__props, { expose: __expose, emit: __emit }) {
const { ns } = timeOptions;
const props = __props;
const emit = __emit;
const slots = useSlots();
const slotKeys = Object.keys(slots);
const formatTimeMap = () => {
const hour = "hour";
const minute = "minute";
const second = "second";
return {
[hour]: {
type: hour,
format: "YYYY-MM-DD",
totalNumber: 24
},
[minute]: {
type: minute,
format: "YYYY-MM-DD HH",
totalNumber: 60
},
[second]: {
type: second,
format: "YYYY-MM-DD HH:mm",
totalNumber: 60
}
};
};
const format = computed(() => {
const { showTime, expandFormat } = props;
return showTime ? `${expandFormat || "YYYY-MM-DD"} HH:mm:ss` : `${expandFormat || "YYYY-MM-DD"}`;
});
const dateValue = computed(() => {
if (props.value) {
if (props.isTimeStamp) {
return dayjs(+props.value);
} else {
const formatTime = setFormatValue(String(props.value));
return dayjs(formatTime);
}
} else {
return null;
}
});
const setFormatValue = (value) => {
let formatValue = cloneDeep(value);
if (!formatValue) return value;
const { showTime } = props;
const hasSpace = formatValue.includes(" ");
if (showTime) {
if (!hasSpace) formatValue = `${formatValue} 00:00:00`;
} else {
if (hasSpace) formatValue = get(formatValue.split(" "), "[0]", "");
}
if (formatValue !== value) {
emit("update:value", formatValue);
return formatValue;
} else {
return value;
}
};
const disabledDate = (current) => {
const { beginTime: time1, endTime: time2 } = props;
const beginTime = dayjs(time1).startOf("day").format("YYYY-MM-DD HH:mm:ss");
const endTime = dayjs(time2).endOf("day").format("YYYY-MM-DD HH:mm:ss");
if (time1 && !time2) {
return current < dayjs(beginTime);
} else if (!time1 && time2) {
return current > dayjs(endTime);
} else if (time1 && time2) {
return current < dayjs(beginTime) || current > dayjs(endTime);
}
};
const getDisabledDateTime = (momentTime, formatInfo) => {
const { type, format: format2, totalNumber } = formatInfo;
const formatMap = formatTimeMap();
const startTime = dayjs(props.beginTime).startOf(type);
const startVal = startTime.valueOf();
const endTime = dayjs(props.endTime).startOf(type);
const endVal = endTime.valueOf();
const disabledTimes = [];
const currentTime = dayjs(momentTime).format(format2);
for (let t = 0; t < totalNumber; t++) {
const time = String(t).padStart(2, "0");
let fullTimeStr = "";
switch (formatInfo.type) {
case formatMap.hour.type: {
fullTimeStr = `${currentTime} ${time}:00:00`;
break;
}
case formatMap.minute.type: {
fullTimeStr = `${currentTime}:${time}:00`;
break;
}
case formatMap.second.type: {
fullTimeStr = `${currentTime}:${time}`;
}
}
const fullValue = new Date(fullTimeStr).getTime();
if (fullValue < startVal || fullValue > endVal) {
disabledTimes.push(t);
}
}
return disabledTimes;
};
const disabledDateTime = (currentDate) => {
let hasdisabledTimes = true;
const formatMap = formatTimeMap();
const { beginTime, endTime } = props;
if (!beginTime && !endTime) hasdisabledTimes = false;
return {
disabledHours: () => hasdisabledTimes ? getDisabledDateTime(currentDate, formatMap.hour) : [],
disabledMinutes: () => hasdisabledTimes ? getDisabledDateTime(currentDate, formatMap.minute) : [],
disabledSeconds: () => hasdisabledTimes ? getDisabledDateTime(currentDate, formatMap.second) : []
};
};
const handleChange = (date, dateString) => {
const { isTimeStamp } = props;
const dataValue = isTimeStamp ? new Date(dateString).getTime() : dateString;
emit("update:value", dataValue);
emit("change", dataValue);
emit("blur");
};
const handleBlur = (...args) => {
emit("blur", ...args);
};
const _ref = ref();
__expose({
ref: _ref
});
return (_ctx, _cache) => {
return openBlock(), createBlock(unref(DatePicker), mergeProps({
ref_key: "_ref",
ref: _ref
}, _ctx.$attrs, {
class: [unref(ns).b()],
value: dateValue.value,
format: format.value,
"show-time": props.showTime,
placeholder: props.placeholder,
"disabled-date": disabledDate,
"disabled-time": disabledDateTime,
onChange: handleChange,
onBlur: handleBlur
}), createSlots({ _: 2 }, [
renderList(unref(slotKeys), (slot) => {
return {
name: slot,
fn: withCtx((slotItem) => [
renderSlot(_ctx.$slots, slot, normalizeProps(guardReactiveProps(slotItem)))
])
};
})
]), 1040, ["class", "value", "format", "show-time", "placeholder"]);
};
}
});
export {
_sfc_main as default
};