Skip to content

Commit 680b4f4

Browse files
committed
fix(conform-react): support resolveSubmission on server
1 parent 7c54230 commit 680b4f4

5 files changed

Lines changed: 52 additions & 55 deletions

File tree

.changeset/calm-forms-resolve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@conform-to/react': patch
3+
---
4+
5+
fix: make `resolveSubmission` available in React Server Components

packages/conform-react/future/forms.tsx

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { isFieldElement, FieldName } from '@conform-to/dom';
2-
import {
3-
type Submission,
4-
DEFAULT_INTENT_NAME,
5-
defaultSerialize,
6-
FormValue,
7-
} from '@conform-to/dom/future';
2+
import { DEFAULT_INTENT_NAME, defaultSerialize } from '@conform-to/dom/future';
83
import { useContext, useMemo, useId, useState, createContext } from 'react';
94
import {
105
focusFirstInvalidField,
@@ -36,7 +31,6 @@ import type {
3631
IntentDispatcher,
3732
RequireKey,
3833
ValidateResult,
39-
FormIntent,
4034
CustomStateHandler,
4135
} from './types';
4236
import {
@@ -46,10 +40,9 @@ import {
4640
validateStandardSchemaV1,
4741
} from './util';
4842
import {
43+
createSubmissionResolver,
4944
defaultIntentHandlers,
5045
mergeIntentHandlers,
51-
parseIntent,
52-
resolveIntent,
5346
} from './intent';
5447

5548
export function configureForms<
@@ -84,6 +77,7 @@ export function configureForms<
8477
defaultIntentHandlers,
8578
config.intents,
8679
);
80+
const resolveSubmission = createSubmissionResolver(globalIntentHandlers);
8781

8882
/**
8983
* Global configuration with defaults applied
@@ -746,43 +740,6 @@ export function configureForms<
746740
);
747741
}
748742

749-
/**
750-
* Parses and resolves a submission payload using the configured default and global intent handlers.
751-
* Pass `handlers` to extend or override them for a specific call.
752-
*/
753-
function resolveSubmission<
754-
IntentHandlers extends Record<string, IntentHandler<any, any>> = {},
755-
>(
756-
submission: Submission,
757-
options?: {
758-
handlers?: IntentHandlers;
759-
},
760-
): {
761-
intent:
762-
| FormIntent<
763-
Record<string, any>,
764-
DefaultIntentHandlers & GlobalIntentHandlers & IntentHandlers
765-
>
766-
| { type: 'submit'; payload: string | undefined }
767-
| undefined;
768-
targetValue: Record<string, FormValue> | undefined;
769-
} {
770-
const handlers = mergeIntentHandlers(
771-
globalIntentHandlers,
772-
options?.handlers,
773-
);
774-
const intent = parseIntent(submission.intent, { handlers });
775-
const targetValue = resolveIntent(submission, {
776-
handlers,
777-
intent,
778-
});
779-
780-
return {
781-
intent,
782-
targetValue,
783-
};
784-
}
785-
786743
return {
787744
FormProvider,
788745
useForm,
@@ -916,10 +873,3 @@ export const useField = defaultForms.useField;
916873
* ```
917874
*/
918875
export const useIntent = defaultForms.useIntent;
919-
920-
/**
921-
* Parses and resolves a submission payload using the default configured form intent handlers.
922-
*
923-
* See https://conform.guide/api/react/future/resolveSubmission
924-
*/
925-
export const resolveSubmission = defaultForms.resolveSubmission;

packages/conform-react/future/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ export {
4242
useFormMetadata,
4343
useField,
4444
useIntent,
45-
resolveSubmission,
4645
} from './forms';
47-
export { defineIntent } from './intent';
46+
export { defineIntent, resolveSubmission } from './intent';
4847
export { defineCustomState } from './state';
4948
export {
5049
BaseControl,

packages/conform-react/future/intent.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,3 +560,46 @@ export const defaultIntentHandlers: DefaultIntentHandlers = {
560560
remove,
561561
reorder,
562562
};
563+
564+
export function createSubmissionResolver<
565+
GlobalIntentHandlers extends Record<string, IntentHandler<any, any>>,
566+
>(globalIntentHandlers: GlobalIntentHandlers) {
567+
return function resolveSubmission<
568+
IntentHandlers extends Record<string, IntentHandler<any, any>> = {},
569+
>(
570+
submission: Submission,
571+
options?: {
572+
handlers?: IntentHandlers;
573+
},
574+
): {
575+
intent:
576+
| FormIntent<Record<string, any>, GlobalIntentHandlers & IntentHandlers>
577+
| { type: 'submit'; payload: string | undefined }
578+
| undefined;
579+
targetValue: Record<string, FormValue> | undefined;
580+
} {
581+
const handlers = mergeIntentHandlers(
582+
globalIntentHandlers,
583+
options?.handlers,
584+
);
585+
const intent = parseIntent(submission.intent, { handlers });
586+
const targetValue = resolveIntent(submission, {
587+
handlers,
588+
intent,
589+
});
590+
591+
return {
592+
intent,
593+
targetValue,
594+
};
595+
};
596+
}
597+
598+
/**
599+
* Parses and resolves a submission payload using the default configured form intent handlers.
600+
*
601+
* See https://conform.guide/api/react/future/resolveSubmission
602+
*/
603+
export const resolveSubmission = createSubmissionResolver(
604+
defaultIntentHandlers,
605+
);

packages/conform-react/tests/resolveSubmission.browser.test.tsx renamed to packages/conform-react/tests/resolveSubmission.test.ts

File renamed without changes.

0 commit comments

Comments
 (0)