@vaadin/field-base 25.2.0-alpha1 → 25.2.0-alpha11

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.
@@ -8,222 +8,221 @@ import { dedupeMixin } from '@open-wc/dedupe-mixin';
8
8
  /**
9
9
  * A mixin to store the reference to an input element
10
10
  * and add input and change event listeners to it.
11
- *
12
- * @polymerMixin
13
11
  */
14
- export const InputMixin = dedupeMixin(
15
- (superclass) =>
16
- class InputMixinClass extends superclass {
17
- static get properties() {
18
- return {
19
- /**
20
- * A reference to the input element controlled by the mixin.
21
- * Any component implementing this mixin is expected to provide it
22
- * by using `this._setInputElement(input)` API. A typical case is
23
- * using `InputController` that does this automatically.
24
- *
25
- * @protected
26
- * @type {!HTMLElement}
27
- */
28
- inputElement: {
29
- type: Object,
30
- readOnly: true,
31
- observer: '_inputElementChanged',
32
- sync: true,
33
- },
34
-
35
- /**
36
- * String used to define input type.
37
- * @protected
38
- */
39
- type: {
40
- type: String,
41
- readOnly: true,
42
- },
43
-
44
- /**
45
- * The value of the field.
46
- */
47
- value: {
48
- type: String,
49
- value: '',
50
- observer: '_valueChanged',
51
- notify: true,
52
- sync: true,
53
- },
54
- };
12
+ const InputMixinImplementation = (superclass) => {
13
+ return class InputMixinClass extends superclass {
14
+ static get properties() {
15
+ return {
16
+ /**
17
+ * A reference to the input element controlled by the mixin.
18
+ * Any component implementing this mixin is expected to provide it
19
+ * by using `this._setInputElement(input)` API. A typical case is
20
+ * using `InputController` that does this automatically.
21
+ *
22
+ * @protected
23
+ * @type {!HTMLElement}
24
+ */
25
+ inputElement: {
26
+ type: Object,
27
+ readOnly: true,
28
+ observer: '_inputElementChanged',
29
+ sync: true,
30
+ },
31
+
32
+ /**
33
+ * String used to define input type.
34
+ * @protected
35
+ */
36
+ type: {
37
+ type: String,
38
+ readOnly: true,
39
+ },
40
+
41
+ /**
42
+ * The value of the field.
43
+ */
44
+ value: {
45
+ type: String,
46
+ value: '',
47
+ observer: '_valueChanged',
48
+ notify: true,
49
+ sync: true,
50
+ },
51
+ };
52
+ }
53
+
54
+ constructor() {
55
+ super();
56
+
57
+ this._boundOnInput = this._onInput.bind(this);
58
+ this._boundOnChange = this._onChange.bind(this);
59
+ }
60
+
61
+ /**
62
+ * Indicates whether the value is different from the default one.
63
+ * Override if the `value` property has a type other than `string`.
64
+ *
65
+ * @protected
66
+ */
67
+ get _hasValue() {
68
+ return this.value != null && this.value !== '';
69
+ }
70
+
71
+ /**
72
+ * A property for accessing the input element's value.
73
+ *
74
+ * Override this getter if the property is different from the default `value` one.
75
+ *
76
+ * @protected
77
+ * @return {string}
78
+ */
79
+ get _inputElementValueProperty() {
80
+ return 'value';
81
+ }
82
+
83
+ /**
84
+ * The input element's value.
85
+ *
86
+ * @protected
87
+ * @return {string}
88
+ */
89
+ get _inputElementValue() {
90
+ return this.inputElement ? this.inputElement[this._inputElementValueProperty] : undefined;
91
+ }
92
+
93
+ /**
94
+ * The input element's value.
95
+ *
96
+ * @protected
97
+ */
98
+ set _inputElementValue(value) {
99
+ if (this.inputElement) {
100
+ this.inputElement[this._inputElementValueProperty] = value;
55
101
  }
56
-
57
- constructor() {
58
- super();
59
-
60
- this._boundOnInput = this._onInput.bind(this);
61
- this._boundOnChange = this._onChange.bind(this);
62
- }
63
-
64
- /**
65
- * Indicates whether the value is different from the default one.
66
- * Override if the `value` property has a type other than `string`.
67
- *
68
- * @protected
69
- */
70
- get _hasValue() {
71
- return this.value != null && this.value !== '';
72
- }
73
-
74
- /**
75
- * A property for accessing the input element's value.
76
- *
77
- * Override this getter if the property is different from the default `value` one.
78
- *
79
- * @protected
80
- * @return {string}
81
- */
82
- get _inputElementValueProperty() {
83
- return 'value';
84
- }
85
-
86
- /**
87
- * The input element's value.
88
- *
89
- * @protected
90
- * @return {string}
91
- */
92
- get _inputElementValue() {
93
- return this.inputElement ? this.inputElement[this._inputElementValueProperty] : undefined;
94
- }
95
-
96
- /**
97
- * The input element's value.
98
- *
99
- * @protected
100
- */
101
- set _inputElementValue(value) {
102
- if (this.inputElement) {
103
- this.inputElement[this._inputElementValueProperty] = value;
104
- }
102
+ }
103
+
104
+ /**
105
+ * Clear the value of the field.
106
+ */
107
+ clear() {
108
+ this.value = '';
109
+
110
+ // Clear the input immediately without waiting for the observer.
111
+ // Otherwise, when using Lit, the old value would be restored.
112
+ this._inputElementValue = '';
113
+ }
114
+
115
+ /**
116
+ * Add event listeners to the input element instance.
117
+ * Override this method to add custom listeners.
118
+ * @param {!HTMLElement} input
119
+ * @protected
120
+ */
121
+ _addInputListeners(input) {
122
+ input.addEventListener('input', this._boundOnInput);
123
+ input.addEventListener('change', this._boundOnChange);
124
+ }
125
+
126
+ /**
127
+ * Remove event listeners from the input element instance.
128
+ * @param {!HTMLElement} input
129
+ * @protected
130
+ */
131
+ _removeInputListeners(input) {
132
+ input.removeEventListener('input', this._boundOnInput);
133
+ input.removeEventListener('change', this._boundOnChange);
134
+ }
135
+
136
+ /**
137
+ * A method to forward the value property set on the field
138
+ * programmatically back to the input element value.
139
+ * Override this method to perform additional checks,
140
+ * for example to skip this in certain conditions.
141
+ * @param {string} value
142
+ * @protected
143
+ */
144
+ _forwardInputValue(value) {
145
+ // Value might be set before an input element is initialized.
146
+ // This case should be handled separately by a component that
147
+ // implements this mixin, for example in `connectedCallback`.
148
+ if (!this.inputElement) {
149
+ return;
105
150
  }
106
151
 
107
- /**
108
- * Clear the value of the field.
109
- */
110
- clear() {
111
- this.value = '';
112
-
113
- // Clear the input immediately without waiting for the observer.
114
- // Otherwise, when using Lit, the old value would be restored.
115
- this._inputElementValue = '';
116
- }
117
-
118
- /**
119
- * Add event listeners to the input element instance.
120
- * Override this method to add custom listeners.
121
- * @param {!HTMLElement} input
122
- * @protected
123
- */
124
- _addInputListeners(input) {
125
- input.addEventListener('input', this._boundOnInput);
126
- input.addEventListener('change', this._boundOnChange);
127
- }
128
-
129
- /**
130
- * Remove event listeners from the input element instance.
131
- * @param {!HTMLElement} input
132
- * @protected
133
- */
134
- _removeInputListeners(input) {
135
- input.removeEventListener('input', this._boundOnInput);
136
- input.removeEventListener('change', this._boundOnChange);
152
+ this._inputElementValue = value != null ? value : '';
153
+ }
154
+
155
+ /**
156
+ * @param {HTMLElement | undefined} input
157
+ * @param {HTMLElement | undefined} oldInput
158
+ * @protected
159
+ */
160
+ _inputElementChanged(input, oldInput) {
161
+ if (input) {
162
+ this._addInputListeners(input);
163
+ } else if (oldInput) {
164
+ this._removeInputListeners(oldInput);
137
165
  }
138
-
139
- /**
140
- * A method to forward the value property set on the field
141
- * programmatically back to the input element value.
142
- * Override this method to perform additional checks,
143
- * for example to skip this in certain conditions.
144
- * @param {string} value
145
- * @protected
146
- */
147
- _forwardInputValue(value) {
148
- // Value might be set before an input element is initialized.
149
- // This case should be handled separately by a component that
150
- // implements this mixin, for example in `connectedCallback`.
151
- if (!this.inputElement) {
152
- return;
153
- }
154
-
155
- this._inputElementValue = value != null ? value : '';
166
+ }
167
+
168
+ /**
169
+ * An input event listener used to update the field value.
170
+ *
171
+ * @param {Event} event
172
+ * @protected
173
+ */
174
+ _onInput(event) {
175
+ // In the case a custom web component is passed as `inputElement`,
176
+ // the actual native input element, on which the event occurred,
177
+ // can be inside shadow trees.
178
+ const target = event.composedPath()[0];
179
+ // Ignore fake input events e.g. used by clear button.
180
+ this.__userInput = event.isTrusted;
181
+ this.value = target.value;
182
+ this.__userInput = false;
183
+ }
184
+
185
+ /**
186
+ * A change event listener.
187
+ * Override this method with an actual implementation.
188
+ * @param {Event} _event
189
+ * @protected
190
+ */
191
+ _onChange(_event) {}
192
+
193
+ /**
194
+ * Toggle the has-value attribute based on the value property.
195
+ *
196
+ * @param {boolean} hasValue
197
+ * @protected
198
+ */
199
+ _toggleHasValue(hasValue) {
200
+ this.toggleAttribute('has-value', hasValue);
201
+ }
202
+
203
+ /**
204
+ * Observer called when a value property changes.
205
+ * @param {string | undefined} newVal
206
+ * @param {string | undefined} oldVal
207
+ * @protected
208
+ */
209
+ _valueChanged(newVal, oldVal) {
210
+ this._toggleHasValue(this._hasValue);
211
+
212
+ // Setting initial value to empty string, do nothing.
213
+ if (newVal === '' && oldVal === undefined) {
214
+ return;
156
215
  }
157
216
 
158
- /**
159
- * @param {HTMLElement | undefined} input
160
- * @param {HTMLElement | undefined} oldInput
161
- * @protected
162
- */
163
- _inputElementChanged(input, oldInput) {
164
- if (input) {
165
- this._addInputListeners(input);
166
- } else if (oldInput) {
167
- this._removeInputListeners(oldInput);
168
- }
217
+ // Value is set by the user, no need to sync it back to input.
218
+ if (this.__userInput) {
219
+ return;
169
220
  }
170
221
 
171
- /**
172
- * An input event listener used to update the field value.
173
- *
174
- * @param {Event} event
175
- * @protected
176
- */
177
- _onInput(event) {
178
- // In the case a custom web component is passed as `inputElement`,
179
- // the actual native input element, on which the event occurred,
180
- // can be inside shadow trees.
181
- const target = event.composedPath()[0];
182
- // Ignore fake input events e.g. used by clear button.
183
- this.__userInput = event.isTrusted;
184
- this.value = target.value;
185
- this.__userInput = false;
186
- }
222
+ // Setting a value programmatically, sync it to input element.
223
+ this._forwardInputValue(newVal);
224
+ }
225
+ };
226
+ };
187
227
 
188
- /**
189
- * A change event listener.
190
- * Override this method with an actual implementation.
191
- * @param {Event} _event
192
- * @protected
193
- */
194
- _onChange(_event) {}
195
-
196
- /**
197
- * Toggle the has-value attribute based on the value property.
198
- *
199
- * @param {boolean} hasValue
200
- * @protected
201
- */
202
- _toggleHasValue(hasValue) {
203
- this.toggleAttribute('has-value', hasValue);
204
- }
205
-
206
- /**
207
- * Observer called when a value property changes.
208
- * @param {string | undefined} newVal
209
- * @param {string | undefined} oldVal
210
- * @protected
211
- */
212
- _valueChanged(newVal, oldVal) {
213
- this._toggleHasValue(this._hasValue);
214
-
215
- // Setting initial value to empty string, do nothing.
216
- if (newVal === '' && oldVal === undefined) {
217
- return;
218
- }
219
-
220
- // Value is set by the user, no need to sync it back to input.
221
- if (this.__userInput) {
222
- return;
223
- }
224
-
225
- // Setting a value programmatically, sync it to input element.
226
- this._forwardInputValue(newVal);
227
- }
228
- },
229
- );
228
+ export const InputMixin = dedupeMixin(InputMixinImplementation);
@@ -9,6 +9,8 @@ import { SlotChildObserveController } from '@vaadin/component-base/src/slot-chil
9
9
  * A controller to manage the label element.
10
10
  */
11
11
  export class LabelController extends SlotChildObserveController {
12
+ constructor(host: HTMLElement);
13
+
12
14
  /**
13
15
  * String used for the label.
14
16
  */
@@ -7,8 +7,6 @@ import { LabelController } from './label-controller.js';
7
7
 
8
8
  /**
9
9
  * A mixin to provide label via corresponding property or named slot.
10
- *
11
- * @polymerMixin
12
10
  */
13
11
  export const LabelMixin = (superclass) =>
14
12
  class LabelMixinClass extends superclass {
@@ -38,7 +36,7 @@ export const LabelMixin = (superclass) =>
38
36
  /** @protected */
39
37
  get _labelId() {
40
38
  const node = this._labelNode;
41
- return node && node.id;
39
+ return node?.id;
42
40
  }
43
41
 
44
42
  /** @protected */
@@ -4,8 +4,24 @@
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import type { ReactiveController } from 'lit';
7
+ import type { LabelController } from './label-controller.js';
7
8
 
8
9
  /**
9
10
  * A controller for linking a `<label>` element with an `<input>` element.
10
11
  */
11
- export class LabelledInputController implements ReactiveController {}
12
+ export class LabelledInputController implements ReactiveController {
13
+ constructor(input: HTMLInputElement, labelController: LabelController);
14
+
15
+ /**
16
+ * The input element to link with the label.
17
+ */
18
+ input: HTMLInputElement;
19
+
20
+ /**
21
+ * Phantom optional method to satisfy the `ReactiveController` interface
22
+ * (TS2559: an interface with all-optional members needs at least one
23
+ * declared property in common). Not implemented at runtime; Lit's
24
+ * `addController` invokes it via optional chaining.
25
+ */
26
+ hostConnected?(): void;
27
+ }
@@ -7,9 +7,6 @@ import { InputConstraintsMixin } from './input-constraints-mixin.js';
7
7
 
8
8
  /**
9
9
  * A mixin to provide `pattern` property.
10
- *
11
- * @polymerMixin
12
- * @mixes InputConstraintsMixin
13
10
  */
14
11
  export const PatternMixin = (superclass) =>
15
12
  class PatternMixinClass extends InputConstraintsMixin(superclass) {
@@ -15,8 +15,8 @@ export const button = css`
15
15
  -webkit-user-select: none;
16
16
  user-select: none;
17
17
  /* Ensure minimum click target (WCAG) */
18
- padding: max(0px, (24px - 1lh) / 2);
19
- margin: min(0px, (24px - 1lh) / -2);
18
+ padding: max(0px, (24px - var(--vaadin-icon-size, 1lh)) / 2);
19
+ margin: min(0px, (24px - var(--vaadin-icon-size, 1lh)) / -2);
20
20
  }
21
21
 
22
22
  /* Icon */
@@ -8,4 +8,6 @@ import { SlotController } from '@vaadin/component-base/src/slot-controller.js';
8
8
  /**
9
9
  * A controller to create and initialize slotted `<textarea>` element.
10
10
  */
11
- export class TextAreaController extends SlotController {}
11
+ export class TextAreaController extends SlotController {
12
+ constructor(host: HTMLElement, callback?: (node: HTMLTextAreaElement) => void);
13
+ }