Skip to content

Commit 97c3567

Browse files
fix: update form validity check algorithm to not call checkValidity on all changes
1 parent 2a7c282 commit 97c3567

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

Diff for: src/utils.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,18 @@ export const initLabels = (ref: ICustomElement, labels: LabelsList): void => {
8787
* @return {void}
8888
*/
8989
export const setFormValidity = (form: HTMLFormElement) => {
90-
const valid = form.checkValidity();
91-
form.toggleAttribute('internals-invalid', !valid);
92-
form.toggleAttribute('internals-valid', valid);
90+
const nativeControlValidity = Array.from(form.elements)
91+
.map(
92+
(element: Element & { validity: ValidityState }) => element.validity.valid
93+
);
94+
const polyfilledVaidity = Array.from(formElementsMap.get(form))
95+
.filter(control => control.isConnected)
96+
.map((control: ICustomElement) =>
97+
internalsMap.get(control).validity.valid
98+
);
99+
const hasInvalid = [...nativeControlValidity, ...polyfilledVaidity].includes(false);
100+
form.toggleAttribute('internals-invalid', hasInvalid);
101+
form.toggleAttribute('internals-valid', !hasInvalid);
93102
}
94103

95104
/**

Diff for: test/ElementInternals.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,18 @@ describe('The ElementInternals polyfill', () => {
9595
input.required = input.required;
9696
input.dispatchEvent(new Event(eventType, options));
9797
}
98-
98+
9999
beforeEach(async () => {
100100
form = await fixture(`<form><test-el></test-el></form>`);
101101
input = form.querySelector('test-el');
102102
input.required = true;
103103
simultateEvent('input');
104104
});
105-
105+
106106
afterEach(async () => {
107107
await fixtureCleanup(form);
108108
});
109-
109+
110110
it('form should be invalid when form-associated custom element is invalid', () => {
111111
expect(form.checkValidity()).to.be.false;
112112
});

0 commit comments

Comments
 (0)