-
-
Notifications
You must be signed in to change notification settings - Fork 322
Expand file tree
/
Copy pathdatepicker.js
More file actions
58 lines (48 loc) · 1.62 KB
/
datepicker.js
File metadata and controls
58 lines (48 loc) · 1.62 KB
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
import { translate, currentLocale } from "alchemy_admin/i18n"
import flatpickr from "flatpickr"
const locale = currentLocale()
class Datepicker extends HTMLElement {
// Load the locales for flatpickr before setting it up.
async connectedCallback() {
// English is the default locale for flatpickr, so we don't need to load it
if (locale !== "en") {
await import(`flatpickr/${locale}.js`)
}
// Bail out if the element was disconnected while the locale was loading.
// Otherwise flatpickr would leak a calendar onto a detached input.
if (!this.isConnected) return
this.flatpickr = flatpickr(this.inputField, this.flatpickrOptions)
}
disconnectedCallback() {
this.flatpickr?.destroy()
}
get flatpickrOptions() {
const enableTime = /time/.test(this.inputType)
const options = {
// alchemy_i18n supports `zh_CN` etc., but flatpickr only has two-letter codes (`zh`)
locale: locale.slice(0, 2),
altInput: true,
altFormat: translate(`formats.${this.inputType}`),
altInputClass: "flatpickr-input",
enableTime,
noCalendar: this.inputType === "time",
time_24hr: translate("formats.time_24hr"),
onValueUpdate(_selectedDates, _dateStr, instance) {
instance.element
.closest("alchemy-element-editor")
?.setDirty(this.inputField)
}
}
if (enableTime) {
options.dateFormat = "Z"
}
return options
}
get inputField() {
return this.querySelector("input")
}
get inputType() {
return this.getAttribute("input-type") || "date"
}
}
customElements.define("alchemy-datepicker", Datepicker)