Skip to content

Commit 92aacc8

Browse files
authored
Merge pull request #1230 from Patternslib/fix-pat-depends
fix(pat-depends): Listen and dispatch input instead of change events.
2 parents 865c5c3 + 307a336 commit 92aacc8

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/pat/depends/depends.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,22 @@ class Pattern extends BasePattern {
3636
this.set_state();
3737

3838
for (const input of this.handler.getAllInputs()) {
39+
// Note: One input can be a dependency for multiple other dependent
40+
// elements. Therefore we need to bind the events not uniquely and
41+
// add a uuid to the event bindings id.
42+
3943
events.add_event_listener(
4044
input,
41-
"change",
42-
`pat-depends--change--${this.uuid}`, // We need to support multiple events per dependant ...
43-
this.set_state.bind(this)
44-
);
45-
events.add_event_listener(
46-
input,
47-
"keyup",
48-
`pat-depends--keyup--${this.uuid}`, // ... therefore we need to add a uuid to the event id ...
45+
"input",
46+
`pat-depends--input--${this.uuid}`,
4947
this.set_state.bind(this)
5048
);
5149

5250
if (input.form) {
5351
events.add_event_listener(
5452
input.form,
5553
"reset",
56-
`pat-depends--reset--${this.uuid}`, // ... to not override previously set event handlers.
54+
`pat-depends--reset--${this.uuid}`,
5755
async () => {
5856
// TODO: note sure, what this timeout is for.
5957
await utils.timeout(50);
@@ -77,8 +75,9 @@ class Pattern extends BasePattern {
7775
const inputs = dom.find_inputs(this.el);
7876
for (const input of inputs) {
7977
input.disabled = false;
80-
// Trigger the change event after disabling so that any other bound actions can react on that.
81-
input.dispatchEvent(events.change_event());
78+
// Trigger the input after disabling so that any other bound
79+
// actions can react on that.
80+
input.dispatchEvent(events.input_event());
8281
}
8382
if (this.el.tagName === "A") {
8483
events.remove_event_listener(this.el, "pat-depends--click");
@@ -96,8 +95,9 @@ class Pattern extends BasePattern {
9695
const inputs = dom.find_inputs(this.el);
9796
for (const input of inputs) {
9897
input.disabled = true;
99-
// Trigger the change event after disabling so that any other bound actions can react on that.
100-
input.dispatchEvent(events.change_event());
98+
// Trigger the input after disabling so that any other bound
99+
// actions can react on that.
100+
input.dispatchEvent(events.input_event());
101101
}
102102
if (this.el.tagName === "A") {
103103
events.add_event_listener(this.el, "click", "pat-depends--click", (e) =>

src/pat/depends/depends.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ describe("pat-depends", function () {
239239
const button2 = document.querySelector("[name=extra]");
240240

241241
button1.checked = true;
242-
button1.dispatchEvent(new Event("change"));
242+
button1.dispatchEvent(new Event("input"));
243243
button2.checked = true;
244-
button2.dispatchEvent(new Event("change"));
244+
button2.dispatchEvent(new Event("input"));
245245

246246
expect(dom.is_visible(dep1)).toBe(true);
247247
expect(dom.is_visible(dep2)).toBe(true);
@@ -250,7 +250,7 @@ describe("pat-depends", function () {
250250
// Even though button2 is still checked, the visibility of dep2 is
251251
// hidden.
252252
button1.checked = false;
253-
button1.dispatchEvent(new Event("change"));
253+
button1.dispatchEvent(new Event("input"));
254254

255255
expect(dom.is_visible(dep1)).toBe(false);
256256
expect(dom.is_visible(dep2)).toBe(false);

0 commit comments

Comments
 (0)