Skip to content

Commit 9abf534

Browse files
committed
fix(pat-depends): Support updating for sub-pat-depends when the parent state is changed.
1 parent 0bbe3f4 commit 9abf534

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

src/pat/depends/depends.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class Pattern extends BasePattern {
7777
const inputs = dom.find_inputs(this.el);
7878
for (const input of inputs) {
7979
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());
8082
}
8183
if (this.el.tagName === "A") {
8284
events.remove_event_listener(this.el, "pat-depends--click");
@@ -94,6 +96,8 @@ class Pattern extends BasePattern {
9496
const inputs = dom.find_inputs(this.el);
9597
for (const input of inputs) {
9698
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());
97101
}
98102
if (this.el.tagName === "A") {
99103
events.add_event_listener(this.el, "click", "pat-depends--click", (e) =>

src/pat/depends/depends.test.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import $ from "jquery";
2+
import dom from "../../core/dom";
23
import pattern from "./depends";
34
import utils from "../../core/utils";
45

@@ -187,4 +188,73 @@ describe("pat-depends", function () {
187188
expect(data.enabled).toBe(false);
188189
});
189190
});
191+
192+
describe("5 - Support pat-depends within a pat-depends controlled tree.", function () {
193+
194+
it("Also updates pat-depends within a pat-depends controlled tree", async function () {
195+
196+
document.body.innerHTML = `
197+
<div data-pat-depends="action: both">
198+
<label>
199+
<input
200+
name="show-tree"
201+
type="checkbox"
202+
/>
203+
show
204+
</label>
205+
206+
<fieldset
207+
class="dep1 pat-depends"
208+
data-pat-depends="condition: show-tree"
209+
>
210+
<label>
211+
<input
212+
name="extra"
213+
type="checkbox"
214+
/>
215+
Extra
216+
</label>
217+
</fieldset>
218+
219+
<!-- This pat-depends node controlled by a checkbox which is
220+
within another pat-depends controlled tree.
221+
-->
222+
<p
223+
class="dep2 pat-depends"
224+
data-pat-depends="condition: extra"
225+
>
226+
You shose extra!
227+
</p>
228+
</div>
229+
`;
230+
231+
const dep1 = document.querySelector(".dep1");
232+
new pattern(dep1);
233+
await utils.timeout(1); // wait a tick for async to settle.
234+
const dep2 = document.querySelector(".dep2");
235+
new pattern(dep2);
236+
await utils.timeout(1); // wait a tick for async to settle.
237+
238+
const button1 = document.querySelector("[name=show-tree]");
239+
const button2 = document.querySelector("[name=extra]");
240+
241+
button1.checked = true;
242+
button1.dispatchEvent(new Event("change"));
243+
button2.checked = true;
244+
button2.dispatchEvent(new Event("change"));
245+
246+
expect(dom.is_visible(dep1)).toBe(true);
247+
expect(dom.is_visible(dep2)).toBe(true);
248+
249+
250+
// Even though button2 is still checked, the visibility of dep2 is
251+
// hidden.
252+
button1.checked = false;
253+
button1.dispatchEvent(new Event("change"));
254+
255+
expect(dom.is_visible(dep1)).toBe(false);
256+
expect(dom.is_visible(dep2)).toBe(false);
257+
});
258+
259+
});
190260
});

0 commit comments

Comments
 (0)