dm-vue3-ui
Version:
This Components Library will help get you started developing in Vue 3.
135 lines (134 loc) • 5.45 kB
JavaScript
import { Tag, InputNumber, SelectOption, Select, Button } from "ant-design-vue/es";
import { defineComponent, ref, openBlock, createElementBlock, createElementVNode, Fragment, renderList, unref, createBlock, normalizeClass, withCtx, createTextVNode, toDisplayString, createVNode, isRef, withDirectives, vShow } from "vue";
import { realTimeValues, timeUnits } from "../../utils/config";
import { $t } from "../../../i18n/index";
import { isTimeNumber } from "../../utils/index";
const _hoisted_1 = { class: "dm-date-picker__real" };
const _hoisted_2 = { class: "quick-select-box" };
const _hoisted_3 = { class: "custom-time-box" };
const _hoisted_4 = { class: "text-span" };
const _sfc_main = /* @__PURE__ */ defineComponent({
__name: "index",
emits: ["change"],
setup(__props, { expose: __expose, emit: __emit }) {
let isError = ref(false);
let quickTags = ref(realTimeValues);
let startTimeValue = ref(5);
let startTimeUnit = ref("s");
let activeTagIndex = ref(-1);
const clickTag = (index) => {
activeTagIndex.value = index;
let currentTag = quickTags.value[index];
startTimeValue.value = currentTag.value;
startTimeUnit.value = currentTag.unit;
};
const validateDate = (value) => {
let flag = isTimeNumber(value);
isError.value = !flag;
return flag;
};
const updateActiveTag = (value, unit) => {
let index = quickTags.value.findIndex((item) => item.value == value && item.unit == unit);
activeTagIndex.value = index;
};
const changeInput = (value) => {
let flag = validateDate(value);
if (flag) {
updateActiveTag(value, startTimeUnit.value);
}
};
const changeSelect = (unit) => {
updateActiveTag(startTimeValue.value, unit);
};
const setValue = (startTime, endTime) => {
let matchStartTime = startTime.match(/^rt-(\d+)(s|m|h|d|w|mon|q|y)$/);
if (matchStartTime && endTime == "rtnow") {
startTimeValue.value = matchStartTime[1];
startTimeUnit.value = matchStartTime[2];
updateActiveTag(startTimeValue.value, startTimeUnit.value);
}
};
const emit = __emit;
const onSubmit = () => {
let flag = validateDate(startTimeValue.value);
if (flag) {
let value = {
date: [`rt-${startTimeValue.value}${startTimeUnit.value}`, `rtnow`],
type: "real"
};
emit("change", value);
}
};
__expose({ setValue });
return (_ctx, _cache) => {
const _component_a_tag = Tag;
const _component_a_input_number = InputNumber;
const _component_a_select_option = SelectOption;
const _component_a_select = Select;
const _component_a_button = Button;
return openBlock(), createElementBlock("div", _hoisted_1, [
createElementVNode("div", _hoisted_2, [
(openBlock(true), createElementBlock(Fragment, null, renderList(unref(quickTags), (tag, index) => {
return openBlock(), createBlock(_component_a_tag, {
key: index,
class: normalizeClass([{ "active-tag": unref(activeTagIndex) == index }]),
onClick: ($event) => clickTag(index)
}, {
default: withCtx(() => [
createTextVNode(toDisplayString(tag.label), 1)
]),
_: 2
}, 1032, ["class", "onClick"]);
}), 128))
]),
createElementVNode("div", _hoisted_3, [
createElementVNode("span", _hoisted_4, toDisplayString(unref($t)("datePicker.real.earliest")) + ":", 1),
createVNode(_component_a_input_number, {
id: "inputNumber",
value: unref(startTimeValue),
"onUpdate:value": _cache[0] || (_cache[0] = ($event) => isRef(startTimeValue) ? startTimeValue.value = $event : startTimeValue = $event),
min: 1,
onChange: changeInput
}, null, 8, ["value"]),
createVNode(_component_a_select, {
value: unref(startTimeUnit),
"onUpdate:value": _cache[1] || (_cache[1] = ($event) => isRef(startTimeUnit) ? startTimeUnit.value = $event : startTimeUnit = $event),
onChange: changeSelect,
dropdownMatchSelectWidth: false
}, {
default: withCtx(() => [
(openBlock(true), createElementBlock(Fragment, null, renderList(unref(timeUnits), (item, index) => {
return openBlock(), createBlock(_component_a_select_option, {
key: index,
label: item.label,
value: item.value
}, {
default: withCtx(() => [
createTextVNode(toDisplayString(item.label), 1)
]),
_: 2
}, 1032, ["label", "value"]);
}), 128))
]),
_: 1
}, 8, ["value"]),
createVNode(_component_a_button, {
type: "primary",
onClick: onSubmit
}, {
default: withCtx(() => [
createTextVNode(toDisplayString(unref($t)("datePicker.confirm")), 1)
]),
_: 1
})
]),
withDirectives(createElementVNode("div", { class: "error-text" }, toDisplayString(unref($t)("datePicker.invalidTime")), 513), [
[vShow, unref(isError)]
])
]);
};
}
});
export {
_sfc_main as default
};