@conform-to/react 1.8.1 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/future/dom.d.ts +36 -0
- package/dist/future/dom.js +226 -0
- package/dist/future/dom.mjs +212 -0
- package/dist/future/hooks.d.ts +145 -54
- package/dist/future/hooks.js +566 -38
- package/dist/future/hooks.mjs +550 -33
- package/dist/future/index.d.ts +4 -3
- package/dist/future/index.js +15 -2
- package/dist/future/index.mjs +2 -2
- package/dist/future/intent.d.ts +35 -0
- package/dist/future/intent.js +305 -0
- package/dist/future/intent.mjs +294 -0
- package/dist/future/state.d.ts +56 -0
- package/dist/future/state.js +300 -0
- package/dist/future/state.mjs +283 -0
- package/dist/future/types.d.ts +360 -0
- package/dist/future/util.d.ts +65 -36
- package/dist/future/util.js +198 -116
- package/dist/future/util.mjs +182 -110
- package/package.json +3 -2
- package/dist/future/context.d.ts +0 -15
- package/dist/future/context.js +0 -12
- package/dist/future/context.mjs +0 -8
package/dist/future/hooks.d.ts
CHANGED
|
@@ -1,49 +1,139 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
1
|
+
import { type Serialize, type SubmissionResult, serialize } from '@conform-to/dom/future';
|
|
2
|
+
import { useEffect } from 'react';
|
|
3
|
+
import type { FormContext, DefaultFieldMetadata, IntentDispatcher, FormMetadata, Fieldset, FormOptions, FieldName, Field, Control, Selector, UseFormDataOptions, ValidateHandler, ErrorHandler, SubmitHandler, FormState, FormRef } from './types';
|
|
4
|
+
export declare const FormConfig: import("react").Context<{
|
|
5
|
+
intentName: string;
|
|
6
|
+
observer: {
|
|
7
|
+
onFieldUpdate(callback: (event: {
|
|
8
|
+
type: "input" | "reset" | "mutation";
|
|
9
|
+
target: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
|
|
10
|
+
}) => void): () => void;
|
|
11
|
+
onFormUpdate(callback: (event: {
|
|
12
|
+
type: "submit" | "input" | "reset" | "mutation";
|
|
13
|
+
target: HTMLFormElement;
|
|
14
|
+
submitter?: HTMLInputElement | HTMLButtonElement | null;
|
|
15
|
+
}) => void): () => void;
|
|
16
|
+
dispose(): void;
|
|
17
|
+
};
|
|
18
|
+
serialize: typeof serialize;
|
|
19
|
+
}>;
|
|
20
|
+
export declare const Form: import("react").Context<FormContext<string>[]>;
|
|
21
|
+
/**
|
|
22
|
+
* Provides form context to child components.
|
|
23
|
+
* Stacks contexts to support nested forms, with latest context taking priority.
|
|
24
|
+
*/
|
|
25
|
+
export declare function FormProvider(props: {
|
|
26
|
+
context: FormContext;
|
|
27
|
+
children: React.ReactNode;
|
|
28
|
+
}): React.ReactElement;
|
|
29
|
+
export declare function useFormContext(formId?: string): FormContext<any>;
|
|
30
|
+
/**
|
|
31
|
+
* Core form hook that manages form state, validation, and submission.
|
|
32
|
+
* Handles both sync and async validation, intent dispatching, and DOM updates.
|
|
33
|
+
*/
|
|
34
|
+
export declare function useConform<ErrorShape, Value = undefined>(formRef: FormRef, options: {
|
|
35
|
+
key?: string;
|
|
36
|
+
serialize: Serialize;
|
|
37
|
+
intentName: string;
|
|
38
|
+
lastResult?: SubmissionResult<NoInfer<ErrorShape>> | null;
|
|
39
|
+
onValidate?: ValidateHandler<ErrorShape, Value>;
|
|
40
|
+
onError?: ErrorHandler<ErrorShape>;
|
|
41
|
+
onSubmit?: SubmitHandler<NoInfer<ErrorShape>, NoInfer<Value>>;
|
|
42
|
+
}): [FormState<ErrorShape>, (event: React.FormEvent<HTMLFormElement>) => void];
|
|
43
|
+
/**
|
|
44
|
+
* The main React hook for form management. Handles form state, validation, and submission
|
|
45
|
+
* while providing access to form metadata, field objects, and form actions.
|
|
46
|
+
*
|
|
47
|
+
* @see https://conform.guide/api/react/future/useForm
|
|
48
|
+
* @example
|
|
49
|
+
* ```tsx
|
|
50
|
+
* const { form, fields } = useForm({
|
|
51
|
+
* onValidate({ payload, error }) {
|
|
52
|
+
* if (!payload.email) {
|
|
53
|
+
* error.fieldErrors.email = ['Required'];
|
|
54
|
+
* }
|
|
55
|
+
* return error;
|
|
56
|
+
* }
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* return (
|
|
60
|
+
* <form {...form.props}>
|
|
61
|
+
* <input name={fields.email.name} defaultValue={fields.email.defaultValue} />
|
|
62
|
+
* <div>{fields.email.errors}</div>
|
|
63
|
+
* </form>
|
|
64
|
+
* );
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export declare function useForm<FormShape extends Record<string, any> = Record<string, any>, ErrorShape = string, Value = undefined>(options: FormOptions<FormShape, ErrorShape, Value>): {
|
|
68
|
+
form: FormMetadata<ErrorShape>;
|
|
69
|
+
fields: Fieldset<FormShape, DefaultFieldMetadata<ErrorShape>>;
|
|
70
|
+
intent: IntentDispatcher;
|
|
46
71
|
};
|
|
72
|
+
/**
|
|
73
|
+
* A React hook that provides access to form-level metadata and state.
|
|
74
|
+
* Requires `FormProvider` context when used in child components.
|
|
75
|
+
*
|
|
76
|
+
* @see https://conform.guide/api/react/future/useFormMetadata
|
|
77
|
+
* @example
|
|
78
|
+
* ```tsx
|
|
79
|
+
* function ErrorSummary() {
|
|
80
|
+
* const form = useFormMetadata();
|
|
81
|
+
*
|
|
82
|
+
* if (!form.invalid) return null;
|
|
83
|
+
*
|
|
84
|
+
* return (
|
|
85
|
+
* <div>Please fix {Object.keys(form.fieldErrors).length} errors</div>
|
|
86
|
+
* );
|
|
87
|
+
* }
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export declare function useFormMetadata<ErrorShape = string[]>(options?: {
|
|
91
|
+
formId?: string;
|
|
92
|
+
}): FormMetadata<ErrorShape>;
|
|
93
|
+
/**
|
|
94
|
+
* A React hook that provides access to a specific field's metadata and state.
|
|
95
|
+
* Requires `FormProvider` context when used in child components.
|
|
96
|
+
*
|
|
97
|
+
* @see https://conform.guide/api/react/future/useField
|
|
98
|
+
* @example
|
|
99
|
+
* ```tsx
|
|
100
|
+
* function FormField({ name, label }) {
|
|
101
|
+
* const field = useField(name);
|
|
102
|
+
*
|
|
103
|
+
* return (
|
|
104
|
+
* <div>
|
|
105
|
+
* <label htmlFor={field.id}>{label}</label>
|
|
106
|
+
* <input id={field.id} name={field.name} defaultValue={field.defaultValue} />
|
|
107
|
+
* {field.errors && <div>{field.errors.join(', ')}</div>}
|
|
108
|
+
* </div>
|
|
109
|
+
* );
|
|
110
|
+
* }
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
export declare function useField<FieldShape = any>(name: FieldName<FieldShape>, options?: {
|
|
114
|
+
formId?: string;
|
|
115
|
+
}): Field<FieldShape>;
|
|
116
|
+
/**
|
|
117
|
+
* A React hook that provides an intent dispatcher for programmatic form actions.
|
|
118
|
+
* Intent dispatchers allow you to trigger form operations like validation, field updates,
|
|
119
|
+
* and array manipulations without manual form submission.
|
|
120
|
+
*
|
|
121
|
+
* @see https://conform.guide/api/react/future/useIntent
|
|
122
|
+
* @example
|
|
123
|
+
* ```tsx
|
|
124
|
+
* function ResetButton() {
|
|
125
|
+
* const buttonRef = useRef<HTMLButtonElement>(null);
|
|
126
|
+
* const intent = useIntent(buttonRef);
|
|
127
|
+
*
|
|
128
|
+
* return (
|
|
129
|
+
* <button type="button" ref={buttonRef} onClick={() => intent.reset()}>
|
|
130
|
+
* Reset Form
|
|
131
|
+
* </button>
|
|
132
|
+
* );
|
|
133
|
+
* }
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
export declare function useIntent(formRef: FormRef): IntentDispatcher;
|
|
47
137
|
/**
|
|
48
138
|
* A React hook that lets you sync the state of an input and dispatch native form events from it.
|
|
49
139
|
* This is useful when emulating native input behavior — typically by rendering a hidden base input
|
|
@@ -76,14 +166,6 @@ export declare function useControl(options?: {
|
|
|
76
166
|
*/
|
|
77
167
|
onFocus?: () => void;
|
|
78
168
|
}): Control;
|
|
79
|
-
type Selector<FormValue, Result> = (formData: FormValue | null, lastResult: Result | undefined) => Result;
|
|
80
|
-
type UseFormDataOptions = {
|
|
81
|
-
/**
|
|
82
|
-
* Set to `true` to preserve file inputs and receive a `FormData` object in the selector.
|
|
83
|
-
* If omitted or `false`, the selector receives a `URLSearchParams` object, where all values are coerced to strings.
|
|
84
|
-
*/
|
|
85
|
-
acceptFiles?: boolean;
|
|
86
|
-
};
|
|
87
169
|
/**
|
|
88
170
|
* A React hook that lets you subscribe to the current `FormData` of a form and derive a custom value from it.
|
|
89
171
|
* The selector runs whenever the form's structure or data changes, and the hook re-renders only when the result is deeply different.
|
|
@@ -100,5 +182,14 @@ export declare function useFormData<Value = any>(formRef: FormRef, select: Selec
|
|
|
100
182
|
export declare function useFormData<Value = any>(formRef: FormRef, select: Selector<URLSearchParams, Value>, options?: UseFormDataOptions & {
|
|
101
183
|
acceptFiles?: boolean;
|
|
102
184
|
}): Value;
|
|
103
|
-
|
|
185
|
+
/**
|
|
186
|
+
* useLayoutEffect is client-only.
|
|
187
|
+
* This basically makes it a no-op on server
|
|
188
|
+
*/
|
|
189
|
+
export declare const useSafeLayoutEffect: typeof useEffect;
|
|
190
|
+
/**
|
|
191
|
+
* Keep a mutable ref in sync with the latest value.
|
|
192
|
+
* Useful to avoid stale closures in event handlers or async callbacks.
|
|
193
|
+
*/
|
|
194
|
+
export declare function useLatest<Value>(value: Value): import("react").MutableRefObject<Value>;
|
|
104
195
|
//# sourceMappingURL=hooks.d.ts.map
|