I was able to understand turbo-streams and I would like now to use them for a dropdown autocomplete component, but I am having issues triggering the post
request to get the turbo-streams after the user selects the item from the dropdown.
I have an html component for a dropdown like so:
<div class="dropdown is-active is-fullwidth" data-controller="dropdown">
...
<input class="input" data-action="input->dropdown#search click->dropdown#clear" data-dropdown-target="userinput" placeholder="Search..." type="search">
And then a controller like so:
application.register("dropdown", class extends Stimulus.Controller {
static get targets() {
return ["dropdownItem", "userinput", "fromDropdown", "ddContent"]
};
initialize() {
this.dropdownItemTargets.forEach((element, index) => {
element.classList.add("is-hidden");
});
this.content.classList.add("is-hidden");
};
clear() {
if (this.input == null) {
this.content.classList.add("is-hidden")
}
};
search() {
if (this.input == "") {
this.content.classList.add("is-hidden");
this.dropdownItemTargets.forEach((element, index) => {
element.classList.add("is-hidden");
});
} else {
this.content.classList.remove("is-hidden");
this.dropdownItemTargets.forEach((element, index) => {
if (element.innerText.includes(this.input)) {
element.classList.remove("is-hidden");
} else {
element.classList.add("is-hidden");
};
});
}
};
set(event) {
event.preventDefault()
const value = event.target.dataset.value
this.input = value
this.content.classList.add("is-hidden")
const baseUrl = "/scheduler/testing"
};
get content() {
return this.ddContentTarget
}
get items() {
return this.itemTarget.value
};
get input() {
return this.userinputTarget.value
};
set input(value) {
return this.userinputTarget.value = value
};
})
The controller works ok but now I want to issue a post request on the action set()
and I am struggling with this. There doesn’t seem an easy way to send a post request from stimulus? What is the best practice? I tried both creating the post request fully in js and using js to submit an html form with no results.
Basically I want a post
request after the user selects the dropdown item, this post
request should not trigger any redirect.