yanzhen-design-vue
Version:
113 lines (112 loc) • 3.56 kB
JavaScript
import { defineComponent, reactive, watch, openBlock, createElementBlock, normalizeClass, unref, createVNode, createElementVNode } from "vue";
import { InputNumber } from "ant-design-vue";
import { rangeNumberName, rangeNumberProps, rangeNumberEmits, rangeNumberOptions } from "./rangeNumber.mjs";
const _sfc_main = /* @__PURE__ */ defineComponent({
...{ name: rangeNumberName },
__name: "rangeNumber",
props: rangeNumberProps,
emits: rangeNumberEmits,
setup(__props, { emit: __emit }) {
const { ns } = rangeNumberOptions;
const props = __props;
const emit = __emit;
const state = reactive({
min: "",
max: ""
});
const updateMinAndMaxValues = (value) => {
if (value) {
const values = value.split(",");
if (values.length === 1) {
const val = values[0];
state.min = val;
state.max = val;
} else {
const minVal = values[0];
const maxVal = values[1];
state.min = minVal;
state.max = maxVal;
}
} else {
state.min = "";
state.max = "";
}
};
watch(
() => props.value,
() => {
updateMinAndMaxValues(props.value);
},
{
immediate: true
}
);
const updateValueForInteger = (value) => {
let result = value;
if (value !== "" && typeof Number(value) === "number" && !Number.isNaN(Number(value)) && props.integer) {
result = Number.parseInt(value);
}
return result;
};
const onBlur = (type) => {
const min = state.min;
const max = state.max;
if (type === "max" && max) {
const precision = props.precision;
if (precision) {
state.max = Number.parseFloat(max).toFixed(precision);
}
} else if (type === "min" && min) {
const precision = props.precision;
if (precision) {
state.min = Number.parseFloat(min).toFixed(precision);
}
}
};
const onInputValue = (value, type) => {
if (type === "max") {
state.max = value;
} else {
state.min = value;
}
let max = state.max;
let min = state.min;
if (min === void 0 || min === null) {
min = "";
}
if (max === void 0 || max === null) {
max = "";
}
if (min === "" && max === "") {
emit("update:value", "");
} else {
min = updateValueForInteger(min);
max = updateValueForInteger(max);
emit("update:value", `${min},${max}`);
updateMinAndMaxValues(`${min},${max}`);
}
};
return (_ctx, _cache) => {
return openBlock(), createElementBlock("div", {
class: normalizeClass([unref(ns).b()])
}, [
createVNode(unref(InputNumber), {
value: state.min,
placeholder: _ctx.placeholder,
onBlur: _cache[0] || (_cache[0] = ($event) => onBlur("min")),
onChange: _cache[1] || (_cache[1] = ($event) => onInputValue($event, "min"))
}, null, 8, ["value", "placeholder"]),
_cache[4] || (_cache[4] = createElementVNode("span", { class: "separator" }, "~", -1)),
createVNode(unref(InputNumber), {
value: state.max,
placeholder: _ctx.placeholder,
onBlur: _cache[2] || (_cache[2] = ($event) => onBlur("max")),
onChange: _cache[3] || (_cache[3] = ($event) => onInputValue($event, "max"))
}, null, 8, ["value", "placeholder"])
], 2);
};
}
});
export {
_sfc_main as default
};