mirror of https://github.com/ElemeFE/element
fix input value binding bug (#1998)
* fix input value binding bug * improve input test & docspull/1956/head^2
parent
441f4c31a9
commit
6e428ffcc7
|
@ -591,7 +591,10 @@ Search data from server-side.
|
||||||
|
|
||||||
| Event Name | Description | Parameters |
|
| Event Name | Description | Parameters |
|
||||||
|----| ----| ----|
|
|----| ----| ----|
|
||||||
|click | triggers when the icon inside Input is clicked | event object |
|
|click | triggers when the icon inside Input is clicked | (event: Event) |
|
||||||
|
| blur | triggers when the icon inside Input is blur | (event: Event) |
|
||||||
|
| focus | triggers when the icon inside Input is focus | (event: Event) |
|
||||||
|
| change | triggers when the icon inside Input value change | (value: string \| number) |
|
||||||
|
|
||||||
### Autocomplete Attributes
|
### Autocomplete Attributes
|
||||||
|
|
||||||
|
|
|
@ -759,9 +759,10 @@ export default {
|
||||||
### Input Events
|
### Input Events
|
||||||
| 事件名称 | 说明 | 回调参数 |
|
| 事件名称 | 说明 | 回调参数 |
|
||||||
|---------|--------|---------|
|
|---------|--------|---------|
|
||||||
| click | 点击 Input 内的图标时触发 | event |
|
| click | 点击 Input 内的图标时触发 | (event: Event) |
|
||||||
| blur | 在 Input 失去焦点时触发 | event |
|
| blur | 在 Input 失去焦点时触发 | (event: Event) |
|
||||||
| focus | 在 Input 或得焦点时触发 | event |
|
| focus | 在 Input 或得焦点时触发 | (event: Event) |
|
||||||
|
| change | 在 Input 值改变时触发 | (value: string \| number) |
|
||||||
|
|
||||||
### Autocomplete Attributes
|
### Autocomplete Attributes
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@
|
||||||
:min="min"
|
:min="min"
|
||||||
:max="max"
|
:max="max"
|
||||||
:form="form"
|
:form="form"
|
||||||
:value="value"
|
:value="currentValue"
|
||||||
ref="input"
|
ref="input"
|
||||||
@input="handleInput"
|
@input="handleInput"
|
||||||
@focus="handleFocus"
|
@focus="handleFocus"
|
||||||
|
@ -76,6 +76,13 @@
|
||||||
|
|
||||||
mixins: [emitter],
|
mixins: [emitter],
|
||||||
|
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
currentValue: this.value,
|
||||||
|
textareaStyle: {}
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
value: [String, Number],
|
value: [String, Number],
|
||||||
placeholder: String,
|
placeholder: String,
|
||||||
|
@ -108,10 +115,23 @@
|
||||||
min: {}
|
min: {}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
computed: {
|
||||||
|
validating() {
|
||||||
|
return this.$parent.validateState === 'validating';
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
watch: {
|
||||||
|
'value'(val, oldValue) {
|
||||||
|
this.setCurrentValue(val);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
handleBlur(event) {
|
handleBlur(event) {
|
||||||
this.$emit('blur', event);
|
this.$emit('blur', event);
|
||||||
this.dispatch('ElFormItem', 'el.form.blur', [this.currentValue]);
|
this.dispatch('ElFormItem', 'el.form.blur', [this.currentValue]);
|
||||||
|
this.currentValue = this.value;
|
||||||
},
|
},
|
||||||
inputSelect() {
|
inputSelect() {
|
||||||
this.$refs.input.select();
|
this.$refs.input.select();
|
||||||
|
@ -130,46 +150,29 @@
|
||||||
this.$emit('focus', event);
|
this.$emit('focus', event);
|
||||||
},
|
},
|
||||||
handleInput(event) {
|
handleInput(event) {
|
||||||
this.currentValue = event.target.value;
|
this.setCurrentValue(event.target.value);
|
||||||
},
|
},
|
||||||
handleIconClick(event) {
|
handleIconClick(event) {
|
||||||
this.$emit('click', event);
|
this.$emit('click', event);
|
||||||
|
},
|
||||||
|
setCurrentValue(value) {
|
||||||
|
if (value === this.currentValue) return;
|
||||||
|
this.$nextTick(_ => {
|
||||||
|
this.resizeTextarea();
|
||||||
|
});
|
||||||
|
this.currentValue = value;
|
||||||
|
this.$emit('input', value);
|
||||||
|
this.$emit('change', value);
|
||||||
|
this.dispatch('ElFormItem', 'el.form.change', [value]);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
data() {
|
|
||||||
return {
|
|
||||||
currentValue: this.value,
|
|
||||||
textareaStyle: {}
|
|
||||||
};
|
|
||||||
},
|
|
||||||
|
|
||||||
created() {
|
created() {
|
||||||
this.$on('inputSelect', this.inputSelect);
|
this.$on('inputSelect', this.inputSelect);
|
||||||
},
|
},
|
||||||
|
|
||||||
mounted() {
|
mounted() {
|
||||||
this.resizeTextarea();
|
this.resizeTextarea();
|
||||||
},
|
|
||||||
|
|
||||||
computed: {
|
|
||||||
validating() {
|
|
||||||
return this.$parent.validateState === 'validating';
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
watch: {
|
|
||||||
'value'(val, oldValue) {
|
|
||||||
this.currentValue = val;
|
|
||||||
},
|
|
||||||
'currentValue'(val) {
|
|
||||||
this.$nextTick(_ => {
|
|
||||||
this.resizeTextarea();
|
|
||||||
});
|
|
||||||
this.$emit('input', val);
|
|
||||||
this.$emit('change', val);
|
|
||||||
this.dispatch('ElFormItem', 'el.form.change', [val]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -104,6 +104,7 @@ describe('Input', () => {
|
||||||
}, true);
|
}, true);
|
||||||
expect(vm.$el.querySelector('.el-textarea__inner').getAttribute('rows')).to.be.equal('3');
|
expect(vm.$el.querySelector('.el-textarea__inner').getAttribute('rows')).to.be.equal('3');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('autosize', done => {
|
it('autosize', done => {
|
||||||
vm = createVue({
|
vm = createVue({
|
||||||
template: `
|
template: `
|
||||||
|
@ -143,4 +144,57 @@ describe('Input', () => {
|
||||||
done();
|
done();
|
||||||
}, 200);
|
}, 200);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Input Events', () => {
|
||||||
|
it('event:focus & blur', done => {
|
||||||
|
vm = createVue({
|
||||||
|
template: `
|
||||||
|
<el-input
|
||||||
|
ref="input"
|
||||||
|
placeholder="请输入内容"
|
||||||
|
value="input">
|
||||||
|
</el-input>
|
||||||
|
`
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
const spyFocus = sinon.spy();
|
||||||
|
const spyBlur = sinon.spy();
|
||||||
|
|
||||||
|
vm.$refs.input.$on('focus', spyFocus);
|
||||||
|
vm.$refs.input.$on('blur', spyBlur);
|
||||||
|
vm.$el.querySelector('input').focus();
|
||||||
|
vm.$el.querySelector('input').blur();
|
||||||
|
|
||||||
|
vm.$nextTick(_ => {
|
||||||
|
expect(spyFocus.calledOnce).to.be.true;
|
||||||
|
expect(spyBlur.calledOnce).to.be.true;
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it('event:change', done => {
|
||||||
|
vm = createVue({
|
||||||
|
template: `
|
||||||
|
<el-input
|
||||||
|
ref="input"
|
||||||
|
placeholder="请输入内容"
|
||||||
|
:value="input">
|
||||||
|
</el-input>
|
||||||
|
`,
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
input: 'a'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
const spy = sinon.spy();
|
||||||
|
vm.$refs.input.$on('change', spy);
|
||||||
|
vm.input = 'b';
|
||||||
|
|
||||||
|
vm.$nextTick(_ => {
|
||||||
|
expect(spy.withArgs('b').calledOnce).to.be.true;
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue