Skip to content

Commit df9effa

Browse files
committed
fix(formAssociatedCallback): make sure internals are available when formAssociatedCallback is called
1 parent c0565b7 commit df9effa

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

Diff for: src/utils.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ export const initForm = (ref: ICustomElement, form: HTMLFormElement, internals:
142142

143143
/** Call formAssociatedCallback if applicable */
144144
if (ref.constructor['formAssociated'] && ref.formAssociatedCallback) {
145-
ref.formAssociatedCallback.apply(ref, [form]);
145+
setTimeout(() => {
146+
ref.formAssociatedCallback.apply(ref, [form]);
147+
}, 0);
146148
}
147149
}
148150
};

Diff for: test/ElementInternals.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import '../dist/index.js';
99

1010
let callCount = 0;
11+
let internalsAvailableInFormAssociatedCallback = false;
1112

1213
class CustomElement extends HTMLElement {
1314
static get formAssociated() {
@@ -59,6 +60,10 @@ class CustomElement extends HTMLElement {
5960
return this._value;
6061
}
6162

63+
formAssociatedCallback() {
64+
internalsAvailableInFormAssociatedCallback = !!this.internals;
65+
}
66+
6267
formDisabledCallback() {
6368
callCount += 1;
6469
}
@@ -277,5 +282,9 @@ describe('The ElementInternals polyfill', () => {
277282
}, 'This field is required');
278283
el.internals.checkValidity();
279284
});
285+
286+
it('will call formAssociatedCallback after internals have been set', () => {
287+
expect(internalsAvailableInFormAssociatedCallback).to.be.true;
288+
})
280289
});
281290
});

0 commit comments

Comments
 (0)