Last active
January 16, 2020 03:09
-
-
Save martijnvbeek/a9c9001f0d39616a2358503e169afa82 to your computer and use it in GitHub Desktop.
// source https://jsbin.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let element = document.querySelector('#auto-complete'); | |
let uri = 'https://example.org/search'; | |
let choice = new Choices(element, { | |
removeItemButton: false, | |
itemSelectText: '', | |
shouldSort: false, | |
}); | |
let timer = null; | |
choice.passedElement.addEventListener('search', function (event) { | |
if (event.detail.value.length > 3) { | |
if (null !== timer) { | |
clearTimeout(timer); | |
} | |
timer = setTimeout(function () { | |
let request = new Request(uri + "?term=" + event.detail.value, { | |
credentials: 'same-origin', | |
}); | |
fetch(request) | |
.then(function (response) { | |
if (200 === response.status) { | |
response.json().then(function (data) { | |
choice.setChoices(data, 'id', 'label', true); | |
}); | |
} | |
}) | |
.catch(function (error) { | |
console.log(error); | |
}) | |
; | |
}, 1000); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment