Skip to content

Commit 3a2be1e

Browse files
authored
feat: add DelegateStateMixin and make other mixins use it (#2455)
1 parent 725d52c commit 3a2be1e

24 files changed

Lines changed: 447 additions & 197 deletions

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ export { AriaLabelMixin } from './src/aria-label-mixin.js';
33
export { CharLengthMixin } from './src/char-length-mixin.js';
44
export { ClearButtonMixin } from './src/clear-button-mixin.js';
55
export { DelegateFocusMixin } from './src/delegate-focus-mixin.js';
6+
export { DelegateInputStateMixin } from './src/delegate-input-state-mixin.js';
7+
export { DelegateStateMixin } from './src/delegate-state-mixin.js';
68
export { DisabledMixin } from './src/disabled-mixin.js';
79
export { FieldAriaMixin } from './src/field-aria-mixin.js';
810
export { FocusMixin } from './src/focus-mixin.js';
9-
export { ForwardInputPropsMixin } from './src/forward-input-props-mixin.js';
1011
export { HelperTextMixin } from './src/helper-text-mixin.js';
1112
export { InputFieldMixin } from './src/input-field-mixin.js';
1213
export { InputMixin } from './src/input-mixin.js';

‎packages/field-base/index.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ export { AriaLabelMixin } from './src/aria-label-mixin.js';
33
export { CharLengthMixin } from './src/char-length-mixin.js';
44
export { ClearButtonMixin } from './src/clear-button-mixin.js';
55
export { DelegateFocusMixin } from './src/delegate-focus-mixin.js';
6+
export { DelegateInputStateMixin } from './src/delegate-input-state-mixin.js';
7+
export { DelegateStateMixin } from './src/delegate-state-mixin.js';
68
export { DisabledMixin } from './src/disabled-mixin.js';
79
export { FieldAriaMixin } from './src/field-aria-mixin.js';
810
export { FocusMixin } from './src/focus-mixin.js';
9-
export { ForwardInputPropsMixin } from './src/forward-input-props-mixin.js';
1011
export { HelperTextMixin } from './src/helper-text-mixin.js';
1112
export { InputFieldMixin } from './src/input-field-mixin.js';
1213
export { InputMixin } from './src/input-mixin.js';

‎packages/field-base/src/char-length-mixin.d.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright (c) 2021 Vaadin Ltd.
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
6-
import { ForwardInputPropsMixin } from './forward-input-props-mixin.js';
6+
import { InputConstraintsMixin } from './input-constraints-mixin.js';
77

88
/**
99
* A mixin to provide `minlength` and `maxlength` properties
@@ -15,7 +15,7 @@ interface CharLengthMixinConstructor {
1515
new (...args: any[]): CharLengthMixin;
1616
}
1717

18-
interface CharLengthMixin extends ForwardInputPropsMixin {
18+
interface CharLengthMixin extends InputConstraintsMixin {
1919
/**
2020
* Maximum number of characters (in Unicode code points) that the user can enter.
2121
*/

‎packages/field-base/src/char-length-mixin.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
66
import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
7-
import { ForwardInputPropsMixin } from './forward-input-props-mixin.js';
7+
import { InputConstraintsMixin } from './input-constraints-mixin.js';
88

99
const CharLengthMixinImplementation = (superclass) =>
10-
class CharLengthMixinClass extends ForwardInputPropsMixin(superclass) {
10+
class CharLengthMixinClass extends InputConstraintsMixin(superclass) {
1111
static get properties() {
1212
return {
1313
/**
@@ -26,8 +26,8 @@ const CharLengthMixinImplementation = (superclass) =>
2626
};
2727
}
2828

29-
static get forwardProps() {
30-
return [...super.forwardProps, 'maxlength', 'minlength'];
29+
static get delegateAttrs() {
30+
return [...super.delegateAttrs, 'maxlength', 'minlength'];
3131
}
3232

3333
static get constraints() {

packages/field-base/src/forward-input-props-mixin.d.ts renamed to packages/field-base/src/delegate-input-state-mixin.d.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33
* Copyright (c) 2021 Vaadin Ltd.
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
6-
import { InputConstraintsMixin } from './input-constraints-mixin.js';
6+
import { DelegateStateMixin } from './delegate-state-mixin.js';
7+
import { InputMixin } from './input-mixin.js';
8+
import { ValidateMixin } from './validate-mixin.js';
79

810
/**
911
* A mixin to forward properties to the input element.
1012
*/
11-
declare function ForwardInputPropsMixin<T extends new (...args: any[]) => {}>(
13+
declare function DelegateInputStateMixin<T extends new (...args: any[]) => {}>(
1214
base: T
13-
): T & ForwardInputPropsMixinConstructor;
15+
): T & DelegateInputStateMixinConstructor;
1416

15-
interface ForwardInputPropsMixinConstructor {
16-
new (...args: any[]): ForwardInputPropsMixin;
17+
interface DelegateInputStateMixinConstructor {
18+
new (...args: any[]): DelegateInputStateMixin;
1719
}
1820

19-
interface ForwardInputPropsMixin extends InputConstraintsMixin {
21+
interface DelegateInputStateMixin extends DelegateStateMixin, ValidateMixin, InputMixin {
2022
/**
2123
* The name of this field.
2224
*/
@@ -38,4 +40,4 @@ interface ForwardInputPropsMixin extends InputConstraintsMixin {
3840
title: string;
3941
}
4042

41-
export { ForwardInputPropsMixinConstructor, ForwardInputPropsMixin };
43+
export { DelegateInputStateMixinConstructor, DelegateInputStateMixin };
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
7+
import { DelegateStateMixin } from './delegate-state-mixin.js';
8+
import { InputMixin } from './input-mixin.js';
9+
import { ValidateMixin } from './validate-mixin.js';
10+
11+
const DelegateInputStateMixinImplementation = (superclass) =>
12+
class DelegateInputStateMixinClass extends DelegateStateMixin(ValidateMixin(InputMixin(superclass))) {
13+
static get properties() {
14+
return {
15+
/**
16+
* The name of this field.
17+
*/
18+
name: {
19+
type: String,
20+
reflectToAttribute: true
21+
},
22+
23+
/**
24+
* A hint to the user of what can be entered in the field.
25+
*/
26+
placeholder: {
27+
type: String,
28+
reflectToAttribute: true
29+
},
30+
31+
/**
32+
* When present, it specifies that the field is read-only.
33+
*/
34+
readonly: {
35+
type: Boolean,
36+
value: false,
37+
reflectToAttribute: true
38+
},
39+
40+
/**
41+
* The text usually displayed in a tooltip popup when the mouse is over the field.
42+
*/
43+
title: {
44+
type: String,
45+
reflectToAttribute: true
46+
}
47+
};
48+
}
49+
50+
static get delegateAttrs() {
51+
return ['name', 'type', 'placeholder', 'required', 'readonly', 'invalid', 'title'];
52+
}
53+
54+
/** @protected */
55+
get _delegateStateTarget() {
56+
return this.inputElement;
57+
}
58+
};
59+
60+
/**
61+
* A mixin to delegate attributes to the input element.
62+
*/
63+
export const DelegateInputStateMixin = dedupingMixin(DelegateInputStateMixinImplementation);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
7+
/**
8+
* A mixin to delegate properties and attributes to a target element.
9+
*/
10+
declare function DelegateStateMixin<T extends new (...args: any[]) => {}>(base: T): T & DelegateStateMixinConstructor;
11+
12+
interface DelegateStateMixinConstructor {
13+
new (...args: any[]): DelegateStateMixin;
14+
}
15+
16+
interface DelegateStateMixin {
17+
/**
18+
* A target element to which attributes and properties are delegated.
19+
*/
20+
_delegateStateTarget: HTMLElement | null;
21+
}
22+
23+
export { DelegateStateMixinConstructor, DelegateStateMixin };
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
7+
8+
const DelegateStateMixinImplementation = (superclass) =>
9+
class DelegateStateMixinClass extends superclass {
10+
/**
11+
* An array of the host attributes to delegate to the target element.
12+
*/
13+
static get delegateAttrs() {
14+
return [];
15+
}
16+
17+
/**
18+
* An array of the host properties to delegate to the target element.
19+
*/
20+
static get delegateProps() {
21+
return [];
22+
}
23+
24+
/**
25+
* A target element to which attributes and properties are delegated.
26+
*/
27+
get _delegateStateTarget() {
28+
console.warn(`Please implement the '_delegateStateTarget' property in <${this.localName}>`);
29+
return null;
30+
}
31+
32+
/** @protected */
33+
ready() {
34+
super.ready();
35+
36+
this._createDelegateAttrsObserver();
37+
this._ensureAttrsDelegated();
38+
39+
this._createDelegatePropsObserver();
40+
this._ensurePropsDelegated();
41+
}
42+
43+
/** @protected */
44+
_createDelegateAttrsObserver() {
45+
this._createMethodObserver(`_delegateAttrsChanged(${this.constructor.delegateAttrs.join(', ')})`);
46+
}
47+
48+
/** @protected */
49+
_createDelegatePropsObserver() {
50+
this._createMethodObserver(`_delegatePropsChanged(${this.constructor.delegateProps.join(', ')})`);
51+
}
52+
53+
/** @protected */
54+
_ensureAttrsDelegated() {
55+
this.constructor.delegateAttrs.forEach((name) => {
56+
this._delegateAttribute(name, this[name]);
57+
});
58+
}
59+
60+
/** @protected */
61+
_ensurePropsDelegated() {
62+
this.constructor.delegateProps.forEach((name) => {
63+
this._delegateProperty(name, this[name]);
64+
});
65+
}
66+
67+
/** @protected */
68+
_delegateAttrsChanged(...values) {
69+
this.constructor.delegateAttrs.forEach((name, index) => {
70+
this._delegateAttribute(name, values[index]);
71+
});
72+
}
73+
74+
/** @protected */
75+
_delegatePropsChanged(...values) {
76+
this.constructor.delegateProps.forEach((name, index) => {
77+
this._delegateProperty(name, values[index]);
78+
});
79+
}
80+
81+
/** @protected */
82+
_delegateAttribute(name, value) {
83+
if (!this._delegateStateTarget) {
84+
return;
85+
}
86+
87+
if (name === 'invalid') {
88+
this._delegateAttribute('aria-invalid', value ? 'true' : false);
89+
}
90+
91+
if (typeof value === 'boolean') {
92+
this._delegateStateTarget.toggleAttribute(name, value);
93+
} else if (value) {
94+
this._delegateStateTarget.setAttribute(name, value);
95+
} else {
96+
this._delegateStateTarget.removeAttribute(name);
97+
}
98+
}
99+
100+
/** @protected */
101+
_delegateProperty(name, value) {
102+
if (!this._delegateStateTarget) {
103+
return;
104+
}
105+
106+
this._delegateStateTarget[name] = value;
107+
}
108+
};
109+
110+
/**
111+
* A mixin to delegate properties and attributes to a target element.
112+
*/
113+
export const DelegateStateMixin = dedupingMixin(DelegateStateMixinImplementation);

‎packages/field-base/src/field-aria-mixin.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ const FieldAriaMixinImplementation = (superclass) =>
2424
}
2525

2626
/** @protected */
27-
connectedCallback() {
28-
super.connectedCallback();
27+
ready() {
28+
super.ready();
2929

3030
this._updateAriaAttribute(this.invalid, this._helperId);
3131
}

0 commit comments

Comments
 (0)