Skip to content

Commit

Permalink
New feature/option: show typeahead on input field focus
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMordred committed Mar 1, 2020
1 parent fba05fe commit e1cd92c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ typeahead-style | String | 'badges' | The autocomplete prompt style. Possible va
typeahead-max-results | Number | 0 | Maximum number of typeahead results to be shown. 0 - unlimited.
typeahead-activation-threshold | Number | 1 | Show typeahead results only after at least this many characters were entered. When set to 0, typeahead with all the available tags will be displayed on input focus.
typeahead-always-show | Boolean | false | Always show typeahead, even if not focused or under typeahead-activation-threshold.
typeahead-show-on-focus | Boolean | true | Show typeahead on input field focus.
typeahead-hide-discard | Boolean | false | Hides the 'Discard Search Results' option.
placeholder | String | 'Add a tag' | The placeholder of the tag input.
discard-search-text | String | 'Discard Search Results' | The 'Discard Search Results' button text.
Expand Down
11 changes: 11 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ <h2 class="is-size-4" style="margin-bottom: 1rem;">Options</h2>
</div>
</div>

<div v-show="typeahead" class="field">
<div class="control">
<label class="checkbox">
<input type="checkbox" v-model="typeaheadShowOnFocus">
Show Typeahead on focus
</label>
</div>
</div>

<div v-show="typeahead" class="field">
<div class="control">
<label class="checkbox">
Expand Down Expand Up @@ -211,6 +220,7 @@ <h2 class="is-size-4" style="margin-bottom: 1rem;">Options</h2>
:typeahead-max-results="typeaheadMaxResults"
:typeahead-activation-threshold="typeaheadActivationThreshold"
:typeahead-always-show="typeheadAlwaysShow"
:typeahead-show-on-focus="typeaheadShowOnFocus"
:typeahead-hide-discard="typeaheadHideDiscard"
:placeholder="placeholder"
:limit="limit"
Expand Down Expand Up @@ -284,6 +294,7 @@ <h2 class="is-size-5" style="margin-top: 1rem;">Data Events (new at the top)</h2
typeaheadMaxResults: 20,
typeaheadActivationThreshold: 1,
typeheadAlwaysShow: false,
typeaheadShowOnFocus: true,
logChangeEvents: false,
eventLog: '',
},
Expand Down
34 changes: 30 additions & 4 deletions src/VoerroTagsInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
@keyup="onKeyUp"
@keyup.esc="clearSearchResults"
@focus="onFocus"
@click="onClick"
@blur="onBlur"
@value="tags">

Expand Down Expand Up @@ -53,7 +54,7 @@

<ul v-else-if="typeaheadStyle === 'dropdown'" :class="`typeahead-${typeaheadStyle}`">
<li v-if="!typeaheadHideDiscard" class="tags-input-typeahead-item-default typeahead-hide-btn"
@mousedown.prevent="clearSearchResults"
@click.prevent="clearSearchResults"
v-text="discardSearchText"></li>

<li v-for="(tag, index) in searchResults"
Expand Down Expand Up @@ -114,6 +115,11 @@ export default {
default: false
},
typeaheadShowOnFocus: {
type: Boolean,
default: true
},
typeaheadHideDiscard: {
type: Boolean,
default: false
Expand Down Expand Up @@ -222,9 +228,17 @@ export default {
if (this.typeaheadAlwaysShow) {
this.searchTag(false);
}
},
mounted () {
// Emit an event
this.$emit('initialized');
document.addEventListener('click', (e) => {
if (e.target !== this.$refs['taginput']) {
this.clearSearchResults();
}
});
},
computed: {
Expand Down Expand Up @@ -456,7 +470,7 @@ export default {
return false;
}
if (this.oldInput != this.input || (!this.searchResults.length && this.typeaheadActivationThreshold == 0) || this.typeaheadAlwaysShow) {
if (this.oldInput != this.input || (!this.searchResults.length && this.typeaheadActivationThreshold == 0) || this.typeaheadAlwaysShow || this.typeaheadShowOnFocus) {
this.searchResults = [];
this.searchSelection = 0;
let input = this.input.trim();
Expand Down Expand Up @@ -549,6 +563,8 @@ export default {
this.searchTag();
});
}
this.$refs['taginput'].focus();
},
/**
Expand Down Expand Up @@ -662,8 +678,18 @@ export default {
* @returns void
*/
onFocus(e) {
this.$emit('focus', e)
this.$emit('focus', e);
},
/**
* Process the onClick event.
*
* @param e
* @returns void
*/
onClick(e) {
this.$emit('click', e);
this.searchTag();
},
Expand Down

0 comments on commit e1cd92c

Please sign in to comment.