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

Feature improve: When mouse move on an option item, it should become the current selected option item. #125

Open
qichunren opened this issue Nov 19, 2022 · 4 comments

Comments

@qichunren
Copy link

See github top left search box for example.

@cmer
Copy link

cmer commented Dec 16, 2022

Isn't that just changing the background with CSS on hover?

@ACPK
Copy link

ACPK commented Feb 28, 2023

@cmer @afcapel The stimulus-autocomplete example keeps the wrong aria-activedescendant value on the input field when the user uses the mouse. Any results that stimulus-autocomplete gave the class of "active" to would need the "active" removed when using the mouse. Additionally, the aria-activedescendant value needs to be set to the value of the result that the user is hovering over.

Solution

Add data-action="mouseenter->autocomplete#show" to results.

<li role="option" data-autocomplete-value="1" 
  data-action="mouseenter->autocomplete#show">
  Mockingbird
</li>

Add show to autocomplete.js

show(event)
  {
    if (event.target) this.select(event.target)
  }

@tmaier
Copy link

tmaier commented Aug 7, 2023

I think I was able to solve this with the following custom stimulus controller, which inherits stimulus-autocomplete:

import { Autocomplete } from 'stimulus-autocomplete'

const optionSelector = "[role='option']:not([aria-disabled])"

export default class extends Autocomplete {
  connect() {
    super.connect()
    this.resultsTarget.addEventListener('mouseover', this.onResultsMouseOver)
  }

  disconnect() {
    super.disconnect()
    if (this.hasResultsTarget) {
      this.resultsTarget.removeEventListener('mouseover', this.onResultsMouseOver)
    }
  }

  onResultsMouseOver = (event) => {
    if (!(event.target instanceof Element)) return
    const selected = event.target.closest(optionSelector)
    if (selected) this.select(selected)
  }
}

@Mecanik
Copy link

Mecanik commented Nov 7, 2023

data-action="mouseenter->autocomplete#show"

This does not work for me. What exactly should change? Nothing changes. Could you share your HTML?

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

5 participants