fix(Field): should not submit form on enter (#6240)

This commit is contained in:
chenjiahan 2020-05-08 21:03:18 +08:00
parent a7968c5f6e
commit 09fdde8c56
2 changed files with 23 additions and 4 deletions

View File

@ -353,11 +353,16 @@ export default createComponent({
}, },
onKeypress(event) { onKeypress(event) {
const ENTER_CODE = 13;
if (event.keyCode === ENTER_CODE) {
preventDefault(event);
// trigger blur after click keyboard search button // trigger blur after click keyboard search button
/* istanbul ignore next */ if (this.type === 'search') {
if (this.type === 'search' && event.keyCode === 13) {
this.blur(); this.blur();
} }
}
this.$emit('keypress', event); this.$emit('keypress', event);
}, },

View File

@ -342,3 +342,17 @@ test('colon prop', () => {
}); });
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
}); });
test('should blur search input on enter', () => {
const wrapper = mount(Field);
const input = wrapper.find('input');
input.element.focus();
input.trigger('keypress.enter');
expect(wrapper.emitted('blur')).toBeFalsy();
wrapper.setProps({ type: 'search' });
input.trigger('keypress.enter');
expect(wrapper.emitted('blur')).toBeTruthy();
});