4
4
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
*/
6
6
import { ComboBoxBaseMixin } from '@vaadin/combo-box/src/vaadin-combo-box-base-mixin.js' ;
7
+ import { I18nMixin } from '@vaadin/component-base/src/i18n-mixin.js' ;
7
8
import { TooltipController } from '@vaadin/component-base/src/tooltip-controller.js' ;
8
9
import { InputControlMixin } from '@vaadin/field-base/src/input-control-mixin.js' ;
9
10
import { InputController } from '@vaadin/field-base/src/input-controller.js' ;
@@ -24,11 +25,15 @@ const MAX_ALLOWED_TIME = '23:59:59.999';
24
25
*
25
26
* @polymerMixin
26
27
* @mixes ComboBoxBaseMixin
28
+ * @mixes I18nMixin
27
29
* @mixes InputControlMixin
28
30
* @mixes PatternMixin
29
31
*/
30
32
export const TimePickerMixin = ( superClass ) =>
31
- class TimePickerMixinClass extends PatternMixin ( ComboBoxBaseMixin ( InputControlMixin ( superClass ) ) ) {
33
+ class TimePickerMixinClass extends I18nMixin (
34
+ timePickerI18nDefaults ,
35
+ PatternMixin ( ComboBoxBaseMixin ( InputControlMixin ( superClass ) ) ) ,
36
+ ) {
32
37
static get properties ( ) {
33
38
return {
34
39
/**
@@ -98,43 +103,6 @@ export const TimePickerMixin = (superClass) =>
98
103
sync : true ,
99
104
} ,
100
105
101
- /**
102
- * The object used to localize this component.
103
- * To change the default localization, replace the entire
104
- * _i18n_ object or just the property you want to modify.
105
- *
106
- * The object has the following JSON structure:
107
- *
108
- * ```
109
- * {
110
- * // A function to format given `Object` as
111
- * // time string. Object is in the format `{ hours: ..., minutes: ..., seconds: ..., milliseconds: ... }`
112
- * formatTime: (time) => {
113
- * // returns a string representation of the given
114
- * // object in `hh` / 'hh:mm' / 'hh:mm:ss' / 'hh:mm:ss.fff' - formats
115
- * },
116
- *
117
- * // A function to parse the given text to an `Object` in the format
118
- * // `{ hours: ..., minutes: ..., seconds: ..., milliseconds: ... }`.
119
- * // Must properly parse (at least) text
120
- * // formatted by `formatTime`.
121
- * parseTime: text => {
122
- * // Parses a string in object/string that can be formatted by`formatTime`.
123
- * }
124
- * }
125
- * ```
126
- *
127
- * Both `formatTime` and `parseTime` need to be implemented
128
- * to ensure the component works properly.
129
- *
130
- * @type {!TimePickerI18n }
131
- */
132
- i18n : {
133
- type : Object ,
134
- sync : true ,
135
- value : ( ) => ( { ...timePickerI18nDefaults } ) ,
136
- } ,
137
-
138
106
/** @private */
139
107
_comboBoxValue : {
140
108
type : String ,
@@ -154,7 +122,7 @@ export const TimePickerMixin = (superClass) =>
154
122
'_openedOrItemsChanged(opened, _dropdownItems)' ,
155
123
'_updateScroller(opened, _dropdownItems, _focusedIndex, _theme)' ,
156
124
'__updateAriaAttributes(_dropdownItems, opened, inputElement)' ,
157
- '__updateDropdownItems(i18n , min, max, step)' ,
125
+ '__updateDropdownItems(__effectiveI18n , min, max, step)' ,
158
126
] ;
159
127
}
160
128
@@ -180,14 +148,53 @@ export const TimePickerMixin = (superClass) =>
180
148
return this . $ . clearButton ;
181
149
}
182
150
151
+ /**
152
+ * The object used to localize this component. To change the default
153
+ * localization, replace this with an object that provides both the
154
+ * time parsing and formatting functions.
155
+ *
156
+ * The object has the following JSON structure:
157
+ *
158
+ * ```
159
+ * {
160
+ * // A function to format given `Object` as
161
+ * // time string. Object is in the format `{ hours: ..., minutes: ..., seconds: ..., milliseconds: ... }`
162
+ * formatTime: (time) => {
163
+ * // returns a string representation of the given
164
+ * // object in `hh` / 'hh:mm' / 'hh:mm:ss' / 'hh:mm:ss.fff' - formats
165
+ * },
166
+ *
167
+ * // A function to parse the given text to an `Object` in the format
168
+ * // `{ hours: ..., minutes: ..., seconds: ..., milliseconds: ... }`.
169
+ * // Must properly parse (at least) text
170
+ * // formatted by `formatTime`.
171
+ * parseTime: text => {
172
+ * // Parses a string in object/string that can be formatted by`formatTime`.
173
+ * }
174
+ * }
175
+ * ```
176
+ *
177
+ * NOTE: `formatTime` and `parseTime` must be implemented in a
178
+ * compatible manner to ensure the component works properly.
179
+ *
180
+ * @return {!TimePickerI18n }
181
+ */
182
+ get i18n ( ) {
183
+ return super . i18n ;
184
+ }
185
+
186
+ set i18n ( value ) {
187
+ super . i18n = value ;
188
+ }
189
+
183
190
/**
184
191
* The input element's value when it cannot be parsed as a time, and an empty string otherwise.
185
192
*
186
193
* @private
187
194
* @return {string }
188
195
*/
189
196
get __unparsableValue ( ) {
190
- if ( this . _inputElementValue && ! this . i18n . parseTime ( this . _inputElementValue ) ) {
197
+ if ( this . _inputElementValue && ! this . __effectiveI18n . parseTime ( this . _inputElementValue ) ) {
191
198
return this . _inputElementValue ;
192
199
}
193
200
@@ -245,8 +252,8 @@ export const TimePickerMixin = (superClass) =>
245
252
checkValidity ( ) {
246
253
return ! ! (
247
254
this . inputElement . checkValidity ( ) &&
248
- ( ! this . value || this . _timeAllowed ( this . i18n . parseTime ( this . value ) ) ) &&
249
- ( ! this . _comboBoxValue || this . i18n . parseTime ( this . _comboBoxValue ) )
255
+ ( ! this . value || this . _timeAllowed ( this . __effectiveI18n . parseTime ( this . value ) ) ) &&
256
+ ( ! this . _comboBoxValue || this . __effectiveI18n . parseTime ( this . _comboBoxValue ) )
250
257
) ;
251
258
}
252
259
@@ -411,7 +418,7 @@ export const TimePickerMixin = (superClass) =>
411
418
// observer where the value can be parsed again, so we set
412
419
// this flag to ensure it does not alter the value.
413
420
this . __useMemo = true ;
414
- this . _comboBoxValue = this . i18n . formatTime ( objWithStep ) ;
421
+ this . _comboBoxValue = this . __effectiveI18n . formatTime ( objWithStep ) ;
415
422
this . __useMemo = false ;
416
423
417
424
this . __commitValueChange ( ) ;
@@ -508,7 +515,7 @@ export const TimePickerMixin = (superClass) =>
508
515
}
509
516
510
517
/** @private */
511
- __updateDropdownItems ( i18n , min , max , step ) {
518
+ __updateDropdownItems ( effectiveI18n , min , max , step ) {
512
519
const minTimeObj = validateTime ( parseISOTime ( min || MIN_ALLOWED_TIME ) , step ) ;
513
520
const minSec = this . __getSec ( minTimeObj ) ;
514
521
@@ -524,7 +531,7 @@ export const TimePickerMixin = (superClass) =>
524
531
}
525
532
526
533
if ( this . value ) {
527
- this . _comboBoxValue = i18n . formatTime ( i18n . parseTime ( this . value ) ) ;
534
+ this . _comboBoxValue = effectiveI18n . formatTime ( effectiveI18n . parseTime ( this . value ) ) ;
528
535
}
529
536
}
530
537
@@ -560,7 +567,7 @@ export const TimePickerMixin = (superClass) =>
560
567
while ( time + step >= minSec && time + step <= maxSec ) {
561
568
const timeObj = validateTime ( this . __addStep ( time * 1000 , step ) , step ) ;
562
569
time += step ;
563
- const formatted = this . i18n . formatTime ( timeObj ) ;
570
+ const formatted = this . __effectiveI18n . formatTime ( timeObj ) ;
564
571
generatedList . push ( { label : formatted , value : formatted } ) ;
565
572
}
566
573
@@ -606,8 +613,8 @@ export const TimePickerMixin = (superClass) =>
606
613
return ;
607
614
}
608
615
609
- const parsedObj = this . __useMemo ? this . __memoValue : this . i18n . parseTime ( value ) ;
610
- const newValue = this . i18n . formatTime ( parsedObj ) || '' ;
616
+ const parsedObj = this . __useMemo ? this . __memoValue : this . __effectiveI18n . parseTime ( value ) ;
617
+ const newValue = this . __effectiveI18n . formatTime ( parsedObj ) || '' ;
611
618
612
619
if ( parsedObj ) {
613
620
if ( value !== newValue ) {
@@ -644,7 +651,7 @@ export const TimePickerMixin = (superClass) =>
644
651
645
652
/** @private */
646
653
__updateInputValue ( obj ) {
647
- const timeString = this . i18n . formatTime ( validateTime ( obj , this . step ) ) || '' ;
654
+ const timeString = this . __effectiveI18n . formatTime ( validateTime ( obj , this . step ) ) || '' ;
648
655
this . _inputElementValue = timeString ;
649
656
this . _comboBoxValue = timeString ;
650
657
}
@@ -657,8 +664,8 @@ export const TimePickerMixin = (superClass) =>
657
664
* @protected
658
665
*/
659
666
_timeAllowed ( time ) {
660
- const parsedMin = this . i18n . parseTime ( this . min || MIN_ALLOWED_TIME ) ;
661
- const parsedMax = this . i18n . parseTime ( this . max || MAX_ALLOWED_TIME ) ;
667
+ const parsedMin = this . __effectiveI18n . parseTime ( this . min || MIN_ALLOWED_TIME ) ;
668
+ const parsedMax = this . __effectiveI18n . parseTime ( this . max || MAX_ALLOWED_TIME ) ;
662
669
663
670
return (
664
671
( ! this . __getMsec ( parsedMin ) || this . __getMsec ( time ) >= this . __getMsec ( parsedMin ) ) &&
0 commit comments