Commit accebb77 authored by baiyaaaaa's avatar baiyaaaaa Committed by 杨奕

input-number auto adjust min max value

parent bbd779c2
...@@ -96,26 +96,20 @@ ...@@ -96,26 +96,20 @@
} }
}, },
data() { data() {
// correct the init value
let value = this.value;
if (value < this.min) {
this.$emit('input', this.min);
value = this.min;
}
if (value > this.max) {
this.$emit('input', this.max);
value = this.max;
}
return { return {
currentValue: value currentValue: 0
}; };
}, },
watch: { watch: {
value(value) { value: {
const newVal = Number(value); immediate: true,
if (!isNaN(newVal) && newVal <= this.max && newVal >= this.min) { handler(value) {
let newVal = Number(value);
if (isNaN(newVal)) return;
if (newVal >= this.max) newVal = this.max;
if (newVal <= this.min) newVal = this.min;
this.currentValue = newVal; this.currentValue = newVal;
this.$emit('input', newVal);
} }
} }
}, },
...@@ -178,11 +172,12 @@ ...@@ -178,11 +172,12 @@
}, },
setCurrentValue(newVal) { setCurrentValue(newVal) {
const oldVal = this.currentValue; const oldVal = this.currentValue;
if (newVal <= this.max && newVal >= this.min && oldVal !== newVal) { if (newVal >= this.max) newVal = this.max;
if (newVal <= this.min) newVal = this.min;
if (oldVal === newVal) return;
this.$emit('change', newVal, oldVal); this.$emit('change', newVal, oldVal);
this.$emit('input', newVal); this.$emit('input', newVal);
this.currentValue = newVal; this.currentValue = newVal;
}
}, },
handleInput(value) { handleInput(value) {
const newVal = Number(value); const newVal = Number(value);
......
...@@ -249,7 +249,7 @@ describe('InputNumber', () => { ...@@ -249,7 +249,7 @@ describe('InputNumber', () => {
const inputNumber = vm.$refs.inputNumber; const inputNumber = vm.$refs.inputNumber;
vm.value = 100; vm.value = 100;
vm.$nextTick(_ => { vm.$nextTick(_ => {
expect(inputNumber.currentValue).to.be.equal(5); expect(inputNumber.currentValue).to.be.equal(10);
vm.value = 4; vm.value = 4;
vm.$nextTick(_ => { vm.$nextTick(_ => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment