@pyreon/form 0.0.1
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/LICENSE +21 -0
- package/README.md +259 -0
- package/lib/analysis/devtools.js.html +5406 -0
- package/lib/analysis/index.js.html +5406 -0
- package/lib/devtools.js +74 -0
- package/lib/devtools.js.map +1 -0
- package/lib/index.js +451 -0
- package/lib/index.js.map +1 -0
- package/lib/types/devtools.d.ts +67 -0
- package/lib/types/devtools.d.ts.map +1 -0
- package/lib/types/devtools2.d.ts +31 -0
- package/lib/types/devtools2.d.ts.map +1 -0
- package/lib/types/index.d.ts +457 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index2.d.ts +291 -0
- package/lib/types/index2.d.ts.map +1 -0
- package/package.json +57 -0
- package/src/context.ts +67 -0
- package/src/devtools.ts +100 -0
- package/src/index.ts +26 -0
- package/src/tests/devtools.test.ts +206 -0
- package/src/tests/form.test.ts +1860 -0
- package/src/types.ts +116 -0
- package/src/use-field-array.ts +117 -0
- package/src/use-field.ts +69 -0
- package/src/use-form-state.ts +74 -0
- package/src/use-form.ts +436 -0
- package/src/use-watch.ts +69 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { Props, VNode, VNodeChild } from "@pyreon/core";
|
|
2
|
+
import { Computed, Signal } from "@pyreon/reactivity";
|
|
3
|
+
|
|
4
|
+
//#region src/types.d.ts
|
|
5
|
+
type ValidationError = string | undefined;
|
|
6
|
+
/**
|
|
7
|
+
* A reactive value that can be read by calling it.
|
|
8
|
+
* Both `Signal<T>` and `Computed<T>` satisfy this interface.
|
|
9
|
+
*/
|
|
10
|
+
type Accessor<T> = Signal<T> | Computed<T>;
|
|
11
|
+
/**
|
|
12
|
+
* Field validator function. Receives the field value and all current form values
|
|
13
|
+
* for cross-field validation.
|
|
14
|
+
*/
|
|
15
|
+
type ValidateFn<T, TValues = Record<string, unknown>> = (value: T, allValues: TValues) => ValidationError | Promise<ValidationError>;
|
|
16
|
+
type SchemaValidateFn<TValues> = (values: TValues) => Partial<Record<keyof TValues, ValidationError>> | Promise<Partial<Record<keyof TValues, ValidationError>>>;
|
|
17
|
+
interface FieldState<T = unknown> {
|
|
18
|
+
/** Current field value. */
|
|
19
|
+
value: Signal<T>;
|
|
20
|
+
/** Field error message (undefined if no error). */
|
|
21
|
+
error: Signal<ValidationError>;
|
|
22
|
+
/** Whether the field has been blurred at least once. */
|
|
23
|
+
touched: Signal<boolean>;
|
|
24
|
+
/** Whether the field value differs from its initial value. */
|
|
25
|
+
dirty: Signal<boolean>;
|
|
26
|
+
/** Set the field value. */
|
|
27
|
+
setValue: (value: T) => void;
|
|
28
|
+
/** Mark the field as touched (typically on blur). */
|
|
29
|
+
setTouched: () => void;
|
|
30
|
+
/** Reset the field to its initial value and clear error/touched/dirty. */
|
|
31
|
+
reset: () => void;
|
|
32
|
+
}
|
|
33
|
+
/** Props returned by `register()` for binding an input element. */
|
|
34
|
+
interface FieldRegisterProps<T> {
|
|
35
|
+
value: Signal<T>;
|
|
36
|
+
onInput: (e: Event) => void;
|
|
37
|
+
onBlur: () => void;
|
|
38
|
+
checked?: Accessor<boolean>;
|
|
39
|
+
}
|
|
40
|
+
interface FormState<TValues extends Record<string, unknown>> {
|
|
41
|
+
/** Individual field states keyed by field name. */
|
|
42
|
+
fields: { [K in keyof TValues]: FieldState<TValues[K]> };
|
|
43
|
+
/** Whether the form is currently being submitted. */
|
|
44
|
+
isSubmitting: Signal<boolean>;
|
|
45
|
+
/** Whether async validation is currently running. */
|
|
46
|
+
isValidating: Signal<boolean>;
|
|
47
|
+
/** Whether any field has an error (computed — read-only). */
|
|
48
|
+
isValid: Accessor<boolean>;
|
|
49
|
+
/** Whether any field value differs from its initial value (computed — read-only). */
|
|
50
|
+
isDirty: Accessor<boolean>;
|
|
51
|
+
/** Number of times the form has been submitted. */
|
|
52
|
+
submitCount: Signal<number>;
|
|
53
|
+
/** Error thrown by onSubmit (undefined if no error). */
|
|
54
|
+
submitError: Signal<unknown>;
|
|
55
|
+
/** All current form values as a plain object. */
|
|
56
|
+
values: () => TValues;
|
|
57
|
+
/** All current errors as a record. */
|
|
58
|
+
errors: () => Partial<Record<keyof TValues, ValidationError>>;
|
|
59
|
+
/** Set a single field's value. */
|
|
60
|
+
setFieldValue: <K extends keyof TValues>(field: K, value: TValues[K]) => void;
|
|
61
|
+
/** Set a single field's error (e.g. from server-side validation). */
|
|
62
|
+
setFieldError: (field: keyof TValues, error: ValidationError) => void;
|
|
63
|
+
/** Set multiple field errors at once (e.g. from server-side validation). */
|
|
64
|
+
setErrors: (errors: Partial<Record<keyof TValues, ValidationError>>) => void;
|
|
65
|
+
/** Clear all field errors. */
|
|
66
|
+
clearErrors: () => void;
|
|
67
|
+
/** Reset a single field to its initial value. */
|
|
68
|
+
resetField: (field: keyof TValues) => void;
|
|
69
|
+
/**
|
|
70
|
+
* Returns props for binding an input element to a field.
|
|
71
|
+
* For text/select: includes `value` signal, `onInput`, and `onBlur`.
|
|
72
|
+
* For checkboxes: pass `{ type: 'checkbox' }` to also get a `checked` signal.
|
|
73
|
+
* For numbers: pass `{ type: 'number' }` to use `valueAsNumber` on input.
|
|
74
|
+
*/
|
|
75
|
+
register: <K extends keyof TValues & string>(field: K, options?: {
|
|
76
|
+
type?: 'checkbox' | 'number';
|
|
77
|
+
}) => FieldRegisterProps<TValues[K]>;
|
|
78
|
+
/**
|
|
79
|
+
* Submit handler — runs validation, then calls onSubmit if valid.
|
|
80
|
+
* Can be called directly or as a form event handler (calls preventDefault).
|
|
81
|
+
*/
|
|
82
|
+
handleSubmit: (e?: Event) => Promise<void>;
|
|
83
|
+
/** Reset all fields to initial values. */
|
|
84
|
+
reset: () => void;
|
|
85
|
+
/** Validate all fields and return whether the form is valid. */
|
|
86
|
+
validate: () => Promise<boolean>;
|
|
87
|
+
}
|
|
88
|
+
interface UseFormOptions<TValues extends Record<string, unknown>> {
|
|
89
|
+
/** Initial values for each field. */
|
|
90
|
+
initialValues: TValues;
|
|
91
|
+
/** Called with validated values on successful submit. */
|
|
92
|
+
onSubmit: (values: TValues) => void | Promise<void>;
|
|
93
|
+
/** Per-field validators. Receives field value and all form values. */
|
|
94
|
+
validators?: Partial<{ [K in keyof TValues]: ValidateFn<TValues[K], TValues> }>;
|
|
95
|
+
/** Schema-level validator (runs after field validators). */
|
|
96
|
+
schema?: SchemaValidateFn<TValues>;
|
|
97
|
+
/** When to validate: 'blur' (default), 'change', or 'submit'. */
|
|
98
|
+
validateOn?: 'blur' | 'change' | 'submit';
|
|
99
|
+
/** Debounce delay in ms for validators (useful for async validators). */
|
|
100
|
+
debounceMs?: number;
|
|
101
|
+
}
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/use-form.d.ts
|
|
104
|
+
/**
|
|
105
|
+
* Create a signal-based form. Returns reactive field states, form-level
|
|
106
|
+
* signals, and handlers for submit/reset/validate.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* const form = useForm({
|
|
110
|
+
* initialValues: { email: '', password: '', remember: false },
|
|
111
|
+
* validators: {
|
|
112
|
+
* email: (v) => (!v ? 'Required' : undefined),
|
|
113
|
+
* password: (v, all) => (v.length < 8 ? 'Too short' : undefined),
|
|
114
|
+
* },
|
|
115
|
+
* onSubmit: async (values) => { await login(values) },
|
|
116
|
+
* })
|
|
117
|
+
*
|
|
118
|
+
* // Bind with register():
|
|
119
|
+
* // h('input', form.register('email'))
|
|
120
|
+
* // h('input', { type: 'checkbox', ...form.register('remember', { type: 'checkbox' }) })
|
|
121
|
+
*/
|
|
122
|
+
declare function useForm<TValues extends Record<string, unknown>>(options: UseFormOptions<TValues>): FormState<TValues>;
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region src/use-field-array.d.ts
|
|
125
|
+
interface FieldArrayItem<T> {
|
|
126
|
+
/** Stable key for keyed rendering. */
|
|
127
|
+
key: number;
|
|
128
|
+
/** Reactive value for this item. */
|
|
129
|
+
value: Signal<T>;
|
|
130
|
+
}
|
|
131
|
+
interface UseFieldArrayResult<T> {
|
|
132
|
+
/** Reactive list of items with stable keys. */
|
|
133
|
+
items: Signal<FieldArrayItem<T>[]>;
|
|
134
|
+
/** Number of items. */
|
|
135
|
+
length: Computed<number>;
|
|
136
|
+
/** Append a new item to the end. */
|
|
137
|
+
append: (value: T) => void;
|
|
138
|
+
/** Prepend a new item to the start. */
|
|
139
|
+
prepend: (value: T) => void;
|
|
140
|
+
/** Insert an item at the given index. */
|
|
141
|
+
insert: (index: number, value: T) => void;
|
|
142
|
+
/** Remove the item at the given index. */
|
|
143
|
+
remove: (index: number) => void;
|
|
144
|
+
/** Update the value of an item at the given index. */
|
|
145
|
+
update: (index: number, value: T) => void;
|
|
146
|
+
/** Move an item from one index to another. */
|
|
147
|
+
move: (from: number, to: number) => void;
|
|
148
|
+
/** Swap two items by index. */
|
|
149
|
+
swap: (indexA: number, indexB: number) => void;
|
|
150
|
+
/** Replace all items. */
|
|
151
|
+
replace: (values: T[]) => void;
|
|
152
|
+
/** Get all current values as a plain array. */
|
|
153
|
+
values: () => T[];
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Manage a dynamic array of form fields with stable keys.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* const tags = useFieldArray<string>([])
|
|
160
|
+
* tags.append('typescript')
|
|
161
|
+
* tags.append('pyreon')
|
|
162
|
+
* // tags.items() — array of { key, value } for keyed rendering
|
|
163
|
+
*/
|
|
164
|
+
declare function useFieldArray<T>(initial?: T[]): UseFieldArrayResult<T>;
|
|
165
|
+
//#endregion
|
|
166
|
+
//#region src/use-field.d.ts
|
|
167
|
+
interface UseFieldResult<T> {
|
|
168
|
+
/** Current field value (reactive signal). */
|
|
169
|
+
value: Signal<T>;
|
|
170
|
+
/** Field error message (reactive signal). */
|
|
171
|
+
error: Signal<ValidationError>;
|
|
172
|
+
/** Whether the field has been touched (reactive signal). */
|
|
173
|
+
touched: Signal<boolean>;
|
|
174
|
+
/** Whether the field value differs from initial (reactive signal). */
|
|
175
|
+
dirty: Signal<boolean>;
|
|
176
|
+
/** Set the field value. */
|
|
177
|
+
setValue: (value: T) => void;
|
|
178
|
+
/** Mark the field as touched. */
|
|
179
|
+
setTouched: () => void;
|
|
180
|
+
/** Reset the field to its initial value. */
|
|
181
|
+
reset: () => void;
|
|
182
|
+
/** Register props for input binding. */
|
|
183
|
+
register: (opts?: {
|
|
184
|
+
type?: 'checkbox';
|
|
185
|
+
}) => FieldRegisterProps<T>;
|
|
186
|
+
/** Whether the field has an error (computed). */
|
|
187
|
+
hasError: Computed<boolean>;
|
|
188
|
+
/** Whether the field should show its error (touched + has error). */
|
|
189
|
+
showError: Computed<boolean>;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Extract a single field's state and helpers from a form instance.
|
|
193
|
+
* Useful for building isolated field components.
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* function EmailField({ form }: { form: FormState<{ email: string }> }) {
|
|
197
|
+
* const field = useField(form, 'email')
|
|
198
|
+
* return (
|
|
199
|
+
* <>
|
|
200
|
+
* <input {...field.register()} />
|
|
201
|
+
* {field.showError() && <span>{field.error()}</span>}
|
|
202
|
+
* </>
|
|
203
|
+
* )
|
|
204
|
+
* }
|
|
205
|
+
*/
|
|
206
|
+
declare function useField<TValues extends Record<string, unknown>, K extends keyof TValues & string>(form: FormState<TValues>, name: K): UseFieldResult<TValues[K]>;
|
|
207
|
+
//#endregion
|
|
208
|
+
//#region src/use-watch.d.ts
|
|
209
|
+
/**
|
|
210
|
+
* Watch specific field values reactively. Returns a computed signal
|
|
211
|
+
* that re-evaluates when any of the watched fields change.
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* // Watch a single field
|
|
215
|
+
* const email = useWatch(form, 'email')
|
|
216
|
+
* // email() => current email value
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* // Watch multiple fields
|
|
220
|
+
* const [first, last] = useWatch(form, ['firstName', 'lastName'])
|
|
221
|
+
* // first() => firstName value, last() => lastName value
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* // Watch all fields
|
|
225
|
+
* const all = useWatch(form)
|
|
226
|
+
* // all() => { email: '...', password: '...' }
|
|
227
|
+
*/
|
|
228
|
+
declare function useWatch<TValues extends Record<string, unknown>, K extends keyof TValues & string>(form: FormState<TValues>, name: K): Signal<TValues[K]>;
|
|
229
|
+
declare function useWatch<TValues extends Record<string, unknown>, K extends (keyof TValues & string)[]>(form: FormState<TValues>, names: K): { [I in keyof K]: Signal<TValues[K[I] & keyof TValues]> };
|
|
230
|
+
declare function useWatch<TValues extends Record<string, unknown>>(form: FormState<TValues>): Computed<TValues>;
|
|
231
|
+
//#endregion
|
|
232
|
+
//#region src/use-form-state.d.ts
|
|
233
|
+
interface FormStateSummary<TValues extends Record<string, unknown>> {
|
|
234
|
+
isSubmitting: boolean;
|
|
235
|
+
isValidating: boolean;
|
|
236
|
+
isValid: boolean;
|
|
237
|
+
isDirty: boolean;
|
|
238
|
+
submitCount: number;
|
|
239
|
+
submitError: unknown;
|
|
240
|
+
touchedFields: Partial<Record<keyof TValues, boolean>>;
|
|
241
|
+
dirtyFields: Partial<Record<keyof TValues, boolean>>;
|
|
242
|
+
errors: Partial<Record<keyof TValues, ValidationError>>;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Subscribe to the full form state as a single computed signal.
|
|
246
|
+
* Useful for rendering form-level UI (submit button disabled state,
|
|
247
|
+
* error summaries, progress indicators).
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* const state = useFormState(form)
|
|
251
|
+
* // state() => { isSubmitting, isValid, isDirty, errors, ... }
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* // Use a selector for fine-grained reactivity
|
|
255
|
+
* const canSubmit = useFormState(form, (s) => s.isValid && !s.isSubmitting)
|
|
256
|
+
*/
|
|
257
|
+
declare function useFormState<TValues extends Record<string, unknown>>(form: FormState<TValues>): Computed<FormStateSummary<TValues>>;
|
|
258
|
+
declare function useFormState<TValues extends Record<string, unknown>, R>(form: FormState<TValues>, selector: (state: FormStateSummary<TValues>) => R): Computed<R>;
|
|
259
|
+
//#endregion
|
|
260
|
+
//#region src/context.d.ts
|
|
261
|
+
interface FormProviderProps<TValues extends Record<string, unknown>> extends Props {
|
|
262
|
+
form: FormState<TValues>;
|
|
263
|
+
children?: VNodeChild;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Provide a form instance to the component tree so nested components
|
|
267
|
+
* can access it via `useFormContext()`.
|
|
268
|
+
*
|
|
269
|
+
* @example
|
|
270
|
+
* const form = useForm({ initialValues: { email: '' }, onSubmit: ... })
|
|
271
|
+
*
|
|
272
|
+
* <FormProvider form={form}>
|
|
273
|
+
* <EmailField />
|
|
274
|
+
* <SubmitButton />
|
|
275
|
+
* </FormProvider>
|
|
276
|
+
*/
|
|
277
|
+
declare function FormProvider<TValues extends Record<string, unknown>>(props: FormProviderProps<TValues>): VNode;
|
|
278
|
+
/**
|
|
279
|
+
* Access the form instance from the nearest `FormProvider`.
|
|
280
|
+
* Must be called within a component tree wrapped by `FormProvider`.
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* function EmailField() {
|
|
284
|
+
* const form = useFormContext<{ email: string }>()
|
|
285
|
+
* return <input {...form.register('email')} />
|
|
286
|
+
* }
|
|
287
|
+
*/
|
|
288
|
+
declare function useFormContext<TValues extends Record<string, unknown> = Record<string, unknown>>(): FormState<TValues>;
|
|
289
|
+
//#endregion
|
|
290
|
+
export { type Accessor, type FieldArrayItem, type FieldRegisterProps, type FieldState, FormProvider, type FormState, type FormStateSummary, type SchemaValidateFn, type UseFieldArrayResult, type UseFieldResult, type UseFormOptions, type ValidateFn, type ValidationError, useField, useFieldArray, useForm, useFormContext, useFormState, useWatch };
|
|
291
|
+
//# sourceMappingURL=index2.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index2.d.ts","names":[],"sources":["../../src/types.ts","../../src/use-form.ts","../../src/use-field-array.ts","../../src/use-field.ts","../../src/use-watch.ts","../../src/use-form-state.ts","../../src/context.ts"],"mappings":";;;;KAEY,eAAA;;;AAAZ;;KAMY,QAAA,MAAc,MAAA,CAAO,CAAA,IAAK,QAAA,CAAS,CAAA;;;AAA/C;;KAMY,UAAA,cAAwB,MAAA,sBAClC,KAAA,EAAO,CAAA,EACP,SAAA,EAAW,OAAA,KACR,eAAA,GAAkB,OAAA,CAAQ,eAAA;AAAA,KAEnB,gBAAA,aACV,MAAA,EAAQ,OAAA,KAEN,OAAA,CAAQ,MAAA,OAAa,OAAA,EAAS,eAAA,KAC9B,OAAA,CAAQ,OAAA,CAAQ,MAAA,OAAa,OAAA,EAAS,eAAA;AAAA,UAEzB,UAAA;EAjB8B;EAmB7C,KAAA,EAAO,MAAA,CAAO,CAAA;EAnB8B;EAqB5C,KAAA,EAAO,MAAA,CAAO,eAAA;EArBK;EAuBnB,OAAA,EAAS,MAAA;EAvBsB;EAyB/B,KAAA,EAAO,MAAA;EAzBsC;EA2B7C,QAAA,GAAW,KAAA,EAAO,CAAA;EA3B4B;EA6B9C,UAAA;EAvBoB;EAyBpB,KAAA;AAAA;;UAIe,kBAAA;EACf,KAAA,EAAO,MAAA,CAAO,CAAA;EACd,OAAA,GAAU,CAAA,EAAG,KAAA;EACb,MAAA;EACA,OAAA,GAAU,QAAA;AAAA;AAAA,UAGK,SAAA,iBAA0B,MAAA;EApCjB;EAsCxB,MAAA,gBAAsB,OAAA,GAAU,UAAA,CAAW,OAAA,CAAQ,CAAA;EApCxC;EAsCX,YAAA,EAAc,MAAA;EArCX;EAuCH,YAAA,EAAc,MAAA;EAvCe;EAyC7B,OAAA,EAAS,QAAA;EAzCmC;EA2C5C,OAAA,EAAS,QAAA;EAzCiB;EA2C1B,WAAA,EAAa,MAAA;EA1CL;EA4CR,WAAA,EAAa,MAAA;EA1CmB;EA4ChC,MAAA,QAAc,OAAA;EA5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@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pyreon/form",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Signal-based form management for Pyreon",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/pyreon/fundamentals.git",
|
|
9
|
+
"directory": "packages/form"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/pyreon/fundamentals/tree/main/packages/form#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/pyreon/fundamentals/issues"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"lib",
|
|
20
|
+
"src",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"main": "./lib/index.js",
|
|
27
|
+
"module": "./lib/index.js",
|
|
28
|
+
"types": "./lib/types/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"bun": "./src/index.ts",
|
|
32
|
+
"import": "./lib/index.js",
|
|
33
|
+
"types": "./lib/types/index.d.ts"
|
|
34
|
+
},
|
|
35
|
+
"./devtools": {
|
|
36
|
+
"bun": "./src/devtools.ts",
|
|
37
|
+
"import": "./lib/devtools.js",
|
|
38
|
+
"types": "./lib/types/devtools.d.ts"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "vl_rolldown_build",
|
|
43
|
+
"dev": "vl_rolldown_build-watch",
|
|
44
|
+
"test": "vitest run",
|
|
45
|
+
"typecheck": "tsc --noEmit"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@pyreon/core": "^0.2.1",
|
|
49
|
+
"@pyreon/reactivity": "^0.2.1"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@happy-dom/global-registrator": "^20.8.3",
|
|
53
|
+
"@pyreon/core": "^0.2.1",
|
|
54
|
+
"@pyreon/reactivity": "^0.2.1",
|
|
55
|
+
"@pyreon/runtime-dom": "^0.2.1"
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/context.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
pushContext,
|
|
4
|
+
popContext,
|
|
5
|
+
onUnmount,
|
|
6
|
+
useContext,
|
|
7
|
+
} from '@pyreon/core'
|
|
8
|
+
import type { VNodeChild, VNode, Props } from '@pyreon/core'
|
|
9
|
+
import type { FormState } from './types'
|
|
10
|
+
|
|
11
|
+
const FormContext = createContext<FormState<Record<string, unknown>> | null>(
|
|
12
|
+
null,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
export interface FormProviderProps<TValues extends Record<string, unknown>>
|
|
16
|
+
extends Props {
|
|
17
|
+
form: FormState<TValues>
|
|
18
|
+
children?: VNodeChild
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Provide a form instance to the component tree so nested components
|
|
23
|
+
* can access it via `useFormContext()`.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* const form = useForm({ initialValues: { email: '' }, onSubmit: ... })
|
|
27
|
+
*
|
|
28
|
+
* <FormProvider form={form}>
|
|
29
|
+
* <EmailField />
|
|
30
|
+
* <SubmitButton />
|
|
31
|
+
* </FormProvider>
|
|
32
|
+
*/
|
|
33
|
+
export function FormProvider<TValues extends Record<string, unknown>>(
|
|
34
|
+
props: FormProviderProps<TValues>,
|
|
35
|
+
): VNode {
|
|
36
|
+
const frame = new Map([[FormContext.id, props.form]])
|
|
37
|
+
pushContext(frame)
|
|
38
|
+
|
|
39
|
+
onUnmount(() => popContext())
|
|
40
|
+
|
|
41
|
+
const ch = props.children
|
|
42
|
+
return (typeof ch === 'function' ? (ch as () => VNodeChild)() : ch) as VNode
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Access the form instance from the nearest `FormProvider`.
|
|
47
|
+
* Must be called within a component tree wrapped by `FormProvider`.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* function EmailField() {
|
|
51
|
+
* const form = useFormContext<{ email: string }>()
|
|
52
|
+
* return <input {...form.register('email')} />
|
|
53
|
+
* }
|
|
54
|
+
*/
|
|
55
|
+
export function useFormContext<
|
|
56
|
+
TValues extends Record<string, unknown> = Record<string, unknown>,
|
|
57
|
+
>(): FormState<TValues> {
|
|
58
|
+
const form = useContext(FormContext)
|
|
59
|
+
if (!form) {
|
|
60
|
+
throw new Error(
|
|
61
|
+
'[@pyreon/form] useFormContext() must be used within a <FormProvider>.',
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
// Generic narrowing: context stores FormState<Record<string, unknown>>
|
|
65
|
+
// but callers narrow to their specific TValues at the call site.
|
|
66
|
+
return form as FormState<TValues>
|
|
67
|
+
}
|
package/src/devtools.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @pyreon/form devtools introspection API.
|
|
3
|
+
* Import: `import { ... } from "@pyreon/form/devtools"`
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const _activeForms = new Map<string, WeakRef<object>>()
|
|
7
|
+
const _listeners = new Set<() => void>()
|
|
8
|
+
|
|
9
|
+
function _notify(): void {
|
|
10
|
+
for (const listener of _listeners) listener()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Register a form instance for devtools inspection.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* const form = useForm({ ... })
|
|
18
|
+
* registerForm("login-form", form)
|
|
19
|
+
*/
|
|
20
|
+
export function registerForm(name: string, form: object): void {
|
|
21
|
+
_activeForms.set(name, new WeakRef(form))
|
|
22
|
+
_notify()
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Unregister a form instance. */
|
|
26
|
+
export function unregisterForm(name: string): void {
|
|
27
|
+
_activeForms.delete(name)
|
|
28
|
+
_notify()
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Get all registered form names. Cleans up garbage-collected instances. */
|
|
32
|
+
export function getActiveForms(): string[] {
|
|
33
|
+
for (const [name, ref] of _activeForms) {
|
|
34
|
+
if (ref.deref() === undefined) _activeForms.delete(name)
|
|
35
|
+
}
|
|
36
|
+
return [..._activeForms.keys()]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Get a form instance by name (or undefined if GC'd or not registered). */
|
|
40
|
+
export function getFormInstance(name: string): object | undefined {
|
|
41
|
+
const ref = _activeForms.get(name)
|
|
42
|
+
if (!ref) return undefined
|
|
43
|
+
const instance = ref.deref()
|
|
44
|
+
if (!instance) {
|
|
45
|
+
_activeForms.delete(name)
|
|
46
|
+
return undefined
|
|
47
|
+
}
|
|
48
|
+
return instance
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Get a snapshot of a registered form's current state.
|
|
53
|
+
* Returns values, errors, and form-level status signals.
|
|
54
|
+
*/
|
|
55
|
+
export function getFormSnapshot(
|
|
56
|
+
name: string,
|
|
57
|
+
): Record<string, unknown> | undefined {
|
|
58
|
+
const form = getFormInstance(name) as Record<string, unknown> | undefined
|
|
59
|
+
if (!form) return undefined
|
|
60
|
+
return {
|
|
61
|
+
values:
|
|
62
|
+
typeof form.values === 'function'
|
|
63
|
+
? (form.values as () => unknown)()
|
|
64
|
+
: undefined,
|
|
65
|
+
errors:
|
|
66
|
+
typeof form.errors === 'function'
|
|
67
|
+
? (form.errors as () => unknown)()
|
|
68
|
+
: undefined,
|
|
69
|
+
isSubmitting:
|
|
70
|
+
typeof form.isSubmitting === 'function'
|
|
71
|
+
? (form.isSubmitting as () => unknown)()
|
|
72
|
+
: undefined,
|
|
73
|
+
isValid:
|
|
74
|
+
typeof form.isValid === 'function'
|
|
75
|
+
? (form.isValid as () => unknown)()
|
|
76
|
+
: undefined,
|
|
77
|
+
isDirty:
|
|
78
|
+
typeof form.isDirty === 'function'
|
|
79
|
+
? (form.isDirty as () => unknown)()
|
|
80
|
+
: undefined,
|
|
81
|
+
submitCount:
|
|
82
|
+
typeof form.submitCount === 'function'
|
|
83
|
+
? (form.submitCount as () => unknown)()
|
|
84
|
+
: undefined,
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Subscribe to form registry changes. Returns unsubscribe function. */
|
|
89
|
+
export function onFormChange(listener: () => void): () => void {
|
|
90
|
+
_listeners.add(listener)
|
|
91
|
+
return () => {
|
|
92
|
+
_listeners.delete(listener)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** @internal — reset devtools registry (for tests). */
|
|
97
|
+
export function _resetDevtools(): void {
|
|
98
|
+
_activeForms.clear()
|
|
99
|
+
_listeners.clear()
|
|
100
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export { useForm } from './use-form'
|
|
2
|
+
export { useFieldArray } from './use-field-array'
|
|
3
|
+
export { useField } from './use-field'
|
|
4
|
+
export { useWatch } from './use-watch'
|
|
5
|
+
export { useFormState } from './use-form-state'
|
|
6
|
+
export { FormProvider, useFormContext } from './context'
|
|
7
|
+
|
|
8
|
+
export type {
|
|
9
|
+
Accessor,
|
|
10
|
+
FieldState,
|
|
11
|
+
FieldRegisterProps,
|
|
12
|
+
FormState,
|
|
13
|
+
UseFormOptions,
|
|
14
|
+
ValidationError,
|
|
15
|
+
ValidateFn,
|
|
16
|
+
SchemaValidateFn,
|
|
17
|
+
} from './types'
|
|
18
|
+
|
|
19
|
+
export type {
|
|
20
|
+
FieldArrayItem,
|
|
21
|
+
UseFieldArrayResult,
|
|
22
|
+
} from './use-field-array'
|
|
23
|
+
|
|
24
|
+
export type { UseFieldResult } from './use-field'
|
|
25
|
+
|
|
26
|
+
export type { FormStateSummary } from './use-form-state'
|