Skip to content

Commit

Permalink
submission payload
Browse files Browse the repository at this point in the history
  • Loading branch information
timvandam authored and edmundhung committed Aug 17, 2024
1 parent 55a8a4e commit cd91132
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-humans-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@conform-to/dom': patch
---

Replace submission payload unknown types
30 changes: 20 additions & 10 deletions packages/conform-dom/submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ export type SubmissionState = {
validated: Record<string, boolean>;
};

export type SubmissionPayload<Entry extends FormDataEntryValue> =
| Entry
| SubmissionPayload<Entry>[]
| { [key: string]: SubmissionPayload<Entry> };

export type SubmissionContext<Value = null, FormError = string[]> = {
intent: Intent | null;
payload: Record<string, unknown>;
payload: Record<string, SubmissionPayload<FormDataEntryValue>>;
fields: Set<string>;
value?: Value;
error?: Record<string, FormError | null> | null;
Expand All @@ -26,21 +31,21 @@ export type SubmissionContext<Value = null, FormError = string[]> = {
export type Submission<Schema, FormError = string[], FormValue = Schema> =
| {
status: 'success';
payload: Record<string, unknown>;
payload: Record<string, SubmissionPayload<FormDataEntryValue>>;
value: FormValue;
reply(options?: ReplyOptions<FormError>): SubmissionResult<FormError>;
}
| {
status: 'error' | undefined;
payload: Record<string, unknown>;
payload: Record<string, SubmissionPayload<FormDataEntryValue>>;
error: Record<string, FormError | null> | null;
reply(options?: ReplyOptions<FormError>): SubmissionResult<FormError>;
};

export type SubmissionResult<FormError = string[]> = {
status?: 'error' | 'success';
intent?: Intent;
initialValue?: Record<string, unknown> | null;
initialValue?: Record<string, SubmissionPayload<string>> | null;
fields?: string[];
error?: Record<string, FormError | null>;
state?: SubmissionState;
Expand Down Expand Up @@ -275,15 +280,20 @@ export function replySubmission<FormError>(
}
: 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<string, SubmissionPayload<string>>) ?? {};

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),
Expand Down

0 comments on commit cd91132

Please sign in to comment.