@vaadin/field-base 22.0.0-alpha5 → 22.0.0-alpha9

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.
Files changed (77) hide show
  1. package/index.d.ts +7 -15
  2. package/index.js +7 -15
  3. package/package.json +24 -18
  4. package/src/aria-label-controller.d.ts +11 -0
  5. package/src/aria-label-controller.js +53 -0
  6. package/src/checked-mixin.d.ts +21 -0
  7. package/src/checked-mixin.js +54 -0
  8. package/src/delegate-focus-mixin.d.ts +2 -2
  9. package/src/delegate-focus-mixin.js +150 -133
  10. package/src/delegate-state-mixin.d.ts +1 -1
  11. package/src/delegate-state-mixin.js +100 -88
  12. package/src/field-mixin.d.ts +39 -0
  13. package/src/field-mixin.js +317 -0
  14. package/src/input-constraints-mixin.d.ts +4 -2
  15. package/src/input-constraints-mixin.js +107 -64
  16. package/src/input-control-mixin.d.ts +52 -0
  17. package/src/input-control-mixin.js +170 -0
  18. package/src/input-controller.d.ts +11 -0
  19. package/src/input-controller.js +35 -0
  20. package/src/input-field-mixin.d.ts +2 -16
  21. package/src/input-field-mixin.js +125 -80
  22. package/src/input-mixin.d.ts +1 -1
  23. package/src/input-mixin.js +156 -145
  24. package/src/label-mixin.d.ts +2 -2
  25. package/src/label-mixin.js +73 -60
  26. package/src/pattern-mixin.js +9 -9
  27. package/src/shadow-focus-mixin.d.ts +21 -0
  28. package/src/shadow-focus-mixin.js +87 -0
  29. package/src/slot-controller.d.ts +8 -0
  30. package/src/slot-controller.js +36 -0
  31. package/src/slot-label-mixin.d.ts +20 -0
  32. package/src/slot-label-mixin.js +38 -0
  33. package/src/slot-styles-mixin.js +38 -31
  34. package/src/slot-target-mixin.d.ts +32 -0
  35. package/src/slot-target-mixin.js +110 -0
  36. package/src/styles/clear-button-styles.d.ts +8 -0
  37. package/src/styles/clear-button-styles.js +21 -0
  38. package/src/styles/field-shared-styles.d.ts +8 -0
  39. package/src/styles/field-shared-styles.js +29 -0
  40. package/src/styles/input-field-container-styles.d.ts +8 -0
  41. package/src/styles/input-field-container-styles.js +16 -0
  42. package/src/styles/input-field-shared-styles.d.ts +8 -0
  43. package/src/styles/input-field-shared-styles.js +10 -0
  44. package/src/text-area-controller.d.ts +11 -0
  45. package/src/text-area-controller.js +38 -0
  46. package/src/validate-mixin.d.ts +1 -9
  47. package/src/validate-mixin.js +43 -118
  48. package/src/active-mixin.d.ts +0 -26
  49. package/src/active-mixin.js +0 -106
  50. package/src/aria-label-mixin.d.ts +0 -20
  51. package/src/aria-label-mixin.js +0 -71
  52. package/src/char-length-mixin.d.ts +0 -30
  53. package/src/char-length-mixin.js +0 -42
  54. package/src/clear-button-mixin.d.ts +0 -28
  55. package/src/clear-button-mixin.js +0 -82
  56. package/src/delegate-input-state-mixin.d.ts +0 -43
  57. package/src/delegate-input-state-mixin.js +0 -63
  58. package/src/disabled-mixin.d.ts +0 -23
  59. package/src/disabled-mixin.js +0 -60
  60. package/src/field-aria-mixin.d.ts +0 -24
  61. package/src/field-aria-mixin.js +0 -61
  62. package/src/focus-mixin.d.ts +0 -33
  63. package/src/focus-mixin.js +0 -104
  64. package/src/helper-text-mixin.d.ts +0 -24
  65. package/src/helper-text-mixin.js +0 -144
  66. package/src/input-slot-mixin.d.ts +0 -26
  67. package/src/input-slot-mixin.js +0 -71
  68. package/src/keyboard-mixin.d.ts +0 -32
  69. package/src/keyboard-mixin.js +0 -51
  70. package/src/slot-mixin.d.ts +0 -23
  71. package/src/slot-mixin.js +0 -49
  72. package/src/tabindex-mixin.d.ts +0 -29
  73. package/src/tabindex-mixin.js +0 -78
  74. package/src/text-area-slot-mixin.d.ts +0 -21
  75. package/src/text-area-slot-mixin.js +0 -56
  76. package/src/text-field-mixin.d.ts +0 -21
  77. package/src/text-field-mixin.js +0 -17
