Skip to content

Commit 14c9dde

Browse files
committed
Functional client code
1 parent 98ba450 commit 14c9dde

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/js/src/index.tsx

+19-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,25 @@ export function DjangoForm({
106106
const onSubmitEvent = (event) => {
107107
event.preventDefault();
108108
const formData = new FormData(form);
109-
console.log(Object.fromEntries(formData));
110-
onSubmitCallback(Object.fromEntries(formData));
109+
110+
// Convert the FormData object to a plain object by iterating through it
111+
// If duplicate keys are present, convert the value into an array of values
112+
const entries = formData.entries();
113+
const formDataArray = Array.from(entries);
114+
const formDataObject = formDataArray.reduce((acc, [key, value]) => {
115+
if (acc[key]) {
116+
if (Array.isArray(acc[key])) {
117+
acc[key].push(value);
118+
} else {
119+
acc[key] = [acc[key], value];
120+
}
121+
} else {
122+
acc[key] = value;
123+
}
124+
return acc;
125+
}, {});
126+
127+
onSubmitCallback(formDataObject);
111128
};
112129

113130
// Bind the event listener

0 commit comments

Comments
 (0)