Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/pat/date-picker/date-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import utils from "../../core/utils";
const log = logging.getLogger("date-picker");

export const parser = new Parser("date-picker");
parser.addArgument("behavior", "styled", ["native", "styled"]);
parser.addArgument("behavior", "styled", ["native", "styled", "editable"]);
parser.addArgument("week-numbers", [], ["show", "hide"]);
parser.addArgument("i18n"); // URL pointing to JSON resource with i18n values
parser.addArgument("first-day", 0);
Expand All @@ -33,7 +33,6 @@ parser.addAlias("behaviour", "behavior");

export const storage = store.session("pat-date-picker");


export default Base.extend({
name: "date-picker",
trigger: ".pat-date-picker",
Expand Down Expand Up @@ -84,18 +83,21 @@ export default Base.extend({
}
el.insertAdjacentElement("beforebegin", display_el);

// Disable click on label, as this invokes a click on the invisible
// input field which opens the calendar in Firefox and masks a
// click on display_el on Chrome.
// Suppress label activation of the hidden date input while keeping
// clicks on other controls, such as a companion time field.
const label = display_el.closest("label");
if (label) {
events.add_event_listener(
label,
"click",
"pat-date-picker--label",
(e) => {
e.preventDefault();
}
if (
!e.target.closest("input, button, select, textarea, a[href]")
) {
e.preventDefault();
}
},
);
}

Expand Down Expand Up @@ -150,7 +152,10 @@ export default Base.extend({
}
} else if (disabled) {
return;
} else if (utils.checkInputSupport("date", "invalid date")) {
} else if (
this.options.behavior !== "editable" &&
utils.checkInputSupport("date", "invalid date")
) {
// behavior native with native support.
return;
} else if (el.getAttribute("type") === "date") {
Expand Down Expand Up @@ -181,10 +186,10 @@ export default Base.extend({
};

if (el.getAttribute("min")) {
config.minDate = new Date(el.getAttribute("min"));
config.minDate = new Date(`${el.getAttribute("min")}T00:00:00`);
}
if (el.getAttribute("max")) {
config.maxDate = new Date(el.getAttribute("max"));
config.maxDate = new Date(`${el.getAttribute("max")}T00:00:00`);
}

if (this.options.i18n) {
Expand All @@ -195,7 +200,9 @@ export default Base.extend({
i18n = await response.json();
storage.set(this.options.i18n, i18n);
} catch {
log.error(`date-picker could not load i18n for ${this.options.i18n}`);
log.error(
`date-picker could not load i18n for ${this.options.i18n}`,
);
}
}
if (i18n) {
Expand Down
40 changes: 40 additions & 0 deletions src/pat/date-picker/date-picker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,46 @@ describe("pat-date-picker", function () {
expect(el.value).toBe(isodate);
});

it("opens a localized picker on an editable field even with native date input support", async () => {
jest.spyOn(utils, "checkInputSupport").mockReturnValue(true);
document.body.innerHTML = `
<form>
<input
type="date"
name="date"
class="pat-date-picker"
value="2021-10-21"
data-pat-date-picker="behavior: editable; i18n: /keyboard-i18n.json"
/>
</form>
`;
const response = await mock_fetch_i18n();
storage.set("/keyboard-i18n.json", await response.json());
const el = document.querySelector("input");
const instance = new pattern(el);

try {
await events.await_pattern_init(instance);

expect(el.type).toBe("text");
expect(el.value).toBe("2021-10-21");
expect(el.style.display).not.toBe("none");
expect(document.querySelector("time")).toBeNull();

el.click();
const calendar = document.querySelector(".pika-single:not(.is-hidden)");
const month = calendar.querySelector(".pika-select-month option:checked");
expect(month.textContent).toBe("Oktober");

const day = calendar.querySelector('button[data-pika-day="22"]');
day.dispatchEvent(new Event("mousedown", { bubbles: true }));
expect(new FormData(el.form).get("date")).toBe("2021-10-22");
} finally {
instance.pikaday?.destroy();
storage.remove("/keyboard-i18n.json");
}
});

it("12 - does not initialize the date picker in styled behavior when disabled", async () => {
document.body.innerHTML = `
<input
Expand Down
28 changes: 20 additions & 8 deletions src/pat/date-picker/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,31 @@ for browsers which don't yet support the HTML5 date input.

### Examples

#### Falling back to the browser's HTML5 picker if available.
#### Falling back to the browser's HTML5 picker if available

Set the `behavior` option to `native` to use the browser's HTML5 date input
rendering when available.

<input class="pat-date-picker" type="date" data-pat-date-picker="behavior: native">

#### Enforcing the styled non-HTML5 picker universally.
#### Choosing a date with the styled picker

By default this pattern will NOT defer to the browser's HTML5 picker.
The default `styled` behavior displays the date in a `<time>` element that opens
the JavaScript calendar for selection.

<input class="pat-date-picker" type="date">

#### Typing a date or choosing one from a translated calendar

With `behavior: editable`, the pattern changes the `type="date"` field to
`type="text"` when it initializes. Users can type a date in `YYYY-MM-DD` format
or choose one from the JavaScript calendar. Set `i18n` to the URL of a JSON file
to translate the calendar. The browser treats typed values as plain text for
constraint validation.

<input class="pat-date-picker" type="date"
data-pat-date-picker="behavior: editable; i18n: /src/pat/date-picker/i18n.json">

#### Default value

<input class="pat-date-picker" type="date" value="2015-01-01">
Expand All @@ -30,11 +42,11 @@ By default this pattern will NOT defer to the browser's HTML5 picker.

<input class="pat-date-picker" type="date" value="2015-01-01" data-pat-date-picker="first-day: 1">

#### Specifying the "min" and "max" attributes.
#### Specifying the "min" and "max" attributes

<input class="pat-date-picker" min="2015-01-01" max="2015-12-31" type="date">

#### Show the week number.
#### Show the week number

<input class="pat-date-picker" data-pat-date-picker="week-numbers: show;" type="date">

Expand Down Expand Up @@ -72,16 +84,16 @@ The locale option allows you to define the language in which the formatted date

<input class="pat-date-picker" data-pat-date-picker="output-format: Do MMMM YYYY; locale: de" type="date" />


### Option reference

The HTML5 attributes `min` and `max` will be honoured.
The `min` and `max` attributes constrain calendar selections. In `editable`
mode, typed values are outside the scope of native date range validation.

In addition, the following options can be passed to `data-pat-date-picker`:

| Property | Type | Default Value | Available values | Description |
| --------------------------- | ------- | ------------- | ---------------- | ------------------------------------------------------------ |
| **behavior** (or behaviour) | string | styled | native, styled | Styled will always show the styled date picker. Native will use the system native date picker, provided the browser supports this. |
| **behavior** (or behaviour) | string | `styled` | `native`, `styled`, `editable` | `styled` displays the date in a `<time>` element and opens the JavaScript calendar for selection. `native` uses the browser’s date picker when available and falls back to the JavaScript calendar with a text field. `editable` lets users type a date directly or choose one from the JavaScript calendar. |
| **week-numbers** | string | hide | show, hide | "show" will show the weeks' numbers in a leftmost column. |
| **i18n** | URL | | | Provide a URL to a JSON resource which gives the i18n values. |
| **first-day** | Integer | 0 | | Set the first day of the week (0 -> Sunday, 1-> Monday, ...). |
Expand Down
Loading