@@ -4,80 +4,123 @@
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 { DelegateInputStateMixin } from './delegate-input-state-mixin.js';
7
+ import { DelegateStateMixin } from './delegate-state-mixin.js';
8
+ import { InputMixin } from './input-mixin.js';
9
+ import { ValidateMixin } from './validate-mixin.js';
8
10
 
9
- const InputConstraintsMixinImplementation = (superclass) =>
10
- class InputConstraintsMixinClass extends DelegateInputStateMixin(superclass) {
11
- /**
12
- * An array of attributes which participate in the input validation.
13
- * Changing these attributes will cause the input to re-validate.
14
- *
15
- * IMPORTANT: The attributes should be properly delegated to the input element from the host
16
- * which can be achieved by listing them in the list of delegated attributes (see `DelegateStateMixin.delegateAttrs`).
17
- * Otherwise, the input validation will not work correctly.
18
- *
19
- * Note, the `required` attribute is already delegated in the parent `DelegateInputStateMixin`.
20
- */
21
- static get constraints() {
22
- return ['required'];
23
- }
11
+ /**
12
+ * A mixin to combine multiple input validation constraints.
13
+ *
14
+ * @polymerMixin
15
+ * @mixes DelegateStateMixin
16
+ * @mixes InputMixin
17
+ * @mixes ValidateMixin
18
+ */
19
+ export const InputConstraintsMixin = dedupingMixin(
20
+ (superclass) =>
21
+ class InputConstraintsMixinClass extends DelegateStateMixin(ValidateMixin(InputMixin(superclass))) {
22
+ /**
23
+ * An array of attributes which participate in the input validation.
24
+ * Changing these attributes will cause the input to re-validate.
25
+ *
26
+ * IMPORTANT: The attributes should be properly delegated to the input element
27
+ * from the host using `delegateAttrs` getter (see `DelegateStateMixin`).
28
+ * The `required` attribute is already delegated.
29
+ */
30
+ static get constraints() {
31
+ return ['required'];
32
+ }
33
+
34
+ static get delegateAttrs() {
35
+ return [...super.delegateAttrs, 'required'];
36
+ }
24
37
 
25
- /** @protected */
26
- ready() {
27
- super.ready();
38
+ /** @protected */
39
+ ready() {
40
+ super.ready();
28
41
 
29
- this._createConstraintsObserver();
30
- }
42
+ this._createConstraintsObserver();
43
+ }
31
44
 
32
- /**
33
- * Returns true if the current input value satisfies all constraints (if any).
34
- * @return {boolean}
35
- */
36
- checkValidity() {
37
- if (this.inputElement && this.constructor.constraints.some((c) => this.__isValidConstraint(this[c]))) {
38
- return this.inputElement.checkValidity();
39
- } else {
40
- return !this.invalid;
45
+ /**
46
+ * Returns true if the current input value satisfies all constraints (if any).
47
+ * @return {boolean}
48
+ */
49
+ checkValidity() {
50
+ if (this.inputElement && this._hasValidConstraints(this.constructor.constraints.map((c) => this[c]))) {
51
+ return this.inputElement.checkValidity();
52
+ } else {
53
+ return !this.invalid;
54
+ }
41
55
  }
42
- }
43
56
 
44
- /**
45
- * Override this method to customize setting up constraints observer.
46
- * @protected
47
- */
48
- _createConstraintsObserver() {
49
- // This complex observer needs to be added dynamically instead of using `static get observers()`
50
- // to make it possible to tweak this behavior in classes that apply this mixin.
51
- this._createMethodObserver(`_constraintsChanged(${this.constructor.constraints.join(', ')})`);
52
- }
57
+ /**
58
+ * Returns true if some of the provided set of constraints are valid.
59
+ * @param {Array} constraints
60
+ * @return {boolean}
61
+ * @protected
62
+ */
63
+ _hasValidConstraints(constraints) {
64
+ return constraints.some((c) => this.__isValidConstraint(c));
65
+ }
53
66
 
54
- /**
55
- * Override this method to implement custom validation constraints.
56
- * @param {unknown[]} constraints
57
- * @protected
58
- */
59
- _constraintsChanged(...constraints) {
60
- // Prevent marking field as invalid when setting required state
61
- // or any other constraint before a user has entered the value.
62
- if (!this.invalid) {
63
- return;
67
+ /**
68
+ * Override this method to customize setting up constraints observer.
69
+ * @protected
70
+ */
71
+ _createConstraintsObserver() {
72
+ // This complex observer needs to be added dynamically instead of using `static get observers()`
73
+ // to make it possible to tweak this behavior in classes that apply this mixin.
74
+ this._createMethodObserver(`_constraintsChanged(${this.constructor.constraints.join(', ')})`);
64
75
  }
65
76
 
66
- if (constraints.some((c) => this.__isValidConstraint(c))) {
77
+ /**
78
+ * Override this method to implement custom validation constraints.
79
+ * @param {unknown[]} constraints
80
+ * @protected
81
+ */
82
+ _constraintsChanged(...constraints) {
83
+ // Prevent marking field as invalid when setting required state
84
+ // or any other constraint before a user has entered the value.
85
+ if (!this.invalid) {
86
+ return;
87
+ }
88
+
89
+ if (this._hasValidConstraints(constraints)) {
90
+ this.validate();
91
+ } else {
92
+ this.invalid = false;
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Override an event listener inherited from `InputMixin`
98
+ * to capture native `change` event and make sure that
99
+ * a new one is dispatched after validation runs.
100
+ * @param {Event} event
101
+ * @protected
102
+ * @override
103
+ */
104
+ _onChange(event) {
105
+ event.stopPropagation();
106
+
67
107
  this.validate();
68
- } else {
69
- this.invalid = false;
108
+
109
+ this.dispatchEvent(
110
+ new CustomEvent('change', {
111
+ detail: {
112
+ sourceEvent: event
113
+ },
114
+ bubbles: event.bubbles,
115
+ cancelable: event.cancelable
116
+ })
117
+ );
70
118
  }
71
- }
72
119
 
73
- /** @private */
74
- __isValidConstraint(constraint) {
75
- // 0 is valid for `minlength` and `maxlength`
76
- return Boolean(constraint) || constraint === 0;
120
+ /** @private */
121
+ __isValidConstraint(constraint) {
122
+ // 0 is valid for `minlength` and `maxlength`
123
+ return Boolean(constraint) || constraint === 0;
124
+ }
77
125
  }
78
- };
79
-
80
- /**
81
- * A mixin to combine multiple input validation constraints.
82
- */
83
- export const InputConstraintsMixin = dedupingMixin(InputConstraintsMixinImplementation);
126
+ );
@@ -0,0 +1,52 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2021 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import { KeyboardMixin } from '@vaadin/component-base/src/keyboard-mixin.js';
7
+ import { FieldMixin } from './field-mixin.js';
8
+ import { InputConstraintsMixin } from './input-constraints-mixin.js';
9
+
10
+ /**
11
+ * A mixin to provide shared logic for the editable form input controls.
12
+ */
13
+ declare function InputControlMixin<T extends new (...args: any[]) => {}>(base: T): T & InputControlMixinConstructor;
14
+
15
+ interface InputControlMixinConstructor {
16
+ new (...args: any[]): InputControlMixin;
17
+ }
18
+
19
+ interface InputControlMixin extends KeyboardMixin, InputConstraintsMixin, FieldMixin {
20
+ /**
21
+ * Specify that the value should be automatically selected when the field gains focus.
22
+ */
23
+ autoselect: boolean;
24
+
25
+ /**
26
+ * Set to true to display the clear icon which clears the input.
27
+ * @attr {boolean} clear-button-visible
28
+ */
29
+ clearButtonVisible: boolean;
30
+
31
+ /**
32
+ * The name of this field.
33
+ */
34
+ name: string;
35
+
36
+ /**
37
+ * A hint to the user of what can be entered in the field.
38
+ */
39
+ placeholder: string;
40
+
41
+ /**
42
+ * When present, it specifies that the field is read-only.
43
+ */
44
+ readonly: boolean;
45
+
46
+ /**
47
+ * The text usually displayed in a tooltip popup when the mouse is over the field.
48
+ */
49
+ title: string;
50
+ }
51
+
52
+ export { InputControlMixin, InputControlMixinConstructor };
@@ -0,0 +1,170 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2021 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import { KeyboardMixin } from '@vaadin/component-base/src/keyboard-mixin.js';
7
+ import { DelegateFocusMixin } from './delegate-focus-mixin.js';
8
+ import { FieldMixin } from './field-mixin.js';
9
+ import { InputConstraintsMixin } from './input-constraints-mixin.js';
10
+
11
+ /**
12
+ * A mixin to provide shared logic for the editable form input controls.
13
+ *
14
+ * @polymerMixin
15
+ * @mixes DelegateFocusMixin
16
+ * @mixes FieldMixin
17
+ * @mixes InputConstraintsMixin
18
+ * @mixes KeyboardMixin
19
+ */
20
+ export const InputControlMixin = (superclass) =>
21
+ class InputControlMixinClass extends DelegateFocusMixin(
22
+ InputConstraintsMixin(FieldMixin(KeyboardMixin(superclass)))
23
+ ) {
24
+ static get properties() {
25
+ return {
26
+ /**
27
+ * Specify that the value should be automatically selected when the field gains focus.
28
+ */
29
+ autoselect: {
30
+ type: Boolean,
31
+ value: false
32
+ },
33
+
34
+ /**
35
+ * Set to true to display the clear icon which clears the input.
36
+ * @attr {boolean} clear-button-visible
37
+ */
38
+ clearButtonVisible: {
39
+ type: Boolean,
40
+ reflectToAttribute: true,
41
+ value: false
42
+ },
43
+
44
+ /**
45
+ * The name of this field.
46
+ */
47
+ name: {
48
+ type: String,
49
+ reflectToAttribute: true
50
+ },
51
+
52
+ /**
53
+ * A hint to the user of what can be entered in the field.
54
+ */
55
+ placeholder: {
56
+ type: String,
57
+ reflectToAttribute: true
58
+ },
59
+
60
+ /**
61
+ * When present, it specifies that the field is read-only.
62
+ */
63
+ readonly: {
64
+ type: Boolean,
65
+ value: false,
66
+ reflectToAttribute: true
67
+ },
68
+
69
+ /**
70
+ * The text usually displayed in a tooltip popup when the mouse is over the field.
71
+ */
72
+ title: {
73
+ type: String,
74
+ reflectToAttribute: true
75
+ }
76
+ };
77
+ }
78
+
79
+ static get delegateAttrs() {
80
+ return [...super.delegateAttrs, 'name', 'type', 'placeholder', 'readonly', 'invalid', 'title'];
81
+ }
82
+
83
+ /**
84
+ * Any element extending this mixin is required to implement this getter.
85
+ * It returns the reference to the clear button element.
86
+ * @protected
87
+ * @return {Element | null | undefined}
88
+ */
89
+ get clearElement() {
90
+ console.warn(`Please implement the 'clearElement' property in <${this.localName}>`);
91
+ return null;
92
+ }
93
+
94
+ /** @protected */
95
+ ready() {
96
+ super.ready();
97
+
98
+ if (this.clearElement) {
99
+ this.clearElement.addEventListener('click', (e) => this._onClearButtonClick(e));
100
+ }
101
+ }
102
+
103
+ /**
104
+ * @param {Event} event
105
+ * @protected
106
+ */
107
+ _onClearButtonClick(event) {
108
+ event.preventDefault();
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 }));
113
+ }
114
+
115
+ /**
116
+ * Override an event listener from `DelegateFocusMixin`.
117
+ * @param {FocusEvent} event
118
+ * @protected
119
+ * @override
120
+ */
121
+ _onFocus(event) {
122
+ super._onFocus(event);
123
+
124
+ if (this.autoselect && this.inputElement) {
125
+ this.inputElement.select();
126
+ }
127
+ }
128
+
129
+ /**
130
+ * Override an event listener inherited from `KeydownMixin` to clear on Esc.
131
+ * Components that extend this mixin can prevent this behavior by overriding
132
+ * this method without calling `super._onKeyDown` to provide custom logic.
133
+ * @param {KeyboardEvent} event
134
+ * @protected
135
+ * @override
136
+ */
137
+ _onKeyDown(event) {
138
+ super._onKeyDown(event);
139
+
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 }));
144
+ }
145
+ }
146
+
147
+ /**
148
+ * Override an event listener inherited from `InputMixin`
149
+ * to capture native `change` event and make sure that
150
+ * a new one is dispatched after validation runs.
151
+ * @param {Event} event
152
+ * @protected
153
+ * @override
154
+ */
155
+ _onChange(event) {
156
+ event.stopPropagation();
157
+
158
+ this.validate();
159
+
160
+ this.dispatchEvent(
161
+ new CustomEvent('change', {
162
+ detail: {
163
+ sourceEvent: event
164
+ },
165
+ bubbles: event.bubbles,
166
+ cancelable: event.cancelable
167
+ })
168
+ );
169
+ }
170
+ };
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2021 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import { SlotController } from './slot-controller.js';
7
+
8
+ /**
9
+ * A controller to create and initialize slotted `<input>` element.
10
+ */
11
+ export class InputController implements SlotController {}
@@ -0,0 +1,35 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2021 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import { SlotController } from './slot-controller.js';
7
+
8
+ /**
9
+ * A controller to create and initialize slotted `<input>` element.
10
+ */
11
+ export class InputController extends SlotController {
12
+ constructor(host, callback) {
13
+ super(host, [
14
+ 'input',
15
+ () => document.createElement('input'),
16
+ (host, node) => {
17
+ if (host.value) {
18
+ node.setAttribute('value', host.value);
19
+ }
20
+ if (host.type) {
21
+ node.setAttribute('type', host.type);
22
+ }
23
+
24
+ // Ensure every instance has unique ID
25
+ const uniqueId = (InputController._uniqueInputId = 1 + InputController._uniqueInputId || 0);
26
+ host._inputId = `${host.localName}-${uniqueId}`;
27
+ node.id = host._inputId;
28
+
29
+ if (typeof callback == 'function') {
30
+ callback(node);
31
+ }
32
+ }
33
+ ]);
34
+ }
35
+ }
@@ -3,11 +3,7 @@
3
3
  * Copyright (c) 2021 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
