-
Notifications
You must be signed in to change notification settings - Fork 0
/
birthdate-input.js
95 lines (78 loc) · 2.54 KB
/
birthdate-input.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
class BirthdateInput extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
const defaultFields = ["day", "month", "year"];
// get inputs
const [day, month, year] = Array.from(
this.querySelectorAll('input[type="number"]')
);
this.inputs = [day, month, year];
this.inputRefs = { day, month, year };
// add field keys
for (const [index, input] of Object.entries(this.inputs)) {
input.dataset.key = defaultFields[index];
}
// order parts based on the browser default locale
const dateTimeFormat = new Intl.DateTimeFormat(
this.getAttribute("locale") || undefined
);
const parts = dateTimeFormat.formatToParts();
const order = parts
.filter((part) => defaultFields.includes(part.type))
.map((part) => part.type);
// sort fields
for (const key of order) {
const input = this.inputRefs[key];
// append the label
this.append(this.querySelector(`label[for=${input.id}]`));
// then append the input
this.append(input);
}
// just focussed other field, prevent tabbing out of it by accident
let preventTab = false;
this.addEventListener("keydown", (e) => {
if (e.key === "Tab" && preventTab) {
e.preventDefault();
return;
}
});
// auto jump to next field when user inputs numbers
this.addEventListener("keyup", (e) => {
// easier prop access
const input = e.target;
const { value, placeholder, dataset } = input;
const currentIndex = order.indexOf(dataset.key);
// move to previous field
if (e.key === "Backspace" && value.length === 0) {
const previousField = this.inputRefs[order[currentIndex - 1]];
if (!previousField) return;
previousField.focus();
}
// not a number, ignore
if (!/[0-9]/.test(e.key)) return;
// not filled out completely
if (value.length !== placeholder.length) return;
// get next field
const nextField = this.inputRefs[order[currentIndex + 1]];
if (!nextField) return;
// focus this field
preventTab = true;
setTimeout(() => (preventTab = false), 250);
nextField.focus();
});
}
disconnectedCallback() {
// restore original order
for (const input of this.inputs) {
this.append(input);
}
}
}
// auto define if not defined yet
if (!customElements.get("birthdate-input")) {
customElements.define("birthdate-input", BirthdateInput);
}
// export so user can define own element if needed
export { BirthdateInput };