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

[Bug]: Autocomplete selectOption does nothing #479

Open
3 tasks done
ygorma opened this issue May 27, 2024 · 2 comments
Open
3 tasks done

[Bug]: Autocomplete selectOption does nothing #479

ygorma opened this issue May 27, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@ygorma
Copy link

ygorma commented May 27, 2024

Before submitting...

Context

Simply selectOption(); does not select an option.

Current Behavior

No option gets selected.

Expected behavior

Autocomplete loaded with an option selected. It would be perfect if there was an autocomplete initialization option which requires the id and do all the rest for us. Like:

document.addEventListener('DOMContentLoaded', function() {
    var elems = document.querySelectorAll('.autocomplete');
    var instances = M.Autocomplete.init(elems, {
      // specify options here
      minLength: 0, // shows instantly
      data: [
        {id: 12, text: "Apple"},
        {id: 13, text: "Microsoft"},
        {id: 42, text: "Google", image: 'http://placehold.it/250x250'}
      ],
      defaultOption: 12
    });
  });

Possible Solutions or Causes

Using this code to make it work.

selectOption(id) {
    const entry = this.options.data.find((item) => item.id == id);
    if (this.options.isMultiSelect) {
        const li = this.container.querySelector('li[data-id="' + id + '"]');
        const checkbox = li.querySelector('input[type="checkbox"]');
        checkbox.checked = !checkbox.checked;
        if (checkbox.checked)
            this.selectedValues.push(entry);
        else
            this.selectedValues = this.selectedValues.filter((selectedEntry) => selectedEntry.id !== entry.id);
        this.el.focus();
    }
    else {
        // Single-Select
        this.selectedValues = [entry];
        this._refreshInputText();
        this._resetAutocomplete();
        this.close();
    }
    this._updateSelectedInfo();
    this._triggerChanged();
}

Steps to reproduce

<script>

  document.addEventListener('DOMContentLoaded', function() {
    var elems = document.querySelectorAll('[id="1.id_modelo.autocomplete"]');
    var instances = M.Autocomplete.init(elems, {
      // specify options here
      minLength: 0, // shows instantly
      data: [
        {id: 2, text: "Teste"},
        {id: 3, text: "Parecer"}
      ]    
    });

    const autocomplete = document.querySelector('[id="1.id_modelo.autocomplete"]');
    let instance = M.Autocomplete.getInstance(autocomplete);
    instance.selectOption(2);

  });

</script>

Your Environment

  • Version used: Materialize v2.0.4
  • Browser Name and version: Edge 125.0.2535.51
  • Operating System and version (desktop or mobile): Windows 10
  • Additional information you want to tell us: N/A
@ygorma ygorma added the bug Something isn't working label May 27, 2024
@IvanPostu
Copy link
Member

IvanPostu commented Jun 22, 2024

@ygorma Hi, I analyzed implementation of autocomplete component, the idea of selectOption() is to chose an item from dropdown which appears after user write something.

For the case you described here you should call

instance.setValues([
  { id: 2, text: "Teste" },
]);

instead of

instance.selectOption(2);

@IvanPostu
Copy link
Member

@wuda-io Hi, I think this issue can be closed, btw I noticed that both setValues and selectOption aren't covered by tests. Can I prepare a patch which test them linked to this issue or should be opened a separate issue for that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants