Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ import { useFormDraftFormik } from 'formdraft/formik';

const formik = useFormik({
initialValues: { name: '', bio: '' },
onSubmit: async (v) => api.submitProfile(v),
onSubmit: async (values) => {
await api.submitProfile(values);
discard(); // ← clears storage + broadcasts to other tabs + resets formik
},
});

const { status, lastSavedAt, discard } = useFormDraftFormik(formik, {
Expand All @@ -89,21 +92,30 @@ const { status, lastSavedAt, discard } = useFormDraftFormik(formik, {

Restore happens once on mount when storage has a valid draft AND the user hasn't started typing (gated on `formik.dirty`); after that, formik is the source of truth and every value change is persisted automatically.

**On successful submit, call `discard()`** to clear the stored draft and broadcast to other tabs:
**On successful submit, call `discard()`** to clear the stored draft and broadcast to other tabs. Without this, the draft survives in storage and reappears on next mount even though the user has already submitted it. (RHF users have the same responsibility — formdraft never assumes submit "happened" until the host form library tells us.)

```tsx
const { discard } = useFormDraftFormik(formik, options);
## TanStack Form integration

const formik = useFormik({
initialValues: { name: '', bio: '' },
onSubmit: async (values) => {
await api.submitProfile(values);
discard(); // ← clears storage + broadcasts to other tabs + resets formik
```tsx
import { useForm } from '@tanstack/react-form';
import { useFormDraftTanstack } from 'formdraft/tanstack-form';

const form = useForm({
defaultValues: { name: '', bio: '' },
onSubmit: async ({ value }) => {
await api.submitProfile(value);
discard(); // ← clears storage + broadcasts + resets form
},
});

const { status, lastSavedAt, discard } = useFormDraftTanstack(form, {
key: 'profile-form',
schema: zodAdapter(Schema),
sync: api.saveProfile,
});
```

Without this, the draft survives in storage and reappears on next mount even though the user has already submitted it. (RHF users have the same responsibility — formdraft never assumes submit "happened" until the host form library tells us.)
Restore happens once on mount when storage has a valid draft AND the user hasn't already started typing. Restore calls `setFieldValue` per top-level key with `{ dontValidate: true }` so onChange validators don't paint errors against text the user never typed. (We deliberately do *not* pass `dontUpdateMeta`: TanStack's `FieldApi.update` reseeds any field whose `isTouched` is still `false` back to its `defaultValue` prop on the next render, which would silently wipe restored data on the idiomatic `<form.Field name="x" defaultValue="">`.) As with the Formik adapter, **call `discard()` in your `onSubmit`** after a successful API submit so the just-submitted draft doesn't reappear on next page load.

## What it handles

Expand Down
104 changes: 104 additions & 0 deletions package-lock.json

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

10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
"import": "./dist/formik/index.mjs",
"require": "./dist/formik/index.js"
},
"./tanstack-form": {
"types": "./dist/tanstack-form/index.d.ts",
"import": "./dist/tanstack-form/index.mjs",
"require": "./dist/tanstack-form/index.js"
},
"./storage/indexedDB": {
"types": "./dist/storage/indexedDB.d.ts",
"import": "./dist/storage/indexedDB.mjs",
Expand Down Expand Up @@ -52,6 +57,7 @@
"react": ">=18",
"react-hook-form": ">=7.0.0",
"formik": ">=2.4.0",
"@tanstack/react-form": ">=1.0.0",
"zod": ">=3.0.0"
},
"peerDependenciesMeta": {
Expand All @@ -61,13 +67,17 @@
"formik": {
"optional": true
},
"@tanstack/react-form": {
"optional": true
},
"zod": {
"optional": true
}
},
"devDependencies": {
"@playwright/test": "^1.60.0",
"@size-limit/preset-small-lib": "^11.0.0",
"@tanstack/react-form": "^1.32.1",
"@testing-library/react": "^16.0.0",
"@types/node": "^20.19.41",
"@types/react": "^18.3.0",
Expand Down
Loading
Loading