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

Bootstrap 5 Autocomplete returns null onSelectItem #20

Open
awariat opened this issue May 20, 2022 · 2 comments
Open

Bootstrap 5 Autocomplete returns null onSelectItem #20

awariat opened this issue May 20, 2022 · 2 comments

Comments

@awariat
Copy link

awariat commented May 20, 2022

Hi. How are you.
Nice code but sometimes it returns null
my code:

async function getProductsArray() {

  let autoCompleteArray = [];

  try {

    const [productsArray, productsDefault] = await getSettingsProducts();
    productsArray.sort();
    productsArray.forEach((element, index) => {
      autoCompleteArray.push({
        label: element,
        value: element
      });

    });
  
    const field = document.getElementById('productsAutoComplete');
    const ac = new Autocomplete(field, {
        //data: [{label: "I'm a label", value: 42}],
        data: autoCompleteArray,
        maximumItems: 5,
        threshold: 1,
        onSelectItem: ({
            label,
            value
        }) => {
            console.log("user selected:", label, value);//<--------sometimes null null
            document.getElementById('productsAutoComplete').value = "";
        inputVal(label);
        }
    });

  } catch (error) {
    console.log("Get Products Error: ", error);
    // errorInfo(error)
  }
}

It looks like it works, but not always. Sometimes "onSelectItem" returns "null"(about 25%). Even if the same element is clicked. I checked on chrome and Firefox. On Chrome, a bit better.

@M-Hadri
Copy link

M-Hadri commented Jun 24, 2022

just replace this 2 lines in autocomplete.js ( function createItems )..

replace :

let dataLabel = e.target.getAttribute('data-label');
let dataValue = e.target.getAttribute('data-value');

with :

let dataLabel = e.currentTarget.getAttribute('data-label');
let dataValue = e.currentTarget.getAttribute('data-value');

or

let dataLabel = e.target.getAttribute('data-label') || e.target.parentNode.getAttribute('data-label');
let dataValue = e.target.getAttribute('data-value') || e.target.parentNode.getAttribute('data-value');

@frugan-dev
Copy link
Contributor

Fixed #17

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