Skip to content

Commit 1ee04a3

Browse files
committed
feat(form-data): allow passing a form to FormData
Signed-off-by: Logan McAnsh <[email protected]>
1 parent a9203c7 commit 1ee04a3

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

packages/form-data/src/form-data.js

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@ export class FormData {
77
*/
88
constructor(form) {
99
if (form !== undefined) {
10-
const error = isHTMLFormElement(form)
11-
? new TypeError(
12-
"FormData constructor: HTMLFormElement parameter is not supported, if you need it submit an issue"
13-
)
14-
: new TypeError(
15-
"FormData constructor: Argument 1 does not implement interface HTMLFormElement."
16-
);
17-
18-
throw error;
10+
for (const element of form.elements) {
11+
if (isSelectElement(element)) {
12+
for (const option of element.options) {
13+
if (option.selected) {
14+
this.append(element.name, option.value);
15+
}
16+
}
17+
} else if (
18+
isInputElement(element) &&
19+
(element.checked || !['radio', 'checkbox'].includes(element.type)) &&
20+
element.name
21+
) {
22+
this.append(element.name, element.value);
23+
}
24+
}
1925
}
2026

2127
/**
@@ -312,3 +318,21 @@ const BlobFile = class File {
312318
const panic = (error) => {
313319
throw error;
314320
};
321+
322+
/**
323+
*
324+
* @param {Element} element
325+
* @returns {boolean}
326+
*/
327+
function isSelectElement(element) {
328+
return element.tagName === 'SELECT';
329+
}
330+
331+
/**
332+
*
333+
* @param {Element} element
334+
* @returns {boolean}
335+
*/
336+
function isInputElement(element) {
337+
return element.tagName === 'INPUT' || element.tagName === 'TEXTAREA';
338+
}

0 commit comments

Comments
 (0)