Skip to content

Commit 03a764e

Browse files
authored
refactor: simplify password-field observers to use updated (#10013)
1 parent 297f1bb commit 03a764e

File tree

1 file changed

+24
-69
lines changed

1 file changed

+24
-69
lines changed

packages/password-field/src/vaadin-password-field-mixin.js

Lines changed: 24 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export const PasswordFieldMixin = (superClass) =>
2626
*/
2727
revealButtonHidden: {
2828
type: Boolean,
29-
observer: '_revealButtonHiddenChanged',
3029
value: false,
3130
},
3231

@@ -38,7 +37,6 @@ export const PasswordFieldMixin = (superClass) =>
3837
type: Boolean,
3938
value: false,
4039
reflectToAttribute: true,
41-
observer: '_passwordVisibleChanged',
4240
readOnly: true,
4341
},
4442

@@ -64,10 +62,6 @@ export const PasswordFieldMixin = (superClass) =>
6462
};
6563
}
6664

67-
static get observers() {
68-
return ['__i18nChanged(i18n)'];
69-
}
70-
7165
/** @override */
7266
static get delegateAttrs() {
7367
// Do not delegate autocapitalize as it should be always set to "off"
@@ -95,11 +89,6 @@ export const PasswordFieldMixin = (superClass) =>
9589
];
9690
}
9791

98-
/** @protected */
99-
get _revealNode() {
100-
return this._revealButtonController && this._revealButtonController.node;
101-
}
102-
10392
/** @protected */
10493
ready() {
10594
super.ready();
@@ -108,24 +97,41 @@ export const PasswordFieldMixin = (superClass) =>
10897

10998
this._revealButtonController = new SlotController(this, 'reveal', 'vaadin-password-field-button', {
11099
initializer: (btn) => {
111-
btn.disabled = this.disabled;
100+
this._revealNode = btn;
112101

113102
btn.addEventListener('click', this.__boundRevealButtonClick);
114103
btn.addEventListener('mousedown', this.__boundRevealButtonMouseDown);
115104
},
116105
});
117106
this.addController(this._revealButtonController);
118107

119-
this.__updateAriaLabel(this.i18n);
120-
121-
this._updateToggleState(false);
122-
this._toggleRevealHidden(this.revealButtonHidden);
123-
124108
if (this.inputElement) {
125109
this.inputElement.autocapitalize = 'off';
126110
}
127111
}
128112

113+
/** @protected */
114+
updated(props) {
115+
super.updated(props);
116+
117+
if (props.has('disabled')) {
118+
this._revealNode.disabled = this.disabled;
119+
}
120+
121+
if (props.has('revealButtonHidden')) {
122+
this._toggleRevealHidden(this.revealButtonHidden);
123+
}
124+
125+
if (props.has('passwordVisible')) {
126+
this._setType(this.passwordVisible ? 'text' : 'password');
127+
this._revealNode.setAttribute('aria-pressed', this.passwordVisible ? 'true' : 'false');
128+
}
129+
130+
if (props.has('i18n') && this.i18n && this.i18n.reveal) {
131+
this._revealNode.setAttribute('aria-label', this.i18n.reveal);
132+
}
133+
}
134+
129135
/**
130136
* Override an event listener inherited from `InputControlMixin`
131137
* to store the value at the moment of the native `change` event.
@@ -188,31 +194,9 @@ export const PasswordFieldMixin = (superClass) =>
188194
}
189195
}
190196

191-
/** @private */
192-
__updateAriaLabel(i18n) {
193-
if (i18n && i18n.reveal && this._revealNode) {
194-
this._revealNode.setAttribute('aria-label', i18n.reveal);
195-
}
196-
}
197-
198-
/** @private */
199-
__i18nChanged(i18n) {
200-
this.__updateAriaLabel(i18n);
201-
}
202-
203-
/** @private */
204-
_revealButtonHiddenChanged(hidden) {
205-
this._toggleRevealHidden(hidden);
206-
}
207-
208-
/** @private */
209-
_togglePasswordVisibility() {
210-
this._setPasswordVisible(!this.passwordVisible);
211-
}
212-
213197
/** @private */
214198
_onRevealButtonClick() {
215-
this._togglePasswordVisibility();
199+
this._setPasswordVisible(!this.passwordVisible);
216200
}
217201

218202
/** @private */
@@ -239,33 +223,4 @@ export const PasswordFieldMixin = (superClass) =>
239223
}
240224
}
241225
}
242-
243-
/** @private */
244-
_updateToggleState(passwordVisible) {
245-
if (this._revealNode) {
246-
this._revealNode.setAttribute('aria-pressed', passwordVisible ? 'true' : 'false');
247-
}
248-
}
249-
250-
/** @private */
251-
_passwordVisibleChanged(passwordVisible) {
252-
this._setType(passwordVisible ? 'text' : 'password');
253-
254-
this._updateToggleState(passwordVisible);
255-
}
256-
257-
/**
258-
* Override method inherited from `DisabledMixin` to synchronize the reveal button
259-
* disabled state with the password field disabled state.
260-
* @param {boolean} disabled
261-
* @param {boolean} oldDisabled
262-
* @protected
263-
*/
264-
_disabledChanged(disabled, oldDisabled) {
265-
super._disabledChanged(disabled, oldDisabled);
266-
267-
if (this._revealNode) {
268-
this._revealNode.disabled = disabled;
269-
}
270-
}
271226
};

0 commit comments

Comments
 (0)