playbook_ui 16.8.0.pre.alpha.PLAY2965datepickerconsoleerrors16591 → 16.8.0.pre.alpha.PLAY2968timepickerrailsenhancedelementrefactor16753
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/pb_kits/playbook/pb_advanced_table/docs/_advanced_table_sticky_scroll_limitation.jsx +68 -0
- data/app/pb_kits/playbook/pb_advanced_table/docs/_advanced_table_sticky_scroll_limitation.md +7 -0
- data/app/pb_kits/playbook/pb_advanced_table/docs/example.yml +1 -0
- data/app/pb_kits/playbook/pb_advanced_table/docs/index.js +1 -0
- data/app/pb_kits/playbook/pb_icon/_icon.scss +2 -1
- data/app/pb_kits/playbook/pb_pb_circle_chart/docs/_pb_circle_chart_centered_data.html.erb +90 -0
- data/app/pb_kits/playbook/pb_pb_circle_chart/docs/_pb_circle_chart_centered_data.jsx +100 -0
- data/app/pb_kits/playbook/pb_pb_circle_chart/docs/_pb_circle_chart_centered_data.md +1 -0
- data/app/pb_kits/playbook/pb_pb_circle_chart/docs/_pb_circle_chart_default.jsx +1 -1
- data/app/pb_kits/playbook/pb_pb_circle_chart/docs/example.yml +2 -0
- data/app/pb_kits/playbook/pb_pb_circle_chart/docs/index.js +2 -1
- data/app/pb_kits/playbook/pb_table/docs/_table_with_filter_variant_external_filter_rails.md +1 -1
- data/app/pb_kits/playbook/pb_text_input/_text_input.scss +37 -0
- data/app/pb_kits/playbook/pb_time_picker/_time_picker.scss +26 -3
- data/app/pb_kits/playbook/pb_time_picker/docs/_time_picker_on_handler.html.erb +24 -0
- data/app/pb_kits/playbook/pb_time_picker/docs/_time_picker_on_handler_rails.md +6 -0
- data/app/pb_kits/playbook/pb_time_picker/docs/example.yml +1 -0
- data/app/pb_kits/playbook/pb_time_picker/index.ts +743 -30
- data/app/pb_kits/playbook/pb_time_picker/time_picker.html.erb +168 -1
- data/app/pb_kits/playbook/pb_time_picker/time_picker.rb +56 -26
- data/dist/chunks/{_typeahead-DA__Kgp5.js → _typeahead-DKlUinmj.js} +1 -1
- data/dist/chunks/{lazysizes-B7xYodB-.js → time_picker_helper-Bx8nzyM8.js} +1 -1
- data/dist/chunks/vendor.js +4 -4
- data/dist/menu.yml +2 -2
- data/dist/playbook-rails-react-bindings.js +1 -1
- data/dist/playbook-rails.js +1 -1
- data/dist/playbook.css +1 -1
- data/lib/playbook/forms/builder/time_picker_field.rb +6 -3
- data/lib/playbook/version.rb +1 -1
- metadata +12 -5
- /data/app/pb_kits/playbook/pb_time_picker/docs/{_time_picker_on_handler.md → _time_picker_on_handler_react.md} +0 -0
|
@@ -1,40 +1,753 @@
|
|
|
1
|
-
import PbEnhancedElement from
|
|
1
|
+
import PbEnhancedElement from '../pb_enhanced_element'
|
|
2
|
+
import {
|
|
3
|
+
parseTime,
|
|
4
|
+
parseTimeToMinutes,
|
|
5
|
+
isTimeInRange as isTimeInRangeHelper,
|
|
6
|
+
isAnyAMTimeValid as isAnyAMTimeValidHelper,
|
|
7
|
+
isAnyPMTimeValid as isAnyPMTimeValidHelper,
|
|
8
|
+
getDisplayTime,
|
|
9
|
+
get24HourTime,
|
|
10
|
+
getTimeRangeErrorMessage,
|
|
11
|
+
getTimezoneText,
|
|
12
|
+
getHourConstraints,
|
|
13
|
+
processHourInput,
|
|
14
|
+
normalizeHourOnBlur,
|
|
15
|
+
processMinuteInput,
|
|
16
|
+
normalizeMinuteOnBlur,
|
|
17
|
+
generateHourOptions,
|
|
18
|
+
generateMinuteOptions,
|
|
19
|
+
getValidInitialMeridiem as getValidInitialMeridiemHelper,
|
|
20
|
+
TimeFormat,
|
|
21
|
+
ParsedTime,
|
|
22
|
+
} from './time_picker_helper'
|
|
2
23
|
|
|
3
|
-
const
|
|
4
|
-
const
|
|
24
|
+
const VALIDATION_MESSAGE_CLASS = '.pb_body_kit_negative'
|
|
25
|
+
const VALIDATION_ERROR_SELECTOR = '[data-pb-time-picker-validation-error]'
|
|
26
|
+
|
|
27
|
+
type Meridiem = 'AM' | 'PM'
|
|
28
|
+
|
|
29
|
+
const TIME_PICKER_SELECTOR = '[data-pb-time-picker]'
|
|
30
|
+
const RANGE_ERROR_TEXT_SELECTOR = '[data-pb-time-picker-range-error-text]'
|
|
31
|
+
const TIMEZONE_TEXT_SELECTOR = '[data-pb-time-picker-timezone-text]'
|
|
5
32
|
|
|
6
33
|
export default class PbTimePicker extends PbEnhancedElement {
|
|
7
34
|
static get selector(): string {
|
|
8
|
-
return TIME_PICKER_SELECTOR
|
|
35
|
+
return TIME_PICKER_SELECTOR
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private pickerId: string
|
|
39
|
+
private timeFormat: TimeFormat
|
|
40
|
+
private fieldName: string
|
|
41
|
+
private minTimeMinutes: number | null
|
|
42
|
+
private maxTimeMinutes: number | null
|
|
43
|
+
private required: boolean
|
|
44
|
+
private disabled: boolean
|
|
45
|
+
private validationMessage: string
|
|
46
|
+
private showTimezone: boolean
|
|
47
|
+
private staticError: string
|
|
48
|
+
|
|
49
|
+
private mainInput: HTMLInputElement
|
|
50
|
+
private dropdownContainer: HTMLElement
|
|
51
|
+
private hourInput: HTMLInputElement
|
|
52
|
+
private minuteInput: HTMLInputElement
|
|
53
|
+
private hourDropdown: HTMLElement
|
|
54
|
+
private minuteDropdown: HTMLElement
|
|
55
|
+
private rangeErrorContainer: HTMLElement | null
|
|
56
|
+
private rangeErrorText: HTMLElement | null
|
|
57
|
+
private amRadio: HTMLInputElement | null
|
|
58
|
+
private pmRadio: HTMLInputElement | null
|
|
59
|
+
private amCard: HTMLElement | null
|
|
60
|
+
private pmCard: HTMLElement | null
|
|
61
|
+
|
|
62
|
+
private hour = 12
|
|
63
|
+
private minute = 0
|
|
64
|
+
private meridiem: Meridiem = 'PM'
|
|
65
|
+
private hasSelectedTime = false
|
|
66
|
+
private hourInputValue = '12'
|
|
67
|
+
private minuteInputValue = '00'
|
|
68
|
+
private displayValue = ''
|
|
69
|
+
private lastValidTime: ParsedTime | null = null
|
|
70
|
+
private showDropdown = false
|
|
71
|
+
private showHourDropdown = false
|
|
72
|
+
private showMinuteDropdown = false
|
|
73
|
+
private formSubmitted = false
|
|
74
|
+
|
|
75
|
+
private handleInvalidBound: (event: Event) => void
|
|
76
|
+
private handleClickOutsideBound: (event: MouseEvent) => void
|
|
77
|
+
private handleMeridiemKeyDownBound: (event: KeyboardEvent) => void
|
|
78
|
+
private handleFormSubmitBound: (event: Event) => void
|
|
79
|
+
|
|
80
|
+
private formElement: HTMLFormElement | null = null
|
|
81
|
+
private initialized = false
|
|
82
|
+
|
|
83
|
+
private initializeState(): void {
|
|
84
|
+
if (this.initialized) return
|
|
85
|
+
this.initialized = true
|
|
86
|
+
|
|
87
|
+
const element = this.element as HTMLElement
|
|
88
|
+
this.pickerId = element.id
|
|
89
|
+
const dataset = element.dataset
|
|
90
|
+
|
|
91
|
+
this.timeFormat = (dataset.timeFormat as TimeFormat) || 'AMPM'
|
|
92
|
+
this.fieldName = dataset.fieldName || `${this.pickerId}-time`
|
|
93
|
+
this.minTimeMinutes = parseTimeToMinutes(dataset.minTime)
|
|
94
|
+
this.maxTimeMinutes = parseTimeToMinutes(dataset.maxTime)
|
|
95
|
+
this.required = dataset.required === 'true'
|
|
96
|
+
this.disabled = dataset.disabled === 'true'
|
|
97
|
+
this.validationMessage = dataset.validationMessage || ''
|
|
98
|
+
this.showTimezone = dataset.showTimezone === 'true'
|
|
99
|
+
const staticErrorEl = element.querySelector('.text_input_wrapper ' + VALIDATION_MESSAGE_CLASS) as HTMLElement | null
|
|
100
|
+
this.staticError = staticErrorEl?.textContent?.trim() || ''
|
|
101
|
+
|
|
102
|
+
const initialTimeStr = dataset.value || dataset.defaultTime
|
|
103
|
+
const initialTime = parseTime(initialTimeStr, this.timeFormat)
|
|
104
|
+
const validMeridiem = this.getValidInitialMeridiem(initialTime.meridiem)
|
|
105
|
+
const hasInitial = !!initialTimeStr
|
|
106
|
+
|
|
107
|
+
this.hour = initialTime.hour
|
|
108
|
+
this.minute = initialTime.minute
|
|
109
|
+
this.meridiem = validMeridiem
|
|
110
|
+
this.hasSelectedTime = hasInitial
|
|
111
|
+
this.hourInputValue = initialTime.hour.toString()
|
|
112
|
+
this.minuteInputValue = initialTime.minute.toString().padStart(2, '0')
|
|
113
|
+
this.displayValue = hasInitial
|
|
114
|
+
? getDisplayTime(this.hour, this.minute, this.meridiem, this.timeFormat)
|
|
115
|
+
: ''
|
|
116
|
+
this.lastValidTime = hasInitial && this.isTimeInRange(this.hour, this.minute, this.meridiem)
|
|
117
|
+
? { hour: this.hour, minute: this.minute, meridiem: this.meridiem }
|
|
118
|
+
: null
|
|
119
|
+
|
|
120
|
+
this.mainInput = element.querySelector(`#${this.pickerId}-input`) as HTMLInputElement
|
|
121
|
+
this.fieldName = this.mainInput?.name || this.fieldName
|
|
122
|
+
this.dropdownContainer = element.querySelector('[data-pb-time-picker-dropdown]') as HTMLElement
|
|
123
|
+
this.hourInput = element.querySelector('[data-pb-time-picker-hour-input]') as HTMLInputElement
|
|
124
|
+
this.minuteInput = element.querySelector('[data-pb-time-picker-minute-input]') as HTMLInputElement
|
|
125
|
+
this.hourDropdown = element.querySelector('[data-pb-time-picker-hour-dropdown]') as HTMLElement
|
|
126
|
+
this.minuteDropdown = element.querySelector('[data-pb-time-picker-minute-dropdown]') as HTMLElement
|
|
127
|
+
this.rangeErrorContainer = element.querySelector('[data-pb-time-picker-range-error]')
|
|
128
|
+
this.rangeErrorText = element.querySelector(RANGE_ERROR_TEXT_SELECTOR) as HTMLElement | null
|
|
129
|
+
this.amRadio = element.querySelector(`#${this.pickerId}-am`) as HTMLInputElement | null
|
|
130
|
+
this.pmRadio = element.querySelector(`#${this.pickerId}-pm`) as HTMLInputElement | null
|
|
131
|
+
this.amCard = element.querySelector('.pb_time_picker_meridiem_am') as HTMLElement | null
|
|
132
|
+
this.pmCard = element.querySelector('.pb_time_picker_meridiem_pm') as HTMLElement | null
|
|
133
|
+
|
|
134
|
+
this.handleInvalidBound = this.handleInvalid.bind(this)
|
|
135
|
+
this.handleClickOutsideBound = this.handleClickOutside.bind(this)
|
|
136
|
+
this.handleMeridiemKeyDownBound = this.handleMeridiemKeyDown.bind(this)
|
|
137
|
+
this.handleFormSubmitBound = this.handleFormSubmit.bind(this)
|
|
138
|
+
this.formElement = element.closest('form')
|
|
9
139
|
}
|
|
10
140
|
|
|
11
141
|
connect(): void {
|
|
12
|
-
this.
|
|
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
|
-
|
|
142
|
+
this.initializeState()
|
|
143
|
+
|
|
144
|
+
this.populateDropdowns()
|
|
145
|
+
if (this.showTimezone) this.setTimezoneText()
|
|
146
|
+
this.updateUI()
|
|
147
|
+
this.bindEvents()
|
|
148
|
+
this.bindFormValidation()
|
|
149
|
+
document.addEventListener('invalid', this.handleInvalidBound, true)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
disconnect(): void {
|
|
153
|
+
this.formElement?.removeEventListener('submit', this.handleFormSubmitBound, true)
|
|
154
|
+
document.removeEventListener('invalid', this.handleInvalidBound, true)
|
|
155
|
+
document.removeEventListener('mousedown', this.handleClickOutsideBound)
|
|
156
|
+
this.amRadio?.removeEventListener('keydown', this.handleMeridiemKeyDownBound)
|
|
157
|
+
this.pmRadio?.removeEventListener('keydown', this.handleMeridiemKeyDownBound)
|
|
158
|
+
this.unbindMainInputEvents()
|
|
159
|
+
this.unbindDropdownInputEvents()
|
|
160
|
+
this.initialized = false
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private isTimeInRange(h: number, m: number, mer?: Meridiem): boolean {
|
|
164
|
+
return isTimeInRangeHelper(h, m, mer, this.timeFormat, this.minTimeMinutes, this.maxTimeMinutes)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private isCurrentTimeValid(): boolean {
|
|
168
|
+
return this.isTimeInRange(this.hour, this.minute, this.meridiem)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
private isAnyAMTimeValid(): boolean {
|
|
172
|
+
return isAnyAMTimeValidHelper(this.minTimeMinutes, this.maxTimeMinutes)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private isAnyPMTimeValid(): boolean {
|
|
176
|
+
return isAnyPMTimeValidHelper(this.minTimeMinutes, this.maxTimeMinutes)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
private getValidInitialMeridiem(parsedMeridiem: Meridiem): Meridiem {
|
|
180
|
+
return getValidInitialMeridiemHelper(parsedMeridiem, this.minTimeMinutes, this.maxTimeMinutes)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private get24HourTimeString(): string {
|
|
184
|
+
return get24HourTime(this.hour, this.minute, this.meridiem, this.timeFormat)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
private dispatchChange(time: string): void {
|
|
188
|
+
this.element.dispatchEvent(
|
|
189
|
+
new CustomEvent('pb-time-picker-change', { detail: { time }, bubbles: true }),
|
|
190
|
+
)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
private dispatchClose(time: string): void {
|
|
194
|
+
this.element.dispatchEvent(
|
|
195
|
+
new CustomEvent('pb-time-picker-close', { detail: { time }, bubbles: true }),
|
|
196
|
+
)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
private populateDropdowns(): void {
|
|
200
|
+
this.hourDropdown.innerHTML = ''
|
|
201
|
+
generateHourOptions(this.timeFormat).forEach((h) => {
|
|
202
|
+
const option = document.createElement('div')
|
|
203
|
+
option.className = 'time_dropdown_option'
|
|
204
|
+
option.dataset.hour = h.toString()
|
|
205
|
+
option.textContent = this.timeFormat === '24hour' ? h.toString().padStart(2, '0') : h.toString()
|
|
206
|
+
option.addEventListener('click', () => this.handleHourOptionClick(h))
|
|
207
|
+
this.hourDropdown.appendChild(option)
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
this.minuteDropdown.innerHTML = ''
|
|
211
|
+
generateMinuteOptions().forEach((m) => {
|
|
212
|
+
const option = document.createElement('div')
|
|
213
|
+
option.className = 'time_dropdown_option'
|
|
214
|
+
option.dataset.minute = m.toString()
|
|
215
|
+
option.textContent = m.toString().padStart(2, '0')
|
|
216
|
+
option.addEventListener('click', () => this.handleMinuteOptionClick(m))
|
|
217
|
+
this.minuteDropdown.appendChild(option)
|
|
218
|
+
})
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
private setTimezoneText(): void {
|
|
222
|
+
const timezoneEl = this.element.querySelector(TIMEZONE_TEXT_SELECTOR) as HTMLElement | null
|
|
223
|
+
if (timezoneEl) timezoneEl.textContent = getTimezoneText()
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
private scrollDropdownToSelected(dropdown: HTMLElement): void {
|
|
227
|
+
const selectedOption = dropdown.querySelector('.selected') as HTMLElement
|
|
228
|
+
if (selectedOption) {
|
|
229
|
+
const dropdownHeight = dropdown.clientHeight
|
|
230
|
+
const optionTop = selectedOption.offsetTop
|
|
231
|
+
const optionHeight = selectedOption.clientHeight
|
|
232
|
+
dropdown.scrollTop = optionTop - (dropdownHeight / 2) + (optionHeight / 2)
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
private updateUI(): void {
|
|
237
|
+
const { maxHour, minHour } = getHourConstraints(this.timeFormat)
|
|
238
|
+
|
|
239
|
+
if (this.hasSelectedTime) {
|
|
240
|
+
this.displayValue = getDisplayTime(this.hour, this.minute, this.meridiem, this.timeFormat)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
this.mainInput.value = this.displayValue
|
|
244
|
+
|
|
245
|
+
if (this.hasSelectedTime && this.displayValue.trim()) {
|
|
246
|
+
this.clearRequiredValidity()
|
|
247
|
+
if (this.mainInput.checkValidity()) {
|
|
248
|
+
this.formSubmitted = false
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
this.hourInput.value = this.hourInputValue
|
|
252
|
+
this.minuteInput.value = this.minuteInputValue
|
|
253
|
+
this.hourInput.min = minHour.toString()
|
|
254
|
+
this.hourInput.max = maxHour.toString()
|
|
255
|
+
|
|
256
|
+
const invalidSelection = this.hasSelectedTime && !this.isCurrentTimeValid()
|
|
257
|
+
this.hourInput.classList.toggle('invalid', invalidSelection)
|
|
258
|
+
this.minuteInput.classList.toggle('invalid', invalidSelection)
|
|
259
|
+
|
|
260
|
+
this.dropdownContainer.hidden = !this.showDropdown || this.disabled
|
|
261
|
+
this.hourDropdown.hidden = !this.showHourDropdown
|
|
262
|
+
this.minuteDropdown.hidden = !this.showMinuteDropdown
|
|
263
|
+
|
|
264
|
+
this.hourDropdown.querySelectorAll('.time_dropdown_option').forEach((el) => {
|
|
265
|
+
const option = el as HTMLElement
|
|
266
|
+
const h = parseInt(option.dataset.hour || '0', 10)
|
|
267
|
+
option.classList.toggle('selected', h === this.hour)
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
this.minuteDropdown.querySelectorAll('.time_dropdown_option').forEach((el) => {
|
|
271
|
+
const option = el as HTMLElement
|
|
272
|
+
const m = parseInt(option.dataset.minute || '0', 10)
|
|
273
|
+
option.classList.toggle('selected', m === this.minute)
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
if (this.rangeErrorContainer && this.rangeErrorText) {
|
|
277
|
+
if (invalidSelection) {
|
|
278
|
+
this.rangeErrorContainer.hidden = false
|
|
279
|
+
const { minTime, maxTime } = (this.element as HTMLElement).dataset
|
|
280
|
+
this.rangeErrorText.textContent = getTimeRangeErrorMessage(
|
|
281
|
+
minTime,
|
|
282
|
+
maxTime,
|
|
283
|
+
this.timeFormat,
|
|
284
|
+
)
|
|
285
|
+
} else {
|
|
286
|
+
this.rangeErrorContainer.hidden = true
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (this.amRadio && this.pmRadio) {
|
|
291
|
+
this.amRadio.checked = this.meridiem === 'AM'
|
|
292
|
+
this.pmRadio.checked = this.meridiem === 'PM'
|
|
293
|
+
const amValid = this.isAnyAMTimeValid()
|
|
294
|
+
const pmValid = this.isAnyPMTimeValid()
|
|
295
|
+
this.amRadio.disabled = !amValid
|
|
296
|
+
this.pmRadio.disabled = !pmValid
|
|
297
|
+
this.amCard?.classList.toggle('disabled_meridiem', !amValid)
|
|
298
|
+
this.pmCard?.classList.toggle('disabled_meridiem', !pmValid)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const shouldShowValidationError = this.required && this.formSubmitted && !this.hasSelectedTime
|
|
302
|
+
const errorDisplay = this.staticError
|
|
303
|
+
|| (shouldShowValidationError ? (this.validationMessage || 'Please fill out this field.') : '')
|
|
304
|
+
|
|
305
|
+
this.element.classList.toggle('error', !!errorDisplay)
|
|
306
|
+
this.mainInput.closest('.pb_text_input_kit')?.classList.toggle('error', !!errorDisplay)
|
|
307
|
+
|
|
308
|
+
if (errorDisplay) {
|
|
309
|
+
this.ensureValidationErrorMessage(errorDisplay)
|
|
310
|
+
} else {
|
|
311
|
+
this.removeValidationErrorMessage()
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (this.showHourDropdown) {
|
|
315
|
+
requestAnimationFrame(() => this.scrollDropdownToSelected(this.hourDropdown))
|
|
316
|
+
}
|
|
317
|
+
if (this.showMinuteDropdown) {
|
|
318
|
+
requestAnimationFrame(() => this.scrollDropdownToSelected(this.minuteDropdown))
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
private bindEvents(): void {
|
|
323
|
+
if (this.disabled) return
|
|
324
|
+
|
|
325
|
+
const addOnCard = this.mainInput.closest('.text_input_wrapper_add_on')?.querySelector('.add-on-card') as HTMLElement
|
|
326
|
+
if (addOnCard) {
|
|
327
|
+
addOnCard.style.cursor = 'pointer'
|
|
328
|
+
addOnCard.addEventListener('click', this.handleAddOnClick)
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
this.mainInput.addEventListener('click', this.handleInputClick)
|
|
332
|
+
this.mainInput.addEventListener('focus', this.handleInputFocus)
|
|
333
|
+
this.mainInput.addEventListener('keydown', this.handleInputKeyDown)
|
|
334
|
+
|
|
335
|
+
if (this.required) {
|
|
336
|
+
this.mainInput.addEventListener('change', this.handleMainInputChange)
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
this.hourInput.addEventListener('input', this.handleHourChange)
|
|
340
|
+
this.hourInput.addEventListener('blur', this.handleHourBlur)
|
|
341
|
+
this.hourInput.addEventListener('focus', this.handleHourFocus)
|
|
342
|
+
this.hourInput.addEventListener('keydown', this.handleHourKeyDown)
|
|
343
|
+
this.hourInput.addEventListener('click', this.handleHourInputClick)
|
|
344
|
+
|
|
345
|
+
this.minuteInput.addEventListener('input', this.handleMinuteChange)
|
|
346
|
+
this.minuteInput.addEventListener('blur', this.handleMinuteBlur)
|
|
347
|
+
this.minuteInput.addEventListener('focus', this.handleMinuteFocus)
|
|
348
|
+
this.minuteInput.addEventListener('keydown', this.handleMinuteKeyDown)
|
|
349
|
+
this.minuteInput.addEventListener('click', this.handleMinuteInputClick)
|
|
350
|
+
|
|
351
|
+
this.amRadio?.addEventListener('change', this.handleAmRadioChange)
|
|
352
|
+
this.pmRadio?.addEventListener('change', this.handlePmRadioChange)
|
|
353
|
+
this.amRadio?.addEventListener('keydown', this.handleMeridiemKeyDownBound)
|
|
354
|
+
this.pmRadio?.addEventListener('keydown', this.handleMeridiemKeyDownBound)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
private unbindMainInputEvents(): void {
|
|
358
|
+
const addOnCard = this.mainInput?.closest('.text_input_wrapper_add_on')?.querySelector('.add-on-card') as HTMLElement
|
|
359
|
+
addOnCard?.removeEventListener('click', this.handleAddOnClick)
|
|
360
|
+
this.mainInput?.removeEventListener('click', this.handleInputClick)
|
|
361
|
+
this.mainInput?.removeEventListener('focus', this.handleInputFocus)
|
|
362
|
+
this.mainInput?.removeEventListener('keydown', this.handleInputKeyDown)
|
|
363
|
+
this.mainInput?.removeEventListener('change', this.handleMainInputChange)
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
private unbindDropdownInputEvents(): void {
|
|
367
|
+
this.hourInput?.removeEventListener('input', this.handleHourChange)
|
|
368
|
+
this.hourInput?.removeEventListener('blur', this.handleHourBlur)
|
|
369
|
+
this.hourInput?.removeEventListener('focus', this.handleHourFocus)
|
|
370
|
+
this.hourInput?.removeEventListener('keydown', this.handleHourKeyDown)
|
|
371
|
+
this.hourInput?.removeEventListener('click', this.handleHourInputClick)
|
|
372
|
+
this.minuteInput?.removeEventListener('input', this.handleMinuteChange)
|
|
373
|
+
this.minuteInput?.removeEventListener('blur', this.handleMinuteBlur)
|
|
374
|
+
this.minuteInput?.removeEventListener('focus', this.handleMinuteFocus)
|
|
375
|
+
this.minuteInput?.removeEventListener('keydown', this.handleMinuteKeyDown)
|
|
376
|
+
this.minuteInput?.removeEventListener('click', this.handleMinuteInputClick)
|
|
377
|
+
this.amRadio?.removeEventListener('change', this.handleAmRadioChange)
|
|
378
|
+
this.pmRadio?.removeEventListener('change', this.handlePmRadioChange)
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
private handleAddOnClick = (e: Event): void => {
|
|
382
|
+
e.preventDefault()
|
|
383
|
+
e.stopPropagation()
|
|
384
|
+
this.mainInput.focus()
|
|
385
|
+
this.openDropdown()
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
private handleInputClick = (): void => {
|
|
389
|
+
this.openDropdown()
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
private handleInputFocus = (): void => {
|
|
393
|
+
this.openDropdown()
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
private handleMainInputChange = (): void => {
|
|
397
|
+
if (this.mainInput.checkValidity()) {
|
|
398
|
+
this.formSubmitted = false
|
|
399
|
+
}
|
|
400
|
+
this.updateUI()
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
private bindFormValidation(): void {
|
|
404
|
+
if (!this.required || !this.formElement) return
|
|
405
|
+
|
|
406
|
+
this.formElement.addEventListener('submit', this.handleFormSubmitBound, true)
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
private handleFormSubmit(): void {
|
|
410
|
+
this.syncRequiredValidity()
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
private syncRequiredValidity(): void {
|
|
414
|
+
if (!this.required) {
|
|
415
|
+
this.mainInput.setCustomValidity('')
|
|
416
|
+
return
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
const isEmpty = !this.hasSelectedTime || !this.mainInput.value.trim()
|
|
420
|
+
|
|
421
|
+
if (isEmpty) {
|
|
422
|
+
this.mainInput.setCustomValidity(
|
|
423
|
+
this.validationMessage || 'Please fill out this field.',
|
|
424
|
+
)
|
|
425
|
+
} else {
|
|
426
|
+
this.mainInput.setCustomValidity('')
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
private clearRequiredValidity(): void {
|
|
431
|
+
if (this.mainInput) {
|
|
432
|
+
this.mainInput.setCustomValidity('')
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
private ensureValidationErrorMessage(text: string): void {
|
|
437
|
+
let errorBody = this.element.querySelector(VALIDATION_ERROR_SELECTOR) as HTMLElement | null
|
|
438
|
+
|| this.element.querySelector(`.text_input_wrapper ${VALIDATION_MESSAGE_CLASS}`) as HTMLElement | null
|
|
439
|
+
|
|
440
|
+
if (!errorBody) {
|
|
441
|
+
errorBody = document.createElement('div')
|
|
442
|
+
errorBody.className = 'pb_body_kit pb_body_kit_negative'
|
|
443
|
+
errorBody.dataset.pbTimePickerValidationError = 'true'
|
|
444
|
+
errorBody.setAttribute('role', 'alert')
|
|
445
|
+
errorBody.setAttribute('aria-live', 'polite')
|
|
446
|
+
this.mainInput.closest('.text_input_wrapper')?.appendChild(errorBody)
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
errorBody.textContent = text
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
private removeValidationErrorMessage(): void {
|
|
453
|
+
if (this.staticError) return
|
|
454
|
+
|
|
455
|
+
this.element.querySelector(VALIDATION_ERROR_SELECTOR)?.remove()
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
private openDropdown(): void {
|
|
459
|
+
this.showDropdown = true
|
|
460
|
+
document.addEventListener('mousedown', this.handleClickOutsideBound)
|
|
461
|
+
this.updateUI()
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
private closeDropdown(skipValidation = false): void {
|
|
465
|
+
this.showHourDropdown = false
|
|
466
|
+
this.showMinuteDropdown = false
|
|
467
|
+
document.removeEventListener('mousedown', this.handleClickOutsideBound)
|
|
468
|
+
|
|
469
|
+
if (!this.hasSelectedTime) {
|
|
470
|
+
this.showDropdown = false
|
|
471
|
+
this.updateUI()
|
|
472
|
+
return
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const currentTimeValid = skipValidation || this.isCurrentTimeValid()
|
|
476
|
+
|
|
477
|
+
if (currentTimeValid) {
|
|
478
|
+
const timeString = this.get24HourTimeString()
|
|
479
|
+
this.displayValue = getDisplayTime(this.hour, this.minute, this.meridiem, this.timeFormat)
|
|
480
|
+
this.lastValidTime = { hour: this.hour, minute: this.minute, meridiem: this.meridiem }
|
|
481
|
+
this.showDropdown = false
|
|
482
|
+
this.dispatchClose(timeString)
|
|
483
|
+
} else if (this.lastValidTime) {
|
|
484
|
+
this.hour = this.lastValidTime.hour
|
|
485
|
+
this.minute = this.lastValidTime.minute
|
|
486
|
+
this.meridiem = this.lastValidTime.meridiem
|
|
487
|
+
this.hourInputValue = this.lastValidTime.hour.toString()
|
|
488
|
+
this.minuteInputValue = this.lastValidTime.minute.toString().padStart(2, '0')
|
|
489
|
+
this.displayValue = getDisplayTime(
|
|
490
|
+
this.lastValidTime.hour,
|
|
491
|
+
this.lastValidTime.minute,
|
|
492
|
+
this.lastValidTime.meridiem,
|
|
493
|
+
this.timeFormat,
|
|
494
|
+
)
|
|
495
|
+
this.showDropdown = false
|
|
496
|
+
} else {
|
|
497
|
+
const defaultState = parseTime(undefined, this.timeFormat)
|
|
498
|
+
const validMeridiem = this.getValidInitialMeridiem(defaultState.meridiem)
|
|
499
|
+
this.hour = defaultState.hour
|
|
500
|
+
this.minute = defaultState.minute
|
|
501
|
+
this.meridiem = validMeridiem
|
|
502
|
+
this.hourInputValue = defaultState.hour.toString()
|
|
503
|
+
this.minuteInputValue = defaultState.minute.toString().padStart(2, '0')
|
|
504
|
+
this.displayValue = ''
|
|
505
|
+
this.hasSelectedTime = false
|
|
506
|
+
this.showDropdown = false
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
this.updateUI()
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
private clearTimePicker(): void {
|
|
513
|
+
this.hasSelectedTime = false
|
|
514
|
+
this.lastValidTime = null
|
|
515
|
+
this.displayValue = ''
|
|
516
|
+
const defaultState = parseTime(undefined, this.timeFormat)
|
|
517
|
+
const validMeridiem = this.getValidInitialMeridiem(defaultState.meridiem)
|
|
518
|
+
this.hour = defaultState.hour
|
|
519
|
+
this.minute = defaultState.minute
|
|
520
|
+
this.meridiem = validMeridiem
|
|
521
|
+
this.hourInputValue = defaultState.hour.toString()
|
|
522
|
+
this.minuteInputValue = defaultState.minute.toString().padStart(2, '0')
|
|
523
|
+
this.dispatchChange('')
|
|
524
|
+
this.updateUI()
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
private handleInvalid(event: Event): void {
|
|
528
|
+
if (event.target !== this.mainInput) return
|
|
529
|
+
|
|
530
|
+
this.formSubmitted = true
|
|
531
|
+
this.updateUI()
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
private handleClickOutside(event: MouseEvent): void {
|
|
535
|
+
if (!this.element.contains(event.target as Node)) {
|
|
536
|
+
this.closeDropdown()
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
private handleInputKeyDown = (e: KeyboardEvent): void => {
|
|
541
|
+
if (e.key === 'Enter') {
|
|
542
|
+
e.preventDefault()
|
|
543
|
+
this.closeDropdown()
|
|
544
|
+
} else if (e.key === 'Escape') {
|
|
545
|
+
e.preventDefault()
|
|
546
|
+
this.closeDropdown()
|
|
547
|
+
} else if (e.key === 'Tab' && !e.shiftKey && this.showDropdown) {
|
|
548
|
+
e.preventDefault()
|
|
549
|
+
this.hourInput.focus()
|
|
550
|
+
} else if (e.key === 'Backspace' || e.key === 'Delete') {
|
|
551
|
+
e.preventDefault()
|
|
552
|
+
this.clearTimePicker()
|
|
553
|
+
} else if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
|
|
554
|
+
e.preventDefault()
|
|
555
|
+
if (!this.showDropdown) this.openDropdown()
|
|
556
|
+
} else if (e.key !== 'Tab') {
|
|
557
|
+
e.preventDefault()
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
private handleHourChange = (e: Event): void => {
|
|
562
|
+
const rawValue = (e.target as HTMLInputElement).value
|
|
563
|
+
const result = processHourInput(rawValue, this.timeFormat)
|
|
564
|
+
if (!result.isValid) return
|
|
565
|
+
this.hourInputValue = result.value
|
|
566
|
+
if (result.hour !== null) {
|
|
567
|
+
this.hour = result.hour
|
|
568
|
+
this.hasSelectedTime = true
|
|
569
|
+
this.dispatchChange(this.get24HourTimeString())
|
|
570
|
+
this.updateUI()
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
private handleHourFocus = (e: Event): void => {
|
|
575
|
+
(e.target as HTMLInputElement).select()
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
private handleHourBlur = (): void => {
|
|
579
|
+
const result = normalizeHourOnBlur(this.hourInputValue, this.hour, this.timeFormat)
|
|
580
|
+
this.hour = result.hour
|
|
581
|
+
this.hourInputValue = result.displayValue
|
|
582
|
+
this.updateUI()
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
private handleHourInputClick = (): void => {
|
|
586
|
+
this.showHourDropdown = !this.showHourDropdown
|
|
587
|
+
this.showMinuteDropdown = false
|
|
588
|
+
this.updateUI()
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
private handleHourOptionClick(h: number): void {
|
|
592
|
+
this.hour = h
|
|
593
|
+
this.hourInputValue = h.toString()
|
|
594
|
+
this.hasSelectedTime = true
|
|
595
|
+
this.showHourDropdown = false
|
|
596
|
+
this.dispatchChange(this.get24HourTimeString())
|
|
597
|
+
this.updateUI()
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
private handleMinuteChange = (e: Event): void => {
|
|
601
|
+
const rawValue = (e.target as HTMLInputElement).value
|
|
602
|
+
const result = processMinuteInput(rawValue)
|
|
603
|
+
if (!result.isValid) return
|
|
604
|
+
this.minuteInputValue = result.value
|
|
605
|
+
if (result.minute !== null) {
|
|
606
|
+
this.minute = result.minute
|
|
607
|
+
this.hasSelectedTime = true
|
|
608
|
+
this.dispatchChange(this.get24HourTimeString())
|
|
609
|
+
this.updateUI()
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
private handleMinuteFocus = (e: Event): void => {
|
|
614
|
+
(e.target as HTMLInputElement).select()
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
private handleMinuteBlur = (): void => {
|
|
618
|
+
const result = normalizeMinuteOnBlur(this.minuteInputValue, this.minute)
|
|
619
|
+
this.minute = result.minute
|
|
620
|
+
this.minuteInputValue = result.displayValue
|
|
621
|
+
this.updateUI()
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
private handleMinuteInputClick = (): void => {
|
|
625
|
+
this.showMinuteDropdown = !this.showMinuteDropdown
|
|
626
|
+
this.showHourDropdown = false
|
|
627
|
+
this.updateUI()
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
private handleMinuteOptionClick(m: number): void {
|
|
631
|
+
this.minute = m
|
|
632
|
+
this.minuteInputValue = m.toString().padStart(2, '0')
|
|
633
|
+
this.hasSelectedTime = true
|
|
634
|
+
this.showMinuteDropdown = false
|
|
635
|
+
this.dispatchChange(this.get24HourTimeString())
|
|
636
|
+
this.updateUI()
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
private handleMeridiemChange(mer: Meridiem): void {
|
|
640
|
+
this.meridiem = mer
|
|
641
|
+
this.hasSelectedTime = true
|
|
642
|
+
this.dispatchChange(this.get24HourTimeString())
|
|
643
|
+
this.updateUI()
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
private handleAmRadioChange = (): void => {
|
|
647
|
+
if (this.amRadio?.checked) this.handleMeridiemChange('AM')
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
private handlePmRadioChange = (): void => {
|
|
651
|
+
if (this.pmRadio?.checked) this.handleMeridiemChange('PM')
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
private handleHourKeyDown = (e: KeyboardEvent): void => {
|
|
655
|
+
const { maxHour, minHour } = getHourConstraints(this.timeFormat)
|
|
656
|
+
|
|
657
|
+
if (e.key === 'Tab' && !e.shiftKey) {
|
|
658
|
+
e.preventDefault()
|
|
659
|
+
this.showHourDropdown = false
|
|
660
|
+
this.minuteInput.focus()
|
|
661
|
+
this.updateUI()
|
|
662
|
+
} else if (e.key === 'Enter' || e.key === 'Escape') {
|
|
663
|
+
e.preventDefault()
|
|
664
|
+
this.showHourDropdown = false
|
|
665
|
+
this.closeDropdown()
|
|
666
|
+
} else if (e.key === 'ArrowDown') {
|
|
667
|
+
e.preventDefault()
|
|
668
|
+
const newHour = this.hour >= maxHour ? minHour : this.hour + 1
|
|
669
|
+
this.setHour(newHour)
|
|
670
|
+
} else if (e.key === 'ArrowUp') {
|
|
671
|
+
e.preventDefault()
|
|
672
|
+
const newHour = this.hour <= minHour ? maxHour : this.hour - 1
|
|
673
|
+
this.setHour(newHour)
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
private setHour(newHour: number): void {
|
|
678
|
+
this.hour = newHour
|
|
679
|
+
this.hourInputValue = this.timeFormat === '24hour'
|
|
680
|
+
? newHour.toString().padStart(2, '0')
|
|
681
|
+
: newHour.toString()
|
|
682
|
+
this.hasSelectedTime = true
|
|
683
|
+
this.dispatchChange(this.get24HourTimeString())
|
|
684
|
+
this.updateUI()
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
private handleMinuteKeyDown = (e: KeyboardEvent): void => {
|
|
688
|
+
if (e.key === 'Tab' && e.shiftKey) {
|
|
689
|
+
e.preventDefault()
|
|
690
|
+
this.showMinuteDropdown = false
|
|
691
|
+
this.hourInput.focus()
|
|
692
|
+
this.updateUI()
|
|
693
|
+
} else if (e.key === 'Tab' && !e.shiftKey) {
|
|
694
|
+
e.preventDefault()
|
|
695
|
+
this.showMinuteDropdown = false
|
|
696
|
+
if (this.timeFormat === '24hour') {
|
|
697
|
+
this.closeDropdown()
|
|
698
|
+
} else if (this.meridiem === 'AM') {
|
|
699
|
+
this.amRadio?.focus()
|
|
700
|
+
} else {
|
|
701
|
+
this.pmRadio?.focus()
|
|
702
|
+
}
|
|
703
|
+
} else if (e.key === 'Enter' || e.key === 'Escape') {
|
|
704
|
+
e.preventDefault()
|
|
705
|
+
this.showMinuteDropdown = false
|
|
706
|
+
this.closeDropdown()
|
|
707
|
+
} else if (e.key === 'ArrowDown') {
|
|
708
|
+
e.preventDefault()
|
|
709
|
+
const newMinute = this.minute >= 59 ? 0 : this.minute + 1
|
|
710
|
+
this.setMinute(newMinute)
|
|
711
|
+
} else if (e.key === 'ArrowUp') {
|
|
712
|
+
e.preventDefault()
|
|
713
|
+
const newMinute = this.minute <= 0 ? 59 : this.minute - 1
|
|
714
|
+
this.setMinute(newMinute)
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
private setMinute(newMinute: number): void {
|
|
719
|
+
this.minute = newMinute
|
|
720
|
+
this.minuteInputValue = newMinute.toString().padStart(2, '0')
|
|
721
|
+
this.hasSelectedTime = true
|
|
722
|
+
this.dispatchChange(this.get24HourTimeString())
|
|
723
|
+
this.updateUI()
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
private handleMeridiemKeyDown(e: KeyboardEvent): void {
|
|
727
|
+
if (e.key === 'Tab' && e.shiftKey) {
|
|
728
|
+
e.preventDefault()
|
|
729
|
+
this.minuteInput.focus()
|
|
730
|
+
} else if (e.key === 'Tab' && !e.shiftKey) {
|
|
731
|
+
e.preventDefault()
|
|
732
|
+
this.closeDropdown()
|
|
733
|
+
} else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
|
|
734
|
+
e.preventDefault()
|
|
735
|
+
if (this.meridiem !== 'AM' && this.isAnyAMTimeValid()) {
|
|
736
|
+
this.handleMeridiemChange('AM')
|
|
737
|
+
this.amRadio?.focus()
|
|
738
|
+
}
|
|
739
|
+
} else if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
|
|
740
|
+
e.preventDefault()
|
|
741
|
+
if (this.meridiem !== 'PM' && this.isAnyPMTimeValid()) {
|
|
742
|
+
this.handleMeridiemChange('PM')
|
|
743
|
+
this.pmRadio?.focus()
|
|
744
|
+
}
|
|
745
|
+
} else if (e.key === 'Enter' || e.key === ' ') {
|
|
746
|
+
e.preventDefault()
|
|
747
|
+
this.closeDropdown()
|
|
748
|
+
} else if (e.key === 'Escape') {
|
|
749
|
+
e.preventDefault()
|
|
750
|
+
this.closeDropdown()
|
|
38
751
|
}
|
|
39
752
|
}
|
|
40
753
|
}
|