Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new behavior to autocomplete component #85

Merged
26 changes: 18 additions & 8 deletions packages/autocomplete/src/autocomplete.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@keydown.up.native.prevent="highlight(highlightedIndex - 1)"
@keydown.down.native.prevent="highlight(highlightedIndex + 1)"
@keydown.enter.native="handleKeyEnter"
@keydown.native.tab="handleKeyEnter"
@keydown.native.tab="handleKeyTab"
>
<template slot="prepend" v-if="$slots.prepend">
<slot name="prepend"></slot>
Expand Down Expand Up @@ -139,6 +139,10 @@
blurOnSelect: {
type: Boolean,
default: true
},
tabSelectsSuggestion: {
type: Boolean,
default: false
}
},
data() {
Expand All @@ -148,7 +152,7 @@
loading: false,
highlightedIndex: -1,
suggestionDisabled: false,
selected: false
handledSelection: false
};
},
computed: {
Expand Down Expand Up @@ -198,9 +202,9 @@
},
handleInput(value) {
this.$emit('input', value);
this.selected = false;
if (!this.blurOnSelect) { this.$refs.input.ignoreNextBlur(true); }
this.handledSelection = false;
this.suggestionDisabled = false;
if (!this.blurOnSelect) { this.$refs.input.ignoreNextBlur(true); }
if (!this.triggerOnFocus && !value) {
this.suggestionDisabled = true;
this.suggestions = [];
Expand All @@ -219,7 +223,6 @@
}
},
handleBlur(event) {
this.activated = false;
this.$emit('blur', event);
},
handleClear() {
Expand All @@ -228,11 +231,18 @@
},
close(e) {
this.activated = false;
if (!this.selected && !this.blurOnSelect) {
this.selected = true;
if (!this.handledSelection && !this.blurOnSelect) {
this.$refs.input.blur();
}
},
handleKeyTab(e) {
if (this.tabSelectsSuggestion) {
this.select(this.suggestions[this.highlightedIndex]);
} else {
if (!this.blurOnSelect) { this.$refs.input.ignoreNextBlur(false); }
this.close(e);
}
},
handleKeyEnter(e) {
if (!this.blurOnSelect) { this.$refs.input.ignoreNextBlur(false); }
if (this.suggestionVisible && this.highlightedIndex >= 0 && this.highlightedIndex < this.suggestions.length) {
Expand All @@ -247,7 +257,7 @@
}
},
select(item) {
this.selected = true;
this.handledSelection = true;
if (this.fillOnSelect) { this.$emit('input', item[this.valueKey]); }
this.$emit('select', item);
this.$nextTick(_ => {
Expand Down