fix input-number change event

pull/2260/merge
baiyaaaaa 2017-01-11 00:39:35 +08:00 committed by 杨奕
parent 14c4646dbd
commit 7d300727d3
3 changed files with 28 additions and 19 deletions

View File

@ -21,10 +21,11 @@
> >
</span> </span>
<el-input <el-input
v-model.number="currentValue" :value="currentValue"
@keydown.up.native="increase" @keydown.up.native="increase"
@keydown.down.native="decrease" @keydown.down.native="decrease"
@blur="handleBlur" @blur="handleBlur"
@input="handleInput"
:disabled="disabled" :disabled="disabled"
:size="size" :size="size"
:max="max" :max="max"
@ -111,16 +112,10 @@
}; };
}, },
watch: { watch: {
value(val) { value(value) {
this.currentValue = val; const newVal = Number(value);
}, if (!isNaN(newVal) && newVal <= this.max && newVal >= this.min) {
this.currentValue = newVal;
currentValue(newVal, oldVal) {
if (newVal <= this.max && newVal >= this.min) {
this.$emit('change', newVal, oldVal);
this.$emit('input', newVal);
} else {
this.currentValue = oldVal;
} }
} }
}, },
@ -169,17 +164,31 @@
const value = this.value || 0; const value = this.value || 0;
const newVal = this._increase(value, this.step); const newVal = this._increase(value, this.step);
if (newVal > this.max) return; if (newVal > this.max) return;
this.currentValue = newVal; this.setCurrentValue(newVal);
}, },
decrease() { decrease() {
if (this.disabled || this.minDisabled) return; if (this.disabled || this.minDisabled) return;
const value = this.value || 0; const value = this.value || 0;
const newVal = this._decrease(value, this.step); const newVal = this._decrease(value, this.step);
if (newVal < this.min) return; if (newVal < this.min) return;
this.currentValue = newVal; this.setCurrentValue(newVal);
}, },
handleBlur() { handleBlur() {
this.$refs.input.setCurrentValue(this.currentValue); this.$refs.input.setCurrentValue(this.currentValue);
},
setCurrentValue(newVal) {
const oldVal = this.currentValue;
if (newVal <= this.max && newVal >= this.min && oldVal !== newVal) {
this.$emit('change', newVal, oldVal);
this.$emit('input', newVal);
this.currentValue = newVal;
}
},
handleInput(value) {
const newVal = Number(value);
if (!isNaN(newVal)) {
this.setCurrentValue(newVal);
}
} }
} }
}; };

View File

@ -237,7 +237,7 @@ describe('InputNumber', () => {
it('invalid value reset', done => { it('invalid value reset', done => {
vm = createVue({ vm = createVue({
template: ` template: `
<el-input-number v-model="value" :min="5" :max="10"> <el-input-number v-model="value" :min="5" :max="10" ref="inputNumber">
</el-input-number> </el-input-number>
`, `,
data() { data() {
@ -246,18 +246,18 @@ describe('InputNumber', () => {
}; };
} }
}, true); }, true);
const inputNumber = vm.$refs.inputNumber;
vm.value = 100; vm.value = 100;
vm.$nextTick(_ => { vm.$nextTick(_ => {
expect(vm.value).to.be.equal(5); expect(inputNumber.currentValue).to.be.equal(5);
vm.value = 4; vm.value = 4;
vm.$nextTick(_ => { vm.$nextTick(_ => {
expect(vm.value).to.be.equal(5); expect(inputNumber.currentValue).to.be.equal(5);
vm.value = 'dsajkhd'; vm.value = 'dsajkhd';
vm.$nextTick(_ => { vm.$nextTick(_ => {
expect(vm.value).to.be.equal(5); expect(inputNumber.currentValue).to.be.equal(5);
done(); done();
}); });
}); });

View File

@ -176,7 +176,7 @@ describe('Slider', () => {
setTimeout(() => { setTimeout(() => {
triggerEvent(vm.$el.querySelector('.el-input-number'), 'keyup'); triggerEvent(vm.$el.querySelector('.el-input-number'), 'keyup');
const inputNumber = vm.$el.querySelector('.el-input-number').__vue__; const inputNumber = vm.$el.querySelector('.el-input-number').__vue__;
inputNumber.currentValue = 40; inputNumber.setCurrentValue(40);
setTimeout(() => { setTimeout(() => {
expect(vm.value).to.equal(40); expect(vm.value).to.equal(40);
done(); done();