Skip to content

Commit

Permalink
fix: ensure latest formdata to be used on form submit
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed Aug 16, 2024
1 parent d71c693 commit 3e1f69d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
29 changes: 16 additions & 13 deletions packages/conform-react/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
useSyncExternalStore,
useRef,
} from 'react';
import { flushSync } from 'react-dom';

export type Pretty<T> = { [K in keyof T]: T[K] } & {};

Expand Down Expand Up @@ -484,20 +485,22 @@ export function createFormContext<
return {
...context,
submit(event) {
const submitEvent = event.nativeEvent as SubmitEvent;
const result = context.submit(submitEvent);

if (
!result.submission ||
result.submission.status === 'success' ||
result.submission.error === null
) {
if (!result.formData.has(INTENT)) {
onSubmit?.(event, result);
flushSync(() => {
const submitEvent = event.nativeEvent as SubmitEvent;
const result = context.submit(submitEvent);

if (
!result.submission ||
result.submission.status === 'success' ||
result.submission.error === null
) {
if (!result.formData.has(INTENT)) {
onSubmit?.(event, result);
}
} else {
event.preventDefault();
}
} else {
event.preventDefault();
}
});
},
onUpdate(options) {
onSubmit = options.onSubmit;
Expand Down
5 changes: 4 additions & 1 deletion packages/conform-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@
"@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-node-resolve": "^13.3.0",
"@types/react": "^18.2.43",
"@types/react-dom": "^18.3.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rollup-plugin-copy": "^3.4.0",
"rollup": "^2.79.1"
},
"peerDependencies": {
"react": ">=18"
"react": ">=18",
"react-dom": ">=18"
},
"keywords": [
"constraint-validation",
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3e1f69d

Please sign in to comment.