vant-fork
Version:
Lightweight Mobile UI Components built on Vue
144 lines (131 loc) • 3.41 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _create = _interopRequireDefault(require("../utils/create"));
var _default = (0, _create.default)({
render: function render() {
var _vm = this;
var _h = _vm.$createElement;
var _c = _vm._self._c || _h;
return _c('div', {
class: _vm.b()
}, [_c('button', {
class: _vm.b('minus', {
disabled: _vm.minusDisabled
}),
on: {
"click": function click($event) {
_vm.onChange('minus');
}
}
}), _c('input', {
class: _vm.b('input'),
attrs: {
"type": "number",
"disabled": _vm.disabled || _vm.disableInput
},
domProps: {
"value": _vm.currentValue
},
on: {
"input": _vm.onInput,
"blur": _vm.onBlur
}
}), _c('button', {
class: _vm.b('plus', {
disabled: _vm.plusDisabled
}),
on: {
"click": function click($event) {
_vm.onChange('plus');
}
}
})]);
},
name: 'stepper',
props: {
value: null,
integer: Boolean,
disabled: 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(this.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 (+value !== formatted) {
event.target.value = formatted;
}
this.currentValue = 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;
this.currentValue = this.range(value);
this.$emit(type);
},
onBlur: function onBlur(event) {
this.currentValue = this.range(this.currentValue);
this.$emit('blur', event);
}
}
});
exports.default = _default;