Skip to content

Commit 11e9a0b

Browse files
committed
feat(pat auto suggest): Improve pat-validation integration.
Allow pat-validate to check for validity when select2 was interacted with but no value selected.
1 parent 57c974b commit 11e9a0b

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

src/pat/auto-suggest/auto-suggest.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ export default Base.extend({
8888
if (this.el.tagName === "INPUT") {
8989
config = this.create_input_config(config);
9090
}
91-
this.$el.select2(config);
91+
const $sel2 = this.$el.select2(config);
92+
9293
dom.hide(this.el); // hide input, but keep active (e.g. for validation)
9394

9495
this.$el.on("pat-update", (e, data) => {
@@ -101,6 +102,20 @@ export default Base.extend({
101102
}
102103
});
103104

105+
// Allow pat-validate to check for validity when select2 was interacted
106+
// with but no value selected.
107+
const initiate_empty_check = () => {
108+
const val = $sel2.select2("val");
109+
if (val?.length === 0) {
110+
// catches "" and []
111+
// blur the input field so that pat-validate can kick in when
112+
// nothing was selected.
113+
this.el.dispatchEvent(events.blur_event());
114+
}
115+
};
116+
this.$el.on("select2-close", initiate_empty_check.bind(this));
117+
this.$el.on("select2-blur", initiate_empty_check.bind(this));
118+
104119
this.$el.on("change", () => {
105120
// The jQuery "change" event isn't caught by normal JavaScript
106121
// event listeners, e.g. in pat-validation.

src/pat/auto-suggest/auto-suggest.test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,5 +300,66 @@ describe("pat-autosuggest", function () {
300300

301301
expect(spy).toHaveBeenCalled();
302302
});
303+
304+
it("4.2 - Works with pat-validate on an empty selection.", async function () {
305+
const pattern_validation = (await import("../validation/validation")).default; // prettier-ignore
306+
307+
document.body.innerHTML = `
308+
<form class="pat-validation" data-pat-validation="delay: 0">
309+
<input
310+
name="words"
311+
class="pat-autosuggest"
312+
required
313+
data-pat-autosuggest="words: apple, orange, pear" />
314+
</form>
315+
`;
316+
317+
const form = document.querySelector("form");
318+
const input = document.querySelector("input");
319+
320+
new pattern_validation(form);
321+
new pattern(input);
322+
await utils.timeout(1); // wait a tick for async to settle.
323+
324+
// Open the select2 dropdown
325+
$(".select2-input").click();
326+
await utils.timeout(1); // wait a tick for async to settle.
327+
328+
// Close it without selecting something.
329+
$(input).select2("close");
330+
331+
await utils.timeout(1); // wait a tick for async to settle.
332+
333+
// There should be a error message from pat-validation.
334+
expect(form.querySelectorAll("em.warning").length).toBe(1);
335+
});
336+
337+
it("4.3 - Works with pat-validate when empty and focus moves away.", async function () {
338+
const pattern_validation = (await import("../validation/validation")).default; // prettier-ignore
339+
340+
document.body.innerHTML = `
341+
<form class="pat-validation" data-pat-validation="delay: 0">
342+
<input
343+
name="words"
344+
class="pat-autosuggest"
345+
required
346+
data-pat-autosuggest="words: apple, orange, pear" />
347+
</form>
348+
`;
349+
350+
const form = document.querySelector("form");
351+
const input = document.querySelector("input");
352+
353+
new pattern_validation(form);
354+
new pattern(input);
355+
await utils.timeout(1); // wait a tick for async to settle.
356+
357+
// Move focus away from select2
358+
$(".select2-input")[0].dispatchEvent(events.blur_event());
359+
await utils.timeout(1); // wait a tick for async to settle.
360+
361+
// There should be a error message from pat-validation.
362+
expect(form.querySelectorAll("em.warning").length).toBe(1);
363+
});
303364
});
304365
});

0 commit comments

Comments
 (0)