question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Hide suggestion list on blur

See original GitHub issue
  • vue-autosuggest version: 1.8.3
  • node version: 10.15.3
  • yarn version: 1.15.2

Relevant code or config

https://jsfiddle.net/bthqsfyz/2/

Problem description: First thank you for this component. I am not a javascript nor a Vue expert but I did my best to search the repo before filing this issue. I want to hide the suggest list when blur event is fired on the input. For example as shown in the fiddle I have three inputs, user uses tab key to navigate, autosuggest input gets focus and list is shown, user wants to move on to the next field and presses tab key but the autosuggest list is still shown. I know that pressing the esc key will close the list but I don’t want to force the user to have to press the esc when they’re done with the field. I went through the documentation and searched the whole repo but could not find a method to close the suggestion list myself.

Suggested solution: I still think I am missing something and there should be a method to close the list. for example I can use blur event and when it is fired I call the hide/close method.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
darrenjenningscommented, Nov 18, 2019

@ifdotpy yea good catch. If you detect what is being clicked before you close then you can get around it:

https://jsfiddle.net/darrenjennings/ajv19e0s/66/

e.g.

      onBlur (e) {
        // relatedTarget is null when you click a suggestion
      	if(e.relatedTarget && e.relatedTarget.tagName === 'INPUT') {
      		this.closed = true          
        }
      },
1reaction
darrenjenningscommented, Aug 25, 2020

@brandonkelly this should be possible just as well by resetting the suggestions to empty. Here’s an example with 1.8.1 (the version craftcms is currently using).

codesandbox.io/s/18x-close-on-blur-ldyeu

In case that link eventually expires… The gist of the fix is to reset the suggestions on focus and clear them on blur:

<vue-autosuggest
      :suggestions="autoSuggestions"
      :input-props="{onInputChange, autocomplete: 'off', id:'autosuggest__input', placeholder:'Do you feel lucky, punk?'}"
      @selected="selectHandler"
      @blur="onBlur"
      @focus="suggestionData = db.getCharacters()"
    >
      <template slot-scope="{suggestion}">
        <span class="my-suggestion-item">{{suggestion.item}}</span>
      </template>
    </vue-autosuggest>
import { VueAutosuggest } from "vue-autosuggest";
const db = {
  getCharacters: () => [
    "Frodo",
    "Samwise",
    "Gandalf",
    "Galadriel",
    "Faramir",
    "Éowyn"
  ]
};
export default {
  name: "HelloWorld",

  components: {
    VueAutosuggest
  },
  data: function() {
    return {
      db,
      query: "",
      selected: null,
      suggestionData: []
    };
  },
  computed: {
    autoSuggestions() {
      return [
        { data: this.suggestionData.filter(s => s.indexOf(this.query) > -1) }
      ];
    }
  },
  methods: {
    onBlur(e) {
      if (e.relatedTarget && e.relatedTarget.tagName === "INPUT") {
        this.suggestionData = [];
      }
    },
    onInputChange(e) {
      this.query = e;
    },
    selectHandler(e) {
      this.selected = e;
    }
  }
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Hiding the suggestion list of an autocomplete AJAX control on ...
I used a trick by adding setTimeout when "autosuggest" box hides by blur event. But this trick has a problem of active element...
Read more >
Focusing: focus/blur - The Modern JavaScript Tutorial
The blur handler checks if the field has an email entered, and if not – shows an error. The focus handler hides the...
Read more >
Hidden Auto Suggestion drop-down values - How to handle in ...
Open developer tool. From the right panel, go to 'Event Listeners' tab. Look for 'blur' property. Click on 'Remove' button next ...
Read more >
Hide Autocomplete Panel on Blur - v6 - CodeMirror
How do I hide the autocomplete suggestions when you blur away from the editor. ... The docs list closeCompletion 's type as Command...
Read more >
Creating an Autosuggest Textbox with JavaScript, Part 2
The dropdown suggestion list is nothing more than an absolutely-positioned <div/> containing several other <div/> s. It's placed directly under the textbox ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found