diff --git a/.changeset/gentle-humans-eat.md b/.changeset/gentle-humans-eat.md new file mode 100644 index 00000000..743c497d --- /dev/null +++ b/.changeset/gentle-humans-eat.md @@ -0,0 +1,5 @@ +--- +'@conform-to/dom': patch +--- + +Replace submission payload unknown types diff --git a/packages/conform-dom/submission.ts b/packages/conform-dom/submission.ts index 8fcf2969..c13ec74b 100644 --- a/packages/conform-dom/submission.ts +++ b/packages/conform-dom/submission.ts @@ -14,9 +14,14 @@ export type SubmissionState = { validated: Record; }; +export type SubmissionPayload = + | Entry + | SubmissionPayload[] + | { [key: string]: SubmissionPayload }; + export type SubmissionContext = { intent: Intent | null; - payload: Record; + payload: Record>; fields: Set; value?: Value; error?: Record | null; @@ -26,13 +31,13 @@ export type SubmissionContext = { export type Submission = | { status: 'success'; - payload: Record; + payload: Record>; value: FormValue; reply(options?: ReplyOptions): SubmissionResult; } | { status: 'error' | undefined; - payload: Record; + payload: Record>; error: Record | null; reply(options?: ReplyOptions): SubmissionResult; }; @@ -40,7 +45,7 @@ export type Submission = export type SubmissionResult = { status?: 'error' | 'success'; intent?: Intent; - initialValue?: Record | null; + initialValue?: Record> | null; fields?: string[]; error?: Record; state?: SubmissionState; @@ -275,15 +280,20 @@ export function replySubmission( } : undefined; + const initialValue = + (normalize( + context.payload, + // We can't serialize the file and send it back from the server, but we can preserve it in the client + typeof document !== 'undefined', + // We need the file on the client because it's treated as the form value + // But we will exclude the File type for now as it's only used by the internal + // form state and we will remove the need to preserve the file on the client soon + ) as Record>) ?? {}; + return { status: context.intent ? undefined : error ? 'error' : 'success', intent: context.intent ? context.intent : undefined, - initialValue: - normalize( - context.payload, - // We can't serialize the file and send it back from the server, but we can preserve it in the client - typeof document !== 'undefined', - ) ?? {}, + initialValue, error, state: context.state, fields: Array.from(context.fields),