@vaadin/custom-field 24.2.0-dev.f254716fe → 24.3.0-alpha1

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.
@@ -3,34 +3,14 @@
3
3
  * Copyright (c) 2019 - 2023 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
- import { FlattenedNodesObserver } from '@polymer/polymer/lib/utils/flattened-nodes-observer.js';
7
6
  import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
8
- import { FocusMixin } from '@vaadin/a11y-base/src/focus-mixin.js';
9
- import { KeyboardMixin } from '@vaadin/a11y-base/src/keyboard-mixin.js';
7
+ import { defineCustomElement } from '@vaadin/component-base/src/define.js';
10
8
  import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
11
- import { TooltipController } from '@vaadin/component-base/src/tooltip-controller.js';
12
- import { FieldMixin } from '@vaadin/field-base/src/field-mixin.js';
13
- import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
9
+ import { registerStyles, ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
10
+ import { CustomFieldMixin } from './vaadin-custom-field-mixin.js';
11
+ import { customFieldStyles } from './vaadin-custom-field-styles.js';
14
12
 
15
- /**
16
- * Default implementation of the parse function that creates individual field
17
- * values from the single component value.
18
- * @param value
19
- * @returns {*}
20
- */
21
- const defaultParseValue = (value) => {
22
- return value.split('\t');
23
- };
24
-
25
- /**
26
- * Default implementation of the format function that creates a single component
27
- * value from individual field values.
28
- * @param inputValues
29
- * @returns {*}
30
- */
31
- const defaultFormatValue = (inputValues) => {
32
- return inputValues.join('\t');
33
- };
13
+ registerStyles('vaadin-custom-field', customFieldStyles, { moduleId: 'vaadin-custom-field-styles' });
34
14
 
35
15
  /**
36
16
  * `<vaadin-custom-field>` is a web component for wrapping multiple components as a single field.
@@ -71,59 +51,31 @@ const defaultFormatValue = (inputValues) => {
71
51
  *
72
52
  * @fires {Event} change - Fired when the user commits a value change for any of the internal inputs.
73
53
  * @fires {Event} internal-tab - Fired on Tab keydown triggered from the internal inputs, meaning focus will not leave the inputs.
54
+ * @fires {CustomEvent} dirty-changed - Fired when the `dirty` property changes.
74
55
  * @fires {CustomEvent} invalid-changed - Fired when the `invalid` property changes.
75
56
  * @fires {CustomEvent} value-changed - Fired when the `value` property changes.
76
57
  * @fires {CustomEvent} validated - Fired whenever the field is validated.
77
58
  *
59
+ * @customElement
78
60
  * @extends HTMLElement
79
- * @mixes FieldMixin
80
- * @mixes FocusMixin
61
+ * @mixes CustomFieldMixin
81
62
  * @mixes ElementMixin
82
- * @mixes KeyboardMixin
83
63
  * @mixes ThemableMixin
84
64
  */
85
- class CustomField extends FieldMixin(FocusMixin(KeyboardMixin(ThemableMixin(ElementMixin(PolymerElement))))) {
65
+ class CustomField extends CustomFieldMixin(ThemableMixin(ElementMixin(PolymerElement))) {
86
66
  static get is() {
87
67
  return 'vaadin-custom-field';
88
68
  }
89
69
 
90
70
  static get template() {
91
71
  return html`
92
- <style>
93
- :host {
94
- display: inline-flex;
95
- }
96
-
97
- :host::before {
98
- content: '\\2003';
99
- width: 0;
100
- display: inline-block;
101
- /* Size and position this element on the same vertical position as the input-field element
102
- to make vertical align for the host element work as expected */
103
- }
104
-
105
- :host([hidden]) {
106
- display: none !important;
107
- }
108
-
109
- .vaadin-custom-field-container {
110
- width: 100%;
111
- display: flex;
112
- flex-direction: column;
113
- }
114
-
115
- .inputs-wrapper {
116
- flex: none;
117
- }
118
- </style>
119
-
120
72
  <div class="vaadin-custom-field-container">
121
73
  <div part="label" on-click="focus">
122
74
  <slot name="label"></slot>
123
75
  <span part="required-indicator" aria-hidden="true"></span>
124
76
  </div>
125
77
 
126
- <div class="inputs-wrapper" on-change="__onInputChange">
78
+ <div class="inputs-wrapper" on-change="_onChange" on-input="_onInput">
127
79
  <slot id="slot"></slot>
128
80
  </div>
129
81
 
@@ -140,272 +92,6 @@ class CustomField extends FieldMixin(FocusMixin(KeyboardMixin(ThemableMixin(Elem
140
92
  `;
141
93
  }
142
94
 
143
- static get properties() {
144
- return {
145
- /**
146
- * The name of the control, which is submitted with the form data.
147
- */
148
- name: String,
149
-
150
- /**
151
- * The value of the field. When wrapping several inputs, it will contain `\t`
152
- * (Tab character) as a delimiter indicating parts intended to be used as the
153
- * corresponding inputs values.
154
- * Use the [`formatValue`](#/elements/vaadin-custom-field#property-formatValue)
155
- * and [`parseValue`](#/elements/vaadin-custom-field#property-parseValue)
156
- * properties to customize this behavior.
157
- */
158
- value: {
159
- type: String,
160
- observer: '__valueChanged',
161
- notify: true,
162
- },
163
-
164
- /**
165
- * Array of available input nodes
166
- * @type {!Array<!HTMLElement> | undefined}
167
- */
168
- inputs: {
169
- type: Array,
170
- readOnly: true,
171
- },
172
-
173
- /**
174
- * A function to format the values of the individual fields contained by
175
- * the custom field into a single component value. The function receives
176
- * an array of all values of the individual fields in the order of their
177
- * presence in the DOM, and must return a single component value.
178
- * This function is called each time a value of an internal field is
179
- * changed.
180
- *
181
- * Example:
182
- * ```js
183
- * customField.formatValue = (fieldValues) => {
184
- * return fieldValues.join("-");
185
- * }
186
- * ```
187
- * @type {!CustomFieldFormatValueFn | undefined}
188
- */
189
- formatValue: {
190
- type: Function,
191
- },
192
-
193
- /**
194
- * A function to parse the component value into values for the individual
195
- * fields contained by the custom field. The function receives the
196
- * component value, and must return an array of values for the individual
197
- * fields in the order of their presence in the DOM.
198
- * The function is called each time the value of the component changes.
199
- *
200
- * Example:
201
- * ```js
202
- * customField.parseValue = (componentValue) => {
203
- * return componentValue.split("-");
204
- * }
205
- * ```
206
- * @type {!CustomFieldParseValueFn | undefined}
207
- */
208
- parseValue: {
209
- type: Function,
210
- },
211
- };
212
- }
213
-
214
- /** @protected */
215
- connectedCallback() {
216
- super.connectedCallback();
217
-
218
- if (this.__observer) {
219
- this.__observer.connect();
220
- }
221
- }
222
-
223
- /** @protected */
224
- disconnectedCallback() {
225
- super.disconnectedCallback();
226
-
227
- if (this.__observer) {
228
- this.__observer.disconnect();
229
- }
230
- }
231
-
232
- /** @protected */
233
- ready() {
234
- super.ready();
235
-
236
- // See https://github.com/vaadin/vaadin-web-components/issues/94
237
- this.setAttribute('role', 'group');
238
-
239
- this.ariaTarget = this;
240
-
241
- this.__setInputsFromSlot();
242
- this.__observer = new FlattenedNodesObserver(this.$.slot, () => {
243
- this.__setInputsFromSlot();
244
- });
245
-
246
- this._tooltipController = new TooltipController(this);
247
- this.addController(this._tooltipController);
248
- this._tooltipController.setShouldShow((target) => {
249
- const inputs = target.inputs || [];
250
- return !inputs.some((el) => el.opened);
251
- });
252
- }
253
-
254
- /** @protected */
255
- focus() {
256
- if (this.inputs && this.inputs[0]) {
257
- this.inputs[0].focus();
258
- }
259
- }
260
-
261
- /**
262
- * Override method inherited from `FocusMixin` to validate on blur.
263
- * @param {boolean} focused
264
- * @protected
265
- */
266
- _setFocused(focused) {
267
- super._setFocused(focused);
268
-
269
- if (!focused) {
270
- this.validate();
271
- }
272
- }
273
-
274
- /**
275
- * Override method inherited from `FocusMixin` to not remove focused
276
- * state when focus moves to another input in the custom field.
277
- * @param {FocusEvent} event
278
- * @return {boolean}
279
- * @protected
280
- */
281
- _shouldRemoveFocus(event) {
282
- const { relatedTarget } = event;
283
- return !this.inputs.some((el) => relatedTarget === (el.focusElement || el));
284
- }
285
-
286
- /**
287
- * Returns true if the current inputs values satisfy all constraints (if any).
288
- *
289
- * @return {boolean}
290
- */
291
- checkValidity() {
292
- const invalidFields = this.inputs.filter((input) => !(input.validate || input.checkValidity).call(input));
293
-
294
- if (invalidFields.length || (this.required && !this.value.trim())) {
295
- // Either 1. one of the input fields is invalid or
296
- // 2. the custom field itself is required but doesn't have a value
297
- return false;
298
- }
299
- return true;
300
- }
301
-
302
- /**
303
- * @param {KeyboardEvent} e
304
- * @protected
305
- * @override
306
- */
307
- _onKeyDown(e) {
308
- if (e.key === 'Tab') {
309
- if (
310
- (this.inputs.indexOf(e.target) < this.inputs.length - 1 && !e.shiftKey) ||
311
- (this.inputs.indexOf(e.target) > 0 && e.shiftKey)
312
- ) {
313
- this.dispatchEvent(new CustomEvent('internal-tab'));
314
- } else {
315
- // FIXME(yuriy): remove this workaround when value should not be updated before focusout
316
- this.__setValue();
317
- }
318
- }
319
- }
320
-
321
- /** @private */
322
- __onInputChange(event) {
323
- // Stop native change events
324
- event.stopPropagation();
325
-
326
- this.__setValue();
327
- this.validate();
328
- this.dispatchEvent(
329
- new CustomEvent('change', {
330
- bubbles: true,
331
- cancelable: false,
332
- detail: {
333
- value: this.value,
334
- },
335
- }),
336
- );
337
- }
338
-
339
- /** @private */
340
- __setValue() {
341
- this.__settingValue = true;
342
- const formatFn = this.formatValue || defaultFormatValue;
343
- this.value = formatFn.apply(this, [this.inputs.map((input) => input.value)]);
344
- this.__settingValue = false;
345
- }
346
-
347
- /**
348
- * Like querySelectorAll('*') but also gets all elements through any nested slots recursively
349
- * @private
350
- */
351
- __queryAllAssignedElements(elem) {
352
- const result = [];
353
- let elements;
354
- if (elem.tagName === 'SLOT') {
355
- elements = elem.assignedElements({ flatten: true });
356
- } else {
357
- result.push(elem);
358
- elements = Array.from(elem.children);
359
- }
360
- elements.forEach((elem) => result.push(...this.__queryAllAssignedElements(elem)));
361
- return result;
362
- }
363
-
364
- /** @private */
365
- __isInput(node) {
366
- const isSlottedInput = node.getAttribute('slot') === 'input' || node.getAttribute('slot') === 'textarea';
367
- return !isSlottedInput && (node.validate || node.checkValidity);
368
- }
369
-
370
- /** @private */
371
- __getInputsFromSlot() {
372
- return this.__queryAllAssignedElements(this.$.slot).filter((node) => this.__isInput(node));
373
- }
374
-
375
- /** @private */
376
- __setInputsFromSlot() {
377
- this._setInputs(this.__getInputsFromSlot());
378
- this.__setValue();
379
- }
380
-
381
- /** @private */
382
- __toggleHasValue(value) {
383
- this.toggleAttribute('has-value', value !== null && value.trim() !== '');
384
- }
385
-
386
- /** @private */
387
- __valueChanged(value, oldValue) {
388
- if (this.__settingValue || !this.inputs) {
389
- return;
390
- }
391
-
392
- this.__toggleHasValue(value);
393
-
394
- const parseFn = this.parseValue || defaultParseValue;
395
- const valuesArray = parseFn.apply(this, [value]);
396
- if (!valuesArray || valuesArray.length === 0) {
397
- console.warn('Value parser has not provided values array');
398
- return;
399
- }
400
-
401
- this.inputs.forEach((input, id) => {
402
- input.value = valuesArray[id];
403
- });
404
- if (oldValue !== undefined) {
405
- this.validate();
406
- }
407
- }
408
-
409
95
  /**
410
96
  * Fired when the user commits a value change for any of the internal inputs.
411
97
  *
@@ -413,6 +99,6 @@ class CustomField extends FieldMixin(FocusMixin(KeyboardMixin(ThemableMixin(Elem
413
99
  */
414
100
  }
415
101
 
416
- customElements.define(CustomField.is, CustomField);
102
+ defineCustomElement(CustomField);
417
103
 
418
104
  export { CustomField };
package/web-types.json ADDED
@@ -0,0 +1,295 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/web-types",
3
+ "name": "@vaadin/custom-field",
4
+ "version": "24.3.0-alpha1",
5
+ "description-markup": "markdown",
6
+ "contributions": {
7
+ "html": {
8
+ "elements": [
9
+ {
10
+ "name": "vaadin-custom-field",
11
+ "description": "`<vaadin-custom-field>` is a web component for wrapping multiple components as a single field.\n\n```\n<vaadin-custom-field label=\"Appointment time\">\n <vaadin-date-picker></vaadin-date-picker>\n <vaadin-time-picker></vaadin-time-picker>\n</vaadin-custom-field>\n```\n\n### Styling\n\nThe following shadow DOM parts are available for styling:\n\nPart name | Description\n---------------------|----------------\n`label` | The slotted label element wrapper\n`helper-text` | The slotted helper text element wrapper\n`error-message` | The slotted error message element wrapper\n`required-indicator` | The `required` state indicator element\n\nThe following state attributes are available for styling:\n\nAttribute | Description | Part name\n--------------------|-------------------------------------------|------------\n`invalid` | Set when the element is invalid | :host\n`focused` | Set when the element is focused | :host\n`has-label` | Set when the element has a label | :host\n`has-value` | Set when the element has a value | :host\n`has-helper` | Set when the element has helper text | :host\n`has-error-message` | Set when the element has an error message | :host\n\nYou may also manually set `disabled` or `readonly` attribute on this component to make the label\npart look visually the same as on a `<vaadin-text-field>` when it is disabled or readonly.\n\nSee [Styling Components](https://vaadin.com/docs/latest/styling/styling-components) documentation.",
12
+ "attributes": [
13
+ {
14
+ "name": "label",
15
+ "description": "The label text for the input node.\nWhen no light dom defined via [slot=label], this value will be used.",
16
+ "value": {
17
+ "type": [
18
+ "string",
19
+ "null",
20
+ "undefined"
21
+ ]
22
+ }
23
+ },
24
+ {
25
+ "name": "invalid",
26
+ "description": "Set to true when the field is invalid.",
27
+ "value": {
28
+ "type": [
29
+ "boolean",
30
+ "null",
31
+ "undefined"
32
+ ]
33
+ }
34
+ },
35
+ {
36
+ "name": "required",
37
+ "description": "Specifies that the user must fill in a value.",
38
+ "value": {
39
+ "type": [
40
+ "boolean",
41
+ "null",
42
+ "undefined"
43
+ ]
44
+ }
45
+ },
46
+ {
47
+ "name": "error-message",
48
+ "description": "Error to show when the field is invalid.",
49
+ "value": {
50
+ "type": [
51
+ "string",
52
+ "null",
53
+ "undefined"
54
+ ]
55
+ }
56
+ },
57
+ {
58
+ "name": "helper-text",
59
+ "description": "String used for the helper text.",
60
+ "value": {
61
+ "type": [
62
+ "string",
63
+ "null",
64
+ "undefined"
65
+ ]
66
+ }
67
+ },
68
+ {
69
+ "name": "accessible-name",
70
+ "description": "String used to label the component to screen reader users.",
71
+ "value": {
72
+ "type": [
73
+ "string",
74
+ "null",
75
+ "undefined"
76
+ ]
77
+ }
78
+ },
79
+ {
80
+ "name": "accessible-name-ref",
81
+ "description": "Id of the element used as label of the component to screen reader users.",
82
+ "value": {
83
+ "type": [
84
+ "string",
85
+ "null",
86
+ "undefined"
87
+ ]
88
+ }
89
+ },
90
+ {
91
+ "name": "name",
92
+ "description": "The name of the control, which is submitted with the form data.",
93
+ "value": {
94
+ "type": [
95
+ "string",
96
+ "null",
97
+ "undefined"
98
+ ]
99
+ }
100
+ },
101
+ {
102
+ "name": "value",
103
+ "description": "The value of the field. When wrapping several inputs, it will contain `\\t`\n(Tab character) as a delimiter indicating parts intended to be used as the\ncorresponding inputs values.\nUse the [`formatValue`](https://cdn.vaadin.com/vaadin-web-components/24.3.0-alpha1/#/elements/vaadin-custom-field#property-formatValue)\nand [`parseValue`](https://cdn.vaadin.com/vaadin-web-components/24.3.0-alpha1/#/elements/vaadin-custom-field#property-parseValue)\nproperties to customize this behavior.",
104
+ "value": {
105
+ "type": [
106
+ "string",
107
+ "null",
108
+ "undefined"
109
+ ]
110
+ }
111
+ },
112
+ {
113
+ "name": "dirty",
114
+ "description": "Whether the field is dirty.\n\nThe field is automatically marked as dirty once the user triggers\nan `input` or `change` event. Additionally, the field can be manually\nmarked as dirty by setting the property to `true`.",
115
+ "value": {
116
+ "type": [
117
+ "boolean",
118
+ "null",
119
+ "undefined"
120
+ ]
121
+ }
122
+ },
123
+ {
124
+ "name": "theme",
125
+ "description": "The theme variants to apply to the component.",
126
+ "value": {
127
+ "type": [
128
+ "string",
129
+ "null",
130
+ "undefined"
131
+ ]
132
+ }
133
+ }
134
+ ],
135
+ "js": {
136
+ "properties": [
137
+ {
138
+ "name": "label",
139
+ "description": "The label text for the input node.\nWhen no light dom defined via [slot=label], this value will be used.",
140
+ "value": {
141
+ "type": [
142
+ "string",
143
+ "null",
144
+ "undefined"
145
+ ]
146
+ }
147
+ },
148
+ {
149
+ "name": "invalid",
150
+ "description": "Set to true when the field is invalid.",
151
+ "value": {
152
+ "type": [
153
+ "boolean",
154
+ "null",
155
+ "undefined"
156
+ ]
157
+ }
158
+ },
159
+ {
160
+ "name": "required",
161
+ "description": "Specifies that the user must fill in a value.",
162
+ "value": {
163
+ "type": [
164
+ "boolean",
165
+ "null",
166
+ "undefined"
167
+ ]
168
+ }
169
+ },
170
+ {
171
+ "name": "errorMessage",
172
+ "description": "Error to show when the field is invalid.",
173
+ "value": {
174
+ "type": [
175
+ "string",
176
+ "null",
177
+ "undefined"
178
+ ]
179
+ }
180
+ },
181
+ {
182
+ "name": "helperText",
183
+ "description": "String used for the helper text.",
184
+ "value": {
185
+ "type": [
186
+ "string",
187
+ "null",
188
+ "undefined"
189
+ ]
190
+ }
191
+ },
192
+ {
193
+ "name": "accessibleName",
194
+ "description": "String used to label the component to screen reader users.",
195
+ "value": {
196
+ "type": [
197
+ "string",
198
+ "null",
199
+ "undefined"
200
+ ]
201
+ }
202
+ },
203
+ {
204
+ "name": "accessibleNameRef",
205
+ "description": "Id of the element used as label of the component to screen reader users.",
206
+ "value": {
207
+ "type": [
208
+ "string",
209
+ "null",
210
+ "undefined"
211
+ ]
212
+ }
213
+ },
214
+ {
215
+ "name": "name",
216
+ "description": "The name of the control, which is submitted with the form data.",
217
+ "value": {
218
+ "type": [
219
+ "string",
220
+ "null",
221
+ "undefined"
222
+ ]
223
+ }
224
+ },
225
+ {
226
+ "name": "value",
227
+ "description": "The value of the field. When wrapping several inputs, it will contain `\\t`\n(Tab character) as a delimiter indicating parts intended to be used as the\ncorresponding inputs values.\nUse the [`formatValue`](https://cdn.vaadin.com/vaadin-web-components/24.3.0-alpha1/#/elements/vaadin-custom-field#property-formatValue)\nand [`parseValue`](https://cdn.vaadin.com/vaadin-web-components/24.3.0-alpha1/#/elements/vaadin-custom-field#property-parseValue)\nproperties to customize this behavior.",
228
+ "value": {
229
+ "type": [
230
+ "string",
231
+ "null",
232
+ "undefined"
233
+ ]
234
+ }
235
+ },
236
+ {
237
+ "name": "formatValue",
238
+ "description": "A function to format the values of the individual fields contained by\nthe custom field into a single component value. The function receives\nan array of all values of the individual fields in the order of their\npresence in the DOM, and must return a single component value.\nThis function is called each time a value of an internal field is\nchanged.\n\nExample:\n```js\ncustomField.formatValue = (fieldValues) => {\n return fieldValues.join(\"-\");\n}\n```",
239
+ "value": {
240
+ "type": [
241
+ "CustomFieldFormatValueFn",
242
+ "undefined"
243
+ ]
244
+ }
245
+ },
246
+ {
247
+ "name": "parseValue",
248
+ "description": "A function to parse the component value into values for the individual\nfields contained by the custom field. The function receives the\ncomponent value, and must return an array of values for the individual\nfields in the order of their presence in the DOM.\nThe function is called each time the value of the component changes.\n\nExample:\n```js\ncustomField.parseValue = (componentValue) => {\n return componentValue.split(\"-\");\n}\n```",
249
+ "value": {
250
+ "type": [
251
+ "CustomFieldParseValueFn",
252
+ "undefined"
253
+ ]
254
+ }
255
+ },
256
+ {
257
+ "name": "dirty",
258
+ "description": "Whether the field is dirty.\n\nThe field is automatically marked as dirty once the user triggers\nan `input` or `change` event. Additionally, the field can be manually\nmarked as dirty by setting the property to `true`.",
259
+ "value": {
260
+ "type": [
261
+ "boolean",
262
+ "null",
263
+ "undefined"
264
+ ]
265
+ }
266
+ }
267
+ ],
268
+ "events": [
269
+ {
270
+ "name": "validated",
271
+ "description": "Fired whenever the field is validated."
272
+ },
273
+ {
274
+ "name": "change",
275
+ "description": "Fired when the user commits a value change for any of the internal inputs."
276
+ },
277
+ {
278
+ "name": "invalid-changed",
279
+ "description": "Fired when the `invalid` property changes."
280
+ },
281
+ {
282
+ "name": "value-changed",
283
+ "description": "Fired when the `value` property changes."
284
+ },
285
+ {
286
+ "name": "dirty-changed",
287
+ "description": "Fired when the `dirty` property changes."
288
+ }
289
+ ]
290
+ }
291
+ }
292
+ ]
293
+ }
294
+ }
295
+ }