Skip to content

Commit 7130a7d

Browse files
refactor: rewrite time-picker value formatting logic to use updated (#12468) (#12476)
Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
1 parent beb3492 commit 7130a7d

1 file changed

Lines changed: 30 additions & 19 deletions

File tree

packages/time-picker/src/vaadin-time-picker-mixin.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ export const TimePickerMixin = (superClass) =>
110110
'_openedOrItemsChanged(opened, _dropdownItems)',
111111
'_updateScroller(opened, _dropdownItems, _focusedIndex, _theme, _comboBoxValue)',
112112
'__updateAriaAttributes(_dropdownItems, opened, inputElement)',
113-
'__updateDropdownItems(__effectiveI18n, min, max, step)',
114113
];
115114
}
116115

@@ -230,6 +229,23 @@ export const TimePickerMixin = (superClass) =>
230229
this.addController(this._tooltipController);
231230
}
232231

232+
/** @protected */
233+
updated(props) {
234+
super.updated(props);
235+
236+
if (['__effectiveI18n', 'min', 'max', 'step'].some((prop) => props.has(prop))) {
237+
this.__updateDropdownItems();
238+
}
239+
240+
if (props.has('step')) {
241+
this.__updateValue(this.__getTimeObject(this.value));
242+
}
243+
244+
if (props.has('__effectiveI18n') && this.value) {
245+
this.__updateInputValue(this.__getTimeObject(this.value));
246+
}
247+
}
248+
233249
/**
234250
* Returns true if the current input value satisfies all constraints (if any).
235251
* You can override this method for custom validations.
@@ -457,6 +473,15 @@ export const TimePickerMixin = (superClass) =>
457473
return result;
458474
}
459475

476+
/**
477+
* Returning Object in the format `{ hours: ..., minutes: ..., seconds: ..., milliseconds: ... }`
478+
* from an ISO 8601 time, stripped to the resolution defined by the step.
479+
* @private
480+
*/
481+
__getTimeObject(timeString) {
482+
return validateTime(parseISOTime(timeString), this.step);
483+
}
484+
460485
/**
461486
* Returning seconds from Object in the format `{ hours: ..., minutes: ..., seconds: ..., milliseconds: ... }`
462487
* @private
@@ -503,25 +528,11 @@ export const TimePickerMixin = (superClass) =>
503528
}
504529

505530
/** @private */
506-
__updateDropdownItems(effectiveI18n, min, max, step) {
507-
const minTimeObj = validateTime(parseISOTime(min || MIN_ALLOWED_TIME), step);
508-
const minSec = this.__getSec(minTimeObj);
509-
510-
const maxTimeObj = validateTime(parseISOTime(max || MAX_ALLOWED_TIME), step);
511-
const maxSec = this.__getSec(maxTimeObj);
531+
__updateDropdownItems() {
532+
const minSec = this.__getSec(this.__getTimeObject(this.min || MIN_ALLOWED_TIME));
533+
const maxSec = this.__getSec(this.__getTimeObject(this.max || MAX_ALLOWED_TIME));
512534

513-
this._dropdownItems = this.__generateDropdownList(minSec, maxSec, step);
514-
515-
const parsedValue = validateTime(parseISOTime(this.value), step);
516-
517-
if (step !== this.__oldStep) {
518-
this.__oldStep = step;
519-
this.__updateValue(parsedValue);
520-
}
521-
522-
if (this.value) {
523-
this._comboBoxValue = effectiveI18n.formatTime(parsedValue);
524-
}
535+
this._dropdownItems = this.__generateDropdownList(minSec, maxSec, this.step);
525536
}
526537

527538
/** @private */

0 commit comments

Comments
 (0)