Skip to content

Commit f09decc

Browse files
authored
feat: add mixins for text-field and related components (#2245)
1 parent af05e71 commit f09decc

8 files changed

Lines changed: 976 additions & 0 deletions

File tree

‎packages/field-base/index.d.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ export { FieldAriaMixin } from './src/field-aria-mixin.js';
55
export { FocusMixin } from './src/focus-mixin.js';
66
export { HelperTextMixin } from './src/helper-text-mixin.js';
77
export { InputAriaMixin } from './src/input-aria-mixin.js';
8+
export { InputFieldMixin } from './src/input-field-mixin.js';
89
export { InputMixin } from './src/input-mixin.js';
910
export { InputPropsMixin } from './src/input-props-mixin.js';
1011
export { LabelMixin } from './src/label-mixin.js';
1112
export { SlotMixin } from './src/slot-mixin.js';
13+
export { TextFieldMixin } from './src/text-field-mixin.js';
1214
export { ValidateMixin } from './src/validate-mixin.js';

‎packages/field-base/index.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ export { FieldAriaMixin } from './src/field-aria-mixin.js';
55
export { FocusMixin } from './src/focus-mixin.js';
66
export { HelperTextMixin } from './src/helper-text-mixin.js';
77
export { InputAriaMixin } from './src/input-aria-mixin.js';
8+
export { InputFieldMixin } from './src/input-field-mixin.js';
89
export { InputMixin } from './src/input-mixin.js';
910
export { InputPropsMixin } from './src/input-props-mixin.js';
1011
export { LabelMixin } from './src/label-mixin.js';
1112
export { SlotMixin } from './src/slot-mixin.js';
13+
export { TextFieldMixin } from './src/text-field-mixin.js';
1214
export { ValidateMixin } from './src/validate-mixin.js';
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 { ClearButtonMixin } from './clear-button-mixin.js';
7+
import { DelegateFocusMixin } from './delegate-focus-mixin.js';
8+
import { FieldAriaMixin } from './field-aria-mixin.js';
9+
import { InputPropsMixin } from './input-props-mixin.js';
10+
11+
/**
12+
* A mixin to provide logic for vaadin-text-field and related components.
13+
*/
14+
declare function InputFieldMixin<T extends new (...args: any[]) => {}>(base: T): T & InputFieldMixinConstructor;
15+
16+
interface InputFieldMixinConstructor {
17+
new (...args: any[]): InputFieldMixin;
18+
}
19+
20+
interface InputFieldMixin extends ClearButtonMixin, DelegateFocusMixin, FieldAriaMixin, InputPropsMixin {
21+
readonly inputElement: HTMLElement | undefined;
22+
23+
/**
24+
* Whether the value of the control can be automatically completed by the browser.
25+
* List of available options at:
26+
* https://developer.mozilla.org/en/docs/Web/HTML/Element/input#attr-autocomplete
27+
*/
28+
autocomplete: string | undefined;
29+
30+
/**
31+
* This is a property supported by Safari that is used to control whether
32+
* autocorrection should be enabled when the user is entering/editing the text.
33+
* Possible values are:
34+
* on: Enable autocorrection.
35+
* off: Disable autocorrection.
36+
*/
37+
autocorrect: 'on' | 'off' | undefined;
38+
39+
/**
40+
* This is a property supported by Safari and Chrome that is used to control whether
41+
* autocapitalization should be enabled when the user is entering/editing the text.
42+
* Possible values are:
43+
* characters: Characters capitalization.
44+
* words: Words capitalization.
45+
* sentences: Sentences capitalization.
46+
* none: No capitalization.
47+
*/
48+
autocapitalize: 'on' | 'off' | 'none' | 'characters' | 'words' | 'sentences' | undefined;
49+
50+
/**
51+
* Specify that the value should be automatically selected when the field gains focus.
52+
*/
53+
autoselect: boolean;
54+
55+
/**
56+
* The value of the field.
57+
*/
58+
value: string;
59+
60+
/**
61+
* Returns true if the current input value satisfies all constraints (if any).
62+
*/
63+
checkValidity(): boolean;
64+
}
65+
66+
export { InputFieldMixin, InputFieldMixinConstructor };
Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
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 { Debouncer } from '@polymer/polymer/lib/utils/debounce.js';
7+
import { animationFrame } from '@polymer/polymer/lib/utils/async.js';
8+
import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
9+
import { ClearButtonMixin } from './clear-button-mixin.js';
10+
import { DelegateFocusMixin } from './delegate-focus-mixin.js';
11+
import { FieldAriaMixin } from './field-aria-mixin.js';
12+
import { InputPropsMixin } from './input-props-mixin.js';
13+
14+
const InputFieldMixinImplementation = (superclass) =>
15+
class InputFieldMixinClass extends ClearButtonMixin(FieldAriaMixin(InputPropsMixin(DelegateFocusMixin(superclass)))) {
16+
static get properties() {
17+
return {
18+
/**
19+
* Whether the value of the control can be automatically completed by the browser.
20+
* List of available options at:
21+
* https://developer.mozilla.org/en/docs/Web/HTML/Element/input#attr-autocomplete
22+
*/
23+
autocomplete: {
24+
type: String
25+
},
26+
27+
/**
28+
* This is a property supported by Safari that is used to control whether
29+
* autocorrection should be enabled when the user is entering/editing the text.
30+
* Possible values are:
31+
* on: Enable autocorrection.
32+
* off: Disable autocorrection.
33+
*/
34+
autocorrect: {
35+
type: String
36+
},
37+
38+
/**
39+
* This is a property supported by Safari and Chrome that is used to control whether
40+
* autocapitalization should be enabled when the user is entering/editing the text.
41+
* Possible values are:
42+
* characters: Characters capitalization.
43+
* words: Words capitalization.
44+
* sentences: Sentences capitalization.
45+
* none: No capitalization.
46+
*/
47+
autocapitalize: {
48+
type: String
49+
},
50+
51+
/**
52+
* Specify that the value should be automatically selected when the field gains focus.
53+
*/
54+
autoselect: {
55+
type: Boolean,
56+
value: false
57+
},
58+
59+
/**
60+
* The value of the field.
61+
*/
62+
value: {
63+
type: String,
64+
value: '',
65+
observer: '_valueChanged',
66+
notify: true
67+
}
68+
};
69+
}
70+
71+
static get hostProps() {
72+
return [...super.hostProps, 'autocapitalize', 'autocomplete', 'autocorrect'];
73+
}
74+
75+
static get observers() {
76+
return ['__observeOffsetHeight(errorMessage, invalid, label, helperText)'];
77+
}
78+
79+
/**
80+
* Element used by `FieldAriaMixin` to set ARIA attributes.
81+
* @protected
82+
*/
83+
get _ariaTarget() {
84+
return this._inputNode;
85+
}
86+
87+
/**
88+
* Element used by `DelegatesFocusMixin` to handle focus.
89+
* @return {!HTMLInputElement}
90+
*/
91+
get focusElement() {
92+
return this._inputNode;
93+
}
94+
95+
constructor() {
96+
super();
97+
98+
this._boundOnInput = this._onInput.bind(this);
99+
this._boundOnBlur = this._onBlur.bind(this);
100+
this._boundOnFocus = this._onFocus.bind(this);
101+
}
102+
103+
/** @protected */
104+
connectedCallback() {
105+
super.connectedCallback();
106+
107+
if (this._inputNode) {
108+
this._addInputListeners(this._inputNode);
109+
110+
// Discard value set on the custom slotted input.
111+
if (this._inputNode.value !== this.value) {
112+
console.warn(`Please define value on the <${this.localName}> component!`);
113+
this._inputNode.value = '';
114+
}
115+
116+
if (this.value) {
117+
this._inputNode.value = this.value;
118+
this.validate();
119+
}
120+
}
121+
}
122+
123+
/** @protected */
124+
disconnectedCallback() {
125+
super.disconnectedCallback();
126+
127+
if (this._inputNode) {
128+
this._removeInputListeners(this._inputNode);
129+
}
130+
}
131+
132+
/** @protected */
133+
ready() {
134+
super.ready();
135+
136+
// Lumo theme defines a max-height transition for the "error-message"
137+
// part on invalid state change.
138+
const errorPart = this.shadowRoot.querySelector('[part="error-message"]');
139+
if (errorPart) {
140+
errorPart.addEventListener('transitionend', () => {
141+
this.__observeOffsetHeight();
142+
});
143+
}
144+
}
145+
146+
/**
147+
* Returns true if the current input value satisfies all constraints (if any).
148+
* @return {boolean}
149+
*/
150+
checkValidity() {
151+
if (this.required) {
152+
return this._inputNode ? this._inputNode.checkValidity() : undefined;
153+
} else {
154+
return !this.invalid;
155+
}
156+
}
157+
158+
// Workaround for https://github.com/Polymer/polymer/issues/5259
159+
get __data() {
160+
return this.__dataValue || {};
161+
}
162+
163+
set __data(value) {
164+
this.__dataValue = value;
165+
}
166+
167+
/**
168+
* @param {HTMLElement} node
169+
* @protected
170+
*/
171+
_addInputListeners(node) {
172+
node.addEventListener('input', this._boundOnInput);
173+
node.addEventListener('blur', this._boundOnBlur);
174+
node.addEventListener('focus', this._boundOnFocus);
175+
}
176+
177+
/**
178+
* @param {HTMLElement} node
179+
* @protected
180+
*/
181+
_removeInputListeners(node) {
182+
node.removeEventListener('input', this._boundOnInput);
183+
node.removeEventListener('blur', this._boundOnBlur);
184+
node.removeEventListener('focus', this._boundOnFocus);
185+
}
186+
187+
/** @private */
188+
_onFocus() {
189+
if (this.autoselect && this._inputNode) {
190+
this._inputNode.select();
191+
}
192+
}
193+
194+
/** @private */
195+
_onBlur() {
196+
this.validate();
197+
}
198+
199+
/**
200+
* @param {Event} event
201+
* @protected
202+
*/
203+
_onInput(event) {
204+
// Ignore manual clear button events
205+
this.__userInput = event.isTrusted;
206+
this.value = event.target.value;
207+
this.__userInput = false;
208+
}
209+
210+
/**
211+
* Dispatch an event if a specific size measurement property has changed.
212+
* Supporting multiple properties here is needed for `vaadin-text-area`.
213+
* @protected
214+
*/
215+
_dispatchIronResizeEventIfNeeded(prop, value) {
216+
const oldSize = '__old' + prop;
217+
if (this[oldSize] !== undefined && this[oldSize] !== value) {
218+
this.dispatchEvent(new CustomEvent('iron-resize', { bubbles: true, composed: true }));
219+
}
220+
221+
this[oldSize] = value;
222+
}
223+
224+
/** @private */
225+
__observeOffsetHeight() {
226+
this.__observeOffsetHeightDebouncer = Debouncer.debounce(
227+
this.__observeOffsetHeightDebouncer,
228+
animationFrame,
229+
() => {
230+
this._dispatchIronResizeEventIfNeeded('Height', this.offsetHeight);
231+
}
232+
);
233+
}
234+
235+
/**
236+
* @param {unknown} newVal
237+
* @param {unknown} oldVal
238+
* @protected
239+
*/
240+
_valueChanged(newVal, oldVal) {
241+
// Setting initial value to empty string, skip validation
242+
if (newVal === '' && oldVal === undefined) {
243+
return;
244+
}
245+
246+
if (newVal !== '' && newVal != null) {
247+
this.setAttribute('has-value', '');
248+
} else {
249+
this.removeAttribute('has-value');
250+
}
251+
252+
// Value is set before an element is connected to the DOM:
253+
// this case is handled separately in `connectedCallback`.
254+
if (!this._inputNode) {
255+
return;
256+
}
257+
258+
// Value is set by the user, no need to sync it back to input.
259+
// Also no need to validate, as we call `validate` on blur.
260+
if (this.__userInput) {
261+
return;
262+
}
263+
264+
// Setting a value programmatically, sync it to input element.
265+
if (newVal != undefined) {
266+
this._inputNode.value = newVal;
267+
} else {
268+
this.clear();
269+
}
270+
271+
// Validate the field after a new value is set programmatically.
272+
if (this.invalid) {
273+
this.validate();
274+
}
275+
}
276+
};
277+
278+
/**
279+
* A mixin to provide logic for vaadin-text-field and related components.
280+
*/
281+
export const InputFieldMixin = dedupingMixin(InputFieldMixinImplementation);

0 commit comments

Comments
 (0)