@vaadin/field-base 22.0.4 → 22.0.5

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vaadin/field-base",
3
- "version": "22.0.4",
3
+ "version": "22.0.5",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -31,7 +31,7 @@
31
31
  "dependencies": {
32
32
  "@open-wc/dedupe-mixin": "^1.3.0",
33
33
  "@polymer/polymer": "^3.0.0",
34
- "@vaadin/component-base": "^22.0.4",
34
+ "@vaadin/component-base": "^22.0.5",
35
35
  "lit": "^2.0.0"
36
36
  },
37
37
  "devDependencies": {
@@ -39,5 +39,5 @@
39
39
  "@vaadin/testing-helpers": "^0.3.2",
40
40
  "sinon": "^9.2.1"
41
41
  },
42
- "gitHead": "55891f68d4da41e846e06dfe51dceba1665e41ce"
42
+ "gitHead": "3e1990867670f3de2dec6fa1200d945623b2710c"
43
43
  }
@@ -6,13 +6,18 @@
6
6
  import { Constructor } from '@open-wc/dedupe-mixin';
7
7
  import { DisabledMixinClass } from '@vaadin/component-base/src/disabled-mixin.js';
8
8
  import { FocusMixinClass } from '@vaadin/component-base/src/focus-mixin.js';
9
+ import { TabindexMixinClass } from '@vaadin/component-base/src/tabindex-mixin.js';
9
10
 
10
11
  /**
11
12
  * A mixin to forward focus to an element in the light DOM.
12
13
  */
13
14
  export declare function DelegateFocusMixin<T extends Constructor<HTMLElement>>(
14
15
  base: T
15
- ): T & Constructor<DelegateFocusMixinClass> & Constructor<DisabledMixinClass> & Constructor<FocusMixinClass>;
16
+ ): T &
17
+ Constructor<DelegateFocusMixinClass> &
18
+ Constructor<DisabledMixinClass> &
19
+ Constructor<FocusMixinClass> &
20
+ Constructor<TabindexMixinClass>;
16
21
 
