Skip to content

Commit 49e9e8e

Browse files
committed
do not serialize React components into form fields
1 parent 2a647a7 commit 49e9e8e

File tree

1 file changed

+12
-1
lines changed
  • packages/@headlessui-react/src/utils

1 file changed

+12
-1
lines changed

packages/@headlessui-react/src/utils/form.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isValidElement } from 'react'
2+
13
type Entries = [string, string][]
24

35
export function objectToFormEntries(
@@ -31,7 +33,7 @@ function append(entries: Entries, key: string, value: any): void {
3133
entries.push([key, `${value}`])
3234
} else if (value === null || value === undefined) {
3335
entries.push([key, ''])
34-
} else {
36+
} else if (isPlainObject(value) && !isValidElement(value)) {
3537
objectToFormEntries(value, key, entries)
3638
}
3739
}
@@ -62,3 +64,12 @@ export function attemptSubmit(elementInForm: HTMLElement) {
6264
// because then the `submit` event won't be fired and `onSubmit` listeners won't be fired.
6365
form.requestSubmit?.()
6466
}
67+
68+
function isPlainObject<T>(value: T): value is T & Record<keyof T, unknown> {
69+
if (Object.prototype.toString.call(value) !== '[object Object]') {
70+
return false
71+
}
72+
73+
let prototype = Object.getPrototypeOf(value)
74+
return prototype === null || Object.getPrototypeOf(prototype) === null
75+
}

0 commit comments

Comments
 (0)