-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathcalendar_input_controller.js
More file actions
170 lines (140 loc) · 4.89 KB
/
calendar_input_controller.js
File metadata and controls
170 lines (140 loc) · 4.89 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { Controller } from "@hotwired/stimulus"
// Connects to data-controller="ruby-ui--calendar-input"
export default class extends Controller {
static outlets = ["ruby-ui--calendar"]
static values = {
format: { type: String, default: "MM-dd-yyyy" },
placeholder: { type: String, default: "" },
labelAnimation: { type: Boolean, default: false },
labelClasses: { type: String, default: "" },
labelSeparator: { type: String, default: "" }
}
connect() {
this.isProgrammaticUpdate = false
this.cacheLabel()
if (!this.element.placeholder && this.placeholderValue) this.element.placeholder = this.placeholderValue
this.addEventListeners()
}
disconnect() {
this.removeEventListeners()
}
addEventListeners() {
this.boundHandleInput = this.handleInput.bind(this)
this.element.addEventListener("input", this.boundHandleInput)
}
removeEventListeners() {
if (this.boundHandleInput) {
this.element.removeEventListener("input", this.boundHandleInput)
}
}
handleInput(event) {
if (this.isProgrammaticUpdate) return
const value = event.target.value
if (this.matchesFormat(value)) {
const oldValue = this.element.value
this.syncCalendar(value)
this.dispatchDateChange(oldValue, value)
}
if (this.labelEl) this.updateFloatingLabel(value)
}
syncCalendar(inputValue) {
if (this.rubyUiCalendarOutlets.length === 0) return
if (!this.matchesFormat(inputValue)) return
const date = this.parseDate(inputValue)
if (!this.isValidDate(date)) return
const iso = this.toISOString(inputValue)
this.rubyUiCalendarOutlets.forEach((outlet) => {
if (outlet.selectedDateValue === iso) return
outlet.selectedDateValue = iso
})
}
matchesFormat(value) {
return /^\d{2}-\d{2}-\d{4}$/.test(value)
}
parseDate(value) {
const m = value.match(/^(\d{2})-(\d{2})-(\d{4})$/)
if (!m) return null
const [part1, part2, yearStr] = m.slice(1)
const year = parseInt(yearStr, 10)
const isAmericanFormat = this.formatValue === "MM-dd-yyyy"
const month = parseInt(isAmericanFormat ? part1 : part2, 10)
const day = parseInt(isAmericanFormat ? part2 : part1, 10)
if (!this.#validateDateComponents(month, day, year)) return null
return new Date(year, month - 1, day)
}
#validateDateComponents(month, day, year) {
if (month < 1 || month > 12 || day < 1 || day > 31) return false
if (day > new Date(year, month, 0).getDate()) return false
return true
}
toISOString(value) {
const m = value.match(/^(\d{2})-(\d{2})-(\d{4})$/)
if (!m) return null
const part1 = m[1]
const part2 = m[2]
const year = m[3]
if (this.formatValue === "MM-dd-yyyy") {
return `${year}-${part1}-${part2}`
} else {
return `${year}-${part2}-${part1}`
}
}
isValidDate(date) {
return date instanceof Date && !isNaN(date.getTime())
}
cacheLabel() {
this.labelEl = this.element.previousElementSibling
if (this.labelEl?.tagName === "LABEL") {
const separator = this.labelSeparatorValue || " "
this.labelBaseText = this.labelEl.textContent.split(separator)[0].trim()
} else {
this.labelBaseText = null
}
}
updateFloatingLabel(inputValue) {
if (!this.labelEl || !this.labelBaseText) return
if (!this.labelAnimationValue) return
if (inputValue && inputValue.length > 0) {
const separator = this.labelSeparatorValue || " "
this.labelEl.textContent = `${this.labelBaseText}${separator}${this.placeholderValue}`
this.addLabelClasses()
} else {
this.labelEl.textContent = this.labelBaseText
this.removeLabelClasses()
}
}
addLabelClasses() {
if (!this.labelClassesValue) return
this.labelEl.classList.add(...this.labelClassesValue.split(' '))
}
removeLabelClasses() {
if (!this.labelClassesValue) return
this.labelEl.classList.remove(...this.labelClassesValue.split(' '))
}
setValue(value) {
this.isProgrammaticUpdate = true
const formattedValue = (this.formatValue && this.formatValue !== "MM-dd-yyyy") ? this.formatDateForInput(value) : value
this.element.value = formattedValue
if (this.labelEl) this.updateFloatingLabel(formattedValue)
requestAnimationFrame(() => { this.isProgrammaticUpdate = false })
}
formatDateForInput(dateString) {
const match = dateString.match(/^(\d{4})-(\d{2})-(\d{2})$/)
if (!match) return dateString
const [, year, month, day] = match
if (this.formatValue === "MM-dd-yyyy") {
return `${month}-${day}-${year}`
} else {
return `${day}-${month}-${year}`
}
}
dispatchDateChange(oldValue, newValue) {
if (oldValue === newValue) return
const event = new CustomEvent("date:changed", {
detail: { oldValue, newValue, format: this.formatValue },
bubbles: true,
cancelable: true,
})
this.element.dispatchEvent(event)
}
}