@pyreon/form 0.11.4 → 0.11.6
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/README.md +89 -89
- package/lib/devtools.js.map +1 -1
- package/lib/index.js.map +1 -1
- package/lib/types/index.d.ts +3 -3
- package/package.json +16 -16
- package/src/context.ts +5 -5
- package/src/devtools.ts +6 -6
- package/src/index.ts +10 -13
- package/src/tests/devtools.test.ts +53 -53
- package/src/tests/form-additional.test.tsx +117 -117
- package/src/tests/form.test.tsx +374 -374
- package/src/types.ts +3 -3
- package/src/use-field-array.ts +2 -2
- package/src/use-field.ts +4 -4
- package/src/use-form-state.ts +3 -3
- package/src/use-form.ts +16 -16
- package/src/use-watch.ts +3 -3
package/README.md
CHANGED
|
@@ -11,25 +11,25 @@ bun add @pyreon/form
|
|
|
11
11
|
## Quick Start
|
|
12
12
|
|
|
13
13
|
```tsx
|
|
14
|
-
import { useForm } from
|
|
14
|
+
import { useForm } from '@pyreon/form'
|
|
15
15
|
|
|
16
16
|
function LoginForm() {
|
|
17
17
|
const form = useForm({
|
|
18
|
-
initialValues: { email:
|
|
18
|
+
initialValues: { email: '', password: '' },
|
|
19
19
|
validators: {
|
|
20
|
-
email: (v) => (!v.includes(
|
|
21
|
-
password: (v) => (v.length < 8 ?
|
|
20
|
+
email: (v) => (!v.includes('@') ? 'Invalid email' : undefined),
|
|
21
|
+
password: (v) => (v.length < 8 ? 'Too short' : undefined),
|
|
22
22
|
},
|
|
23
|
-
validateOn:
|
|
23
|
+
validateOn: 'blur',
|
|
24
24
|
onSubmit: async (values) => {
|
|
25
|
-
await fetch(
|
|
25
|
+
await fetch('/api/login', { method: 'POST', body: JSON.stringify(values) })
|
|
26
26
|
},
|
|
27
27
|
})
|
|
28
28
|
|
|
29
29
|
return () => (
|
|
30
30
|
<form onSubmit={form.handleSubmit}>
|
|
31
|
-
<input type="email" {...form.register(
|
|
32
|
-
<input type="password" {...form.register(
|
|
31
|
+
<input type="email" {...form.register('email')} />
|
|
32
|
+
<input type="password" {...form.register('password')} />
|
|
33
33
|
<button type="submit">Login</button>
|
|
34
34
|
</form>
|
|
35
35
|
)
|
|
@@ -42,37 +42,37 @@ function LoginForm() {
|
|
|
42
42
|
|
|
43
43
|
Create a reactive form instance with field states, validation, and submission handling.
|
|
44
44
|
|
|
45
|
-
| Parameter
|
|
46
|
-
|
|
|
47
|
-
| `options.initialValues` | `TValues`
|
|
48
|
-
| `options.onSubmit`
|
|
49
|
-
| `options.validators`
|
|
50
|
-
| `options.schema`
|
|
51
|
-
| `options.validateOn`
|
|
52
|
-
| `options.debounceMs`
|
|
45
|
+
| Parameter | Type | Description |
|
|
46
|
+
| ----------------------- | -------------------------------------------- | ---------------------------------------------------- |
|
|
47
|
+
| `options.initialValues` | `TValues` | Initial values for each field |
|
|
48
|
+
| `options.onSubmit` | `(values: TValues) => void \| Promise<void>` | Called with validated values on submit |
|
|
49
|
+
| `options.validators` | `Partial<Record<keyof TValues, ValidateFn>>` | Per-field validators (receives value + all values) |
|
|
50
|
+
| `options.schema` | `SchemaValidateFn<TValues>` | Schema-level validator (runs after field validators) |
|
|
51
|
+
| `options.validateOn` | `"blur" \| "change" \| "submit"` | When to trigger validation (default: `"blur"`) |
|
|
52
|
+
| `options.debounceMs` | `number` | Debounce delay for validators in ms |
|
|
53
53
|
|
|
54
54
|
**Returns:** `FormState<TValues>` with these properties:
|
|
55
55
|
|
|
56
|
-
| Property
|
|
57
|
-
|
|
|
58
|
-
| `fields`
|
|
59
|
-
| `isSubmitting`
|
|
60
|
-
| `isValidating`
|
|
61
|
-
| `isValid`
|
|
62
|
-
| `isDirty`
|
|
63
|
-
| `submitCount`
|
|
64
|
-
| `submitError`
|
|
65
|
-
| `values()`
|
|
66
|
-
| `errors()`
|
|
67
|
-
| `register(field, opts?)`
|
|
68
|
-
| `handleSubmit(e?)`
|
|
69
|
-
| `validate()`
|
|
70
|
-
| `reset()`
|
|
71
|
-
| `setFieldValue(field, value)` | `Function`
|
|
72
|
-
| `setFieldError(field, error)` | `Function`
|
|
73
|
-
| `setErrors(errors)`
|
|
74
|
-
| `clearErrors()`
|
|
75
|
-
| `resetField(field)`
|
|
56
|
+
| Property | Type | Description |
|
|
57
|
+
| ----------------------------- | ------------------------------------- | -------------------------------------- |
|
|
58
|
+
| `fields` | `Record<keyof TValues, FieldState>` | Individual field states |
|
|
59
|
+
| `isSubmitting` | `Signal<boolean>` | Whether the form is being submitted |
|
|
60
|
+
| `isValidating` | `Signal<boolean>` | Whether async validation is running |
|
|
61
|
+
| `isValid` | `Accessor<boolean>` | Whether all fields pass validation |
|
|
62
|
+
| `isDirty` | `Accessor<boolean>` | Whether any field differs from initial |
|
|
63
|
+
| `submitCount` | `Signal<number>` | Number of submission attempts |
|
|
64
|
+
| `submitError` | `Signal<unknown>` | Error thrown by `onSubmit` |
|
|
65
|
+
| `values()` | `() => TValues` | Get all current values |
|
|
66
|
+
| `errors()` | `() => Record<keyof TValues, string>` | Get all current errors |
|
|
67
|
+
| `register(field, opts?)` | `Function` | Bind an input to a field |
|
|
68
|
+
| `handleSubmit(e?)` | `(e?: Event) => Promise<void>` | Submit handler |
|
|
69
|
+
| `validate()` | `() => Promise<boolean>` | Validate all fields |
|
|
70
|
+
| `reset()` | `() => void` | Reset all fields to initial values |
|
|
71
|
+
| `setFieldValue(field, value)` | `Function` | Set a single field's value |
|
|
72
|
+
| `setFieldError(field, error)` | `Function` | Set a single field's error |
|
|
73
|
+
| `setErrors(errors)` | `Function` | Set multiple field errors |
|
|
74
|
+
| `clearErrors()` | `() => void` | Clear all errors |
|
|
75
|
+
| `resetField(field)` | `Function` | Reset a single field |
|
|
76
76
|
|
|
77
77
|
```tsx
|
|
78
78
|
const form = useForm({
|
|
@@ -94,20 +94,20 @@ const form = useForm({
|
|
|
94
94
|
|
|
95
95
|
Extract a single field's state with computed helpers. Useful for building isolated field components.
|
|
96
96
|
|
|
97
|
-
| Parameter | Type
|
|
98
|
-
|
|
|
99
|
-
| `form`
|
|
100
|
-
| `name`
|
|
97
|
+
| Parameter | Type | Description |
|
|
98
|
+
| --------- | ------------------------ | ---------------------------- |
|
|
99
|
+
| `form` | `FormState<TValues>` | Form instance from `useForm` |
|
|
100
|
+
| `name` | `keyof TValues & string` | Field name |
|
|
101
101
|
|
|
102
102
|
**Returns:** `UseFieldResult<T>` with `value`, `error`, `touched`, `dirty`, `setValue`, `setTouched`, `reset`, `register`, `hasError` (Computed), `showError` (Computed: touched AND has error).
|
|
103
103
|
|
|
104
104
|
```tsx
|
|
105
105
|
function EmailField({ form }) {
|
|
106
|
-
const field = useField(form,
|
|
106
|
+
const field = useField(form, 'email')
|
|
107
107
|
return () => (
|
|
108
108
|
<div>
|
|
109
109
|
<input {...field.register()} />
|
|
110
|
-
{() => field.showError() ? <span>{field.error()}</span> : null}
|
|
110
|
+
{() => (field.showError() ? <span>{field.error()}</span> : null)}
|
|
111
111
|
</div>
|
|
112
112
|
)
|
|
113
113
|
}
|
|
@@ -117,44 +117,44 @@ function EmailField({ form }) {
|
|
|
117
117
|
|
|
118
118
|
Manage a dynamic array of form fields with stable keys for keyed rendering.
|
|
119
119
|
|
|
120
|
-
| Parameter | Type
|
|
121
|
-
|
|
|
120
|
+
| Parameter | Type | Description |
|
|
121
|
+
| --------- | ----- | ------------------------------------ |
|
|
122
122
|
| `initial` | `T[]` | Initial array values (default: `[]`) |
|
|
123
123
|
|
|
124
124
|
**Returns:** `UseFieldArrayResult<T>` with:
|
|
125
125
|
|
|
126
|
-
| Property
|
|
127
|
-
|
|
|
128
|
-
| `items`
|
|
129
|
-
| `length`
|
|
130
|
-
| `append(value)`
|
|
131
|
-
| `prepend(value)`
|
|
132
|
-
| `insert(index, value)` | `Function`
|
|
133
|
-
| `remove(index)`
|
|
134
|
-
| `update(index, value)` | `Function`
|
|
135
|
-
| `move(from, to)`
|
|
136
|
-
| `swap(a, b)`
|
|
137
|
-
| `replace(values)`
|
|
138
|
-
| `values()`
|
|
126
|
+
| Property | Type | Description |
|
|
127
|
+
| ---------------------- | ----------------------------- | ----------------------------------------- |
|
|
128
|
+
| `items` | `Signal<FieldArrayItem<T>[]>` | Reactive list with `{ key, value }` items |
|
|
129
|
+
| `length` | `Computed<number>` | Number of items |
|
|
130
|
+
| `append(value)` | `(value: T) => void` | Add to end |
|
|
131
|
+
| `prepend(value)` | `(value: T) => void` | Add to start |
|
|
132
|
+
| `insert(index, value)` | `Function` | Insert at index |
|
|
133
|
+
| `remove(index)` | `(index: number) => void` | Remove at index |
|
|
134
|
+
| `update(index, value)` | `Function` | Update item at index |
|
|
135
|
+
| `move(from, to)` | `Function` | Move item between indices |
|
|
136
|
+
| `swap(a, b)` | `Function` | Swap two items |
|
|
137
|
+
| `replace(values)` | `(values: T[]) => void` | Replace all items |
|
|
138
|
+
| `values()` | `() => T[]` | Get current values as plain array |
|
|
139
139
|
|
|
140
140
|
```ts
|
|
141
|
-
const tags = useFieldArray<string>([
|
|
142
|
-
tags.append(
|
|
143
|
-
tags.items()
|
|
141
|
+
const tags = useFieldArray<string>(['typescript'])
|
|
142
|
+
tags.append('pyreon')
|
|
143
|
+
tags.items() // [{ key: 0, value: Signal("typescript") }, { key: 1, value: Signal("pyreon") }]
|
|
144
144
|
```
|
|
145
145
|
|
|
146
146
|
### `useWatch(form, name?)`
|
|
147
147
|
|
|
148
148
|
Watch specific field values reactively.
|
|
149
149
|
|
|
150
|
-
| Signature
|
|
151
|
-
|
|
|
152
|
-
| `useWatch(form, "email")`
|
|
150
|
+
| Signature | Returns |
|
|
151
|
+
| ----------------------------------- | ------------------------------------------- |
|
|
152
|
+
| `useWatch(form, "email")` | `Signal<string>` — single field value |
|
|
153
153
|
| `useWatch(form, ["first", "last"])` | `[Signal, Signal]` — tuple of field signals |
|
|
154
|
-
| `useWatch(form)`
|
|
154
|
+
| `useWatch(form)` | `Computed<TValues>` — all fields as object |
|
|
155
155
|
|
|
156
156
|
```ts
|
|
157
|
-
const email = useWatch(form,
|
|
157
|
+
const email = useWatch(form, 'email')
|
|
158
158
|
// email() re-evaluates reactively when the email field changes
|
|
159
159
|
|
|
160
160
|
const all = useWatch(form)
|
|
@@ -165,9 +165,9 @@ const all = useWatch(form)
|
|
|
165
165
|
|
|
166
166
|
Subscribe to the full form state as a computed signal. Optionally pass a selector for fine-grained reactivity.
|
|
167
167
|
|
|
168
|
-
| Parameter
|
|
169
|
-
|
|
|
170
|
-
| `form`
|
|
168
|
+
| Parameter | Type | Description |
|
|
169
|
+
| ---------- | -------------------------------- | ---------------------------- |
|
|
170
|
+
| `form` | `FormState<TValues>` | Form instance |
|
|
171
171
|
| `selector` | `(state: FormStateSummary) => R` | Optional projection function |
|
|
172
172
|
|
|
173
173
|
**Returns:** `Computed<FormStateSummary>` or `Computed<R>` when using a selector.
|
|
@@ -176,10 +176,10 @@ Subscribe to the full form state as a computed signal. Optionally pass a selecto
|
|
|
176
176
|
|
|
177
177
|
```ts
|
|
178
178
|
const state = useFormState(form)
|
|
179
|
-
state().isValid
|
|
179
|
+
state().isValid // boolean
|
|
180
180
|
|
|
181
181
|
const canSubmit = useFormState(form, (s) => s.isValid && !s.isSubmitting)
|
|
182
|
-
canSubmit()
|
|
182
|
+
canSubmit() // boolean
|
|
183
183
|
```
|
|
184
184
|
|
|
185
185
|
### `FormProvider` / `useFormContext()`
|
|
@@ -188,14 +188,14 @@ Context pattern for sharing a form instance with nested components.
|
|
|
188
188
|
|
|
189
189
|
```tsx
|
|
190
190
|
// Parent:
|
|
191
|
-
|
|
191
|
+
;<FormProvider form={form}>
|
|
192
192
|
<EmailField />
|
|
193
193
|
</FormProvider>
|
|
194
194
|
|
|
195
195
|
// Child:
|
|
196
196
|
function EmailField() {
|
|
197
197
|
const form = useFormContext<{ email: string }>()
|
|
198
|
-
return () => <input {...form.register(
|
|
198
|
+
return () => <input {...form.register('email')} />
|
|
199
199
|
}
|
|
200
200
|
```
|
|
201
201
|
|
|
@@ -207,12 +207,12 @@ Use `setErrors()` to apply errors returned from your API.
|
|
|
207
207
|
|
|
208
208
|
```ts
|
|
209
209
|
const form = useForm({
|
|
210
|
-
initialValues: { email:
|
|
210
|
+
initialValues: { email: '' },
|
|
211
211
|
onSubmit: async (values) => {
|
|
212
|
-
const res = await fetch(
|
|
212
|
+
const res = await fetch('/api/register', { method: 'POST', body: JSON.stringify(values) })
|
|
213
213
|
if (!res.ok) {
|
|
214
214
|
const errors = await res.json()
|
|
215
|
-
form.setErrors(errors)
|
|
215
|
+
form.setErrors(errors) // { email: "Already taken" }
|
|
216
216
|
}
|
|
217
217
|
},
|
|
218
218
|
})
|
|
@@ -235,20 +235,20 @@ const form = useForm({
|
|
|
235
235
|
|
|
236
236
|
## Types
|
|
237
237
|
|
|
238
|
-
| Type
|
|
239
|
-
|
|
|
240
|
-
| `Accessor<T>`
|
|
241
|
-
| `FormState<TValues>`
|
|
242
|
-
| `FieldState<T>`
|
|
243
|
-
| `FieldRegisterProps<T>`
|
|
244
|
-
| `UseFormOptions<TValues>`
|
|
245
|
-
| `ValidateFn<T, TValues>`
|
|
246
|
-
| `SchemaValidateFn<TValues>` | `(values: TValues) => Record<keyof TValues, string>`
|
|
247
|
-
| `ValidationError`
|
|
248
|
-
| `FieldArrayItem<T>`
|
|
249
|
-
| `UseFieldArrayResult<T>`
|
|
250
|
-
| `UseFieldResult<T>`
|
|
251
|
-
| `FormStateSummary<TValues>` | Snapshot object returned by `useFormState`
|
|
238
|
+
| Type | Description |
|
|
239
|
+
| --------------------------- | ---------------------------------------------------------------------------------------- |
|
|
240
|
+
| `Accessor<T>` | `Signal<T> \| Computed<T>` — a readable reactive value |
|
|
241
|
+
| `FormState<TValues>` | Full form instance returned by `useForm` |
|
|
242
|
+
| `FieldState<T>` | Per-field state: `value`, `error`, `touched`, `dirty`, `setValue`, `setTouched`, `reset` |
|
|
243
|
+
| `FieldRegisterProps<T>` | Props returned by `register()`: `value`, `onInput`, `onBlur`, `checked?` |
|
|
244
|
+
| `UseFormOptions<TValues>` | Options for `useForm` |
|
|
245
|
+
| `ValidateFn<T, TValues>` | `(value: T, allValues: TValues) => ValidationError \| Promise<ValidationError>` |
|
|
246
|
+
| `SchemaValidateFn<TValues>` | `(values: TValues) => Record<keyof TValues, string>` |
|
|
247
|
+
| `ValidationError` | `string \| undefined` |
|
|
248
|
+
| `FieldArrayItem<T>` | `{ key: number, value: Signal<T> }` |
|
|
249
|
+
| `UseFieldArrayResult<T>` | Return type of `useFieldArray` |
|
|
250
|
+
| `UseFieldResult<T>` | Return type of `useField` |
|
|
251
|
+
| `FormStateSummary<TValues>` | Snapshot object returned by `useFormState` |
|
|
252
252
|
|
|
253
253
|
## Gotchas
|
|
254
254
|
|
package/lib/devtools.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"devtools.js","names":[],"sources":["../src/devtools.ts"],"sourcesContent":["/**\n * @pyreon/form devtools introspection API.\n * Import: `import { ... } from \"@pyreon/form/devtools\"`\n */\n\nconst _activeForms = new Map<string, WeakRef<object>>()\nconst _listeners = new Set<() => void>()\n\nfunction _notify(): void {\n for (const listener of _listeners) listener()\n}\n\n/**\n * Register a form instance for devtools inspection.\n *\n * @example\n * const form = useForm({ ... })\n * registerForm(\"login-form\", form)\n */\nexport function registerForm(name: string, form: object): void {\n _activeForms.set(name, new WeakRef(form))\n _notify()\n}\n\n/** Unregister a form instance. */\nexport function unregisterForm(name: string): void {\n _activeForms.delete(name)\n _notify()\n}\n\n/** Get all registered form names. Cleans up garbage-collected instances. */\nexport function getActiveForms(): string[] {\n for (const [name, ref] of _activeForms) {\n if (ref.deref() === undefined) _activeForms.delete(name)\n }\n return [..._activeForms.keys()]\n}\n\n/** Get a form instance by name (or undefined if GC'd or not registered). */\nexport function getFormInstance(name: string): object | undefined {\n const ref = _activeForms.get(name)\n if (!ref) return undefined\n const instance = ref.deref()\n if (!instance) {\n _activeForms.delete(name)\n return undefined\n }\n return instance\n}\n\n/**\n * Get a snapshot of a registered form's current state.\n * Returns values, errors, and form-level status signals.\n */\nexport function getFormSnapshot(name: string): Record<string, unknown> | undefined {\n const form = getFormInstance(name) as Record<string, unknown> | undefined\n if (!form) return undefined\n return {\n values: typeof form.values ===
|
|
1
|
+
{"version":3,"file":"devtools.js","names":[],"sources":["../src/devtools.ts"],"sourcesContent":["/**\n * @pyreon/form devtools introspection API.\n * Import: `import { ... } from \"@pyreon/form/devtools\"`\n */\n\nconst _activeForms = new Map<string, WeakRef<object>>()\nconst _listeners = new Set<() => void>()\n\nfunction _notify(): void {\n for (const listener of _listeners) listener()\n}\n\n/**\n * Register a form instance for devtools inspection.\n *\n * @example\n * const form = useForm({ ... })\n * registerForm(\"login-form\", form)\n */\nexport function registerForm(name: string, form: object): void {\n _activeForms.set(name, new WeakRef(form))\n _notify()\n}\n\n/** Unregister a form instance. */\nexport function unregisterForm(name: string): void {\n _activeForms.delete(name)\n _notify()\n}\n\n/** Get all registered form names. Cleans up garbage-collected instances. */\nexport function getActiveForms(): string[] {\n for (const [name, ref] of _activeForms) {\n if (ref.deref() === undefined) _activeForms.delete(name)\n }\n return [..._activeForms.keys()]\n}\n\n/** Get a form instance by name (or undefined if GC'd or not registered). */\nexport function getFormInstance(name: string): object | undefined {\n const ref = _activeForms.get(name)\n if (!ref) return undefined\n const instance = ref.deref()\n if (!instance) {\n _activeForms.delete(name)\n return undefined\n }\n return instance\n}\n\n/**\n * Get a snapshot of a registered form's current state.\n * Returns values, errors, and form-level status signals.\n */\nexport function getFormSnapshot(name: string): Record<string, unknown> | undefined {\n const form = getFormInstance(name) as Record<string, unknown> | undefined\n if (!form) return undefined\n return {\n values: typeof form.values === 'function' ? (form.values as () => unknown)() : undefined,\n errors: typeof form.errors === 'function' ? (form.errors as () => unknown)() : undefined,\n isSubmitting:\n typeof form.isSubmitting === 'function' ? (form.isSubmitting as () => unknown)() : undefined,\n isValid: typeof form.isValid === 'function' ? (form.isValid as () => unknown)() : undefined,\n isDirty: typeof form.isDirty === 'function' ? (form.isDirty as () => unknown)() : undefined,\n submitCount:\n typeof form.submitCount === 'function' ? (form.submitCount as () => unknown)() : undefined,\n }\n}\n\n/** Subscribe to form registry changes. Returns unsubscribe function. */\nexport function onFormChange(listener: () => void): () => void {\n _listeners.add(listener)\n return () => {\n _listeners.delete(listener)\n }\n}\n\n/** @internal — reset devtools registry (for tests). */\nexport function _resetDevtools(): void {\n _activeForms.clear()\n _listeners.clear()\n}\n"],"mappings":";;;;;AAKA,MAAM,+BAAe,IAAI,KAA8B;AACvD,MAAM,6BAAa,IAAI,KAAiB;AAExC,SAAS,UAAgB;AACvB,MAAK,MAAM,YAAY,WAAY,WAAU;;;;;;;;;AAU/C,SAAgB,aAAa,MAAc,MAAoB;AAC7D,cAAa,IAAI,MAAM,IAAI,QAAQ,KAAK,CAAC;AACzC,UAAS;;;AAIX,SAAgB,eAAe,MAAoB;AACjD,cAAa,OAAO,KAAK;AACzB,UAAS;;;AAIX,SAAgB,iBAA2B;AACzC,MAAK,MAAM,CAAC,MAAM,QAAQ,aACxB,KAAI,IAAI,OAAO,KAAK,OAAW,cAAa,OAAO,KAAK;AAE1D,QAAO,CAAC,GAAG,aAAa,MAAM,CAAC;;;AAIjC,SAAgB,gBAAgB,MAAkC;CAChE,MAAM,MAAM,aAAa,IAAI,KAAK;AAClC,KAAI,CAAC,IAAK,QAAO;CACjB,MAAM,WAAW,IAAI,OAAO;AAC5B,KAAI,CAAC,UAAU;AACb,eAAa,OAAO,KAAK;AACzB;;AAEF,QAAO;;;;;;AAOT,SAAgB,gBAAgB,MAAmD;CACjF,MAAM,OAAO,gBAAgB,KAAK;AAClC,KAAI,CAAC,KAAM,QAAO;AAClB,QAAO;EACL,QAAQ,OAAO,KAAK,WAAW,aAAc,KAAK,QAA0B,GAAG;EAC/E,QAAQ,OAAO,KAAK,WAAW,aAAc,KAAK,QAA0B,GAAG;EAC/E,cACE,OAAO,KAAK,iBAAiB,aAAc,KAAK,cAAgC,GAAG;EACrF,SAAS,OAAO,KAAK,YAAY,aAAc,KAAK,SAA2B,GAAG;EAClF,SAAS,OAAO,KAAK,YAAY,aAAc,KAAK,SAA2B,GAAG;EAClF,aACE,OAAO,KAAK,gBAAgB,aAAc,KAAK,aAA+B,GAAG;EACpF;;;AAIH,SAAgB,aAAa,UAAkC;AAC7D,YAAW,IAAI,SAAS;AACxB,cAAa;AACX,aAAW,OAAO,SAAS;;;;AAK/B,SAAgB,iBAAuB;AACrC,cAAa,OAAO;AACpB,YAAW,OAAO"}
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/context.ts","../src/use-field.ts","../src/use-field-array.ts","../src/use-form.ts","../src/use-form-state.ts","../src/use-watch.ts"],"sourcesContent":["import type { Props, VNode, VNodeChild } from \"@pyreon/core\"\nimport { createContext, provide, useContext } from \"@pyreon/core\"\nimport type { FormState } from \"./types\"\n\nconst FormContext = createContext<FormState<Record<string, unknown>> | null>(null)\n\nexport interface FormProviderProps<TValues extends Record<string, unknown>> extends Props {\n form: FormState<TValues>\n children?: VNodeChild\n}\n\n/**\n * Provide a form instance to the component tree so nested components\n * can access it via `useFormContext()`.\n *\n * @example\n * const form = useForm({ initialValues: { email: '' }, onSubmit: ... })\n *\n * <FormProvider form={form}>\n * <EmailField />\n * <SubmitButton />\n * </FormProvider>\n */\nexport function FormProvider<TValues extends Record<string, unknown>>(\n props: FormProviderProps<TValues>,\n): VNode {\n provide(FormContext, props.form as FormState<Record<string, unknown>>)\n\n const ch = props.children\n return (typeof ch === \"function\" ? (ch as () => VNodeChild)() : ch) as VNode\n}\n\n/**\n * Access the form instance from the nearest `FormProvider`.\n * Must be called within a component tree wrapped by `FormProvider`.\n *\n * @example\n * function EmailField() {\n * const form = useFormContext<{ email: string }>()\n * return <input {...form.register('email')} />\n * }\n */\nexport function useFormContext<\n TValues extends Record<string, unknown> = Record<string, unknown>,\n>(): FormState<TValues> {\n const form = useContext(FormContext)\n if (!form) {\n throw new Error(\"[@pyreon/form] useFormContext() must be used within a <FormProvider>.\")\n }\n // Generic narrowing: context stores FormState<Record<string, unknown>>\n // but callers narrow to their specific TValues at the call site.\n return form as FormState<TValues>\n}\n","import type { Computed, Signal } from \"@pyreon/reactivity\"\nimport { computed } from \"@pyreon/reactivity\"\nimport type { FieldRegisterProps, FieldState, FormState, ValidationError } from \"./types\"\n\nexport interface UseFieldResult<T> {\n /** Current field value (reactive signal). */\n value: Signal<T>\n /** Field error message (reactive signal). */\n error: Signal<ValidationError>\n /** Whether the field has been touched (reactive signal). */\n touched: Signal<boolean>\n /** Whether the field value differs from initial (reactive signal). */\n dirty: Signal<boolean>\n /** Set the field value. */\n setValue: (value: T) => void\n /** Mark the field as touched. */\n setTouched: () => void\n /** Reset the field to its initial value. */\n reset: () => void\n /** Register props for input binding. */\n register: (opts?: { type?: \"checkbox\" }) => FieldRegisterProps<T>\n /** Whether the field has an error (computed). */\n hasError: Computed<boolean>\n /** Whether the field should show its error (touched + has error). */\n showError: Computed<boolean>\n}\n\n/**\n * Extract a single field's state and helpers from a form instance.\n * Useful for building isolated field components.\n *\n * @example\n * function EmailField({ form }: { form: FormState<{ email: string }> }) {\n * const field = useField(form, 'email')\n * return (\n * <>\n * <input {...field.register()} />\n * {field.showError() && <span>{field.error()}</span>}\n * </>\n * )\n * }\n */\nexport function useField<TValues extends Record<string, unknown>, K extends keyof TValues & string>(\n form: FormState<TValues>,\n name: K,\n): UseFieldResult<TValues[K]> {\n const fieldState: FieldState<TValues[K]> = form.fields[name]\n\n const hasError = computed(() => fieldState.error() !== undefined)\n const showError = computed(() => fieldState.touched() && hasError())\n\n return {\n value: fieldState.value,\n error: fieldState.error,\n touched: fieldState.touched,\n dirty: fieldState.dirty,\n setValue: fieldState.setValue,\n setTouched: fieldState.setTouched,\n reset: fieldState.reset,\n register: (opts?) => form.register(name, opts),\n hasError,\n showError,\n }\n}\n","import type { Computed, Signal } from \"@pyreon/reactivity\"\nimport { computed, signal } from \"@pyreon/reactivity\"\n\nexport interface FieldArrayItem<T> {\n /** Stable key for keyed rendering. */\n key: number\n /** Reactive value for this item. */\n value: Signal<T>\n}\n\nexport interface UseFieldArrayResult<T> {\n /** Reactive list of items with stable keys. */\n items: Signal<FieldArrayItem<T>[]>\n /** Number of items. */\n length: Computed<number>\n /** Append a new item to the end. */\n append: (value: T) => void\n /** Prepend a new item to the start. */\n prepend: (value: T) => void\n /** Insert an item at the given index. */\n insert: (index: number, value: T) => void\n /** Remove the item at the given index. */\n remove: (index: number) => void\n /** Update the value of an item at the given index. */\n update: (index: number, value: T) => void\n /** Move an item from one index to another. */\n move: (from: number, to: number) => void\n /** Swap two items by index. */\n swap: (indexA: number, indexB: number) => void\n /** Replace all items. */\n replace: (values: T[]) => void\n /** Get all current values as a plain array. */\n values: () => T[]\n}\n\n/**\n * Manage a dynamic array of form fields with stable keys.\n *\n * @example\n * const tags = useFieldArray<string>([])\n * tags.append('typescript')\n * tags.append('pyreon')\n * // tags.items() — array of { key, value } for keyed rendering\n */\nexport function useFieldArray<T>(initial: T[] = []): UseFieldArrayResult<T> {\n let nextKey = 0\n const makeItem = (value: T): FieldArrayItem<T> => ({\n key: nextKey++,\n value: signal(value),\n })\n\n const items = signal<FieldArrayItem<T>[]>(initial.map(makeItem))\n const length = computed(() => items().length)\n\n return {\n items,\n length,\n\n append(value: T) {\n items.update((arr) => [...arr, makeItem(value)])\n },\n\n prepend(value: T) {\n items.update((arr) => [makeItem(value), ...arr])\n },\n\n insert(index: number, value: T) {\n items.update((arr) => {\n const next = [...arr]\n next.splice(index, 0, makeItem(value))\n return next\n })\n },\n\n remove(index: number) {\n items.update((arr) => arr.filter((_, i) => i !== index))\n },\n\n update(index: number, value: T) {\n const current = items.peek()\n const item = current[index]\n if (item) {\n item.value.set(value)\n }\n },\n\n move(from: number, to: number) {\n items.update((arr) => {\n const next = [...arr]\n const [item] = next.splice(from, 1)\n if (item) next.splice(to, 0, item)\n return next\n })\n },\n\n swap(indexA: number, indexB: number) {\n items.update((arr) => {\n const next = [...arr]\n const a = next[indexA]\n const b = next[indexB]\n if (a && b) {\n next[indexA] = b\n next[indexB] = a\n }\n return next\n })\n },\n\n replace(values: T[]) {\n items.set(values.map(makeItem))\n },\n\n values() {\n return items.peek().map((item) => item.value.peek())\n },\n }\n}\n","import { onUnmount } from \"@pyreon/core\"\nimport type { Signal } from \"@pyreon/reactivity\"\nimport { computed, effect, signal } from \"@pyreon/reactivity\"\nimport type {\n FieldRegisterProps,\n FieldState,\n FormState,\n UseFormOptions,\n ValidationError,\n} from \"./types\"\n\n/**\n * Create a signal-based form. Returns reactive field states, form-level\n * signals, and handlers for submit/reset/validate.\n *\n * @example\n * const form = useForm({\n * initialValues: { email: '', password: '', remember: false },\n * validators: {\n * email: (v) => (!v ? 'Required' : undefined),\n * password: (v, all) => (v.length < 8 ? 'Too short' : undefined),\n * },\n * onSubmit: async (values) => { await login(values) },\n * })\n *\n * // Bind with register():\n * // h('input', form.register('email'))\n * // h('input', { type: 'checkbox', ...form.register('remember', { type: 'checkbox' }) })\n */\nexport function useForm<TValues extends Record<string, unknown>>(\n options: UseFormOptions<TValues>,\n): FormState<TValues> {\n const { initialValues, onSubmit, validators, schema, validateOn = \"blur\", debounceMs } = options\n\n // Build field states\n const fieldEntries = Object.entries(initialValues) as [\n keyof TValues & string,\n TValues[keyof TValues],\n ][]\n\n const fields = {} as { [K in keyof TValues]: FieldState<TValues[K]> }\n\n // Debounce timers per field (only allocated when debounceMs is set)\n const debounceTimers: Partial<Record<keyof TValues, ReturnType<typeof setTimeout>>> = {}\n\n // Validation version per field — used to discard stale async results\n const validationVersions: Partial<Record<keyof TValues, number>> = {}\n\n // Helper to get all current values (used by cross-field validators)\n const getValues = (): TValues => {\n const values = {} as TValues\n for (const [name] of fieldEntries) {\n ;(values as Record<string, unknown>)[name] =\n fields[name]?.value.peek() ?? (initialValues as Record<string, unknown>)[name]\n }\n return values\n }\n\n // Clear all pending debounce timers\n const clearAllTimers = () => {\n for (const key of Object.keys(debounceTimers)) {\n clearTimeout(debounceTimers[key as keyof TValues])\n delete debounceTimers[key as keyof TValues]\n }\n }\n\n const isValidating = signal(false)\n const submitError = signal<unknown>(undefined)\n\n // Track whether the form has been disposed (unmounted)\n let disposed = false\n\n for (const [name, initial] of fieldEntries) {\n const valueSig = signal(initial) as Signal<TValues[typeof name]>\n const errorSig = signal<ValidationError>(undefined)\n const touchedSig = signal(false)\n const dirtySig = signal(false)\n\n // Initialize validation version\n validationVersions[name] = 0\n\n const runValidation = async (value: TValues[typeof name]) => {\n const fieldValidator = validators?.[name]\n if (fieldValidator) {\n // Bump version to track this validation run\n validationVersions[name] = (validationVersions[name] ?? 0) + 1\n const currentVersion = validationVersions[name]\n try {\n const result = await fieldValidator(value, getValues())\n // Only apply result if this is still the latest validation and not disposed\n if (!disposed && validationVersions[name] === currentVersion) {\n errorSig.set(result)\n }\n return result\n } catch (err) {\n // Validator threw — treat as error string if possible\n if (!disposed && validationVersions[name] === currentVersion) {\n const message = err instanceof Error ? err.message : String(err)\n errorSig.set(message)\n }\n return err instanceof Error ? err.message : String(err)\n }\n }\n errorSig.set(undefined)\n return undefined\n }\n\n const validateField = debounceMs\n ? (value: TValues[typeof name]) => {\n clearTimeout(debounceTimers[name])\n return new Promise<ValidationError>((resolve) => {\n debounceTimers[name] = setTimeout(async () => {\n resolve(await runValidation(value))\n }, debounceMs)\n })\n }\n : runValidation\n\n // Auto-validate on change if configured\n if (validateOn === \"change\") {\n effect(() => {\n const v = valueSig()\n validateField(v)\n })\n }\n\n fields[name] = {\n value: valueSig,\n error: errorSig,\n touched: touchedSig,\n dirty: dirtySig,\n setValue: (value: TValues[typeof name]) => {\n valueSig.set(value)\n // Deep comparison for objects/arrays, reference for primitives\n dirtySig.set(!structuredEqual(value, initial))\n },\n setTouched: () => {\n touchedSig.set(true)\n if (validateOn === \"blur\") {\n validateField(valueSig.peek())\n }\n },\n reset: () => {\n valueSig.set(initial as TValues[typeof name])\n errorSig.set(undefined)\n touchedSig.set(false)\n dirtySig.set(false)\n clearTimeout(debounceTimers[name])\n },\n } as FieldState<TValues[typeof name]>\n }\n\n // Clean up debounce timers and cancel in-flight validators on unmount\n onUnmount(() => {\n disposed = true\n clearAllTimers()\n })\n\n const isSubmitting = signal(false)\n const submitCount = signal(0)\n\n // Form-level computed signals\n const isValid = computed(() => {\n for (const name of fieldEntries.map(([n]) => n)) {\n if (fields[name].error() !== undefined) return false\n }\n return true\n })\n\n const isDirty = computed(() => {\n for (const name of fieldEntries.map(([n]) => n)) {\n if (fields[name].dirty()) return true\n }\n return false\n })\n\n const getErrors = (): Partial<Record<keyof TValues, ValidationError>> => {\n const errors = {} as Partial<Record<keyof TValues, ValidationError>>\n for (const [name] of fieldEntries) {\n const err = fields[name].error.peek()\n if (err !== undefined) errors[name] = err\n }\n return errors\n }\n\n const validate = async (): Promise<boolean> => {\n // Cancel any pending debounced validations\n clearAllTimers()\n\n isValidating.set(true)\n\n try {\n const allValues = getValues()\n\n // Clear all errors before re-validating\n for (const [name] of fieldEntries) {\n fields[name].error.set(undefined)\n }\n\n // Run field-level validators with all values for cross-field support\n await Promise.all(\n fieldEntries.map(async ([name]) => {\n const fieldValidator = validators?.[name]\n if (fieldValidator) {\n // Bump version so any in-flight debounced validation is discarded\n validationVersions[name] = (validationVersions[name] ?? 0) + 1\n const currentVersion = validationVersions[name]\n try {\n const error = await fieldValidator(fields[name].value.peek(), allValues)\n if (validationVersions[name] === currentVersion) {\n fields[name].error.set(error)\n }\n } catch (err) {\n if (validationVersions[name] === currentVersion) {\n fields[name].error.set(err instanceof Error ? err.message : String(err))\n }\n }\n }\n }),\n )\n\n // Run schema-level validator — only set schema errors for fields\n // that don't already have a field-level error (field-level wins)\n if (schema) {\n try {\n const schemaErrors = await schema(allValues)\n for (const [name] of fieldEntries) {\n const schemaError = schemaErrors[name]\n if (schemaError !== undefined && fields[name].error.peek() === undefined) {\n fields[name].error.set(schemaError)\n }\n }\n } catch (err) {\n // Schema validator threw — set as submitError rather than losing it\n submitError.set(err)\n return false\n }\n }\n\n // Re-check: any field with an error means invalid\n for (const [name] of fieldEntries) {\n if (fields[name].error.peek() !== undefined) return false\n }\n return true\n } finally {\n isValidating.set(false)\n }\n }\n\n const handleSubmit = async (e?: Event) => {\n if (e && typeof e.preventDefault === \"function\") {\n e.preventDefault()\n }\n\n submitError.set(undefined)\n submitCount.update((n) => n + 1)\n\n // Mark all fields as touched\n for (const [name] of fieldEntries) {\n fields[name].touched.set(true)\n }\n\n const valid = await validate()\n if (!valid) return\n\n isSubmitting.set(true)\n try {\n await onSubmit(getValues())\n } catch (err) {\n submitError.set(err)\n throw err\n } finally {\n isSubmitting.set(false)\n }\n }\n\n const reset = () => {\n clearAllTimers()\n for (const [name] of fieldEntries) {\n fields[name].reset()\n }\n submitCount.set(0)\n submitError.set(undefined)\n }\n\n const setFieldValue = <K extends keyof TValues>(field: K, value: TValues[K]) => {\n if (!fields[field]) {\n throw new Error(\n `[@pyreon/form] Field \"${String(field)}\" does not exist. Available fields: ${fieldEntries.map(([n]) => n).join(\", \")}`,\n )\n }\n fields[field].setValue(value)\n }\n\n const setFieldError = (field: keyof TValues, error: ValidationError) => {\n if (!fields[field]) {\n throw new Error(\n `[@pyreon/form] Field \"${String(field)}\" does not exist. Available fields: ${fieldEntries.map(([n]) => n).join(\", \")}`,\n )\n }\n fields[field].error.set(error)\n }\n\n const setErrors = (errors: Partial<Record<keyof TValues, ValidationError>>) => {\n for (const [name, error] of Object.entries(errors)) {\n setFieldError(name as keyof TValues, error as ValidationError)\n }\n }\n\n const clearErrors = () => {\n for (const [name] of fieldEntries) {\n fields[name].error.set(undefined)\n }\n }\n\n const resetField = (field: keyof TValues) => {\n if (fields[field]) {\n fields[field].reset()\n }\n }\n\n // Memoized register props per field+type combo\n const registerCache = new Map<string, FieldRegisterProps<unknown>>()\n\n const register = <K extends keyof TValues & string>(\n field: K,\n opts?: { type?: \"checkbox\" | \"number\" },\n ): FieldRegisterProps<TValues[K]> => {\n const cacheKey = `${field}:${opts?.type ?? \"text\"}`\n const cached = registerCache.get(cacheKey)\n if (cached) return cached as FieldRegisterProps<TValues[K]>\n\n const fieldState = fields[field]\n const props: FieldRegisterProps<TValues[K]> = {\n value: fieldState.value,\n onInput: (e: Event) => {\n const target = e.target as HTMLInputElement\n if (opts?.type === \"checkbox\") {\n fieldState.setValue(target.checked as TValues[K])\n } else if (opts?.type === \"number\") {\n const num = target.valueAsNumber\n fieldState.setValue((Number.isNaN(num) ? target.value : num) as TValues[K])\n } else {\n fieldState.setValue(target.value as TValues[K])\n }\n },\n onBlur: () => {\n fieldState.setTouched()\n },\n }\n\n if (opts?.type === \"checkbox\") {\n props.checked = computed(() => Boolean(fieldState.value()))\n }\n\n registerCache.set(cacheKey, props as FieldRegisterProps<unknown>)\n return props\n }\n\n return {\n fields,\n isSubmitting,\n isValidating,\n isValid,\n isDirty,\n submitCount,\n submitError,\n values: getValues,\n errors: getErrors,\n setFieldValue,\n setFieldError,\n setErrors,\n clearErrors,\n resetField,\n register,\n handleSubmit,\n reset,\n validate,\n }\n}\n\n/** Deep structural equality with depth limit to guard against circular references. */\nfunction structuredEqual(a: unknown, b: unknown, depth = 0): boolean {\n if (Object.is(a, b)) return true\n if (a == null || b == null) return false\n if (typeof a !== typeof b) return false\n // Bail at depth 10 — treat as not equal to avoid infinite recursion\n if (depth > 10) return false\n\n if (Array.isArray(a) && Array.isArray(b)) {\n if (a.length !== b.length) return false\n for (let i = 0; i < a.length; i++) {\n if (!structuredEqual(a[i], b[i], depth + 1)) return false\n }\n return true\n }\n\n if (typeof a === \"object\" && typeof b === \"object\") {\n const aObj = a as Record<string, unknown>\n const bObj = b as Record<string, unknown>\n const aKeys = Object.keys(aObj)\n const bKeys = Object.keys(bObj)\n if (aKeys.length !== bKeys.length) return false\n for (const key of aKeys) {\n if (!structuredEqual(aObj[key], bObj[key], depth + 1)) return false\n }\n return true\n }\n\n return false\n}\n","import type { Computed } from \"@pyreon/reactivity\"\nimport { computed } from \"@pyreon/reactivity\"\nimport type { FormState, ValidationError } from \"./types\"\n\nexport interface FormStateSummary<TValues extends Record<string, unknown>> {\n isSubmitting: boolean\n isValidating: boolean\n isValid: boolean\n isDirty: boolean\n submitCount: number\n submitError: unknown\n touchedFields: Partial<Record<keyof TValues, boolean>>\n dirtyFields: Partial<Record<keyof TValues, boolean>>\n errors: Partial<Record<keyof TValues, ValidationError>>\n}\n\n/**\n * Subscribe to the full form state as a single computed signal.\n * Useful for rendering form-level UI (submit button disabled state,\n * error summaries, progress indicators).\n *\n * @example\n * const state = useFormState(form)\n * // state() => { isSubmitting, isValid, isDirty, errors, ... }\n *\n * @example\n * // Use a selector for fine-grained reactivity\n * const canSubmit = useFormState(form, (s) => s.isValid && !s.isSubmitting)\n */\nexport function useFormState<TValues extends Record<string, unknown>>(\n form: FormState<TValues>,\n): Computed<FormStateSummary<TValues>>\n\nexport function useFormState<TValues extends Record<string, unknown>, R>(\n form: FormState<TValues>,\n selector: (state: FormStateSummary<TValues>) => R,\n): Computed<R>\n\nexport function useFormState<TValues extends Record<string, unknown>, R>(\n form: FormState<TValues>,\n selector?: (state: FormStateSummary<TValues>) => R,\n): Computed<FormStateSummary<TValues>> | Computed<R> {\n const buildSummary = (): FormStateSummary<TValues> => {\n const touchedFields = {} as Partial<Record<keyof TValues, boolean>>\n const dirtyFields = {} as Partial<Record<keyof TValues, boolean>>\n const errors = {} as Partial<Record<keyof TValues, ValidationError>>\n\n for (const key of Object.keys(form.fields) as (keyof TValues & string)[]) {\n const field = form.fields[key]\n if (field.touched()) touchedFields[key] = true\n if (field.dirty()) dirtyFields[key] = true\n const err = field.error()\n if (err !== undefined) errors[key] = err\n }\n\n return {\n isSubmitting: form.isSubmitting(),\n isValidating: form.isValidating(),\n isValid: form.isValid(),\n isDirty: form.isDirty(),\n submitCount: form.submitCount(),\n submitError: form.submitError(),\n touchedFields,\n dirtyFields,\n errors,\n }\n }\n\n if (selector) {\n return computed(() => selector(buildSummary()))\n }\n\n return computed(buildSummary)\n}\n","import type { Computed, Signal } from \"@pyreon/reactivity\"\nimport { computed } from \"@pyreon/reactivity\"\nimport type { FormState } from \"./types\"\n\n/**\n * Watch specific field values reactively. Returns a computed signal\n * that re-evaluates when any of the watched fields change.\n *\n * @example\n * // Watch a single field\n * const email = useWatch(form, 'email')\n * // email() => current email value\n *\n * @example\n * // Watch multiple fields\n * const [first, last] = useWatch(form, ['firstName', 'lastName'])\n * // first() => firstName value, last() => lastName value\n *\n * @example\n * // Watch all fields\n * const all = useWatch(form)\n * // all() => { email: '...', password: '...' }\n */\nexport function useWatch<TValues extends Record<string, unknown>, K extends keyof TValues & string>(\n form: FormState<TValues>,\n name: K,\n): Signal<TValues[K]>\n\nexport function useWatch<\n TValues extends Record<string, unknown>,\n K extends (keyof TValues & string)[],\n>(form: FormState<TValues>, names: K): { [I in keyof K]: Signal<TValues[K[I] & keyof TValues]> }\n\nexport function useWatch<TValues extends Record<string, unknown>>(\n form: FormState<TValues>,\n): Computed<TValues>\n\nexport function useWatch<TValues extends Record<string, unknown>, K extends keyof TValues & string>(\n form: FormState<TValues>,\n nameOrNames?: K | K[],\n): Signal<TValues[K]> | Signal<TValues[K]>[] | Computed<TValues> {\n // Watch all fields\n if (nameOrNames === undefined) {\n return computed(() => {\n const result = {} as TValues\n for (const key of Object.keys(form.fields) as (keyof TValues & string)[]) {\n ;(result as Record<string, unknown>)[key] = form.fields[key].value()\n }\n return result\n })\n }\n\n // Watch multiple fields\n if (Array.isArray(nameOrNames)) {\n return nameOrNames.map((name) => form.fields[name].value) as Signal<TValues[K]>[]\n }\n\n // Watch single field\n return form.fields[nameOrNames].value\n}\n"],"mappings":";;;;AAIA,MAAM,cAAc,cAAyD,KAAK;;;;;;;;;;;;;AAmBlF,SAAgB,aACd,OACO;AACP,SAAQ,aAAa,MAAM,KAA2C;CAEtE,MAAM,KAAK,MAAM;AACjB,QAAQ,OAAO,OAAO,aAAc,IAAyB,GAAG;;;;;;;;;;;;AAalE,SAAgB,iBAEQ;CACtB,MAAM,OAAO,WAAW,YAAY;AACpC,KAAI,CAAC,KACH,O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SACF,QAAO,eAAe,SAAS,cAAc,CAAC,CAAC;AAGjD,QAAO,SAAS,aAAa;;;;;ACnC/B,SAAgB,SACd,MACA,aAC+D;AAE/D,KAAI,gBAAgB,OAClB,QAAO,eAAe;EACpB,MAAM,SAAS,EAAE;AACjB,OAAK,MAAM,OAAO,OAAO,KAAK,KAAK,OAAO,CACvC,CAAC,OAAmC,OAAO,KAAK,OAAO,KAAK,OAAO;AAEtE,SAAO;GACP;AAIJ,KAAI,MAAM,QAAQ,YAAY,CAC5B,QAAO,YAAY,KAAK,SAAS,KAAK,OAAO,MAAM,MAAM;AAI3D,QAAO,KAAK,OAAO,aAAa"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/context.ts","../src/use-field.ts","../src/use-field-array.ts","../src/use-form.ts","../src/use-form-state.ts","../src/use-watch.ts"],"sourcesContent":["import type { Props, VNode, VNodeChild } from '@pyreon/core'\nimport { createContext, provide, useContext } from '@pyreon/core'\nimport type { FormState } from './types'\n\nconst FormContext = createContext<FormState<Record<string, unknown>> | null>(null)\n\nexport interface FormProviderProps<TValues extends Record<string, unknown>> extends Props {\n form: FormState<TValues>\n children?: VNodeChild\n}\n\n/**\n * Provide a form instance to the component tree so nested components\n * can access it via `useFormContext()`.\n *\n * @example\n * const form = useForm({ initialValues: { email: '' }, onSubmit: ... })\n *\n * <FormProvider form={form}>\n * <EmailField />\n * <SubmitButton />\n * </FormProvider>\n */\nexport function FormProvider<TValues extends Record<string, unknown>>(\n props: FormProviderProps<TValues>,\n): VNode {\n provide(FormContext, props.form as FormState<Record<string, unknown>>)\n\n const ch = props.children\n return (typeof ch === 'function' ? (ch as () => VNodeChild)() : ch) as VNode\n}\n\n/**\n * Access the form instance from the nearest `FormProvider`.\n * Must be called within a component tree wrapped by `FormProvider`.\n *\n * @example\n * function EmailField() {\n * const form = useFormContext<{ email: string }>()\n * return <input {...form.register('email')} />\n * }\n */\nexport function useFormContext<\n TValues extends Record<string, unknown> = Record<string, unknown>,\n>(): FormState<TValues> {\n const form = useContext(FormContext)\n if (!form) {\n throw new Error('[@pyreon/form] useFormContext() must be used within a <FormProvider>.')\n }\n // Generic narrowing: context stores FormState<Record<string, unknown>>\n // but callers narrow to their specific TValues at the call site.\n return form as FormState<TValues>\n}\n","import type { Computed, Signal } from '@pyreon/reactivity'\nimport { computed } from '@pyreon/reactivity'\nimport type { FieldRegisterProps, FieldState, FormState, ValidationError } from './types'\n\nexport interface UseFieldResult<T> {\n /** Current field value (reactive signal). */\n value: Signal<T>\n /** Field error message (reactive signal). */\n error: Signal<ValidationError>\n /** Whether the field has been touched (reactive signal). */\n touched: Signal<boolean>\n /** Whether the field value differs from initial (reactive signal). */\n dirty: Signal<boolean>\n /** Set the field value. */\n setValue: (value: T) => void\n /** Mark the field as touched. */\n setTouched: () => void\n /** Reset the field to its initial value. */\n reset: () => void\n /** Register props for input binding. */\n register: (opts?: { type?: 'checkbox' }) => FieldRegisterProps<T>\n /** Whether the field has an error (computed). */\n hasError: Computed<boolean>\n /** Whether the field should show its error (touched + has error). */\n showError: Computed<boolean>\n}\n\n/**\n * Extract a single field's state and helpers from a form instance.\n * Useful for building isolated field components.\n *\n * @example\n * function EmailField({ form }: { form: FormState<{ email: string }> }) {\n * const field = useField(form, 'email')\n * return (\n * <>\n * <input {...field.register()} />\n * {field.showError() && <span>{field.error()}</span>}\n * </>\n * )\n * }\n */\nexport function useField<TValues extends Record<string, unknown>, K extends keyof TValues & string>(\n form: FormState<TValues>,\n name: K,\n): UseFieldResult<TValues[K]> {\n const fieldState: FieldState<TValues[K]> = form.fields[name]\n\n const hasError = computed(() => fieldState.error() !== undefined)\n const showError = computed(() => fieldState.touched() && hasError())\n\n return {\n value: fieldState.value,\n error: fieldState.error,\n touched: fieldState.touched,\n dirty: fieldState.dirty,\n setValue: fieldState.setValue,\n setTouched: fieldState.setTouched,\n reset: fieldState.reset,\n register: (opts?) => form.register(name, opts),\n hasError,\n showError,\n }\n}\n","import type { Computed, Signal } from '@pyreon/reactivity'\nimport { computed, signal } from '@pyreon/reactivity'\n\nexport interface FieldArrayItem<T> {\n /** Stable key for keyed rendering. */\n key: number\n /** Reactive value for this item. */\n value: Signal<T>\n}\n\nexport interface UseFieldArrayResult<T> {\n /** Reactive list of items with stable keys. */\n items: Signal<FieldArrayItem<T>[]>\n /** Number of items. */\n length: Computed<number>\n /** Append a new item to the end. */\n append: (value: T) => void\n /** Prepend a new item to the start. */\n prepend: (value: T) => void\n /** Insert an item at the given index. */\n insert: (index: number, value: T) => void\n /** Remove the item at the given index. */\n remove: (index: number) => void\n /** Update the value of an item at the given index. */\n update: (index: number, value: T) => void\n /** Move an item from one index to another. */\n move: (from: number, to: number) => void\n /** Swap two items by index. */\n swap: (indexA: number, indexB: number) => void\n /** Replace all items. */\n replace: (values: T[]) => void\n /** Get all current values as a plain array. */\n values: () => T[]\n}\n\n/**\n * Manage a dynamic array of form fields with stable keys.\n *\n * @example\n * const tags = useFieldArray<string>([])\n * tags.append('typescript')\n * tags.append('pyreon')\n * // tags.items() — array of { key, value } for keyed rendering\n */\nexport function useFieldArray<T>(initial: T[] = []): UseFieldArrayResult<T> {\n let nextKey = 0\n const makeItem = (value: T): FieldArrayItem<T> => ({\n key: nextKey++,\n value: signal(value),\n })\n\n const items = signal<FieldArrayItem<T>[]>(initial.map(makeItem))\n const length = computed(() => items().length)\n\n return {\n items,\n length,\n\n append(value: T) {\n items.update((arr) => [...arr, makeItem(value)])\n },\n\n prepend(value: T) {\n items.update((arr) => [makeItem(value), ...arr])\n },\n\n insert(index: number, value: T) {\n items.update((arr) => {\n const next = [...arr]\n next.splice(index, 0, makeItem(value))\n return next\n })\n },\n\n remove(index: number) {\n items.update((arr) => arr.filter((_, i) => i !== index))\n },\n\n update(index: number, value: T) {\n const current = items.peek()\n const item = current[index]\n if (item) {\n item.value.set(value)\n }\n },\n\n move(from: number, to: number) {\n items.update((arr) => {\n const next = [...arr]\n const [item] = next.splice(from, 1)\n if (item) next.splice(to, 0, item)\n return next\n })\n },\n\n swap(indexA: number, indexB: number) {\n items.update((arr) => {\n const next = [...arr]\n const a = next[indexA]\n const b = next[indexB]\n if (a && b) {\n next[indexA] = b\n next[indexB] = a\n }\n return next\n })\n },\n\n replace(values: T[]) {\n items.set(values.map(makeItem))\n },\n\n values() {\n return items.peek().map((item) => item.value.peek())\n },\n }\n}\n","import { onUnmount } from '@pyreon/core'\nimport type { Signal } from '@pyreon/reactivity'\nimport { computed, effect, signal } from '@pyreon/reactivity'\nimport type {\n FieldRegisterProps,\n FieldState,\n FormState,\n UseFormOptions,\n ValidationError,\n} from './types'\n\n/**\n * Create a signal-based form. Returns reactive field states, form-level\n * signals, and handlers for submit/reset/validate.\n *\n * @example\n * const form = useForm({\n * initialValues: { email: '', password: '', remember: false },\n * validators: {\n * email: (v) => (!v ? 'Required' : undefined),\n * password: (v, all) => (v.length < 8 ? 'Too short' : undefined),\n * },\n * onSubmit: async (values) => { await login(values) },\n * })\n *\n * // Bind with register():\n * // h('input', form.register('email'))\n * // h('input', { type: 'checkbox', ...form.register('remember', { type: 'checkbox' }) })\n */\nexport function useForm<TValues extends Record<string, unknown>>(\n options: UseFormOptions<TValues>,\n): FormState<TValues> {\n const { initialValues, onSubmit, validators, schema, validateOn = 'blur', debounceMs } = options\n\n // Build field states\n const fieldEntries = Object.entries(initialValues) as [\n keyof TValues & string,\n TValues[keyof TValues],\n ][]\n\n const fields = {} as { [K in keyof TValues]: FieldState<TValues[K]> }\n\n // Debounce timers per field (only allocated when debounceMs is set)\n const debounceTimers: Partial<Record<keyof TValues, ReturnType<typeof setTimeout>>> = {}\n\n // Validation version per field — used to discard stale async results\n const validationVersions: Partial<Record<keyof TValues, number>> = {}\n\n // Helper to get all current values (used by cross-field validators)\n const getValues = (): TValues => {\n const values = {} as TValues\n for (const [name] of fieldEntries) {\n ;(values as Record<string, unknown>)[name] =\n fields[name]?.value.peek() ?? (initialValues as Record<string, unknown>)[name]\n }\n return values\n }\n\n // Clear all pending debounce timers\n const clearAllTimers = () => {\n for (const key of Object.keys(debounceTimers)) {\n clearTimeout(debounceTimers[key as keyof TValues])\n delete debounceTimers[key as keyof TValues]\n }\n }\n\n const isValidating = signal(false)\n const submitError = signal<unknown>(undefined)\n\n // Track whether the form has been disposed (unmounted)\n let disposed = false\n\n for (const [name, initial] of fieldEntries) {\n const valueSig = signal(initial) as Signal<TValues[typeof name]>\n const errorSig = signal<ValidationError>(undefined)\n const touchedSig = signal(false)\n const dirtySig = signal(false)\n\n // Initialize validation version\n validationVersions[name] = 0\n\n const runValidation = async (value: TValues[typeof name]) => {\n const fieldValidator = validators?.[name]\n if (fieldValidator) {\n // Bump version to track this validation run\n validationVersions[name] = (validationVersions[name] ?? 0) + 1\n const currentVersion = validationVersions[name]\n try {\n const result = await fieldValidator(value, getValues())\n // Only apply result if this is still the latest validation and not disposed\n if (!disposed && validationVersions[name] === currentVersion) {\n errorSig.set(result)\n }\n return result\n } catch (err) {\n // Validator threw — treat as error string if possible\n if (!disposed && validationVersions[name] === currentVersion) {\n const message = err instanceof Error ? err.message : String(err)\n errorSig.set(message)\n }\n return err instanceof Error ? err.message : String(err)\n }\n }\n errorSig.set(undefined)\n return undefined\n }\n\n const validateField = debounceMs\n ? (value: TValues[typeof name]) => {\n clearTimeout(debounceTimers[name])\n return new Promise<ValidationError>((resolve) => {\n debounceTimers[name] = setTimeout(async () => {\n resolve(await runValidation(value))\n }, debounceMs)\n })\n }\n : runValidation\n\n // Auto-validate on change if configured\n if (validateOn === 'change') {\n effect(() => {\n const v = valueSig()\n validateField(v)\n })\n }\n\n fields[name] = {\n value: valueSig,\n error: errorSig,\n touched: touchedSig,\n dirty: dirtySig,\n setValue: (value: TValues[typeof name]) => {\n valueSig.set(value)\n // Deep comparison for objects/arrays, reference for primitives\n dirtySig.set(!structuredEqual(value, initial))\n },\n setTouched: () => {\n touchedSig.set(true)\n if (validateOn === 'blur') {\n validateField(valueSig.peek())\n }\n },\n reset: () => {\n valueSig.set(initial as TValues[typeof name])\n errorSig.set(undefined)\n touchedSig.set(false)\n dirtySig.set(false)\n clearTimeout(debounceTimers[name])\n },\n } as FieldState<TValues[typeof name]>\n }\n\n // Clean up debounce timers and cancel in-flight validators on unmount\n onUnmount(() => {\n disposed = true\n clearAllTimers()\n })\n\n const isSubmitting = signal(false)\n const submitCount = signal(0)\n\n // Form-level computed signals\n const isValid = computed(() => {\n for (const name of fieldEntries.map(([n]) => n)) {\n if (fields[name].error() !== undefined) return false\n }\n return true\n })\n\n const isDirty = computed(() => {\n for (const name of fieldEntries.map(([n]) => n)) {\n if (fields[name].dirty()) return true\n }\n return false\n })\n\n const getErrors = (): Partial<Record<keyof TValues, ValidationError>> => {\n const errors = {} as Partial<Record<keyof TValues, ValidationError>>\n for (const [name] of fieldEntries) {\n const err = fields[name].error.peek()\n if (err !== undefined) errors[name] = err\n }\n return errors\n }\n\n const validate = async (): Promise<boolean> => {\n // Cancel any pending debounced validations\n clearAllTimers()\n\n isValidating.set(true)\n\n try {\n const allValues = getValues()\n\n // Clear all errors before re-validating\n for (const [name] of fieldEntries) {\n fields[name].error.set(undefined)\n }\n\n // Run field-level validators with all values for cross-field support\n await Promise.all(\n fieldEntries.map(async ([name]) => {\n const fieldValidator = validators?.[name]\n if (fieldValidator) {\n // Bump version so any in-flight debounced validation is discarded\n validationVersions[name] = (validationVersions[name] ?? 0) + 1\n const currentVersion = validationVersions[name]\n try {\n const error = await fieldValidator(fields[name].value.peek(), allValues)\n if (validationVersions[name] === currentVersion) {\n fields[name].error.set(error)\n }\n } catch (err) {\n if (validationVersions[name] === currentVersion) {\n fields[name].error.set(err instanceof Error ? err.message : String(err))\n }\n }\n }\n }),\n )\n\n // Run schema-level validator — only set schema errors for fields\n // that don't already have a field-level error (field-level wins)\n if (schema) {\n try {\n const schemaErrors = await schema(allValues)\n for (const [name] of fieldEntries) {\n const schemaError = schemaErrors[name]\n if (schemaError !== undefined && fields[name].error.peek() === undefined) {\n fields[name].error.set(schemaError)\n }\n }\n } catch (err) {\n // Schema validator threw — set as submitError rather than losing it\n submitError.set(err)\n return false\n }\n }\n\n // Re-check: any field with an error means invalid\n for (const [name] of fieldEntries) {\n if (fields[name].error.peek() !== undefined) return false\n }\n return true\n } finally {\n isValidating.set(false)\n }\n }\n\n const handleSubmit = async (e?: Event) => {\n if (e && typeof e.preventDefault === 'function') {\n e.preventDefault()\n }\n\n submitError.set(undefined)\n submitCount.update((n) => n + 1)\n\n // Mark all fields as touched\n for (const [name] of fieldEntries) {\n fields[name].touched.set(true)\n }\n\n const valid = await validate()\n if (!valid) return\n\n isSubmitting.set(true)\n try {\n await onSubmit(getValues())\n } catch (err) {\n submitError.set(err)\n throw err\n } finally {\n isSubmitting.set(false)\n }\n }\n\n const reset = () => {\n clearAllTimers()\n for (const [name] of fieldEntries) {\n fields[name].reset()\n }\n submitCount.set(0)\n submitError.set(undefined)\n }\n\n const setFieldValue = <K extends keyof TValues>(field: K, value: TValues[K]) => {\n if (!fields[field]) {\n throw new Error(\n `[@pyreon/form] Field \"${String(field)}\" does not exist. Available fields: ${fieldEntries.map(([n]) => n).join(', ')}`,\n )\n }\n fields[field].setValue(value)\n }\n\n const setFieldError = (field: keyof TValues, error: ValidationError) => {\n if (!fields[field]) {\n throw new Error(\n `[@pyreon/form] Field \"${String(field)}\" does not exist. Available fields: ${fieldEntries.map(([n]) => n).join(', ')}`,\n )\n }\n fields[field].error.set(error)\n }\n\n const setErrors = (errors: Partial<Record<keyof TValues, ValidationError>>) => {\n for (const [name, error] of Object.entries(errors)) {\n setFieldError(name as keyof TValues, error as ValidationError)\n }\n }\n\n const clearErrors = () => {\n for (const [name] of fieldEntries) {\n fields[name].error.set(undefined)\n }\n }\n\n const resetField = (field: keyof TValues) => {\n if (fields[field]) {\n fields[field].reset()\n }\n }\n\n // Memoized register props per field+type combo\n const registerCache = new Map<string, FieldRegisterProps<unknown>>()\n\n const register = <K extends keyof TValues & string>(\n field: K,\n opts?: { type?: 'checkbox' | 'number' },\n ): FieldRegisterProps<TValues[K]> => {\n const cacheKey = `${field}:${opts?.type ?? 'text'}`\n const cached = registerCache.get(cacheKey)\n if (cached) return cached as FieldRegisterProps<TValues[K]>\n\n const fieldState = fields[field]\n const props: FieldRegisterProps<TValues[K]> = {\n value: fieldState.value,\n onInput: (e: Event) => {\n const target = e.target as HTMLInputElement\n if (opts?.type === 'checkbox') {\n fieldState.setValue(target.checked as TValues[K])\n } else if (opts?.type === 'number') {\n const num = target.valueAsNumber\n fieldState.setValue((Number.isNaN(num) ? target.value : num) as TValues[K])\n } else {\n fieldState.setValue(target.value as TValues[K])\n }\n },\n onBlur: () => {\n fieldState.setTouched()\n },\n }\n\n if (opts?.type === 'checkbox') {\n props.checked = computed(() => Boolean(fieldState.value()))\n }\n\n registerCache.set(cacheKey, props as FieldRegisterProps<unknown>)\n return props\n }\n\n return {\n fields,\n isSubmitting,\n isValidating,\n isValid,\n isDirty,\n submitCount,\n submitError,\n values: getValues,\n errors: getErrors,\n setFieldValue,\n setFieldError,\n setErrors,\n clearErrors,\n resetField,\n register,\n handleSubmit,\n reset,\n validate,\n }\n}\n\n/** Deep structural equality with depth limit to guard against circular references. */\nfunction structuredEqual(a: unknown, b: unknown, depth = 0): boolean {\n if (Object.is(a, b)) return true\n if (a == null || b == null) return false\n if (typeof a !== typeof b) return false\n // Bail at depth 10 — treat as not equal to avoid infinite recursion\n if (depth > 10) return false\n\n if (Array.isArray(a) && Array.isArray(b)) {\n if (a.length !== b.length) return false\n for (let i = 0; i < a.length; i++) {\n if (!structuredEqual(a[i], b[i], depth + 1)) return false\n }\n return true\n }\n\n if (typeof a === 'object' && typeof b === 'object') {\n const aObj = a as Record<string, unknown>\n const bObj = b as Record<string, unknown>\n const aKeys = Object.keys(aObj)\n const bKeys = Object.keys(bObj)\n if (aKeys.length !== bKeys.length) return false\n for (const key of aKeys) {\n if (!structuredEqual(aObj[key], bObj[key], depth + 1)) return false\n }\n return true\n }\n\n return false\n}\n","import type { Computed } from '@pyreon/reactivity'\nimport { computed } from '@pyreon/reactivity'\nimport type { FormState, ValidationError } from './types'\n\nexport interface FormStateSummary<TValues extends Record<string, unknown>> {\n isSubmitting: boolean\n isValidating: boolean\n isValid: boolean\n isDirty: boolean\n submitCount: number\n submitError: unknown\n touchedFields: Partial<Record<keyof TValues, boolean>>\n dirtyFields: Partial<Record<keyof TValues, boolean>>\n errors: Partial<Record<keyof TValues, ValidationError>>\n}\n\n/**\n * Subscribe to the full form state as a single computed signal.\n * Useful for rendering form-level UI (submit button disabled state,\n * error summaries, progress indicators).\n *\n * @example\n * const state = useFormState(form)\n * // state() => { isSubmitting, isValid, isDirty, errors, ... }\n *\n * @example\n * // Use a selector for fine-grained reactivity\n * const canSubmit = useFormState(form, (s) => s.isValid && !s.isSubmitting)\n */\nexport function useFormState<TValues extends Record<string, unknown>>(\n form: FormState<TValues>,\n): Computed<FormStateSummary<TValues>>\n\nexport function useFormState<TValues extends Record<string, unknown>, R>(\n form: FormState<TValues>,\n selector: (state: FormStateSummary<TValues>) => R,\n): Computed<R>\n\nexport function useFormState<TValues extends Record<string, unknown>, R>(\n form: FormState<TValues>,\n selector?: (state: FormStateSummary<TValues>) => R,\n): Computed<FormStateSummary<TValues>> | Computed<R> {\n const buildSummary = (): FormStateSummary<TValues> => {\n const touchedFields = {} as Partial<Record<keyof TValues, boolean>>\n const dirtyFields = {} as Partial<Record<keyof TValues, boolean>>\n const errors = {} as Partial<Record<keyof TValues, ValidationError>>\n\n for (const key of Object.keys(form.fields) as (keyof TValues & string)[]) {\n const field = form.fields[key]\n if (field.touched()) touchedFields[key] = true\n if (field.dirty()) dirtyFields[key] = true\n const err = field.error()\n if (err !== undefined) errors[key] = err\n }\n\n return {\n isSubmitting: form.isSubmitting(),\n isValidating: form.isValidating(),\n isValid: form.isValid(),\n isDirty: form.isDirty(),\n submitCount: form.submitCount(),\n submitError: form.submitError(),\n touchedFields,\n dirtyFields,\n errors,\n }\n }\n\n if (selector) {\n return computed(() => selector(buildSummary()))\n }\n\n return computed(buildSummary)\n}\n","import type { Computed, Signal } from '@pyreon/reactivity'\nimport { computed } from '@pyreon/reactivity'\nimport type { FormState } from './types'\n\n/**\n * Watch specific field values reactively. Returns a computed signal\n * that re-evaluates when any of the watched fields change.\n *\n * @example\n * // Watch a single field\n * const email = useWatch(form, 'email')\n * // email() => current email value\n *\n * @example\n * // Watch multiple fields\n * const [first, last] = useWatch(form, ['firstName', 'lastName'])\n * // first() => firstName value, last() => lastName value\n *\n * @example\n * // Watch all fields\n * const all = useWatch(form)\n * // all() => { email: '...', password: '...' }\n */\nexport function useWatch<TValues extends Record<string, unknown>, K extends keyof TValues & string>(\n form: FormState<TValues>,\n name: K,\n): Signal<TValues[K]>\n\nexport function useWatch<\n TValues extends Record<string, unknown>,\n K extends (keyof TValues & string)[],\n>(form: FormState<TValues>, names: K): { [I in keyof K]: Signal<TValues[K[I] & keyof TValues]> }\n\nexport function useWatch<TValues extends Record<string, unknown>>(\n form: FormState<TValues>,\n): Computed<TValues>\n\nexport function useWatch<TValues extends Record<string, unknown>, K extends keyof TValues & string>(\n form: FormState<TValues>,\n nameOrNames?: K | K[],\n): Signal<TValues[K]> | Signal<TValues[K]>[] | Computed<TValues> {\n // Watch all fields\n if (nameOrNames === undefined) {\n return computed(() => {\n const result = {} as TValues\n for (const key of Object.keys(form.fields) as (keyof TValues & string)[]) {\n ;(result as Record<string, unknown>)[key] = form.fields[key].value()\n }\n return result\n })\n }\n\n // Watch multiple fields\n if (Array.isArray(nameOrNames)) {\n return nameOrNames.map((name) => form.fields[name].value) as Signal<TValues[K]>[]\n }\n\n // Watch single field\n return form.fields[nameOrNames].value\n}\n"],"mappings":";;;;AAIA,MAAM,cAAc,cAAyD,KAAK;;;;;;;;;;;;;AAmBlF,SAAgB,aACd,OACO;AACP,SAAQ,aAAa,MAAM,KAA2C;CAEtE,MAAM,KAAK,MAAM;AACjB,QAAQ,OAAO,OAAO,aAAc,IAAyB,GAAG;;;;;;;;;;;;AAalE,SAAgB,iBAEQ;CACtB,MAAM,OAAO,WA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AC/B;GACA;GACA;GACD;;AAGH,KAAI,SACF,QAAO,eAAe,SAAS,cAAc,CAAC,CAAC;AAGjD,QAAO,SAAS,aAAa;;;;;ACnC/B,SAAgB,SACd,MACA,aAC+D;AAE/D,KAAI,gBAAgB,OAClB,QAAO,eAAe;EACpB,MAAM,SAAS,EAAE;AACjB,OAAK,MAAM,OAAO,OAAO,KAAK,KAAK,OAAO,CACvC,CAAC,OAAmC,OAAO,KAAK,OAAO,KAAK,OAAO;AAEtE,SAAO;GACP;AAIJ,KAAI,MAAM,QAAQ,YAAY,CAC5B,QAAO,YAAY,KAAK,SAAS,KAAK,OAAO,MAAM,MAAM;AAI3D,QAAO,KAAK,OAAO,aAAa"}
|
package/lib/types/index.d.ts
CHANGED
|
@@ -73,7 +73,7 @@ interface FormState<TValues extends Record<string, unknown>> {
|
|
|
73
73
|
* For numbers: pass `{ type: 'number' }` to use `valueAsNumber` on input.
|
|
74
74
|
*/
|
|
75
75
|
register: <K extends keyof TValues & string>(field: K, options?: {
|
|
76
|
-
type?:
|
|
76
|
+
type?: 'checkbox' | 'number';
|
|
77
77
|
}) => FieldRegisterProps<TValues[K]>;
|
|
78
78
|
/**
|
|
79
79
|
* Submit handler — runs validation, then calls onSubmit if valid.
|
|
@@ -95,7 +95,7 @@ interface UseFormOptions<TValues extends Record<string, unknown>> {
|
|
|
95
95
|
/** Schema-level validator (runs after field validators). */
|
|
96
96
|
schema?: SchemaValidateFn<TValues>;
|
|
97
97
|
/** When to validate: 'blur' (default), 'change', or 'submit'. */
|
|
98
|
-
validateOn?:
|
|
98
|
+
validateOn?: 'blur' | 'change' | 'submit';
|
|
99
99
|
/** Debounce delay in ms for validators (useful for async validators). */
|
|
100
100
|
debounceMs?: number;
|
|
101
101
|
}
|
|
@@ -148,7 +148,7 @@ interface UseFieldResult<T> {
|
|
|
148
148
|
reset: () => void;
|
|
149
149
|
/** Register props for input binding. */
|
|
150
150
|
register: (opts?: {
|
|
151
|
-
type?:
|
|
151
|
+
type?: 'checkbox';
|
|
152
152
|
}) => FieldRegisterProps<T>;
|
|
153
153
|
/** Whether the field has an error (computed). */
|
|
154
154
|
hasError: Computed<boolean>;
|
package/package.json
CHANGED
|
@@ -1,20 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pyreon/form",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.6",
|
|
4
4
|
"description": "Signal-based form management for Pyreon",
|
|
5
|
+
"homepage": "https://github.com/pyreon/pyreon/tree/main/packages/form#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/pyreon/pyreon/issues"
|
|
8
|
+
},
|
|
5
9
|
"license": "MIT",
|
|
6
10
|
"repository": {
|
|
7
11
|
"type": "git",
|
|
8
12
|
"url": "https://github.com/pyreon/pyreon.git",
|
|
9
13
|
"directory": "packages/fundamentals/form"
|
|
10
14
|
},
|
|
11
|
-
"homepage": "https://github.com/pyreon/pyreon/tree/main/packages/form#readme",
|
|
12
|
-
"bugs": {
|
|
13
|
-
"url": "https://github.com/pyreon/pyreon/issues"
|
|
14
|
-
},
|
|
15
|
-
"publishConfig": {
|
|
16
|
-
"access": "public"
|
|
17
|
-
},
|
|
18
15
|
"files": [
|
|
19
16
|
"lib",
|
|
20
17
|
"src",
|
|
@@ -38,21 +35,24 @@
|
|
|
38
35
|
"types": "./lib/types/devtools.d.ts"
|
|
39
36
|
}
|
|
40
37
|
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
41
|
"scripts": {
|
|
42
42
|
"build": "vl_rolldown_build",
|
|
43
43
|
"dev": "vl_rolldown_build-watch",
|
|
44
44
|
"test": "vitest run",
|
|
45
45
|
"typecheck": "tsc --noEmit",
|
|
46
|
-
"lint": "
|
|
47
|
-
},
|
|
48
|
-
"peerDependencies": {
|
|
49
|
-
"@pyreon/core": "^0.11.4",
|
|
50
|
-
"@pyreon/reactivity": "^0.11.4"
|
|
46
|
+
"lint": "oxlint ."
|
|
51
47
|
},
|
|
52
48
|
"devDependencies": {
|
|
53
49
|
"@happy-dom/global-registrator": "^20.8.3",
|
|
54
|
-
"@pyreon/core": "^0.11.
|
|
55
|
-
"@pyreon/reactivity": "^0.11.
|
|
56
|
-
"@pyreon/runtime-dom": "^0.11.
|
|
50
|
+
"@pyreon/core": "^0.11.6",
|
|
51
|
+
"@pyreon/reactivity": "^0.11.6",
|
|
52
|
+
"@pyreon/runtime-dom": "^0.11.6"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"@pyreon/core": "^0.11.6",
|
|
56
|
+
"@pyreon/reactivity": "^0.11.6"
|
|
57
57
|
}
|
|
58
58
|
}
|
package/src/context.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { Props, VNode, VNodeChild } from
|
|
2
|
-
import { createContext, provide, useContext } from
|
|
3
|
-
import type { FormState } from
|
|
1
|
+
import type { Props, VNode, VNodeChild } from '@pyreon/core'
|
|
2
|
+
import { createContext, provide, useContext } from '@pyreon/core'
|
|
3
|
+
import type { FormState } from './types'
|
|
4
4
|
|
|
5
5
|
const FormContext = createContext<FormState<Record<string, unknown>> | null>(null)
|
|
6
6
|
|
|
@@ -27,7 +27,7 @@ export function FormProvider<TValues extends Record<string, unknown>>(
|
|
|
27
27
|
provide(FormContext, props.form as FormState<Record<string, unknown>>)
|
|
28
28
|
|
|
29
29
|
const ch = props.children
|
|
30
|
-
return (typeof ch ===
|
|
30
|
+
return (typeof ch === 'function' ? (ch as () => VNodeChild)() : ch) as VNode
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
/**
|
|
@@ -45,7 +45,7 @@ export function useFormContext<
|
|
|
45
45
|
>(): FormState<TValues> {
|
|
46
46
|
const form = useContext(FormContext)
|
|
47
47
|
if (!form) {
|
|
48
|
-
throw new Error(
|
|
48
|
+
throw new Error('[@pyreon/form] useFormContext() must be used within a <FormProvider>.')
|
|
49
49
|
}
|
|
50
50
|
// Generic narrowing: context stores FormState<Record<string, unknown>>
|
|
51
51
|
// but callers narrow to their specific TValues at the call site.
|
package/src/devtools.ts
CHANGED
|
@@ -56,14 +56,14 @@ export function getFormSnapshot(name: string): Record<string, unknown> | undefin
|
|
|
56
56
|
const form = getFormInstance(name) as Record<string, unknown> | undefined
|
|
57
57
|
if (!form) return undefined
|
|
58
58
|
return {
|
|
59
|
-
values: typeof form.values ===
|
|
60
|
-
errors: typeof form.errors ===
|
|
59
|
+
values: typeof form.values === 'function' ? (form.values as () => unknown)() : undefined,
|
|
60
|
+
errors: typeof form.errors === 'function' ? (form.errors as () => unknown)() : undefined,
|
|
61
61
|
isSubmitting:
|
|
62
|
-
typeof form.isSubmitting ===
|
|
63
|
-
isValid: typeof form.isValid ===
|
|
64
|
-
isDirty: typeof form.isDirty ===
|
|
62
|
+
typeof form.isSubmitting === 'function' ? (form.isSubmitting as () => unknown)() : undefined,
|
|
63
|
+
isValid: typeof form.isValid === 'function' ? (form.isValid as () => unknown)() : undefined,
|
|
64
|
+
isDirty: typeof form.isDirty === 'function' ? (form.isDirty as () => unknown)() : undefined,
|
|
65
65
|
submitCount:
|
|
66
|
-
typeof form.submitCount ===
|
|
66
|
+
typeof form.submitCount === 'function' ? (form.submitCount as () => unknown)() : undefined,
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
|