17
22
  export declare class DelegateFocusMixinClass {
18
23
  /**
@@ -4,19 +4,19 @@
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
7
- import { DisabledMixin } from '@vaadin/component-base/src/disabled-mixin.js';
8
7
  import { FocusMixin } from '@vaadin/component-base/src/focus-mixin.js';
8
+ import { TabindexMixin } from '@vaadin/component-base/src/tabindex-mixin.js';
9
9
 
10
10
  /**
11
11
  * A mixin to forward focus to an element in the light DOM.
12
12
  *
13
13
  * @polymerMixin
14
- * @mixes DisabledMixin
15
14
  * @mixes FocusMixin
15
+ * @mixes TabindexMixin
16
16
  */
17
17
  export const DelegateFocusMixin = dedupingMixin(
18
18
  (superclass) =>
19
- class DelegateFocusMixinClass extends FocusMixin(DisabledMixin(superclass)) {
19
+ class DelegateFocusMixinClass extends FocusMixin(TabindexMixin(superclass)) {
20
20
  static get properties() {
21
21
  return {
22
22
  /**
@@ -40,6 +40,19 @@ export const DelegateFocusMixin = dedupingMixin(
40
40
  type: Object,
41
41
  readOnly: true,
42
42
  observer: '_focusElementChanged'
43
+ },
44
+
45
+ /**
46
+ * Indicates whether the element can be focused and where it participates in sequential keyboard navigation.
47
+ *
48
+ * By default, the host element does not have tabindex attribute. Instead, `focusElement` should have it.
49
+ * Toggling `tabindex` attribute on the host element propagates its value to `focusElement`.
50
+ *
51
+ * @protected
52
+ */
53
+ tabindex: {
54
+ type: Number,
55
+ value: undefined
43
56
  }
44
57
  };
45
58
  }
@@ -103,6 +116,7 @@ export const DelegateFocusMixin = dedupingMixin(
103
116
  if (element) {
104
117
  element.disabled = this.disabled;
105
118
  this._addFocusListeners(element);
119
+ this.__forwardTabIndex(this.tabindex);
106
120
  } else if (oldElement) {
107
121
  this._removeFocusListeners(oldElement);
108
122
  }
@@ -162,10 +176,12 @@ export const DelegateFocusMixin = dedupingMixin(
162
176
 
163
177
  /**
164
178
  * @param {boolean} disabled
179
+ * @param {boolean} oldDisabled
165
180
  * @protected
181
+ * @override
166
182
  */
167
- _disabledChanged(disabled) {
168
- super._disabledChanged(disabled);
183
+ _disabledChanged(disabled, oldDisabled) {
184
+ super._disabledChanged(disabled, oldDisabled);
169
185
 
170
186
  if (this.focusElement) {
171
187
  this.focusElement.disabled = disabled;
@@ -175,5 +191,37 @@ export const DelegateFocusMixin = dedupingMixin(
175
191
  this.blur();
176
192
  }
177
193
  }
194
+
195
+ /**
196
+ * Override an observer from `TabindexMixin`.
197
+ * Do not call super to remove tabindex attribute
198
+ * from the host after it has been forwarded.
199
+ * @param {string} tabindex
200
+ * @protected
201
+ * @override
202
+ */
203
+ _tabindexChanged(tabindex) {
204
+ this.__forwardTabIndex(tabindex);
205
+ }
206
+
207
+ /** @private */
208
+ __forwardTabIndex(tabindex) {
209
+ if (tabindex !== undefined && this.focusElement) {
210
+ this.focusElement.tabIndex = tabindex;
211
+
212
+ // Preserve tabindex="-1" on the host element
213
+ if (tabindex !== -1) {
214
+ this.tabindex = undefined;
215
+ }
216
+ }
217
+
218
+ if (this.disabled && tabindex) {
219
+ // If tabindex attribute was changed while component was disabled
220
+ if (tabindex !== -1) {
221
+ this.__lastTabIndex = tabindex;
222
+ }
223
+ this.tabindex = undefined;
224
+ }
225
+ }
178
226
  }
179
227
  );
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import { SlotController } from '@vaadin/component-base/src/slot-controller.js';
7
+
8
+ /**
9
+ * A controller that manages the error message node content.
10
+ */
11
+ export class ErrorController extends SlotController {
12
+ /**
13
+ * ID attribute value set on the error message element.
14
+ */
15
+ readonly errorId: string;
16
+
17
+ /**
18
+ * String used for the error message text content.
19
+ */
20
+ protected errorMessage: string | null | undefined;
21
+
22
+ /**
23
+ * Set to true when the host element is invalid.
24
+ */
25
+ protected invalid: boolean;
26
+
27
+ /**
28
+ * Set the error message element text content.
29
+ */
30
+ setErrorMessage(errorMessage: string | null | undefined): void;
31
+
32
+ /**
33
+ * Set invalid state for detecting whether to show error message.
34
+ */
35
+ setInvalid(invalid: boolean): void;
36
+ }
@@ -0,0 +1,134 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import { SlotController } from '@vaadin/component-base/src/slot-controller.js';
7
+
8
+ /**
9
+ * A controller that manages the error message node content.
10
+ */
11
+ export class ErrorController extends SlotController {
12
+ constructor(host) {
13
+ super(
14
+ host,
15
+ 'error-message',
16
+ () => document.createElement('div'),
17
+ (_host, node) => {
18
+ this.__updateErrorId(node);
19
+
20
+ this.__updateHasError();
21
+ }
22
+ );
23
+ }
24
+
25
+ /**
26
+ * ID attribute value set on the error message element.
27
+ *
28
+ * @return {string}
29
+ */
30
+ get errorId() {
31
+ return this.node && this.node.id;
32
+ }
33
+
34
+ /**
35
+ * Set the error message element text content.
36
+ *
37
+ * @param {string} errorMessage
38
+ */
39
+ setErrorMessage(errorMessage) {
40
+ this.errorMessage = errorMessage;
41
+
42
+ this.__updateHasError();
43
+ }
44
+
45
+ /**
46
+ * Set invalid state for detecting whether to show error message.
47
+ *
48
+ * @param {boolean} invalid
49
+ */
50
+ setInvalid(invalid) {
51
+ this.invalid = invalid;
52
+
53
+ this.__updateHasError();
54
+ }
55
+
56
+ /**
57
+ * Override to initialize the newly added custom label.
58
+ *
59
+ * @param {Node} errorNode
60
+ * @protected
61
+ * @override
62
+ */
63
+ initCustomNode(errorNode) {
64
+ this.__updateErrorId(errorNode);
65
+
66
+ // Save the custom error message content on the host.
67
+ if (errorNode.textContent && !this.errorMessage) {
68
+ this.errorMessage = errorNode.textContent.trim();
69
+ }
70
+
71
+ this.__updateHasError();
72
+ }
73
+
74
+ /**
75
+ * Override to cleanup label node when it's removed.
76
+ *
77
+ * @param {Node} node
78
+ * @protected
79
+ * @override
80
+ */
81
+ teardownNode(node) {
82
+ let errorNode = this.getSlotChild();
83
+
84
+ // If custom error was removed, restore the default one.
85
+ if (!errorNode && node !== this.defaultNode) {
86
+ errorNode = this.attachDefaultNode();
87
+
88
+ // Run initializer to update default label and ID.
89
+ this.initNode(errorNode);
90
+ }
91
+
92
+ this.__updateHasError();
93
+ }
94
+
95
+ /**
96
+ * @param {string} error
97
+ * @private
98
+ */
99
+ __isNotEmpty(error) {
100
+ return Boolean(error && error.trim() !== '');
101
+ }
102
+
103
+ /** @private */
104
+ __updateHasError() {
105
+ const errorNode = this.node;
106
+ const hasError = Boolean(this.invalid && this.__isNotEmpty(this.errorMessage));
107
+
108
+ // Update both default and custom error message node.
109
+ if (errorNode) {
110
+ errorNode.textContent = hasError ? this.errorMessage : '';
111
+ errorNode.hidden = !hasError;
112
+
113
+ // Role alert will make the error message announce immediately
114
+ // as the field becomes invalid
115
+ if (hasError) {
116
+ errorNode.setAttribute('role', 'alert');
117
+ } else {
118
+ errorNode.removeAttribute('role');
119
+ }
120
+ }
121
+
122
+ this.host.toggleAttribute('has-error-message', hasError);
123
+ }
124
+
125
+ /**
126
+ * @param {HTMLElement} errorNode
127
+ * @private
128
+ */
129
+ __updateErrorId(errorNode) {
130
+ if (!errorNode.id) {
131
+ errorNode.id = this.defaultId;
132
+ }
133
+ }
134
+ }
@@ -6,7 +6,7 @@
6
6
  import { animationFrame } from '@vaadin/component-base/src/async.js';
7
7
  import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
8
8
  import { Debouncer } from '@vaadin/component-base/src/debounce.js';
9
- import { SlotMixin } from '@vaadin/component-base/src/slot-mixin.js';
9
+ import { ErrorController } from './error-controller.js';
10
10
  import { FieldAriaController } from './field-aria-controller.js';
11
11
  import { HelperController } from './helper-controller.js';
12
12
  import { LabelMixin } from './label-mixin.js';
@@ -18,11 +18,10 @@ import { ValidateMixin } from './validate-mixin.js';
18
18
  * @polymerMixin
19
19
  * @mixes ControllerMixin
20
20
  * @mixes LabelMixin
21
- * @mixes SlotMixin
22
21
  * @mixes ValidateMixin
23
22
  */
24
23
  export const FieldMixin = (superclass) =>
25
- class FieldMixinClass extends ValidateMixin(LabelMixin(ControllerMixin(SlotMixin(superclass)))) {
24
+ class FieldMixinClass extends ValidateMixin(LabelMixin(ControllerMixin(superclass))) {
26
25
  static get properties() {
27
26
  return {
28
27
  /**
@@ -40,7 +39,8 @@ export const FieldMixin = (superclass) =>
40
39
  * @attr {string} error-message
41
40
  */
42
41
  errorMessage: {
43
- type: String
42
+ type: String,
43
+ observer: '_errorMessageChanged'
44
44
  },
45
45
 
46
46
  /**
@@ -57,33 +57,25 @@ export const FieldMixin = (superclass) =>
57
57
  };
58
58
  }
59
59
 
60
- /** @protected */
61
- get slots() {
62
- return {
63
- ...super.slots,
64
- 'error-message': () => {
65
- const error = document.createElement('div');
66
- error.textContent = this.errorMessage;
67
- return error;
68
- }
69
- };
70
- }
71
-
72
60
  static get observers() {
73
61
  return [
74
62
  '__observeOffsetHeight(errorMessage, invalid, label, helperText)',
75
- '_updateErrorMessage(invalid, errorMessage)',
76
63
  '_invalidChanged(invalid)',
77
64
  '_requiredChanged(required)'
78
65
  ];
79
66
  }
80
67
 
68
+ /** @protected */
69
+ get _errorId() {
70
+ return this._errorController.errorId;
71
+ }
72
+
81
73
  /**
82
74
  * @protected
83
75
  * @return {HTMLElement}
84
76
  */
85
77
  get _errorNode() {
86
- return this._getDirectSlotChild('error-message');
78
+ return this._errorController.node;
87
79
  }
88
80
 
89
81
  /** @protected */
@@ -102,15 +94,13 @@ export const FieldMixin = (superclass) =>
102
94
  constructor() {
103
95
  super();
104
96
 
105
- // Ensure every instance has unique ID
106
- const uniqueId = (FieldMixinClass._uniqueFieldId = 1 + FieldMixinClass._uniqueFieldId || 0);
107
- this._errorId = `error-${this.localName}-${uniqueId}`;
108
-
109
97
  this._fieldAriaController = new FieldAriaController(this);
110
98
  this._helperController = new HelperController(this);
99
+ this._errorController = new ErrorController(this);
111
100
 
112
101
  this.addController(this._fieldAriaController);
113
102
  this.addController(this._helperController);
103
+ this.addController(this._errorController);
114
104
 
115
105
  this._labelController.addEventListener('label-changed', (event) => {
116
106
  const { hasLabel, node } = event.detail;
@@ -123,29 +113,6 @@ export const FieldMixin = (superclass) =>
123
113
  });
124
114
  }
125
115
 
126
- /** @protected */
127
- ready() {
128
- super.ready();
129
-
130
- const error = this._errorNode;
131
- if (error) {
132
- error.id = this._errorId;
133
-
134
- this.__applyCustomError();
135
-
136
- this._updateErrorMessage(this.invalid, this.errorMessage);
137
- }
138
- }
139
-
140
- /** @private */
141
- __applyCustomError() {
142
- const error = this.__errorMessage;
143
- if (error && error !== this.errorMessage) {
144
- this.errorMessage = error;
145
- delete this.__errorMessage;
146
- }
147
- }
148
-
149
116
  /** @private */
150
117
  __helperChanged(hasHelper, helperNode) {
151
118
  if (hasHelper) {
@@ -192,32 +159,11 @@ export const FieldMixin = (superclass) =>
192
159
  }
193
160
 
194
161
  /**
195
- * @param {boolean} invalid
196
162
  * @param {string | null | undefined} errorMessage
197
163
  * @protected
198
164
  */
199
- _updateErrorMessage(invalid, errorMessage) {
200
- const error = this._errorNode;
201
- if (!error) {
202
- return;
203
- }
204
-
205
- // save the custom error message content
206
- if (error.textContent && !errorMessage) {
207
- this.__errorMessage = error.textContent.trim();
208
- }
209
- const hasError = Boolean(invalid && errorMessage);
210
- error.textContent = hasError ? errorMessage : '';
211
- error.hidden = !hasError;
212
- this.toggleAttribute('has-error-message', hasError);
213
-
214
- // Role alert will make the error message announce immediately
215
- // as the field becomes invalid
216
- if (hasError) {
217
- error.setAttribute('role', 'alert');
218
- } else {
219
- error.removeAttribute('role');
220
- }
165
+ _errorMessageChanged(errorMessage) {
166
+ this._errorController.setErrorMessage(errorMessage);
221
167
  }
222
168
 
223
169
  /**
@@ -251,6 +197,8 @@ export const FieldMixin = (superclass) =>
251
197
  * @protected
252
198
  */
253
199
  _invalidChanged(invalid) {
200
+ this._errorController.setInvalid(invalid);
201
+
254
202
  // This timeout is needed to prevent NVDA from announcing the error message twice:
255
203
  // 1. Once adding the `[role=alert]` attribute by the `_updateErrorMessage` method (OK).
256
204
  // 2. Once linking the error ID with the ARIA target here (unwanted).
@@ -259,7 +207,7 @@ export const FieldMixin = (superclass) =>
259
207
  // Error message ID needs to be dynamically added / removed based on the validity
260
208
  // Otherwise assistive technologies would announce the error, even if we hide it.
261
209
  if (invalid) {
262
- this._fieldAriaController.setErrorId(this._errorId);
210
+ this._fieldAriaController.setErrorId(this._errorController.errorId);
263
211
  } else {
264
212
  this._fieldAriaController.setErrorId(null);
265
213
  }
@@ -107,9 +107,7 @@ export const InputControlMixin = (superclass) =>
107
107
  _onClearButtonClick(event) {
108
108
  event.preventDefault();
109
109
  this.inputElement.focus();
110
- this.clear();
111
- this.inputElement.dispatchEvent(new Event('input', { bubbles: true, composed: true }));
112
- this.inputElement.dispatchEvent(new Event('change', { bubbles: true }));
110
+ this.__clear();
113
111
  }
114
112
 
115
113
  /**
@@ -137,10 +135,8 @@ export const InputControlMixin = (superclass) =>
137
135
  _onKeyDown(event) {
138
136
  super._onKeyDown(event);
139
137
 
140
- if (event.key === 'Escape' && this.clearButtonVisible) {
141
- const dispatchChange = !!this.value;
142
- this.clear();
143
- dispatchChange && this.inputElement.dispatchEvent(new Event('change', { bubbles: true }));
138
+ if (event.key === 'Escape' && this.clearButtonVisible && !!this.value) {
139
+ this.__clear();
144
140
  }
145
141
  }
146
142
 
@@ -167,4 +163,11 @@ export const InputControlMixin = (superclass) =>
167
163
  })
168
164
  );
169
165
  }
166
+
167
+ /** @private */
168
+ __clear() {
169
+ this.clear();
170
+ this.inputElement.dispatchEvent(new Event('input', { bubbles: true, composed: true }));
171
+ this.inputElement.dispatchEvent(new Event('change', { bubbles: true }));
172
+ }
170
173
  };
@@ -41,6 +41,8 @@ export class LabelController extends SlotController {
41
41
  * @override
42
42
  */
43
43
  initCustomNode(labelNode) {
44
+ this.__updateLabelId(labelNode);
45
+
44
46
  const hasLabel = this.__hasLabel(labelNode);
45
47
  this.__toggleHasLabel(hasLabel);
46
48
  }
@@ -4,7 +4,6 @@
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { KeyboardMixin } from '@vaadin/component-base/src/keyboard-mixin.js';
7
- import { TabindexMixin } from '@vaadin/component-base/src/tabindex-mixin.js';
8
7
  import { DelegateFocusMixin } from './delegate-focus-mixin.js';
9
8
 
10
9
  /**
@@ -13,10 +12,23 @@ import { DelegateFocusMixin } from './delegate-focus-mixin.js';
13
12
  * @polymerMixin
14
13
  * @mixes DelegateFocusMixin
15
14
  * @mixes KeyboardMixin
16
- * @mixes TabindexMixin
17
15
  */
18
16
  export const ShadowFocusMixin = (superClass) =>
19
- class ShadowFocusMixinClass extends TabindexMixin(DelegateFocusMixin(KeyboardMixin(superClass))) {
17
+ class ShadowFocusMixinClass extends DelegateFocusMixin(KeyboardMixin(superClass)) {
18
+ static get properties() {
19
+ return {
20
+ /**
21
+ * Indicates whether the element can be focused and where it participates in sequential keyboard navigation.
22
+ *
23
+ * @protected
24
+ */
25
+ tabindex: {
26
+ type: Number,
27
+ value: 0
28
+ }
29
+ };
30
+ }
31
+
20
32
  /**
21
33
  * Override an event listener from `KeyboardMixin`
22
34
  * to prevent setting `focused` on Shift Tab.