chore(forms): add revision legacy form detection (#26477)

This commit is contained in:
Liam DeBeasi
2022-12-13 16:32:33 -05:00
committed by GitHub
parent 16404ab12f
commit 8fff65cb66

View File

@@ -18,8 +18,7 @@ export const createLegacyFormController = (el: AllowedFormElements): LegacyFormC
* can check to see if the component has slotted text
* in the light DOM.
*/
const hasLabelProp =
(controlEl as any).label !== undefined || (controlEl.shadowRoot !== null && controlEl.textContent !== '');
const hasLabelProp = (controlEl as any).label !== undefined || hasLabelSlot(controlEl);
const hasAriaLabelAttribute = controlEl.hasAttribute('aria-label');
/**
@@ -38,3 +37,33 @@ export const createLegacyFormController = (el: AllowedFormElements): LegacyFormC
export type LegacyFormController = {
hasLegacyControl: () => boolean;
};
const hasLabelSlot = (controlEl: HTMLElement) => {
const root = controlEl.shadowRoot;
if (root === null) {
return false;
}
/**
* Components that have a named label slot
* also have other slots, so we need to query for
* anything that is explicitly passed to slot="label"
*/
if (NAMED_LABEL_SLOT_COMPONENTS.includes(controlEl.tagName) && controlEl.querySelector('[slot="label"]') !== null) {
return true;
}
/**
* Components that have an unnamed slot for the label
* have no other slots, so we can check the textContent
* of the element.
*/
if (UNNAMED_LABEL_SLOT_COMPONENTS.includes(controlEl.tagName) && controlEl.textContent !== '') {
return true;
}
return false;
};
const NAMED_LABEL_SLOT_COMPONENTS: string[] = [];
const UNNAMED_LABEL_SLOT_COMPONENTS = ['ION-TOGGLE'];