UNPKG

lulouis-vant

Version:

Lightweight Mobile UI Components built on Vue

161 lines (146 loc) 3.8 kB
"use strict"; exports.__esModule = true; exports.default = void 0; var _utils = require("../utils"); var _use = (0, _utils.use)('stepper'), sfc = _use[0], bem = _use[1]; var _default = sfc({ props: { value: null, integer: Boolean, disabled: Boolean, asyncChange: Boolean, disableInput: Boolean, min: { type: [String, Number], default: 1 }, max: { type: [String, Number], default: Infinity }, step: { type: [String, Number], default: 1 }, defaultValue: { type: [String, Number], default: 1 } }, data: function data() { var value = this.range((0, _utils.isDef)(this.value) ? this.value : this.defaultValue); if (value !== +this.value) { this.$emit('input', value); } return { currentValue: value }; }, computed: { minusDisabled: function minusDisabled() { return this.disabled || this.currentValue <= this.min; }, plusDisabled: function plusDisabled() { return this.disabled || this.currentValue >= this.max; } }, watch: { value: function value(val) { if (val !== this.currentValue) { this.currentValue = this.format(val); } }, currentValue: function currentValue(val) { this.$emit('input', val); this.$emit('change', val); } }, methods: { // filter illegal characters format: function format(value) { value = String(value).replace(/[^0-9.-]/g, ''); return value === '' ? 0 : this.integer ? Math.floor(value) : +value; }, // limit value range range: function range(value) { return Math.max(Math.min(this.max, this.format(value)), this.min); }, onInput: function onInput(event) { var value = event.target.value; var formatted = this.format(value); if (!this.asyncChange) { if (+value !== formatted) { event.target.value = formatted; } this.currentValue = formatted; } else { event.target.value = this.currentValue; this.$emit('input', formatted); this.$emit('change', formatted); } }, onChange: function onChange(type) { if (this[type + "Disabled"]) { this.$emit('overlimit', type); return; } var diff = type === 'minus' ? -this.step : +this.step; var value = Math.round((this.currentValue + diff) * 100) / 100; if (!this.asyncChange) { this.currentValue = this.range(value); } else { this.$emit('input', value); this.$emit('change', value); } this.$emit(type); }, onBlur: function onBlur(event) { this.currentValue = this.range(this.currentValue); this.$emit('blur', event); // fix edge case when input is empty and min is 0 if (this.currentValue === 0) { event.target.value = this.currentValue; } } }, render: function render(h) { var _this = this; var onChange = function onChange(type) { return function () { _this.onChange(type); }; }; return h("div", { "class": bem() }, [h("button", { "class": bem('minus', { disabled: this.minusDisabled }), "on": { "click": onChange('minus') } }), h("input", { "attrs": { "type": "number", "disabled": this.disabled || this.disableInput }, "class": bem('input'), "domProps": { "value": this.currentValue }, "on": { "input": this.onInput, "blur": this.onBlur } }), h("button", { "class": bem('plus', { disabled: this.plusDisabled }), "on": { "click": onChange('plus') } })]); } }); exports.default = _default;