@vaadin/password-field 24.4.0-alpha1 → 24.4.0-dev.223e39f050

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,8 +6,8 @@
6
6
  import './vaadin-password-field-button.js';
7
7
  import { html } from '@polymer/polymer/lib/utils/html-tag.js';
8
8
  import { defineCustomElement } from '@vaadin/component-base/src/define.js';
9
- import { SlotController } from '@vaadin/component-base/src/slot-controller.js';
10
9
  import { TextField } from '@vaadin/text-field/src/vaadin-text-field.js';
10
+ import { PasswordFieldMixin } from './vaadin-password-field-mixin.js';
11
11
 
12
12
  const ownTemplate = html`
13
13
  <div part="reveal-button" slot="suffix">
@@ -51,8 +51,9 @@ let memoizedTemplate;
51
51
  *
52
52
  * @customElement
53
53
  * @extends TextField
54
+ * @mixes PasswordFieldMixin
54
55
  */
55
- export class PasswordField extends TextField {
56
+ export class PasswordField extends PasswordFieldMixin(TextField) {
56
57
  static get is() {
57
58
  return 'vaadin-password-field';
58
59
  }
@@ -72,251 +73,6 @@ export class PasswordField extends TextField {
72
73
 
73
74
  return memoizedTemplate;
74
75
  }
75
-
76
- static get properties() {
77
- return {
78
- /**
79
- * Set to true to hide the eye icon which toggles the password visibility.
80
- * @attr {boolean} reveal-button-hidden
81
- */
82
- revealButtonHidden: {
83
- type: Boolean,
84
- observer: '_revealButtonHiddenChanged',
85
- value: false,
86
- },
87
-
88
- /**
89
- * True if the password is visible ([type=text]).
90
- * @attr {boolean} password-visible
91
- */
92
- passwordVisible: {
93
- type: Boolean,
94
- value: false,
95
- reflectToAttribute: true,
96
- observer: '_passwordVisibleChanged',
97
- readOnly: true,
98
- },
99
-
100
- /**
101
- * An object with translated strings used for localization.
102
- * It has the following structure and default values:
103
- *
104
- * ```
105
- * {
106
- * // Translation of the reveal icon button accessible label
107
- * reveal: 'Show password'
108
- * }
109
- * ```
110
- */
111
- i18n: {
112
- type: Object,
113
- value: () => {
114
- return {
115
- reveal: 'Show password',
116
- };
117
- },
118
- },
119
- };
120
- }
121
-
122
- static get observers() {
123
- return ['__i18nChanged(i18n.*)'];
124
- }
125
-
126
- constructor() {
127
- super();
128
- this._setType('password');
129
- this.__boundRevealButtonClick = this._onRevealButtonClick.bind(this);
130
- this.__boundRevealButtonMouseDown = this._onRevealButtonMouseDown.bind(this);
131
- this.__lastChange = '';
132
- }
133
-
134
- /** @protected */
135
- get slotStyles() {
136
- const tag = this.localName;
137
- return [
138
- ...super.slotStyles,
139
- `
140
- ${tag} [slot="input"]::-ms-reveal {
141
- display: none;
142
- }
143
- `,
144
- ];
145
- }
146
-
147
- /** @protected */
148
- get _revealNode() {
149
- return this._revealButtonController && this._revealButtonController.node;
150
- }
151
-
152
- /** @protected */
153
- ready() {
154
- super.ready();
155
-
156
- this._revealPart = this.shadowRoot.querySelector('[part="reveal-button"]');
157
-
158
- this._revealButtonController = new SlotController(this, 'reveal', 'vaadin-password-field-button', {
159
- initializer: (btn) => {
160
- btn.disabled = this.disabled;
161
-
162
- btn.addEventListener('click', this.__boundRevealButtonClick);
163
- btn.addEventListener('mousedown', this.__boundRevealButtonMouseDown);
164
- },
165
- });
166
- this.addController(this._revealButtonController);
167
-
168
- this.__updateAriaLabel(this.i18n);
169
-
170
- this._updateToggleState(false);
171
- this._toggleRevealHidden(this.revealButtonHidden);
172
-
173
- if (this.inputElement) {
174
- this.inputElement.autocapitalize = 'off';
175
- }
176
- }
177
-
178
- /**
179
- * Override an event listener inherited from `InputControlMixin`
180
- * to store the value at the moment of the native `change` event.
181
- * @param {Event} event
182
- * @protected
183
- * @override
184
- */
185
- _onChange(event) {
186
- super._onChange(event);
187
-
188
- this.__lastChange = this.inputElement.value;
189
- }
190
-
191
- /**
192
- * Override method inherited from `FocusMixin` to mark field as focused
193
- * when focus moves to the reveal button using Shift Tab.
194
- * @param {Event} event
195
- * @return {boolean}
196
- * @protected
197
- */
198
- _shouldSetFocus(event) {
199
- return event.target === this.inputElement || event.target === this._revealNode;
200
- }
201
-
202
- /**
203
- * Override method inherited from `FocusMixin` to not hide password
204
- * when focus moves to the reveal button or back to the input.
205
- * @param {Event} event
206
- * @return {boolean}
207
- * @protected
208
- */
209
- _shouldRemoveFocus(event) {
210
- return !(
211
- event.relatedTarget === this._revealNode ||
212
- (event.relatedTarget === this.inputElement && event.target === this._revealNode)
213
- );
214
- }
215
-
216
- /**
217
- * Override method inherited from `FocusMixin` to toggle password visibility.
218
- * @param {boolean} focused
219
- * @protected
220
- * @override
221
- */
222
- _setFocused(focused) {
223
- super._setFocused(focused);
224
-
225
- if (!focused) {
226
- this._setPasswordVisible(false);
227
-
228
- // Detect if `focusout` was prevented and if so, dispatch `change` event manually.
229
- if (this.__lastChange !== this.inputElement.value) {
230
- this.__lastChange = this.inputElement.value;
231
- this.dispatchEvent(new CustomEvent('change', { bubbles: true }));
232
- }
233
- } else {
234
- const isButtonFocused = this.getRootNode().activeElement === this._revealNode;
235
- // Remove focus-ring from the field when the reveal button gets focused
236
- this.toggleAttribute('focus-ring', this._keyboardActive && !isButtonFocused);
237
- }
238
- }
239
-
240
- /** @private */
241
- __updateAriaLabel(i18n) {
242
- if (i18n.reveal && this._revealNode) {
243
- this._revealNode.setAttribute('aria-label', i18n.reveal);
244
- }
245
- }
246
-
247
- /** @private */
248
- __i18nChanged(i18n) {
249
- this.__updateAriaLabel(i18n.base);
250
- }
251
-
252
- /** @private */
253
- _revealButtonHiddenChanged(hidden) {
254
- this._toggleRevealHidden(hidden);
255
- }
256
-
257
- /** @private */
258
- _togglePasswordVisibility() {
259
- this._setPasswordVisible(!this.passwordVisible);
260
- }
261
-
262
- /** @private */
263
- _onRevealButtonClick() {
264
- this._togglePasswordVisibility();
265
- }
266
-
267
- /** @private */
268
- _onRevealButtonMouseDown(e) {
269
- // Cancel the following focusout event
270
- e.preventDefault();
271
-
272
- // Focus the input to avoid problem with password still visible
273
- // when user clicks the reveal button and then clicks outside.
274
- this.inputElement.focus();
275
- }
276
-
277
- /** @private */
278
- _toggleRevealHidden(hidden) {
279
- if (this._revealNode) {
280
- if (hidden) {
281
- this._revealPart.setAttribute('hidden', '');
282
- this._revealNode.setAttribute('tabindex', '-1');
283
- this._revealNode.setAttribute('aria-hidden', 'true');
284
- } else {
285
- this._revealPart.removeAttribute('hidden');
286
- this._revealNode.setAttribute('tabindex', '0');
287
- this._revealNode.removeAttribute('aria-hidden');
288
- }
289
- }
290
- }
291
-
292
- /** @private */
293
- _updateToggleState(passwordVisible) {
294
- if (this._revealNode) {
295
- this._revealNode.setAttribute('aria-pressed', passwordVisible ? 'true' : 'false');
296
- }
297
- }
298
-
299
- /** @private */
300
- _passwordVisibleChanged(passwordVisible) {
301
- this._setType(passwordVisible ? 'text' : 'password');
302
-
303
- this._updateToggleState(passwordVisible);
304
- }
305
-
306
- /**
307
- * Override method inherited from `DisabledMixin` to synchronize the reveal button
308
- * disabled state with the password field disabled state.
309
- * @param {boolean} disabled
310
- * @param {boolean} oldDisabled
311
- * @protected
312
- */
313
- _disabledChanged(disabled, oldDisabled) {
314
- super._disabledChanged(disabled, oldDisabled);
315
-
316
- if (this._revealNode) {
317
- this._revealNode.disabled = disabled;
318
- }
319
- }
320
76
  }
321
77
 
322
78
  defineCustomElement(PasswordField);