Skip to content

Commit bb1c702

Browse files
daKmoRtlouisse
authored andcommitted
fix(field): no DomHelpers for FormControl and apply all needed mixins
1 parent b0543f0 commit bb1c702

File tree

1 file changed

+43
-24
lines changed

1 file changed

+43
-24
lines changed

packages/field/src/FormControlMixin.js

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { html, css, nothing, dedupeMixin } from '@lion/core';
1+
import { html, css, nothing, dedupeMixin, SlotMixin } from '@lion/core';
22
import { ObserverMixin } from '@lion/core/src/ObserverMixin.js';
33

44
/**
@@ -14,7 +14,7 @@ import { ObserverMixin } from '@lion/core/src/ObserverMixin.js';
1414
export const FormControlMixin = dedupeMixin(
1515
superclass =>
1616
// eslint-disable-next-line no-shadow, no-unused-vars
17-
class FormControlMixin extends ObserverMixin(superclass) {
17+
class FormControlMixin extends ObserverMixin(SlotMixin(superclass)) {
1818
static get properties() {
1919
return {
2020
...super.properties,
@@ -75,8 +75,21 @@ export const FormControlMixin = dedupeMixin(
7575
};
7676
}
7777

78+
/** @deprecated will be this._inputNode in next breaking release */
7879
get inputElement() {
79-
return (this.$$slot && this.$$slot('input')) || this.querySelector('[slot=input]'); // eslint-disable-line
80+
return this.__getDirectSlotChild('input');
81+
}
82+
83+
get _labelNode() {
84+
return this.__getDirectSlotChild('label');
85+
}
86+
87+
get _helpTextNode() {
88+
return this.__getDirectSlotChild('help-text');
89+
}
90+
91+
get _feedbackNode() {
92+
return this.__getDirectSlotChild('feedback');
8093
}
8194

8295
constructor() {
@@ -107,29 +120,31 @@ export const FormControlMixin = dedupeMixin(
107120
}
108121

109122
_enhanceLightDomA11y() {
110-
if (this.inputElement) {
111-
this.inputElement.id = this.inputElement.id || this._inputId;
123+
const { inputElement, _labelNode, _helpTextNode, _feedbackNode } = this;
124+
125+
if (inputElement) {
126+
inputElement.id = inputElement.id || this._inputId;
112127
}
113-
if (this.$$slot('label')) {
114-
this.$$slot('label').setAttribute('for', this._inputId);
115-
this.$$slot('label').id = this.$$slot('label').id || `label-${this._inputId}`;
116-
const labelledById = ` ${this.$$slot('label').id}`;
128+
if (_labelNode) {
129+
_labelNode.setAttribute('for', this._inputId);
130+
_labelNode.id = _labelNode.id || `label-${this._inputId}`;
131+
const labelledById = ` ${_labelNode.id}`;
117132
if (this._ariaLabelledby.indexOf(labelledById) === -1) {
118-
this._ariaLabelledby += ` ${this.$$slot('label').id}`;
133+
this._ariaLabelledby += ` ${_labelNode.id}`;
119134
}
120135
}
121-
if (this.$$slot('help-text')) {
122-
this.$$slot('help-text').id = this.$$slot('help-text').id || `help-text-${this._inputId}`;
123-
const describeIdHelpText = ` ${this.$$slot('help-text').id}`;
136+
if (_helpTextNode) {
137+
_helpTextNode.id = _helpTextNode.id || `help-text-${this._inputId}`;
138+
const describeIdHelpText = ` ${_helpTextNode.id}`;
124139
if (this._ariaDescribedby.indexOf(describeIdHelpText) === -1) {
125-
this._ariaDescribedby += ` ${this.$$slot('help-text').id}`;
140+
this._ariaDescribedby += ` ${_helpTextNode.id}`;
126141
}
127142
}
128-
if (this.$$slot('feedback')) {
129-
this.$$slot('feedback').id = this.$$slot('feedback').id || `feedback-${this._inputId}`;
130-
const describeIdFeedback = ` ${this.$$slot('feedback').id}`;
143+
if (_feedbackNode) {
144+
_feedbackNode.id = _feedbackNode.id || `feedback-${this._inputId}`;
145+
const describeIdFeedback = ` ${_feedbackNode.id}`;
131146
if (this._ariaDescribedby.indexOf(describeIdFeedback) === -1) {
132-
this._ariaDescribedby += ` ${this.$$slot('feedback').id}`;
147+
this._ariaDescribedby += ` ${_feedbackNode.id}`;
133148
}
134149
}
135150
this._enhanceLightDomA11yForAdditionalSlots();
@@ -181,7 +196,7 @@ export const FormControlMixin = dedupeMixin(
181196
additionalSlots = ['prefix', 'suffix', 'before', 'after'],
182197
) {
183198
additionalSlots.forEach(additionalSlot => {
184-
const element = this.$$slot(additionalSlot);
199+
const element = this.__getDirectSlotChild(additionalSlot);
185200
if (element) {
186201
element.id = element.id || `${additionalSlot}-${this._inputId}`;
187202
if (element.hasAttribute('data-label') === true) {
@@ -218,14 +233,14 @@ export const FormControlMixin = dedupeMixin(
218233
}
219234

220235
_onLabelChanged({ label }) {
221-
if (this.$$slot && this.$$slot('label')) {
222-
this.$$slot('label').textContent = label;
236+
if (this._labelNode) {
237+
this._labelNode.textContent = label;
223238
}
224239
}
225240

226241
_onHelpTextChanged({ helpText }) {
227-
if (this.$$slot && this.$$slot('help-text')) {
228-
this.$$slot('help-text').textContent = helpText;
242+
if (this._helpTextNode) {
243+
this._helpTextNode.textContent = helpText;
229244
}
230245
}
231246

@@ -552,7 +567,7 @@ export const FormControlMixin = dedupeMixin(
552567

553568
// Returns dom references to all elements that should be referred to by field(s)
554569
_getAriaDescriptionElements() {
555-
return [this.$$slot('help-text'), this.$$slot('feedback')];
570+
return [this._helpTextNode, this._feedbackNode];
556571
}
557572

558573
/**
@@ -574,5 +589,9 @@ export const FormControlMixin = dedupeMixin(
574589
addToAriaDescription(id) {
575590
this._ariaDescribedby += ` ${id}`;
576591
}
592+
593+
__getDirectSlotChild(slotName) {
594+
return [...this.children].find(el => el.slot === slotName);
595+
}
577596
},
578597
);

0 commit comments

Comments
 (0)