Skip to content

Commit 2530923

Browse files
committed
refactor(TimePicker): minor improvements
1 parent f9743bd commit 2530923

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

js/src/time-picker.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ class TimePicker extends BaseComponent {
190190
cancel() {
191191
this._date = this._initialDate
192192
this._input.value = this._initialDate ? this._convertStringToDate(this._initialDate).toLocaleTimeString(this._config.locale) : ''
193+
this._input.dispatchEvent(new Event('change'))
193194
this._timePickerBody.innerHTML = ''
194195
this.hide()
195196
this._createTimePickerSelection()
@@ -198,13 +199,15 @@ class TimePicker extends BaseComponent {
198199
clear() {
199200
this._date = null
200201
this._input.value = ''
202+
this._input.dispatchEvent(new Event('change'))
201203
this._timePickerBody.innerHTML = ''
202204
this._createTimePickerSelection()
203205
}
204206

205207
reset() {
206208
this._date = this._convertStringToDate(this._config.time)
207209
this._input.value = this._convertStringToDate(this._config.time).toLocaleTimeString(this._config.locale)
210+
this._input.dispatchEvent(new Event('change'))
208211
this._timePickerBody.innerHTML = ''
209212
this._createTimePickerSelection()
210213
}
@@ -256,9 +259,6 @@ class TimePicker extends BaseComponent {
256259
if (isValidTime(event.target.value)) {
257260
this._date = this._convertStringToDate(event.target.value)
258261

259-
// eslint-disable-next-line no-console
260-
console.log(this._date)
261-
262262
EventHandler.trigger(this._element, EVENT_TIME_CHANGE, {
263263
timeString: this._date ? this._date.toTimeString() : null,
264264
localeTimeString: this._date ? this._date.toLocaleTimeString() : null,
@@ -267,24 +267,28 @@ class TimePicker extends BaseComponent {
267267
}
268268
})
269269

270-
EventHandler.on(this._input.form, EVENT_SUBMIT, () => {
271-
if (this._input.form.classList.contains(CLASS_NAME_WAS_VALIDATED)) {
272-
if (Number.isNaN(Date.parse(`1970-01-01 ${this._input.value}`))) {
273-
return this._element.classList.add(CLASS_NAME_IS_INVALID)
274-
}
270+
if (this._config.container === 'dropdown') {
271+
EventHandler.on(this._input.form, EVENT_SUBMIT, () => {
272+
if (this._input.form.classList.contains(CLASS_NAME_WAS_VALIDATED)) {
273+
if (Number.isNaN(Date.parse(`1970-01-01 ${this._input.value}`))) {
274+
return this._element.classList.add(CLASS_NAME_IS_INVALID)
275+
}
275276

276-
if (this._date instanceof Date) {
277-
return this._element.classList.add(CLASS_NAME_IS_VALID)
278-
}
277+
if (this._date instanceof Date) {
278+
return this._element.classList.add(CLASS_NAME_IS_VALID)
279+
}
279280

280-
this._element.classList.add(CLASS_NAME_IS_INVALID)
281-
}
282-
})
281+
this._element.classList.add(CLASS_NAME_IS_INVALID)
282+
}
283+
})
284+
}
283285
}
284286

285287
_createTimePicker() {
286288
this._element.classList.add(CLASS_NAME_TIME_PICKER)
287289

290+
Manipulator.setDataAttribute(this._element, 'toggle', CLASS_NAME_TIME_PICKER)
291+
288292
if (this._config.size) {
289293
this._element.classList.add(`time-picker-${this._config.size}`)
290294
}
@@ -640,12 +644,13 @@ class TimePicker extends BaseComponent {
640644
_date.setSeconds(Number.parseInt(value, 10))
641645
}
642646

647+
this._date = new Date(_date)
648+
643649
if (this._input) {
644650
this._input.value = _date.toLocaleTimeString(this._config.locale)
651+
this._input.dispatchEvent(new Event('change'))
645652
}
646653

647-
this._date = new Date(_date)
648-
649654
EventHandler.trigger(this._element, EVENT_TIME_CHANGE, {
650655
timeString: _date.toTimeString(),
651656
localeTimeString: _date.toLocaleTimeString(),

0 commit comments

Comments
 (0)