214 lines
7.3 KiB
JavaScript
214 lines
7.3 KiB
JavaScript
"use strict";
|
|
var __defProp = Object.defineProperty;
|
|
var __defProps = Object.defineProperties;
|
|
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
var __spreadValues = (a, b) => {
|
|
for (var prop in b || (b = {}))
|
|
if (__hasOwnProp.call(b, prop))
|
|
__defNormalProp(a, prop, b[prop]);
|
|
if (__getOwnPropSymbols)
|
|
for (var prop of __getOwnPropSymbols(b)) {
|
|
if (__propIsEnum.call(b, prop))
|
|
__defNormalProp(a, prop, b[prop]);
|
|
}
|
|
return a;
|
|
};
|
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
const common_vendor = require("../../../../common/vendor.js");
|
|
if (!Array) {
|
|
const _easycom_wd_icon2 = common_vendor.resolveComponent("wd-icon");
|
|
_easycom_wd_icon2();
|
|
}
|
|
const _easycom_wd_icon = () => "../wd-icon/wd-icon.js";
|
|
if (!Math) {
|
|
_easycom_wd_icon();
|
|
}
|
|
const __default__ = {
|
|
name: "wd-input-number",
|
|
options: {
|
|
virtualHost: true,
|
|
addGlobalClass: true,
|
|
styleIsolation: "shared"
|
|
}
|
|
};
|
|
const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent(__spreadProps(__spreadValues({}, __default__), {
|
|
props: common_vendor.inputNumberProps,
|
|
emits: ["focus", "blur", "change", "update:modelValue"],
|
|
setup(__props, { emit: __emit }) {
|
|
const props = __props;
|
|
const emit = __emit;
|
|
const minDisabled = common_vendor.ref(false);
|
|
const maxDisabled = common_vendor.ref(false);
|
|
const inputValue = common_vendor.ref("");
|
|
common_vendor.watch(
|
|
() => props.modelValue,
|
|
(newValue) => {
|
|
inputValue.value = newValue;
|
|
splitDisabled(newValue);
|
|
},
|
|
{ immediate: true, deep: true }
|
|
);
|
|
common_vendor.watch(
|
|
[() => props.max, () => props.min],
|
|
() => {
|
|
updateBoundary();
|
|
},
|
|
{ immediate: true, deep: true }
|
|
);
|
|
common_vendor.watch(
|
|
() => props.disabled,
|
|
(newValue) => {
|
|
minDisabled.value = newValue;
|
|
maxDisabled.value = newValue;
|
|
},
|
|
{ immediate: true, deep: true }
|
|
);
|
|
function updateBoundary() {
|
|
common_vendor.debounce(() => {
|
|
const value = formatValue(inputValue.value);
|
|
if (!common_vendor.isEqual(inputValue.value, value)) {
|
|
setValue(value);
|
|
}
|
|
splitDisabled(value);
|
|
}, 30)();
|
|
}
|
|
function splitDisabled(value) {
|
|
const { disabled, min, max, step } = props;
|
|
minDisabled.value = disabled || Number(value) <= min || changeStep(value, -step) < min;
|
|
maxDisabled.value = disabled || Number(value) >= max || changeStep(value, step) > max;
|
|
}
|
|
function toPrecision(value) {
|
|
return Number(parseFloat(`${Math.round(value * Math.pow(10, props.precision)) / Math.pow(10, props.precision)}`).toFixed(props.precision));
|
|
}
|
|
function getPrecision(value) {
|
|
if (value === void 0)
|
|
return 0;
|
|
const valueString = value.toString();
|
|
const dotPosition = valueString.indexOf(".");
|
|
let precision = 0;
|
|
if (dotPosition !== -1) {
|
|
precision = valueString.length - dotPosition - 1;
|
|
}
|
|
return precision;
|
|
}
|
|
function toStrictlyStep(value) {
|
|
const stepPrecision = getPrecision(props.step);
|
|
const precisionFactory = Math.pow(10, stepPrecision);
|
|
return Math.round(Number(value) / props.step) * precisionFactory * props.step / precisionFactory;
|
|
}
|
|
function setValue(value, change = true) {
|
|
if (props.allowNull && (!common_vendor.isDef(value) || value === "")) {
|
|
dispatchChangeEvent(value, change);
|
|
return;
|
|
}
|
|
if (props.stepStrictly) {
|
|
value = toStrictlyStep(value);
|
|
}
|
|
if ((value || value === 0) && props.precision !== void 0) {
|
|
value = toPrecision(Number(value));
|
|
}
|
|
if (Number(value) > props.max)
|
|
value = toPrecision(props.max);
|
|
if (Number(value) < props.min)
|
|
value = toPrecision(props.min);
|
|
dispatchChangeEvent(value, change);
|
|
}
|
|
function changeStep(val, step) {
|
|
val = Number(val);
|
|
if (isNaN(val)) {
|
|
return props.min;
|
|
}
|
|
const precisionFactory = Math.pow(10, props.precision);
|
|
return toPrecision((val * precisionFactory + step * precisionFactory) / precisionFactory);
|
|
}
|
|
function sub() {
|
|
if (minDisabled.value || props.disableMinus)
|
|
return;
|
|
const newValue = changeStep(inputValue.value, -props.step);
|
|
dispatchChangeEvent(newValue);
|
|
}
|
|
function add() {
|
|
if (maxDisabled.value || props.disablePlus)
|
|
return;
|
|
const newValue = changeStep(inputValue.value, props.step);
|
|
dispatchChangeEvent(newValue);
|
|
}
|
|
function handleInput(event) {
|
|
const value = event.detail.value || "";
|
|
dispatchChangeEvent(value);
|
|
}
|
|
function handleFocus(event) {
|
|
emit("focus", event.detail);
|
|
}
|
|
function handleBlur() {
|
|
const value = formatValue(inputValue.value);
|
|
if (!common_vendor.isEqual(inputValue.value, value)) {
|
|
setValue(value);
|
|
}
|
|
emit("blur", {
|
|
value
|
|
});
|
|
}
|
|
function dispatchChangeEvent(value, change = true) {
|
|
if (common_vendor.isEqual(inputValue.value, value)) {
|
|
return;
|
|
}
|
|
inputValue.value = value;
|
|
change && emit("update:modelValue", inputValue.value);
|
|
change && emit("change", { value });
|
|
}
|
|
function formatValue(value) {
|
|
if (props.allowNull && (!common_vendor.isDef(value) || value === "")) {
|
|
return "";
|
|
}
|
|
value = Number(value);
|
|
if (isNaN(value)) {
|
|
value = props.min;
|
|
}
|
|
if (props.stepStrictly) {
|
|
value = toStrictlyStep(value);
|
|
}
|
|
if (props.precision !== void 0) {
|
|
value = value.toFixed(props.precision);
|
|
}
|
|
return Number(value);
|
|
}
|
|
return (_ctx, _cache) => {
|
|
return common_vendor.e({
|
|
a: common_vendor.p({
|
|
name: "decrease",
|
|
["custom-class"]: "wd-input-number__action-icon"
|
|
}),
|
|
b: common_vendor.n(`wd-input-number__action ${minDisabled.value || _ctx.disableMinus ? "is-disabled" : ""}`),
|
|
c: common_vendor.o(sub),
|
|
d: !_ctx.withoutInput
|
|
}, !_ctx.withoutInput ? {
|
|
e: common_vendor.s(`${_ctx.inputWidth ? "width: " + _ctx.inputWidth : ""}`),
|
|
f: _ctx.disabled || _ctx.disableInput,
|
|
g: _ctx.placeholder,
|
|
h: common_vendor.o([($event) => inputValue.value = $event.detail.value, handleInput]),
|
|
i: common_vendor.o(handleFocus),
|
|
j: common_vendor.o(handleBlur),
|
|
k: inputValue.value,
|
|
l: common_vendor.o(() => {
|
|
})
|
|
} : {}, {
|
|
m: common_vendor.p({
|
|
name: "add",
|
|
["custom-class"]: "wd-input-number__action-icon"
|
|
}),
|
|
n: common_vendor.n(`wd-input-number__action ${maxDisabled.value || _ctx.disablePlus ? "is-disabled" : ""}`),
|
|
o: common_vendor.o(add),
|
|
p: common_vendor.n(`wd-input-number ${_ctx.customClass} ${_ctx.disabled ? "is-disabled" : ""} ${_ctx.withoutInput ? "is-without-input" : ""}`),
|
|
q: common_vendor.s(_ctx.customStyle)
|
|
});
|
|
};
|
|
}
|
|
}));
|
|
const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-609cd439"]]);
|
|
wx.createComponent(Component);
|