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

Auto-select/highlight the first result on "data load ended" #114

Open
StanBright opened this issue Jul 6, 2022 · 3 comments
Open

Auto-select/highlight the first result on "data load ended" #114

StanBright opened this issue Jul 6, 2022 · 3 comments

Comments

@StanBright
Copy link

Hey, many thanks for this "simple" and effective stimulus controller.

I've been replacing ReactSelect throughout my apps with it.

However, there's still some UX that's bugging me. I'd like to have the first result automatically highlighted so that the user can press enter and select it. A good example is GitHub's search box in the navbar here.

Any ideas what's the easiest way to achieve that?

Thanks,
Stan

@apsylone
Copy link

That's a nice question, it would "force" a selection. I wanted this kind of selection or on "focus out", make the field blank for forcing html5 required validation. Any clues on how to do this ?
Ping @afcapel

@StanBright
Copy link
Author

@apsylone did you manage to find a solution :)?

@thomasbrus
Copy link

@StanBright Here you go

import { Autocomplete as StimulusAutocomplete } from 'stimulus-autocomplete';

export default class Autocomplete extends StimulusAutocomplete {
  replaceResults(html) {
    super.replaceResults(html);
    this.maybeSelectFirstOption();
  }

  maybeSelectFirstOption() {
    if (!this.selectedOption && this.options.length > 0) {
      this.select(this.options[0]);
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants