Skip to content

Commit cad407a

Browse files
author
Nowell Strite
committed
Support custom-elements that participate in forms
Right now react-router does not support component libraries that leverage custom-elements that implement the form participation api. This expands the guards checks to allow these form submit elements to function.
1 parent 39e4a69 commit cad407a

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

contributors.yml

+1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
- Nismit
245245
- nnhjs
246246
- noisypigeon
247+
- nowells
247248
- Nurai1
248249
- Obi-Dann
249250
- OlegDev1

packages/react-router/lib/dom/dom.ts

+13
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ export function isInputElement(object: any): object is HTMLInputElement {
2222
return isHtmlElement(object) && object.tagName.toLowerCase() === "input";
2323
}
2424

25+
// for a custom element to be a form submitter it must also be a form associated custom element
26+
// https://webkit.org/blog/13711/elementinternals-and-form-associated-custom-elements/
27+
// https://web.dev/articles/more-capable-form-controls
28+
function isFormAssociatedCustomElement(object : any) : object is HTMLElement {
29+
return isHtmlCustomElement(object)
30+
&& (object.constructor as any).formAssociated === true;
31+
}
32+
33+
function isHtmlCustomElement(object : any): object is HTMLElement {
34+
return isHtmlElement(object) && object.localName.includes('-') && Boolean(customElements.get(object.localName));
35+
}
36+
2537
type LimitedMouseEvent = Pick<
2638
MouseEvent,
2739
"button" | "metaKey" | "altKey" | "ctrlKey" | "shiftKey"
@@ -278,6 +290,7 @@ export function getFormSubmissionInfo(
278290
formData = new FormData(target);
279291
} else if (
280292
isButtonElement(target) ||
293+
isFormAssociatedCustomElement(target) ||
281294
(isInputElement(target) &&
282295
(target.type === "submit" || target.type === "image"))
283296
) {

0 commit comments

Comments
 (0)