Input: align input change with native (#7258)

* Input: align change event with native ones

* test: update input:change event test
pull/7428/head^2
Jiewei Qian 2017-10-12 14:28:42 +11:00 committed by 杨奕
parent 78d97c8e3f
commit 889fae43fb
2 changed files with 17 additions and 3 deletions

View File

@ -26,6 +26,7 @@
@input="handleInput" @input="handleInput"
@focus="handleFocus" @focus="handleFocus"
@blur="handleBlur" @blur="handleBlur"
@change="handleChange"
:aria-label="label" :aria-label="label"
> >
<!-- 前置内容 --> <!-- 前置内容 -->
@ -65,6 +66,7 @@
:style="textareaStyle" :style="textareaStyle"
@focus="handleFocus" @focus="handleFocus"
@blur="handleBlur" @blur="handleBlur"
@change="handleChange">
:aria-label="label" :aria-label="label"
> >
</textarea> </textarea>
@ -182,7 +184,9 @@
const value = event.target.value; const value = event.target.value;
this.$emit('input', value); this.$emit('input', value);
this.setCurrentValue(value); this.setCurrentValue(value);
this.$emit('change', value); },
handleChange(event) {
this.$emit('change', event.target.value);
}, },
setCurrentValue(value) { setCurrentValue(value) {
if (value === this.currentValue) return; if (value === this.currentValue) return;

View File

@ -207,6 +207,7 @@ describe('Input', () => {
}); });
}); });
it('event:change', done => { it('event:change', done => {
// NOTE: should be same as native's change behavior
vm = createVue({ vm = createVue({
template: ` template: `
<el-input <el-input
@ -222,12 +223,21 @@ describe('Input', () => {
} }
}, true); }, true);
const inputElm = vm.$el.querySelector('input');
const simulateEvent = (text, event) => {
inputElm.value = text;
inputElm.dispatchEvent(new Event(event));
};
const spy = sinon.spy(); const spy = sinon.spy();
vm.$refs.input.$on('change', spy); vm.$refs.input.$on('change', spy);
vm.input = 'b';
// simplified test, component should emit change when native does
simulateEvent('1', 'input');
simulateEvent('2', 'change');
vm.$nextTick(_ => { vm.$nextTick(_ => {
expect(spy.withArgs('b').calledOnce).to.be.false; expect(spy.calledWith('2')).to.be.true;
expect(spy.calledOnce).to.be.true;
done(); done();
}); });
}); });