UNPKG

@vuesax-alpha/nightly

Version:
332 lines (327 loc) • 12.1 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var vue = require('vue'); var lodashUnified = require('lodash-unified'); var index$4 = require('../../input/index.js'); var index$2 = require('../../icon/index.js'); require('../../../directives/index.js'); require('../../../hooks/index.js'); require('../../../utils/index.js'); var iconsVue = require('@vuesax-alpha/icons-vue'); require('../../../constants/index.js'); var inputNumber = require('./input-number2.js'); var pluginVue_exportHelper = require('../../../_virtual/plugin-vue_export-helper.js'); var index = require('../../../hooks/use-namespace/index.js'); var color = require('../../../utils/color.js'); var index$1 = require('../../../hooks/use-common-props/index.js'); var types = require('../../../utils/types.js'); var error = require('../../../utils/error.js'); var event = require('../../../constants/event.js'); var shared = require('@vue/shared'); var index$3 = require('../../../directives/repeat-click/index.js'); const _hoisted_1 = ["onKeydown"]; const _hoisted_2 = ["onKeydown"]; const __default__ = vue.defineComponent({ name: "VsInputNumber" }); const _sfc_main = vue.defineComponent({ ...__default__, props: inputNumber.inputNumberProps, emits: inputNumber.inputNumberEmits, setup(__props, { expose: __expose, emit }) { const props = __props; const ns = index.useNamespace("input-number"); const input = vue.ref(); const data = vue.reactive({ currentValue: props.modelValue, userInput: null }); const wrapperStyle = vue.computed(() => ({ [ns.cssVarBlockName("color")]: color.getVsColor(props.color), [ns.cssVarBlockName("background")]: color.getVsColor(props.background) })); const isDisabled = index$1.useDisabled(vue.toRef(props, "disabled")); const minDisabled = vue.computed( () => types.isNumber(props.modelValue) && props.modelValue <= props.min ); const maxDisabled = vue.computed( () => types.isNumber(props.modelValue) && props.modelValue >= props.max ); const numPrecision = vue.computed(() => { const stepPrecision = getPrecision(props.step); if (!types.isUndefined(props.precision)) { if (stepPrecision > props.precision) { error.debugWarn( "InputNumber", "precision should not be less than the decimal places of step" ); } return props.precision; } else { return Math.max(getPrecision(props.modelValue), stepPrecision); } }); const displayValue = vue.computed(() => { if (data.userInput !== null) { return data.userInput; } let currentValue = data.currentValue; if (lodashUnified.isNil(currentValue)) return ""; if (types.isNumber(currentValue)) { if (Number.isNaN(currentValue)) return ""; if (!types.isUndefined(props.precision)) { currentValue = currentValue.toFixed(props.precision); } } return currentValue; }); const toPrecision = (num, pre) => { if (types.isUndefined(pre)) pre = numPrecision.value; if (pre === 0) return Math.round(num); let snum = String(num); const pointPos = snum.indexOf("."); if (pointPos === -1) return num; const nums = snum.replace(".", "").split(""); const datum = nums[pointPos + pre]; if (!datum) return num; const length = snum.length; if (snum.charAt(length - 1) === "5") { snum = `${snum.slice(0, Math.max(0, length - 1))}6`; } return Number.parseFloat(Number(snum).toFixed(pre)); }; const getPrecision = (value) => { if (lodashUnified.isNil(value)) return 0; const valueString = value.toString(); const dotPosition = valueString.indexOf("."); let precision = 0; if (dotPosition !== -1) { precision = valueString.length - dotPosition - 1; } return precision; }; const ensurePrecision = (val, coefficient = 1) => { if (!types.isNumber(val)) return data.currentValue; return toPrecision(val + props.step * coefficient); }; const increase = () => { if (props.readonly || isDisabled.value || maxDisabled.value) return; const value = Number(displayValue.value) || 0; const newVal = ensurePrecision(value); setCurrentValue(newVal); emit(event.INPUT_EVENT, data.currentValue); }; const decrease = () => { if (props.readonly || isDisabled.value || minDisabled.value) return; const value = Number(displayValue.value) || 0; const newVal = ensurePrecision(value, -1); setCurrentValue(newVal); emit(event.INPUT_EVENT, data.currentValue); }; const verifyValue = (value, update) => { const { max, min, step, precision, stepStrictly, valueOnClear } = props; if (max < min) { error.throwError("InputNumber", "min should not be greater than max."); } let newVal = Number(value); if (lodashUnified.isNil(value) || Number.isNaN(newVal)) { return null; } if (value === "") { if (valueOnClear === null) { return null; } newVal = shared.isString(valueOnClear) ? { min, max }[valueOnClear] : valueOnClear; } if (stepStrictly) { newVal = toPrecision(Math.round(newVal / step) * step, precision); } if (!types.isUndefined(precision)) { newVal = toPrecision(newVal, precision); } if (newVal > max || newVal < min) { newVal = newVal > max ? max : min; update && emit(event.UPDATE_MODEL_EVENT, newVal); } return newVal; }; const setCurrentValue = (value, emitChange = true) => { const oldVal = data.currentValue; const newVal = verifyValue(value); if (!emitChange) { emit(event.UPDATE_MODEL_EVENT, newVal); return; } if (oldVal === newVal) return; data.userInput = null; emit(event.UPDATE_MODEL_EVENT, newVal); emit(event.CHANGE_EVENT, newVal, oldVal); data.currentValue = newVal; }; const handleInput = (value) => { data.userInput = value; const newVal = value === "" ? null : Number(value); emit(event.INPUT_EVENT, newVal); setCurrentValue(newVal, false); }; const handleInputChange = (value) => { const newVal = value !== "" ? Number(value) : ""; if (types.isNumber(newVal) && !Number.isNaN(newVal) || value === "") { setCurrentValue(newVal); } data.userInput = null; }; const focus = () => { var _a, _b; (_b = (_a = input.value) == null ? void 0 : _a.focus) == null ? void 0 : _b.call(_a); }; const blur = () => { var _a, _b; (_b = (_a = input.value) == null ? void 0 : _a.blur) == null ? void 0 : _b.call(_a); }; const handleFocus = (event) => { emit("focus", event); }; const handleBlur = (event) => { emit("blur", event); }; vue.watch( () => props.modelValue, (value) => { const userInput = verifyValue(data.userInput); const newValue = verifyValue(value, true); if (!types.isNumber(userInput) && (!userInput || userInput !== newValue)) { data.currentValue = newValue; data.userInput = null; } }, { immediate: true } ); vue.onMounted(() => { var _a; const { min, max, modelValue } = props; const innerInput = (_a = input.value) == null ? void 0 : _a.inputRef; innerInput.setAttribute("role", "spinbutton"); if (Number.isFinite(max)) { innerInput.setAttribute("aria-valuemax", String(max)); } else { innerInput.removeAttribute("aria-valuemax"); } if (Number.isFinite(min)) { innerInput.setAttribute("aria-valuemin", String(min)); } else { innerInput.removeAttribute("aria-valuemin"); } innerInput.setAttribute( "aria-valuenow", data.currentValue || data.currentValue === 0 ? String(data.currentValue) : "" ); innerInput.setAttribute("aria-disabled", String(isDisabled.value)); if (!types.isNumber(modelValue) && modelValue != null) { let val = Number(modelValue); if (Number.isNaN(val)) { val = null; } emit(event.UPDATE_MODEL_EVENT, val); } }); vue.onUpdated(() => { var _a, _b; const innerInput = (_a = input.value) == null ? void 0 : _a.inputRef; innerInput == null ? void 0 : innerInput.setAttribute("aria-valuenow", `${(_b = data.currentValue) != null ? _b : ""}`); }); __expose({ focus, blur }); return (_ctx, _cache) => { return vue.openBlock(), vue.createElementBlock( "div", { class: vue.normalizeClass([ vue.unref(ns).b(), vue.unref(ns).is("disabled", vue.unref(isDisabled)), vue.unref(ns).is("readonly", _ctx.readonly), vue.unref(ns).is("without-controls", !_ctx.controls) ]), style: vue.normalizeStyle(wrapperStyle.value), onDragstart: _cache[1] || (_cache[1] = vue.withModifiers(() => { }, ["prevent"])) }, [ _ctx.controls ? vue.withDirectives((vue.openBlock(), vue.createElementBlock("span", { key: 0, role: "button", class: vue.normalizeClass([vue.unref(ns).e("decrease"), vue.unref(ns).is("disabled", minDisabled.value)]), onKeydown: vue.withKeys(decrease, ["enter"]) }, [ vue.createVNode(vue.unref(index$2.VsIcon), null, { default: vue.withCtx(() => [ vue.createVNode(vue.unref(iconsVue.Minus)) ]), _: 1 }) ], 42, _hoisted_1)), [ [vue.unref(index$3.RepeatClick), decrease] ]) : vue.createCommentVNode("v-if", true), _ctx.controls ? vue.withDirectives((vue.openBlock(), vue.createElementBlock("span", { key: 1, role: "button", class: vue.normalizeClass([vue.unref(ns).e("increase"), vue.unref(ns).is("disabled", maxDisabled.value)]), onKeydown: vue.withKeys(increase, ["enter"]) }, [ vue.createVNode(vue.unref(index$2.VsIcon), null, { default: vue.withCtx(() => [ vue.createVNode(vue.unref(iconsVue.Plus)) ]), _: 1 }) ], 42, _hoisted_2)), [ [vue.unref(index$3.RepeatClick), increase] ]) : vue.createCommentVNode("v-if", true), vue.createVNode(vue.unref(index$4.VsInput), { id: _ctx.id, ref_key: "input", ref: input, type: "number", block: "", step: _ctx.step, "model-value": displayValue.value, placeholder: _ctx.placeholder, readonly: _ctx.readonly, disabled: vue.unref(isDisabled), max: _ctx.max, min: _ctx.min, name: _ctx.name, label: _ctx.label, onWheel: _cache[0] || (_cache[0] = vue.withModifiers(() => { }, ["prevent"])), onKeydown: [ vue.withKeys(vue.withModifiers(increase, ["prevent"]), ["up"]), vue.withKeys(vue.withModifiers(decrease, ["prevent"]), ["down"]) ], onBlur: handleBlur, onFocus: handleFocus, onInput: handleInput, onChange: handleInputChange }, null, 8, ["id", "step", "model-value", "placeholder", "readonly", "disabled", "max", "min", "name", "label", "onKeydown"]) ], 38 ); }; } }); var InputNumber = /* @__PURE__ */ pluginVue_exportHelper["default"](_sfc_main, [["__file", "/home/runner/work/vuesax-alpha/vuesax-alpha/packages/components/input-number/src/input-number.vue"]]); exports["default"] = InputNumber; //# sourceMappingURL=input-number.js.map