react-form-draft 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ARCHITECTURE.md +33 -0
- package/LICENSE +21 -0
- package/README.md +338 -0
- package/dist/index.cjs +545 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +142 -0
- package/dist/index.d.ts +142 -0
- package/dist/index.js +542 -0
- package/dist/index.js.map +1 -0
- package/package.json +77 -0
package/ARCHITECTURE.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Architecture Notes
|
|
2
|
+
|
|
3
|
+
## Key Design Decisions
|
|
4
|
+
|
|
5
|
+
- The public API is centered on a single `useFormDraft` hook because the MVP is intentionally optimized for React Hook Form usage.
|
|
6
|
+
- Drafts are stored as structured records with `version`, `savedAt`, and `values` so restore logic can safely reject stale or malformed data.
|
|
7
|
+
- Storage is abstracted behind a small synchronous adapter interface. The default adapter uses safe localStorage access, but the hook and core helpers do not depend on browser globals directly.
|
|
8
|
+
- Include and exclude filters support nested dot-paths. `include` runs first, then `exclude` trims that subset. This keeps the rules predictable.
|
|
9
|
+
- Restore merges persisted values into the current form snapshot instead of replacing the entire object. That avoids wiping excluded or non-persisted fields during restore.
|
|
10
|
+
|
|
11
|
+
## Tradeoffs
|
|
12
|
+
|
|
13
|
+
- The MVP is intentionally tied to React Hook Form types rather than introducing a broader generic form abstraction. That keeps the API smaller and the implementation clearer.
|
|
14
|
+
- Storage is synchronous because localStorage is synchronous. Async adapters such as IndexedDB are postponed to avoid complicating the hook contract in v1.0.
|
|
15
|
+
- Equality checks are implemented internally for JSON-like values rather than pulling in a deep comparison dependency. This keeps runtime weight low.
|
|
16
|
+
- The hook does not attempt multi-tab coordination yet. Draft state is scoped to the current tab lifecycle except for storage persistence itself.
|
|
17
|
+
|
|
18
|
+
## Security Considerations
|
|
19
|
+
|
|
20
|
+
- Browser storage is not secure for secrets. The package does not encrypt data and does not claim otherwise.
|
|
21
|
+
- The README documents explicit exclusions for sensitive fields such as passwords, tokens, SSNs, and card data.
|
|
22
|
+
- The package never performs network requests, telemetry, analytics, or dynamic code execution.
|
|
23
|
+
- All storage access is wrapped so storage failures do not crash the host application.
|
|
24
|
+
|
|
25
|
+
## Intentionally Postponed To v1.1
|
|
26
|
+
|
|
27
|
+
- `sessionStorage` adapter
|
|
28
|
+
- examples for custom adapters
|
|
29
|
+
- multi-tab synchronization through the `storage` event
|
|
30
|
+
- before-unload and route-leave helpers
|
|
31
|
+
- richer draft metadata and change tracking
|
|
32
|
+
- field-level transforms and alternative serializers
|
|
33
|
+
- IndexedDB exploration for large drafts
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pooya
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
# react-form-draft
|
|
2
|
+
|
|
3
|
+
`react-form-draft` helps React applications persist and restore non-sensitive form drafts in the browser, with a first-class React Hook Form API.
|
|
4
|
+
|
|
5
|
+
It is designed for long forms, dashboard settings pages, onboarding flows, and other form-heavy screens where users may refresh, close a tab, or return later.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install react-form-draft
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Peer dependencies:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install react react-hook-form
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import { useForm } from 'react-hook-form';
|
|
23
|
+
import { useFormDraft } from 'react-form-draft';
|
|
24
|
+
|
|
25
|
+
interface ProfileFormValues {
|
|
26
|
+
firstName: string;
|
|
27
|
+
lastName: string;
|
|
28
|
+
bio: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function ProfileForm() {
|
|
32
|
+
const form = useForm<ProfileFormValues>({
|
|
33
|
+
defaultValues: {
|
|
34
|
+
firstName: '',
|
|
35
|
+
lastName: '',
|
|
36
|
+
bio: '',
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const draft = useFormDraft({
|
|
41
|
+
form,
|
|
42
|
+
key: 'profile-form',
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<form onSubmit={form.handleSubmit((values) => console.log(values))}>
|
|
47
|
+
<input {...form.register('firstName')} />
|
|
48
|
+
<input {...form.register('lastName')} />
|
|
49
|
+
<textarea {...form.register('bio')} />
|
|
50
|
+
|
|
51
|
+
{draft.hasDraft ? (
|
|
52
|
+
<div>
|
|
53
|
+
<button type="button" onClick={draft.restoreDraft}>
|
|
54
|
+
Restore draft
|
|
55
|
+
</button>
|
|
56
|
+
<button type="button" onClick={draft.discardDraft}>
|
|
57
|
+
Discard draft
|
|
58
|
+
</button>
|
|
59
|
+
</div>
|
|
60
|
+
) : null}
|
|
61
|
+
|
|
62
|
+
<button type="submit">Save</button>
|
|
63
|
+
</form>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## React Hook Form Example
|
|
69
|
+
|
|
70
|
+
```tsx
|
|
71
|
+
import { useForm } from 'react-hook-form';
|
|
72
|
+
import { useFormDraft } from 'react-form-draft';
|
|
73
|
+
|
|
74
|
+
interface SettingsFormValues {
|
|
75
|
+
displayName: string;
|
|
76
|
+
email: string;
|
|
77
|
+
password: string;
|
|
78
|
+
token: string;
|
|
79
|
+
preferences: {
|
|
80
|
+
locale: string;
|
|
81
|
+
timezone: string;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function SettingsForm() {
|
|
86
|
+
const form = useForm<SettingsFormValues>({
|
|
87
|
+
defaultValues: {
|
|
88
|
+
displayName: '',
|
|
89
|
+
email: '',
|
|
90
|
+
password: '',
|
|
91
|
+
token: '',
|
|
92
|
+
preferences: {
|
|
93
|
+
locale: 'en',
|
|
94
|
+
timezone: 'UTC',
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const {
|
|
100
|
+
hasDraft,
|
|
101
|
+
draftMeta,
|
|
102
|
+
status,
|
|
103
|
+
restoreDraft,
|
|
104
|
+
discardDraft,
|
|
105
|
+
saveNow,
|
|
106
|
+
} = useFormDraft({
|
|
107
|
+
form,
|
|
108
|
+
key: 'settings-form',
|
|
109
|
+
version: 'settings-v2',
|
|
110
|
+
debounceMs: 800,
|
|
111
|
+
autoRestore: false,
|
|
112
|
+
clearOnSubmit: true,
|
|
113
|
+
exclude: [
|
|
114
|
+
'password',
|
|
115
|
+
'token',
|
|
116
|
+
],
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
return (
|
|
120
|
+
<form
|
|
121
|
+
onSubmit={form.handleSubmit(async (values) => {
|
|
122
|
+
await saveSettings(values);
|
|
123
|
+
})}
|
|
124
|
+
>
|
|
125
|
+
{hasDraft ? (
|
|
126
|
+
<aside>
|
|
127
|
+
<p>
|
|
128
|
+
Draft saved at{' '}
|
|
129
|
+
{draftMeta ? new Date(draftMeta.savedAt).toLocaleString() : 'unknown'}
|
|
130
|
+
</p>
|
|
131
|
+
<button type="button" onClick={restoreDraft}>
|
|
132
|
+
Restore
|
|
133
|
+
</button>
|
|
134
|
+
<button type="button" onClick={discardDraft}>
|
|
135
|
+
Discard
|
|
136
|
+
</button>
|
|
137
|
+
</aside>
|
|
138
|
+
) : null}
|
|
139
|
+
|
|
140
|
+
<input {...form.register('displayName')} />
|
|
141
|
+
<input {...form.register('email')} />
|
|
142
|
+
<input type="password" {...form.register('password')} />
|
|
143
|
+
|
|
144
|
+
<button type="button" onClick={saveNow}>
|
|
145
|
+
Save Draft Now
|
|
146
|
+
</button>
|
|
147
|
+
<button type="submit">Submit</button>
|
|
148
|
+
|
|
149
|
+
<output>{status}</output>
|
|
150
|
+
</form>
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## API Reference
|
|
156
|
+
|
|
157
|
+
### `useFormDraft(options)`
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
function useFormDraft<TFieldValues extends FieldValues>(
|
|
161
|
+
options: UseFormDraftOptions<TFieldValues>,
|
|
162
|
+
): UseFormDraftResult;
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Options
|
|
166
|
+
|
|
167
|
+
| Option | Type | Default | Notes |
|
|
168
|
+
| --- | --- | --- | --- |
|
|
169
|
+
| `form` | `UseFormReturn<TFieldValues>` | Required | React Hook Form instance returned by `useForm`. |
|
|
170
|
+
| `key` | `string` | Required | Unique storage key for the form draft. |
|
|
171
|
+
| `debounceMs` | `number` | `500` | Delay before writes are persisted. |
|
|
172
|
+
| `version` | `string \| null` | `null` | Drafts with a different version are ignored. |
|
|
173
|
+
| `storage` | `DraftStorageAdapter` | Safe localStorage adapter | Useful for tests or custom storage implementations. |
|
|
174
|
+
| `include` | `Path<TFieldValues>[]` | `undefined` | Only these fields are persisted. |
|
|
175
|
+
| `exclude` | `Path<TFieldValues>[]` | `undefined` | These fields are removed from persistence. |
|
|
176
|
+
| `autoRestore` | `boolean` | `false` | Restores immediately instead of exposing a manual prompt flow. |
|
|
177
|
+
| `clearOnSubmit` | `boolean` | `true` | Removes the draft after a successful submit. |
|
|
178
|
+
| `removeCorruptedDraft` | `boolean` | `true` | Removes malformed JSON drafts automatically. |
|
|
179
|
+
| `resetOptions` | `Parameters<form.reset>[1]` | `undefined` | Forwarded to React Hook Form `reset` during restore. |
|
|
180
|
+
|
|
181
|
+
`include` and `exclude` use dot-paths. When both are provided, `include` is applied first and `exclude` removes paths from that subset.
|
|
182
|
+
|
|
183
|
+
### Return Value
|
|
184
|
+
|
|
185
|
+
| Field | Type | Notes |
|
|
186
|
+
| --- | --- | --- |
|
|
187
|
+
| `hasDraft` | `boolean` | `true` when a valid stored draft is currently available. |
|
|
188
|
+
| `draftMeta` | `DraftMeta \| null` | Includes `key`, `version`, and `savedAt`. |
|
|
189
|
+
| `lastSavedAt` | `number \| null` | Convenience alias for `draftMeta?.savedAt`. |
|
|
190
|
+
| `status` | `'idle' \| 'saved' \| 'restored' \| 'cleared' \| 'error'` | Latest draft action result. |
|
|
191
|
+
| `restoreDraft()` | `() => boolean` | Restores the cached draft into the form and returns whether it succeeded. |
|
|
192
|
+
| `discardDraft()` | `() => void` | Removes the persisted draft without mutating current form values. |
|
|
193
|
+
| `clearDraft()` | `() => void` | Same behavior as `discardDraft`, useful after submit or reset flows. |
|
|
194
|
+
| `saveNow()` | `() => void` | Saves immediately, bypassing the debounce timer. |
|
|
195
|
+
|
|
196
|
+
## Restore And Discard Flow
|
|
197
|
+
|
|
198
|
+
Manual restore mode is the default:
|
|
199
|
+
|
|
200
|
+
```tsx
|
|
201
|
+
const draft = useFormDraft({
|
|
202
|
+
form,
|
|
203
|
+
key: 'article-editor',
|
|
204
|
+
autoRestore: false,
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
if (draft.hasDraft) {
|
|
208
|
+
return (
|
|
209
|
+
<div>
|
|
210
|
+
<button type="button" onClick={draft.restoreDraft}>
|
|
211
|
+
Restore draft
|
|
212
|
+
</button>
|
|
213
|
+
<button type="button" onClick={draft.discardDraft}>
|
|
214
|
+
Start fresh
|
|
215
|
+
</button>
|
|
216
|
+
</div>
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
If you prefer silent restore:
|
|
222
|
+
|
|
223
|
+
```tsx
|
|
224
|
+
useFormDraft({
|
|
225
|
+
form,
|
|
226
|
+
key: 'article-editor',
|
|
227
|
+
autoRestore: true,
|
|
228
|
+
});
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## Include / Exclude Examples
|
|
232
|
+
|
|
233
|
+
Persist only part of a form:
|
|
234
|
+
|
|
235
|
+
```tsx
|
|
236
|
+
useFormDraft({
|
|
237
|
+
form,
|
|
238
|
+
key: 'company-profile',
|
|
239
|
+
include: ['name', 'description', 'contact.email'],
|
|
240
|
+
});
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
Exclude sensitive or irrelevant fields:
|
|
244
|
+
|
|
245
|
+
```tsx
|
|
246
|
+
useFormDraft({
|
|
247
|
+
form,
|
|
248
|
+
key: 'payment-form',
|
|
249
|
+
exclude: [
|
|
250
|
+
'password',
|
|
251
|
+
'token',
|
|
252
|
+
'accessToken',
|
|
253
|
+
'refreshToken',
|
|
254
|
+
'ssn',
|
|
255
|
+
'creditCard',
|
|
256
|
+
'cvv',
|
|
257
|
+
'secret',
|
|
258
|
+
],
|
|
259
|
+
});
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
## Versioning Example
|
|
263
|
+
|
|
264
|
+
```tsx
|
|
265
|
+
useFormDraft({
|
|
266
|
+
form,
|
|
267
|
+
key: 'product-form',
|
|
268
|
+
version: 'product-schema-v3',
|
|
269
|
+
});
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
If the stored draft version does not match, the draft is ignored. This is useful when the form schema changes and older drafts should not be restored.
|
|
273
|
+
|
|
274
|
+
## Custom Storage Adapter
|
|
275
|
+
|
|
276
|
+
The adapter interface is synchronous by design for the MVP:
|
|
277
|
+
|
|
278
|
+
```ts
|
|
279
|
+
interface DraftStorageAdapter {
|
|
280
|
+
getItem(key: string): string | null;
|
|
281
|
+
setItem(key: string, value: string): void;
|
|
282
|
+
removeItem(key: string): void;
|
|
283
|
+
isAvailable?(): boolean;
|
|
284
|
+
}
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
This keeps the package simple while leaving room for future adapters such as `sessionStorage` or IndexedDB-backed implementations.
|
|
288
|
+
|
|
289
|
+
## SSR Note
|
|
290
|
+
|
|
291
|
+
`react-form-draft` is safe to import in SSR environments. The default adapter no-ops when `window.localStorage` is unavailable. Draft reads and writes happen only when the browser storage layer is available.
|
|
292
|
+
|
|
293
|
+
## Security Note
|
|
294
|
+
|
|
295
|
+
This package stores drafts in browser storage. `localStorage` is not secure for secrets.
|
|
296
|
+
|
|
297
|
+
Do not persist passwords, tokens, payment data, national identifiers, or other sensitive fields unless you provide your own secure storage adapter and you fully understand the tradeoffs.
|
|
298
|
+
|
|
299
|
+
Recommended exclusions include:
|
|
300
|
+
|
|
301
|
+
- `password`
|
|
302
|
+
- `token`
|
|
303
|
+
- `accessToken`
|
|
304
|
+
- `refreshToken`
|
|
305
|
+
- `ssn`
|
|
306
|
+
- `creditCard`
|
|
307
|
+
- `cvv`
|
|
308
|
+
- `secret`
|
|
309
|
+
|
|
310
|
+
This package never sends data over the network, does not use analytics, and does not attempt fake “encryption” inside browser storage.
|
|
311
|
+
|
|
312
|
+
## Limitations
|
|
313
|
+
|
|
314
|
+
- The MVP is optimized for React Hook Form.
|
|
315
|
+
- Draft values should be JSON-serializable.
|
|
316
|
+
- The default adapter uses synchronous browser storage.
|
|
317
|
+
- Multi-tab draft synchronization is not included yet.
|
|
318
|
+
|
|
319
|
+
## Build Output
|
|
320
|
+
|
|
321
|
+
The package ships both ESM and CJS builds so it can work cleanly in modern bundlers and older Node/CommonJS integrations. The runtime code stays dependency-light, and type declarations are generated alongside the bundle.
|
|
322
|
+
|
|
323
|
+
## Roadmap
|
|
324
|
+
|
|
325
|
+
Planned post-MVP work:
|
|
326
|
+
|
|
327
|
+
- `sessionStorage` adapter
|
|
328
|
+
- custom adapter cookbook
|
|
329
|
+
- improved last-saved metadata helpers
|
|
330
|
+
- multi-tab synchronization via the `storage` event
|
|
331
|
+
- optional `beforeunload` warning helper
|
|
332
|
+
- optional route-leave guard helper
|
|
333
|
+
- field transformers and custom serializers
|
|
334
|
+
- possible IndexedDB adapter for larger drafts
|
|
335
|
+
|
|
336
|
+
## License
|
|
337
|
+
|
|
338
|
+
MIT
|