darrenjennings / vue-autosuggest

🔍 Vue autosuggest component.
https://darrenjennings.github.io/vue-autosuggest
MIT License
621 stars 91 forks source link

selected() triggered at enter key when there is no suggestion selected #149

Closed twbagustin closed 5 years ago

twbagustin commented 5 years ago

Relevant code or config

    handleKeyStroke(e) {
        (...)
        case 13: // Enter
          debugger;
          e.preventDefault();

          if (this.totalResults > 0 && this.currentIndex >= 0) {
            this.setChangeItem(this.getItemByIndex(this.currentIndex), true);
            this.didSelectFromOptions = true;
          }

          this.loading = true;
          this.listeners.selected(this.didSelectFromOptions);        
          break;
        (...)
      }
    },

What you did: Playing with the lib

What happened:

Reproduction repository: https://codesandbox.io/s/vueautosuggest-api-fetching-57d4e

Problem description:

Suggested solution: I see in the code that you do this.currentIndex == -1, line 302, I don't know when and how the currentIndex can go below 0, but either if (this.totalResults > 0 && this.currentIndex !==null) if it can't go below 0 otherwise if (this.totalResults > 0 && this.currentIndex !==null && this.currentIndex >= 0)

twbagustin commented 5 years ago

Ignore the debugger; line there! And the problem is that selected() is triggered with an empty suggestion because there is no suggestion selected actually! Ohh and p.s Darren, nice to meet you first, I would be happy to help with small fixes here and there (as a freelancer I work more than the day job leaves little time to contribute), I think that for doing so, some docstrings here and there in the methods to understand what X is meant to do, would help a lot!

darrenjennings commented 5 years ago

It is possible to have an index of -1, since we decrement +/-1 when user is keying through results. -1 is therefore able to tell the library that a user has keyed themselves into the input. Would welcome PR's for sure, if you have better reproduction link+steps, we can better identify as I cannot reproduce your issue. I like your idea for better code level docs, and so have created this ticket to do so: #150

darrenjennings commented 5 years ago

Closing due to inactivity. If you can provide more detailed steps then I can look again.

darrenjennings commented 5 years ago

Looks like this came up again in #153 so will re-open to address. I think I see the issue now