Skip to content

Commit 26a16de

Browse files
authored
Allow configuration of debounce in form-autosubmit (#48)
* Add more internal callbacks in elements * Allow configuration of debounce in form-autosubmit
1 parent 1aea6f5 commit 26a16de

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

src/elements/form_autosubmit_element.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,42 @@ import { debounce } from "../lib/debounce";
44
/**
55
* This element submits a form automatically clicking on a hidden submit input/button
66
*
7-
* The form gets submitted on each change, or on input with a debounce.
7+
* The form gets submitted on each change or input with a configurable debounce.
88
* You still need to make sure that this is what the user expects and/or that the submit is captured
99
* and only part of the DOM get's updated using a technique like turbo frames.
1010
* (We don't use `form.submit()` since that event isn't captured by turbo)
11+
*
12+
* The debounce for the change or input event can be configured with `data-change-debounce`
13+
* or `data-input-debounce`. This value is only picked up when the component is connected
14+
* any later changes to this attribute are ignored.
1115
*/
1216
export default class FormAutosubmitElement extends SprinklesElement {
1317
static tagName = "form-autosubmit";
1418
static refs = ["button"];
1519
static events = {
16-
change: "submit",
17-
input: "debouncedSubmit",
20+
change: "handleChange",
21+
input: "handleInput",
1822
};
1923

20-
debouncedSubmit = debounce(this.#submit.bind(this), 250);
24+
beforeConnected() {
25+
this.changeDebounce = this.hasAttribute("data-change-debounce")
26+
? parseInt(this.getAttribute("data-change-debounce"))
27+
: 0;
28+
this.inputDebounce = this.hasAttribute("data-input-debounce")
29+
? parseInt(this.getAttribute("data-input-debounce"))
30+
: 250;
2131

22-
submit() {
23-
this.debouncedSubmit.trigger();
32+
this.handleChange = debounce(this.#submit.bind(this), this.changeDebounce);
33+
this.handleInput = debounce(this.#submit.bind(this), this.inputDebounce);
2434
}
2535

2636
#submit() {
37+
// If one debounce triggers, we clear both
38+
// This avoids the two debounces being triggered close too each other
39+
this.handleChange.clear();
40+
this.handleInput.clear();
41+
42+
// Submit the form
2743
this.refs.button.click();
2844
}
2945
}

src/elements/sprinkles_element.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export default class SprinklesElement extends HTMLElement {
4242
refs = {};
4343

4444
connectedCallback() {
45+
this.beforeConnected();
4546
this.#detectRefs();
4647
this.#bindEvents();
4748
this.afterConnected();
@@ -50,12 +51,15 @@ export default class SprinklesElement extends HTMLElement {
5051
disconnectedCallback() {
5152
this.beforeDisconnect();
5253
this.#unbindEvents();
54+
this.afterDisconnect();
5355
}
5456

5557
// Callbacks
58+
beforeConnected() {}
5659
afterConnected() {}
5760

5861
beforeDisconnect() {}
62+
afterDisconnect() {}
5963

6064
// Setup
6165
#detectRefs() {

src/lib/debounce.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,19 @@ export function debounce(callback, wait) {
3131
};
3232

3333
debounced.trigger = () => {
34+
debounced.clear();
35+
run();
36+
};
37+
38+
debounced.flush = () => {
39+
if (timeout) debounced.trigger();
40+
};
41+
42+
debounced.clear = () => {
3443
if (timeout) {
3544
clearTimeout(timeout);
3645
timeout = null;
3746
}
38-
run();
3947
};
4048

4149
return debounced;

0 commit comments

Comments
 (0)