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

[Question] how to use this in conjunction with dropdown/autocomplete ? #21

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

Comments

@simkimsia
Copy link

A bit like how stackoverflow does it

image

I am looking at the example page https://taufik-nurrohman.js.org/tag-picker/ but still not find something that works like that

@jnnkB
Copy link
Contributor

jnnkB commented Jul 6, 2022

I think you you'd have to build the autocomplete thing yourself. You could probably use the following parts of the API:

picker.on("blur", function () {
	// hide suggestions
})


picker.on("focus", function () {
	// show suggestions
})


const input = document.querySelector(".tag-picker__input");
input.addEventListener("input", (event) => {
	console.log(event, input.innerText);
	// filter suggestions
});

And you could add new tags with picker.set(tag).

@simkimsia
Copy link
Author

ok thank you 🙇🏻‍♂️

@taufik-nurrohman
Copy link
Owner

taufik-nurrohman commented Jul 6, 2022

The idea is that you add a key(down/up) listener to perform an AJAX search then display the results in a dropdown menu that you create yourself.

let debounce;
picker.input.addEventListener('keydown', e => {
    debounce && clearTimeout(debounce);
    debounce = setTimeout(() => {
        let query = this.textContent;
        showSearchResults(query);
    }, 100);
    e.preventDefault();
});

function showSearchResults(query) { /* ... */ }

Then, on every menu item, add an event listener to execute the picker.set() method:

let item = document.createElement('a');
item.textContent = data.title; // From AJAX response
item.addEventListener('click', function(e) {
    picker.set(this.textContent);
    picker.input.textContent = "";
    e.preventDefault();
});

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

No branches or pull requests

3 participants