2024-03-28 13:23:54 -04:00
|
|
|
import { Controller } from "@hotwired/stimulus";
|
2024-03-10 21:38:31 +00:00
|
|
|
|
|
|
|
export default class extends Controller {
|
2024-03-28 13:23:54 -04:00
|
|
|
// By default, auto-submit is "opt-in" to avoid unexpected behavior. Each `auto` target
|
2024-04-25 07:54:56 -04:00
|
|
|
// will trigger a form submission when the configured event is triggered.
|
2024-03-28 13:23:54 -04:00
|
|
|
static targets = ["auto"];
|
2024-04-25 07:54:56 -04:00
|
|
|
static values = {
|
|
|
|
triggerEvent: { type: String, default: "input" },
|
|
|
|
};
|
2024-03-10 21:38:31 +00:00
|
|
|
|
|
|
|
connect() {
|
2024-03-28 13:23:54 -04:00
|
|
|
this.autoTargets.forEach((element) => {
|
2024-04-25 14:55:39 -04:00
|
|
|
const event =
|
|
|
|
element.dataset.autosubmitTriggerEvent || this.triggerEventValue;
|
|
|
|
element.addEventListener(event, this.handleInput);
|
2024-03-28 13:23:54 -04:00
|
|
|
});
|
2024-03-10 21:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
2024-03-28 13:23:54 -04:00
|
|
|
this.autoTargets.forEach((element) => {
|
2024-04-25 14:55:39 -04:00
|
|
|
const event =
|
|
|
|
element.dataset.autosubmitTriggerEvent || this.triggerEventValue;
|
|
|
|
element.removeEventListener(event, this.handleInput);
|
2024-03-28 13:23:54 -04:00
|
|
|
});
|
2024-03-10 21:38:31 +00:00
|
|
|
}
|
|
|
|
|
2024-03-28 13:23:54 -04:00
|
|
|
handleInput = () => {
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
this.timeout = setTimeout(() => {
|
|
|
|
this.element.requestSubmit();
|
|
|
|
}, 500);
|
|
|
|
};
|
2024-03-10 21:38:31 +00:00
|
|
|
}
|