Skip to content

Commit

Permalink
feat(vue-autocomplete): only prevent default when list is open
Browse files Browse the repository at this point in the history
closes #464
  • Loading branch information
devCrossNet committed Sep 28, 2019
1 parent d7ca660 commit 01c4592
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,23 @@ describe('VueAutocomplete.vue', () => {
options: AutocompleteOptionsFixture,
},
});
const e: any = {
preventDefault: jest.fn(),
};

wrapper.vm.onEnterKeyPress();
wrapper.vm.onEnterKeyPress(e);
expect(e.preventDefault).not.toHaveBeenCalled();

wrapper.vm.searchQuery = 'Test';
wrapper.vm.selectedOptionIndex = -1;
wrapper.vm.onEnterKeyPress();
wrapper.setData({ isOpen: true });
wrapper.vm.onEnterKeyPress(e);
expect(wrapper.emitted().change[0][0]).toEqual(AutocompleteOptionsFixture[0]);
expect(e.preventDefault).toHaveBeenCalled();

wrapper.vm.searchQuery = 'Test2';
wrapper.vm.selectedOptionIndex = 1;
wrapper.vm.onEnterKeyPress();
wrapper.vm.onEnterKeyPress(e);
expect(wrapper.emitted().change[1][0]).toEqual(AutocompleteOptionsFixture[1]);
});

Expand Down
8 changes: 6 additions & 2 deletions src/app/shared/components/VueAutocomplete/VueAutocomplete.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
@input="onInput"
@keyup.stop.prevent.down="onArrowDown"
@keydown.stop.prevent.up="onArrowUp"
@keydown.stop.prevent.enter="onEnterKeyPress"
@keydown.stop.enter.tab="onEnterKeyPress"
@focus.stop.prevent="onFocus"
/>

Expand Down Expand Up @@ -248,7 +248,11 @@ export default {
this.isOpen = false;
}
},
onEnterKeyPress() {
onEnterKeyPress(e: any) {
if (this.isOpen) {
e.preventDefault();
}
if (this.searchQuery.length < this.minInputChars || this.options.length === 0) {
return;
}
Expand Down

0 comments on commit 01c4592

Please sign in to comment.