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
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,42 @@ const { status } = useFormDraftRHF(form, { key: 'rhf-profile', schema: zodAdapte
return <form>{/* form.register, etc. */}<span>{status}</span></form>;
```

## Formik integration

```tsx
import { useFormik } from 'formik';
import { useFormDraftFormik } from 'formdraft/formik';

const formik = useFormik({
initialValues: { name: '', bio: '' },
onSubmit: async (v) => api.submitProfile(v),
});

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

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:

```tsx
const { discard } = useFormDraftFormik(formik, options);

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

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.)

## What it handles

| Production form pain | formdraft |
Expand Down
92 changes: 92 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 @@ -17,6 +17,11 @@
"import": "./dist/rhf/index.mjs",
"require": "./dist/rhf/index.js"
},
"./formik": {
"types": "./dist/formik/index.d.ts",
"import": "./dist/formik/index.mjs",
"require": "./dist/formik/index.js"
},
"./storage/indexedDB": {
"types": "./dist/storage/indexedDB.d.ts",
"import": "./dist/storage/indexedDB.mjs",
Expand Down Expand Up @@ -46,12 +51,16 @@
"peerDependencies": {
"react": ">=18",
"react-hook-form": ">=7.0.0",
"formik": ">=2.4.0",
"zod": ">=3.0.0"
},
"peerDependenciesMeta": {
"react-hook-form": {
"optional": true
},
"formik": {
"optional": true
},
"zod": {
"optional": true
}
Expand All @@ -67,6 +76,7 @@
"eslint": "^8.57.0",
"eslint-plugin-react-hooks": "^7.1.1",
"fake-indexeddb": "^6.0.0",
"formik": "^2.4.9",
"jsdom": "^25.0.0",
"playwright": "^1.60.0",
"react": "^18.3.0",
Expand Down
Loading
Loading