Skip to content

Commit 96428e8

Browse files
committed
fix: allow to attach internals on the fly
1 parent 92c87d1 commit 96428e8

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

src/element-internals.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,6 @@ export function forceElementInternalsPolyfill(forceCustomStateSet = true) {
393393
} else if (this.tagName.indexOf('-') === -1) {
394394
throw new Error(`Failed to execute 'attachInternals' on 'HTMLElement': Unable to attach ElementInternals to non-custom elements.`);
395395
}
396-
if (internalsMap.has(this)) {
397-
throw new DOMException(`DOMException: Failed to execute 'attachInternals' on 'HTMLElement': ElementInternals for the specified element was already attached.`);
398-
}
399396
return new ElementInternals(this) as IElementInternals;
400397
}
401398
}

src/utils.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export const formInputCallback = (event: Event) => {
119119
* @param {Event} - The form change event
120120
* @return {void}
121121
*/
122-
export const formChangeCallback = (event: Event) => {
122+
export const formChangeCallback = (event: Event) => {
123123
setFormValidity(findParentForm(event.target));
124124
};
125125

@@ -263,7 +263,7 @@ export const throwIfNotFormAssociated = (ref: ICustomElement, message: string, E
263263
* @param method {'checkValidity'|'reportValidity'} - The original method
264264
* @returns {boolean} The form's validity state
265265
*/
266-
export const overrideFormMethod = (form: HTMLFormElement, returnValue: boolean, method: 'checkValidity'|'reportValidity'): boolean => {
266+
export const overrideFormMethod = (form: HTMLFormElement, returnValue: boolean, method: 'checkValidity' | 'reportValidity'): boolean => {
267267
const elements = formElementsMap.get(form);
268268

269269
/** Some forms won't contain form associated custom elements */
@@ -287,10 +287,17 @@ export const overrideFormMethod = (form: HTMLFormElement, returnValue: boolean,
287287
*/
288288
export const upgradeInternals = (ref: ICustomElement) => {
289289
if (ref.constructor['formAssociated']) {
290-
const internals = internalsMap.get(ref);
290+
let internals = internalsMap.get(ref);
291+
// we might have cases where the internals are not set
292+
if (internals === undefined) {
293+
console.warn('ElementInternals missing from the element', ref);
294+
ref.attachInternals();
295+
internals = internalsMap.get(ref);
296+
}
291297
const { labels, form } = internals;
292298
initLabels(ref, labels);
293299
initForm(ref, form, internals);
300+
294301
}
295302
};
296303

0 commit comments

Comments
 (0)