Skip to content

Commit bf23dbc

Browse files
authored
refactor: add _hasValue getter to InputMixin (#4362)
1 parent 4286494 commit bf23dbc

6 files changed

Lines changed: 35 additions & 25 deletions

File tree

‎packages/combo-box/src/vaadin-combo-box-mixin.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ export const ComboBoxMixin = (subclass) =>
10451045
this.value = '';
10461046
}
10471047

1048-
this._toggleHasValue(this.value !== '');
1048+
this._toggleHasValue(this._hasValue);
10491049
this._inputElementValue = this.value;
10501050
}
10511051
} else {
@@ -1089,7 +1089,7 @@ export const ComboBoxMixin = (subclass) =>
10891089
this._inputElementValue = value;
10901090
}
10911091

1092-
this._toggleHasValue(this.value !== '');
1092+
this._toggleHasValue(this._hasValue);
10931093
} else {
10941094
this.selectedItem = null;
10951095
}

‎packages/date-picker/src/vaadin-date-picker-mixin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ export const DatePickerMixin = (subclass) =>
755755
this._selectedDate = null;
756756
}
757757

758-
this._toggleHasValue(!!value);
758+
this._toggleHasValue(this._hasValue);
759759
}
760760

761761
/** @private */

‎packages/field-base/src/input-mixin.d.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ export declare class InputMixinClass {
2828
*/
2929
value: string;
3030

31+
/**
32+
* Indicates whether the value is different from the default one.
33+
* Override if the `value` property has a type other than `string`.
34+
*/
35+
protected readonly _hasValue: boolean;
36+
3137
/**
3238
* Clear the value of the field.
3339
*/
@@ -47,7 +53,7 @@ export declare class InputMixinClass {
4753

4854
protected _setInputElement(input: HTMLElement): void;
4955

50-
protected _toggleHasValue(value: boolean): void;
56+
protected _toggleHasValue(hasValue: boolean): void;
5157

5258
protected _valueChanged(value?: string, oldValue?: string): void;
5359
}

‎packages/field-base/src/input-mixin.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ export const InputMixin = dedupingMixin(
190190

191191
/**
192192
* Toggle the has-value attribute based on the value property.
193+
*
193194
* @param {boolean} hasValue
194195
* @protected
195196
*/
@@ -204,7 +205,7 @@ export const InputMixin = dedupingMixin(
204205
* @protected
205206
*/
206207
_valueChanged(newVal, oldVal) {
207-
this._toggleHasValue(newVal !== '' && newVal != null);
208+
this._toggleHasValue(this._hasValue);
208209

209210
// Setting initial value to empty string, do nothing.
210211
if (newVal === '' && oldVal === undefined) {
@@ -219,5 +220,15 @@ export const InputMixin = dedupingMixin(
219220
// Setting a value programmatically, sync it to input element.
220221
this._forwardInputValue(newVal);
221222
}
223+
224+
/**
225+
* Indicates whether the value is different from the default one.
226+
* Override if the `value` property has a type other than `string`.
227+
*
228+
* @protected
229+
*/
230+
get _hasValue() {
231+
return this.value != null && this.value !== '';
232+
}
222233
},
223234
);

‎packages/multi-select-combo-box/src/vaadin-multi-select-combo-box.js‎

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -449,12 +449,6 @@ class MultiSelectComboBox extends ResizeMixin(InputControlMixin(ThemableMixin(El
449449
computed: '__computeEffectiveFilteredItems(items, filteredItems, selectedItems, readonly)',
450450
},
451451

452-
/** @protected */
453-
_hasValue: {
454-
type: Boolean,
455-
value: false,
456-
},
457-
458452
/** @private */
459453
_overflowItems: {
460454
type: Array,
@@ -591,16 +585,6 @@ class MultiSelectComboBox extends ResizeMixin(InputControlMixin(ThemableMixin(El
591585
}
592586
}
593587

594-
/**
595-
* Override method inherited from `InputMixin`
596-
* to keep attribute after clearing the input.
597-
* @protected
598-
* @override
599-
*/
600-
_toggleHasValue() {
601-
super._toggleHasValue(this._hasValue);
602-
}
603-
604588
/**
605589
* Implement callback from `ResizeMixin` to update chips.
606590
* @protected
@@ -687,9 +671,7 @@ class MultiSelectComboBox extends ResizeMixin(InputControlMixin(ThemableMixin(El
687671

688672
/** @private */
689673
_selectedItemsChanged(selectedItems) {
690-
this._hasValue = Boolean(selectedItems && selectedItems.length);
691-
692-
this._toggleHasValue();
674+
this._toggleHasValue(this._hasValue);
693675

694676
// Use placeholder for announcing items
695677
if (this._hasValue) {
@@ -1123,6 +1105,17 @@ class MultiSelectComboBox extends ResizeMixin(InputControlMixin(ThemableMixin(El
11231105
__computeEffectiveFilteredItems(items, filteredItems, selectedItems, readonly) {
11241106
return !items && readonly ? selectedItems : filteredItems;
11251107
}
1108+
1109+
/**
1110+
* Override a method from `InputMixin` to
1111+
* compute the presence of value based on `selectedItems`.
1112+
*
1113+
* @protected
1114+
* @override
1115+
*/
1116+
get _hasValue() {
1117+
return this.selectedItems && this.selectedItems.length > 0;
1118+
}
11261119
}
11271120

11281121
customElements.define(MultiSelectComboBox.is, MultiSelectComboBox);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ class TimePicker extends PatternMixin(InputControlMixin(ThemableMixin(ElementMix
563563
this.__updateInputValue(parsedObj);
564564
}
565565

566-
this._toggleHasValue(!!this.value);
566+
this._toggleHasValue(this._hasValue);
567567
}
568568

569569
/** @private */

0 commit comments

Comments
 (0)