- import { AriaLabelMixin } from './aria-label-mixin.js';
7
- import { ClearButtonMixin } from './clear-button-mixin.js';
8
- import { DelegateFocusMixin } from './delegate-focus-mixin.js';
9
- import { InputConstraintsMixin } from './input-constraints-mixin.js';
10
- import { FieldAriaMixin } from './field-aria-mixin.js';
6
+ import { InputControlMixin } from './input-control-mixin.js';
11
7
 
12
8
  /**
13
9
  * A mixin to provide logic for vaadin-text-field and related components.
@@ -18,12 +14,7 @@ interface InputFieldMixinConstructor {
18
14
  new (...args: any[]): InputFieldMixin;
19
15
  }
20
16
 
21
- interface InputFieldMixin
22
- extends AriaLabelMixin,
23
- ClearButtonMixin,
24
- DelegateFocusMixin,
25
- FieldAriaMixin,
26
- InputConstraintsMixin {
17
+ interface InputFieldMixin extends InputControlMixin {
27
18
  /**
28
19
  * Whether the value of the control can be automatically completed by the browser.
29
20
  * List of available options at:
@@ -50,11 +41,6 @@ interface InputFieldMixin
50
41
  * none: No capitalization.
51
42
  */
52
43
  autocapitalize: 'on' | 'off' | 'none' | 'characters' | 'words' | 'sentences' | undefined;
53
-
54
- /**
55
- * Specify that the value should be automatically selected when the field gains focus.
56
- */
57
- autoselect: boolean;
58
44
  }
59
45
 
60
46
  export { InputFieldMixin, InputFieldMixinConstructor };