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.
@@ -0,0 +1,142 @@
1
+ import { FieldValues, UseFormReturn, Path } from 'react-hook-form';
2
+
3
+ /**
4
+ * Synchronous storage adapter used by the draft layer.
5
+ *
6
+ * The optional `isAvailable` hook lets adapters communicate that reads and writes
7
+ * should be skipped entirely, which is helpful for SSR and restricted browsers.
8
+ */
9
+ interface DraftStorageAdapter {
10
+ getItem(key: string): string | null;
11
+ setItem(key: string, value: string): void;
12
+ removeItem(key: string): void;
13
+ isAvailable?(): boolean;
14
+ }
15
+
16
+ type DraftValueMap = Record<string, unknown>;
17
+ type DraftStatus = 'idle' | 'saved' | 'restored' | 'cleared' | 'error';
18
+ interface DraftRecord<TValues extends DraftValueMap = DraftValueMap> {
19
+ version: string | null;
20
+ savedAt: number;
21
+ values: TValues;
22
+ }
23
+ interface DraftMeta {
24
+ key: string;
25
+ version: string | null;
26
+ savedAt: number;
27
+ }
28
+ type UseFormDraftResetOptions<TFieldValues extends FieldValues> = Parameters<UseFormReturn<TFieldValues>['reset']>[1];
29
+ interface UseFormDraftOptions<TFieldValues extends FieldValues> {
30
+ /**
31
+ * React Hook Form instance returned by `useForm`.
32
+ */
33
+ form: UseFormReturn<TFieldValues>;
34
+ /**
35
+ * Unique key used to persist this form's draft.
36
+ */
37
+ key: string;
38
+ /**
39
+ * Delay between value changes and a persisted save.
40
+ *
41
+ * Defaults to `500`.
42
+ */
43
+ debounceMs?: number;
44
+ /**
45
+ * Optional schema or application version.
46
+ *
47
+ * When the stored version does not match, the draft is ignored.
48
+ */
49
+ version?: string | null;
50
+ /**
51
+ * Custom storage adapter. Defaults to a safe localStorage adapter.
52
+ */
53
+ storage?: DraftStorageAdapter;
54
+ /**
55
+ * Fields to persist. Supports nested dot-paths from React Hook Form `Path<T>`.
56
+ *
57
+ * When `include` and `exclude` are both provided, `include` is applied first
58
+ * and `exclude` removes paths from that subset.
59
+ */
60
+ include?: ReadonlyArray<Path<TFieldValues>>;
61
+ /**
62
+ * Fields to omit from persistence. Supports nested dot-paths.
63
+ */
64
+ exclude?: ReadonlyArray<Path<TFieldValues>>;
65
+ /**
66
+ * Restore the draft automatically on initialization.
67
+ *
68
+ * Defaults to `false`.
69
+ */
70
+ autoRestore?: boolean;
71
+ /**
72
+ * Remove the stored draft after a successful form submission.
73
+ *
74
+ * Defaults to `true`.
75
+ */
76
+ clearOnSubmit?: boolean;
77
+ /**
78
+ * Remove malformed JSON drafts so they do not keep failing on future loads.
79
+ *
80
+ * Defaults to `true`.
81
+ */
82
+ removeCorruptedDraft?: boolean;
83
+ /**
84
+ * Optional reset options forwarded to `form.reset` when restoring a draft.
85
+ */
86
+ resetOptions?: UseFormDraftResetOptions<TFieldValues>;
87
+ }
88
+ interface UseFormDraftResult {
89
+ /**
90
+ * Indicates whether a valid draft currently exists in storage.
91
+ */
92
+ hasDraft: boolean;
93
+ /**
94
+ * Metadata for the current draft, if one exists.
95
+ */
96
+ draftMeta: DraftMeta | null;
97
+ /**
98
+ * Convenience alias of `draftMeta?.savedAt`.
99
+ */
100
+ lastSavedAt: number | null;
101
+ /**
102
+ * Lifecycle status for the most recent draft action.
103
+ */
104
+ status: DraftStatus;
105
+ /**
106
+ * Restore the stored draft into the form.
107
+ *
108
+ * Returns `true` when a draft was restored.
109
+ */
110
+ restoreDraft(): boolean;
111
+ /**
112
+ * Remove the persisted draft without mutating current form values.
113
+ *
114
+ * This is an alias of `clearDraft` for the "dismiss restore prompt" use case.
115
+ */
116
+ discardDraft(): void;
117
+ /**
118
+ * Remove the persisted draft without mutating current form values.
119
+ */
120
+ clearDraft(): void;
121
+ /**
122
+ * Persist the current form state immediately.
123
+ */
124
+ saveNow(): void;
125
+ }
126
+
127
+ /**
128
+ * Persist and restore React Hook Form values as a browser draft.
129
+ *
130
+ * The hook defaults to manual restore mode so consuming applications can decide
131
+ * whether to auto-restore or show their own confirmation UI.
132
+ */
133
+ declare function useFormDraft<TFieldValues extends FieldValues>(options: UseFormDraftOptions<TFieldValues>): UseFormDraftResult;
134
+
135
+ /**
136
+ * Safe localStorage adapter used by default by `useFormDraft`.
137
+ *
138
+ * Reads and writes no-op when `window.localStorage` is unavailable or throws.
139
+ */
140
+ declare function createLocalStorageAdapter(): DraftStorageAdapter;
141
+
142
+ export { type DraftMeta, type DraftRecord, type DraftStatus, type DraftStorageAdapter, type UseFormDraftOptions, type UseFormDraftResetOptions, type UseFormDraftResult, createLocalStorageAdapter, useFormDraft };
@@ -0,0 +1,142 @@
1
+ import { FieldValues, UseFormReturn, Path } from 'react-hook-form';
2
+
3
+ /**
4
+ * Synchronous storage adapter used by the draft layer.
5
+ *
6
+ * The optional `isAvailable` hook lets adapters communicate that reads and writes
7
+ * should be skipped entirely, which is helpful for SSR and restricted browsers.
8
+ */
9
+ interface DraftStorageAdapter {
10
+ getItem(key: string): string | null;
11
+ setItem(key: string, value: string): void;
12
+ removeItem(key: string): void;
13
+ isAvailable?(): boolean;
14
+ }
15
+
16
+ type DraftValueMap = Record<string, unknown>;
17
+ type DraftStatus = 'idle' | 'saved' | 'restored' | 'cleared' | 'error';
18
+ interface DraftRecord<TValues extends DraftValueMap = DraftValueMap> {
19
+ version: string | null;
20
+ savedAt: number;
21
+ values: TValues;
22
+ }
23
+ interface DraftMeta {
24
+ key: string;
25
+ version: string | null;
26
+ savedAt: number;
27
+ }
28
+ type UseFormDraftResetOptions<TFieldValues extends FieldValues> = Parameters<UseFormReturn<TFieldValues>['reset']>[1];
29
+ interface UseFormDraftOptions<TFieldValues extends FieldValues> {
30
+ /**
31
+ * React Hook Form instance returned by `useForm`.
32
+ */
33
+ form: UseFormReturn<TFieldValues>;
34
+ /**
35
+ * Unique key used to persist this form's draft.
36
+ */
37
+ key: string;
38
+ /**
39
+ * Delay between value changes and a persisted save.
40
+ *
41
+ * Defaults to `500`.
42
+ */
43
+ debounceMs?: number;
44
+ /**
45
+ * Optional schema or application version.
46
+ *
47
+ * When the stored version does not match, the draft is ignored.
48
+ */
49
+ version?: string | null;
50
+ /**
51
+ * Custom storage adapter. Defaults to a safe localStorage adapter.
52
+ */
53
+ storage?: DraftStorageAdapter;
54
+ /**
55
+ * Fields to persist. Supports nested dot-paths from React Hook Form `Path<T>`.
56
+ *
57
+ * When `include` and `exclude` are both provided, `include` is applied first
58
+ * and `exclude` removes paths from that subset.
59
+ */
60
+ include?: ReadonlyArray<Path<TFieldValues>>;
61
+ /**
62
+ * Fields to omit from persistence. Supports nested dot-paths.
63
+ */
64
+ exclude?: ReadonlyArray<Path<TFieldValues>>;
65
+ /**
66
+ * Restore the draft automatically on initialization.
67
+ *
68
+ * Defaults to `false`.
69
+ */
70
+ autoRestore?: boolean;
71
+ /**
72
+ * Remove the stored draft after a successful form submission.
73
+ *
74
+ * Defaults to `true`.
75
+ */
76
+ clearOnSubmit?: boolean;
77
+ /**
78
+ * Remove malformed JSON drafts so they do not keep failing on future loads.
79
+ *
80
+ * Defaults to `true`.
81
+ */
82
+ removeCorruptedDraft?: boolean;
83
+ /**
84
+ * Optional reset options forwarded to `form.reset` when restoring a draft.
85
+ */
86
+ resetOptions?: UseFormDraftResetOptions<TFieldValues>;
87
+ }
88
+ interface UseFormDraftResult {
89
+ /**
90
+ * Indicates whether a valid draft currently exists in storage.
91
+ */
92
+ hasDraft: boolean;
93
+ /**
94
+ * Metadata for the current draft, if one exists.
95
+ */
96
+ draftMeta: DraftMeta | null;
97
+ /**
98
+ * Convenience alias of `draftMeta?.savedAt`.
99
+ */
100
+ lastSavedAt: number | null;
101
+ /**
102
+ * Lifecycle status for the most recent draft action.
103
+ */
104
+ status: DraftStatus;
105
+ /**
106
+ * Restore the stored draft into the form.
107
+ *
108
+ * Returns `true` when a draft was restored.
109
+ */
110
+ restoreDraft(): boolean;
111
+ /**
112
+ * Remove the persisted draft without mutating current form values.
113
+ *
114
+ * This is an alias of `clearDraft` for the "dismiss restore prompt" use case.
115
+ */
116
+ discardDraft(): void;
117
+ /**
118
+ * Remove the persisted draft without mutating current form values.
119
+ */
120
+ clearDraft(): void;
121
+ /**
122
+ * Persist the current form state immediately.
123
+ */
124
+ saveNow(): void;
125
+ }
126
+
127
+ /**
128
+ * Persist and restore React Hook Form values as a browser draft.
129
+ *
130
+ * The hook defaults to manual restore mode so consuming applications can decide
131
+ * whether to auto-restore or show their own confirmation UI.
132
+ */
133
+ declare function useFormDraft<TFieldValues extends FieldValues>(options: UseFormDraftOptions<TFieldValues>): UseFormDraftResult;
134
+
135
+ /**
136
+ * Safe localStorage adapter used by default by `useFormDraft`.
137
+ *
138
+ * Reads and writes no-op when `window.localStorage` is unavailable or throws.
139
+ */
140
+ declare function createLocalStorageAdapter(): DraftStorageAdapter;
141
+
142
+ export { type DraftMeta, type DraftRecord, type DraftStatus, type DraftStorageAdapter, type UseFormDraftOptions, type UseFormDraftResetOptions, type UseFormDraftResult, createLocalStorageAdapter, useFormDraft };