Skip to content

Commit 11543ea

Browse files
committed
maint(pat-validation): Change to class based pattern.
This is needed for better customization in deriving projects.
1 parent c312b96 commit 11543ea

File tree

4 files changed

+113
-98
lines changed

4 files changed

+113
-98
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ describe("pat-autosuggest", function () {
464464
const form = document.querySelector("form");
465465
const input = document.querySelector("input");
466466

467-
new pattern_validation(form);
467+
const instance_validation = new pattern_validation(form);
468+
await events.await_pattern_init(instance_validation);
468469
new pattern(input);
469470
await utils.timeout(1); // wait a tick for async to settle.
470471

@@ -497,7 +498,8 @@ describe("pat-autosuggest", function () {
497498
const form = document.querySelector("form");
498499
const input = document.querySelector("input");
499500

500-
new pattern_validation(form);
501+
const instance_validation = new pattern_validation(form);
502+
await events.await_pattern_init(instance_validation);
501503
new pattern(input);
502504
await utils.timeout(1); // wait a tick for async to settle.
503505

src/pat/date-picker/date-picker.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import $ from "jquery";
2+
import events from "../../core/events";
23
import pattern from "./date-picker";
34
import pattern_auto_submit from "../auto-submit/auto-submit";
45
import utils from "../../core/utils";
@@ -604,8 +605,9 @@ describe("pat-date-picker", function () {
604605
const el = document.querySelector("input[type=date]");
605606

606607
const pattern_validation = (await import("../validation/validation")).default;
608+
const instance_validation = new pattern_validation(form);
609+
await events.await_pattern_init(instance_validation);
607610

608-
new pattern_validation(form);
609611
new pattern(el);
610612
await utils.timeout(1); // wait a tick for async to settle.
611613

src/pat/validation/validation.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// Patterns validate - Form vlidation
22
import "../../core/polyfills"; // SubmitEvent.submitter for Safari < 15.4 and jsDOM
33
import $ from "jquery";
4-
import Base from "../../core/base";
4+
import { BasePattern } from "../../core/basepattern";
55
import Parser from "../../core/parser";
66
import dom from "../../core/dom";
77
import events from "../../core/events";
88
import logging from "../../core/logging";
99
import utils from "../../core/utils";
10+
import registry from "../../core/registry";
1011

1112
const logger = logging.getLogger("pat-validation");
1213
//logger.setLevel(logging.Level.DEBUG);
@@ -34,12 +35,12 @@ parser.addArgument("error-template");
3435
const KEY_ERROR_EL = "__patternslib__input__error__el";
3536
const KEY_ERROR_MSG = "__patternslib__input__error__msg";
3637

37-
export default Base.extend({
38-
name: "validation",
39-
trigger: "form.pat-validation",
38+
class Pattern extends BasePattern {
39+
static name = "validation";
40+
static trigger = "form.pat-validation";
41+
static parser = parser;
4042

4143
init() {
42-
this.options = parser.parse(this.el, this.options);
4344
events.add_event_listener(
4445
this.el,
4546
"submit",
@@ -63,7 +64,7 @@ export default Base.extend({
6364
// Set ``novalidate`` attribute to disable the browser's validation
6465
// bubbles but not disable the validation API.
6566
this.el.setAttribute("novalidate", "");
66-
},
67+
}
6768

6869
initialize_inputs() {
6970
this.inputs = [
@@ -99,7 +100,7 @@ export default Base.extend({
99100
(e) => debouncer(e)
100101
);
101102
}
102-
},
103+
}
103104

104105
check_input({ input, event, stop = false }) {
105106
if (input.disabled) {
@@ -320,7 +321,7 @@ export default Base.extend({
320321
event.stopImmediatePropagation();
321322
}
322323
this.set_error_message(input);
323-
},
324+
}
324325

325326
set_validity({ input, msg, attribute = null, min = null, max = null }) {
326327
// Replace some variables, as like validate.js
@@ -340,7 +341,7 @@ export default Base.extend({
340341
// Hidden inputs do not participate in validation but we need this
341342
// (e.g. styled date input).
342343
input[KEY_ERROR_MSG] = msg;
343-
},
344+
}
344345

345346
remove_error(input, all_of_group = false) {
346347
// Remove error message and related referencesfrom input.
@@ -365,7 +366,7 @@ export default Base.extend({
365366
}
366367
}
367368
}
368-
},
369+
}
369370

370371
set_error_message(input) {
371372
this.remove_error(input);
@@ -416,10 +417,15 @@ export default Base.extend({
416417
this.check_input({ input: _input, stop: true });
417418
}
418419
}
419-
},
420+
}
420421

421422
error_template(message) {
422423
// Template for the validation message
423424
return `<em class="validation warning message">${message}</em>`;
424-
},
425-
});
425+
}
426+
}
427+
428+
registry.register(Pattern);
429+
430+
export default Pattern;
431+
export { Pattern };

0 commit comments

Comments
 (0)