Skip to content

Commit 57c974b

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

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ export default Base.extend({
151151
firstDay: this.options.firstDay,
152152
showWeekNumber: this.options.weekNumbers === "show",
153153
onSelect: () => this.dispatch_change_event(),
154+
onClose: () => {
155+
if (this.options.behavior === "styled") {
156+
// blur the input field so that pat-validate can kick in when
157+
// nothing was selected.
158+
el.dispatchEvent(events.blur_event());
159+
}
160+
},
154161
};
155162

156163
if (el.getAttribute("min")) {

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

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe("pat-date-picker", function () {
4343
jest.restoreAllMocks();
4444
});
4545

46-
it("Default date picker.", async () => {
46+
it("1 - Default date picker.", async () => {
4747
document.body.innerHTML = '<input type="date" class="pat-date-picker"/>';
4848
const el = document.querySelector("input[type=date]");
4949

@@ -85,7 +85,7 @@ describe("pat-date-picker", function () {
8585
expect(el.value).toBe(isodate);
8686
});
8787

88-
it("Date picker starts at Monday.", async () => {
88+
it("2 - Date picker starts at Monday.", async () => {
8989
document.body.innerHTML =
9090
'<input type="date" class="pat-date-picker" data-pat-date-picker="first-day: 1" />';
9191
const el = document.querySelector("input[type=date]");
@@ -98,7 +98,7 @@ describe("pat-date-picker", function () {
9898
expect(first_day.textContent).toBe("Mon");
9999
});
100100

101-
it("Date picker with pre-set value.", async () => {
101+
it("3 - Date picker with pre-set value.", async () => {
102102
document.body.innerHTML =
103103
'<input type="date" class="pat-date-picker" value="1900-01-01"/>';
104104
const el = document.querySelector("input[type=date]");
@@ -120,7 +120,7 @@ describe("pat-date-picker", function () {
120120
expect(display_el.textContent).toBe("1900-01-01");
121121
});
122122

123-
it("Date picker with week numbers.", async () => {
123+
it("4 - Date picker with week numbers.", async () => {
124124
document.body.innerHTML =
125125
'<input type="date" class="pat-date-picker" data-pat-date-picker="week-numbers: show" value="2017-09-18"/>';
126126
const el = document.querySelector("input[type=date]");
@@ -133,7 +133,7 @@ describe("pat-date-picker", function () {
133133
expect(week_num.textContent).toBe("35");
134134
});
135135

136-
describe("Date picker with i18n", function () {
136+
describe("5 - Date picker with i18n", function () {
137137
describe("with proper json URL", function () {
138138
it("properly localizes the months and weekdays", async () => {
139139
global.fetch = jest.fn().mockImplementation(mock_fetch_i18n);
@@ -177,7 +177,7 @@ describe("pat-date-picker", function () {
177177
});
178178
});
179179

180-
describe("Update one input depending on the other.", function () {
180+
describe("6 - Update one input depending on the other.", function () {
181181
it("Updates with default offset-days", async () => {
182182
const wrapper = document.createElement("div");
183183
wrapper.innerHTML = `
@@ -324,7 +324,7 @@ describe("pat-date-picker", function () {
324324
});
325325
});
326326

327-
it("Formatted date.", async () => {
327+
it("7 - Formatted date.", async () => {
328328
document.body.innerHTML =
329329
'<input type="date" class="pat-date-picker" value="2021-03-09" data-pat-date-picker="output-format: Do MMMM YYYY; locale: de"/>';
330330
const el = document.querySelector("input[type=date]");
@@ -346,7 +346,7 @@ describe("pat-date-picker", function () {
346346
expect(el.value).toBe("2021-03-12");
347347
});
348348

349-
it("Styled behavior with clear button.", async () => {
349+
it("8 - Styled behavior with clear button.", async () => {
350350
document.body.innerHTML =
351351
'<input type="date" class="pat-date-picker" value="2021-03-09"/>';
352352
const el = document.querySelector("input[type=date]");
@@ -400,7 +400,7 @@ describe("pat-date-picker", function () {
400400
expect(clear_button).toBeFalsy();
401401
});
402402

403-
it("Formatted date with clear button.", async () => {
403+
it("9 - Formatted date with clear button.", async () => {
404404
document.body.innerHTML =
405405
'<input type="date" class="pat-date-picker" value="2021-03-09" data-pat-date-picker="output-format: DD.MM.YYYY"/>';
406406
const el = document.querySelector("input[type=date]");
@@ -454,7 +454,7 @@ describe("pat-date-picker", function () {
454454
expect(clear_button).toBeFalsy();
455455
});
456456

457-
it("Required formatted date - no clear button available.", async () => {
457+
it("10 - Required formatted date - no clear button available.", async () => {
458458
document.body.innerHTML =
459459
'<input type="date" class="pat-date-picker" value="2021-03-01" required/>';
460460
const el = document.querySelector("input[type=date]");
@@ -486,7 +486,7 @@ describe("pat-date-picker", function () {
486486
expect(clear_button).toBeFalsy();
487487
});
488488

489-
it("Native behavior with fallback to pika", async () => {
489+
it("11 - Native behavior with fallback to pika", async () => {
490490
// We mocking as if we're not supporting input type date.
491491
jest.spyOn(utils, "checkInputSupport").mockImplementation(() => false);
492492

@@ -528,7 +528,7 @@ describe("pat-date-picker", function () {
528528
expect(el.value).toBe(isodate);
529529
});
530530

531-
it("does not initialize the date picker in styled behavior when disabled", async () => {
531+
it("12 - does not initialize the date picker in styled behavior when disabled", async () => {
532532
document.body.innerHTML = `
533533
<input
534534
type="date"
@@ -559,7 +559,7 @@ describe("pat-date-picker", function () {
559559
expect(document.querySelector(".pika-lendar")).toBeFalsy();
560560
});
561561

562-
it("works with pat-autosubmit", async () => {
562+
it("13 - works with pat-autosubmit", async () => {
563563
document.body.innerHTML = `
564564
<form class="pat-autosubmit" onsubmit="return false;">
565565
<input name="date" type="date" class="pat-date-picker"/>
@@ -581,4 +581,41 @@ describe("pat-date-picker", function () {
581581
await utils.timeout(500); // wait for delay
582582
expect(handle_submit).toHaveBeenCalled();
583583
});
584+
585+
it("14 - Selecting nothing in styled behavior triggers pat-validation.", async () => {
586+
document.body.innerHTML = `
587+
<form class="pat-validation" data-pat-validation="delay: 0">
588+
<input
589+
type="date"
590+
name="date"
591+
class="pat-date-picker"
592+
required
593+
/>
594+
</form>
595+
`;
596+
const form = document.querySelector("form");
597+
const el = document.querySelector("input[type=date]");
598+
599+
const pattern_validation = (await import("../validation/validation")).default;
600+
601+
new pattern_validation(form);
602+
new pattern(el);
603+
await utils.timeout(1); // wait a tick for async to settle.
604+
605+
// Open the date picker
606+
document.querySelector("time").click();
607+
await utils.timeout(1); // wait a tick for async to settle.
608+
609+
// Date picker is opened
610+
expect(document.querySelector(".pika-lendar")).toBeTruthy();
611+
612+
// Close the date picker without selecting a date.
613+
document.body.click();
614+
615+
// Wait for validation to run.
616+
await utils.timeout(1);
617+
618+
// There should be a error message from pat-validation.
619+
expect(form.querySelectorAll("em.warning").length).toBe(1);
620+
});
584621
});

0 commit comments

Comments
 (0)