@conform-to/react 1.17.1 → 1.19.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.
@@ -1,25 +1,28 @@
1
- import { type FieldName, type FormValue, type Serialize, type SubmissionResult, createGlobalFormsObserver } from '@conform-to/dom/future';
1
+ import { type FormValue, type Serialize, type SubmissionResult } from '@conform-to/dom/future';
2
2
  import { useEffect } from 'react';
3
- import type { FormContext, IntentDispatcher, FormMetadata, Fieldset, GlobalFormOptions, FormOptions, FieldMetadata, Control, Selector, UseFormDataOptions, ValidateHandler, ErrorHandler, SubmitHandler, FormState, FormRef, BaseErrorShape, DefaultErrorShape, BaseSchemaType, InferInput, InferOutput } from './types';
4
- import { StandardSchemaV1 } from './standard-schema';
3
+ import type { FormContext, Control, Selector, UseFormDataOptions, ValidateHandler, ErrorHandler, SubmitHandler, FormState, FormRef, BaseControlProps, StandardControlOptions, DefaultControlValue, CheckedControlOptions, CustomControlOptions } from './types';
5
4
  export declare const INITIAL_KEY = "INITIAL_KEY";
6
- export declare const GlobalFormOptionsContext: import("react").Context<GlobalFormOptions & {
7
- observer: ReturnType<typeof createGlobalFormsObserver>;
5
+ export declare const GlobalFormsObserverContext: import("react").Context<{
6
+ onFieldUpdate(callback: (event: {
7
+ type: "input" | "reset" | "mutation";
8
+ target: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
9
+ }) => void): () => void;
10
+ onFormUpdate(callback: (event: {
11
+ type: "submit" | "input" | "reset" | "mutation";
12
+ target: HTMLFormElement;
13
+ submitter?: HTMLInputElement | HTMLButtonElement | null;
14
+ }) => void): () => void;
15
+ onInternalUpdate(callback: (event: {
16
+ target: HTMLFormElement;
17
+ }) => void): () => void;
18
+ dispose(): void;
8
19
  }>;
9
- export declare const FormContextContext: import("react").Context<FormContext<string>[]>;
10
- /**
11
- * Provides form context to child components.
12
- * Stacks contexts to support nested forms, with latest context taking priority.
13
- */
14
- export declare function FormProvider(props: {
15
- context: FormContext;
16
- children: React.ReactNode;
17
- }): React.ReactElement;
20
+ export declare const FormContextContext: import("react").Context<FormContext<any>[]>;
18
21
  /**
19
22
  * Preserves form field values when its contents are unmounted.
20
23
  * Useful for multi-step forms and virtualized lists.
21
24
  *
22
- * @see https://conform.guide/api/react/future/PreserveBoundary
25
+ * See https://conform.guide/api/react/future/PreserveBoundary
23
26
  */
24
27
  export declare function PreserveBoundary(props: {
25
28
  /**
@@ -34,196 +37,34 @@ export declare function PreserveBoundary(props: {
34
37
  form?: string;
35
38
  children: React.ReactNode;
36
39
  }): React.ReactElement;
37
- /**
38
- * @deprecated Replaced by the `configureForms` factory API. This will be removed in the next minor version. If you are not ready to migrate, please pin to `v1.16.0`.
39
- */
40
- export declare function FormOptionsProvider(props: Partial<GlobalFormOptions> & {
41
- children: React.ReactNode;
42
- }): React.ReactElement;
43
40
  export declare function useFormContext(formId?: string): FormContext;
44
41
  /**
45
42
  * Core form hook that manages form state, validation, and submission.
46
43
  * Handles both sync and async validation, intent dispatching, and DOM updates.
47
44
  */
48
- export declare function useConform<FormShape extends Record<string, any>, ErrorShape, Value = undefined, SchemaValue = undefined>(formRef: FormRef, options: {
45
+ export declare function useConform<FormShape extends Record<string, any>, ErrorShape, Value = undefined, SchemaValue = undefined, SchemaErrorShape = unknown>(formRef: FormRef, options: {
49
46
  key?: string | undefined;
50
47
  defaultValue?: Record<string, FormValue> | null | undefined;
51
48
  serialize: Serialize;
52
49
  intentName: string;
53
50
  lastResult?: SubmissionResult<NoInfer<ErrorShape>> | null | undefined;
54
- onValidate?: ValidateHandler<ErrorShape, Value, SchemaValue> | undefined;
51
+ onValidate?: ValidateHandler<ErrorShape, Value, SchemaValue, SchemaErrorShape> | undefined;
55
52
  onError?: ErrorHandler<ErrorShape> | undefined;
56
53
  onSubmit?: SubmitHandler<FormShape, NoInfer<ErrorShape>, NoInfer<Value>> | undefined;
57
54
  }): [FormState<ErrorShape>, (event: React.FormEvent<HTMLFormElement>) => void];
58
- /**
59
- * The main React hook for form management. Handles form state, validation, and submission
60
- * while providing access to form metadata, field objects, and form actions.
61
- *
62
- * It can be called in two ways:
63
- * - **Schema first**: Pass a schema as the first argument for automatic validation with type inference
64
- * - **Manual configuration**: Pass options with custom `onValidate` handler for manual validation
65
- *
66
- * @see https://conform.guide/api/react/future/useForm
67
- * @example Schema first setup with zod:
68
- *
69
- * ```tsx
70
- * const { form, fields } = useForm(zodSchema, {
71
- * lastResult,
72
- * shouldValidate: 'onBlur',
73
- * });
74
- *
75
- * return (
76
- * <form {...form.props}>
77
- * <input name={fields.email.name} defaultValue={fields.email.defaultValue} />
78
- * <div>{fields.email.errors}</div>
79
- * </form>
80
- * );
81
- * ```
82
- *
83
- * @example Manual configuration setup with custom validation:
84
- *
85
- * ```tsx
86
- * const { form, fields } = useForm({
87
- * onValidate({ payload, error }) {
88
- * if (!payload.email) {
89
- * error.fieldErrors.email = ['Required'];
90
- * }
91
- * return error;
92
- * }
93
- * });
94
- *
95
- * return (
96
- * <form {...form.props}>
97
- * <input name={fields.email.name} defaultValue={fields.email.defaultValue} />
98
- * <div>{fields.email.errors}</div>
99
- * </form>
100
- * );
101
- * ```
102
- */
103
- export declare function useForm<Schema extends BaseSchemaType, ErrorShape extends BaseErrorShape = DefaultErrorShape, Value = InferOutput<Schema>>(schema: Schema, options: FormOptions<InferInput<Schema>, ErrorShape, Value, Schema, string extends ErrorShape ? never : 'onValidate'>): {
104
- form: FormMetadata<ErrorShape>;
105
- fields: Fieldset<InferInput<Schema>, ErrorShape>;
106
- intent: IntentDispatcher<InferInput<Schema>>;
107
- };
108
- /**
109
- * @deprecated Use `useForm(schema, options)` instead for better type inference.
110
- */
111
- export declare function useForm<FormShape extends Record<string, any> = Record<string, any>, ErrorShape extends BaseErrorShape = DefaultErrorShape, Value = undefined>(options: FormOptions<FormShape, ErrorShape, Value, undefined, undefined extends Value ? 'onValidate' : never> & {
112
- /**
113
- * @deprecated Use `useForm(schema, options)` instead for better type inference.
114
- *
115
- * Optional standard schema for validation (e.g., Zod, Valibot, Yup).
116
- * Removes the need for manual onValidate setup.
117
- *
118
- */
119
- schema: StandardSchemaV1<FormShape, Value>;
120
- }): {
121
- form: FormMetadata<ErrorShape>;
122
- fields: Fieldset<FormShape, ErrorShape>;
123
- intent: IntentDispatcher<FormShape>;
124
- };
125
- export declare function useForm<FormShape extends Record<string, any> = Record<string, any>, ErrorShape extends BaseErrorShape = DefaultErrorShape, Value = undefined>(options: FormOptions<FormShape, ErrorShape, Value, undefined, 'onValidate'>): {
126
- form: FormMetadata<ErrorShape>;
127
- fields: Fieldset<FormShape, ErrorShape>;
128
- intent: IntentDispatcher<FormShape>;
129
- };
130
- /**
131
- * A React hook that provides access to form-level metadata and state.
132
- * Requires `FormProvider` context when used in child components.
133
- *
134
- * @see https://conform.guide/api/react/future/useFormMetadata
135
- * @example
136
- * ```tsx
137
- * function ErrorSummary() {
138
- * const form = useFormMetadata();
139
- *
140
- * if (form.valid) return null;
141
- *
142
- * return (
143
- * <div>Please fix {Object.keys(form.fieldErrors).length} errors</div>
144
- * );
145
- * }
146
- * ```
147
- */
148
- export declare function useFormMetadata(options?: {
149
- formId?: string;
150
- }): FormMetadata;
151
- /**
152
- * A React hook that provides access to a specific field's metadata and state.
153
- * Requires `FormProvider` context when used in child components.
154
- *
155
- * @see https://conform.guide/api/react/future/useField
156
- * @example
157
- * ```tsx
158
- * function FormField({ name, label }) {
159
- * const field = useField(name);
160
- *
161
- * return (
162
- * <div>
163
- * <label htmlFor={field.id}>{label}</label>
164
- * <input id={field.id} name={field.name} defaultValue={field.defaultValue} />
165
- * {field.errors && <div>{field.errors.join(', ')}</div>}
166
- * </div>
167
- * );
168
- * }
169
- * ```
170
- */
171
- export declare function useField<FieldShape = any>(name: FieldName<FieldShape>, options?: {
172
- formId?: string;
173
- }): FieldMetadata<FieldShape>;
174
- /**
175
- * A React hook that provides an intent dispatcher for programmatic form actions.
176
- * Intent dispatchers allow you to trigger form operations like validation, field updates,
177
- * and array manipulations without manual form submission.
178
- *
179
- * @see https://conform.guide/api/react/future/useIntent
180
- * @example
181
- * ```tsx
182
- * function ResetButton() {
183
- * const buttonRef = useRef<HTMLButtonElement>(null);
184
- * const intent = useIntent(buttonRef);
185
- *
186
- * return (
187
- * <button type="button" ref={buttonRef} onClick={() => intent.reset()}>
188
- * Reset Form
189
- * </button>
190
- * );
191
- * }
192
- * ```
193
- */
194
- export declare function useIntent<FormShape extends Record<string, any>>(formRef: FormRef): IntentDispatcher<FormShape>;
195
55
  /**
196
56
  * A React hook that lets you sync the state of an input and dispatch native form events from it.
197
- * This is useful when emulating native input behavior — typically by rendering a hidden base input
57
+ * This is useful when emulating native input behavior — typically by rendering a hidden base control
198
58
  * and syncing it with a custom input.
199
59
  *
200
- * @example
60
+ * **Example:**
201
61
  * ```ts
202
62
  * const control = useControl(options);
203
63
  * ```
204
64
  */
205
- export declare function useControl(options?: {
206
- /**
207
- * The initial value of the base input. It will be used to set the value
208
- * when the input is first registered.
209
- */
210
- defaultValue?: string | string[] | File | File[] | null | undefined;
211
- /**
212
- * Whether the base input should be checked by default. It will be applied
213
- * when the input is first registered.
214
- */
215
- defaultChecked?: boolean | undefined;
216
- /**
217
- * The value of a checkbox or radio input when checked. This sets the
218
- * value attribute of the base input.
219
- */
220
- value?: string;
221
- /**
222
- * A callback function that is triggered when the base input is focused.
223
- * Use this to delegate focus to a custom input.
224
- */
225
- onFocus?: () => void;
226
- }): Control;
65
+ export declare function useControl<Value, DefaultValue>(options: CustomControlOptions<Value, DefaultValue>): Control<Value, DefaultValue, Value>;
66
+ export declare function useControl<Value extends DefaultControlValue>(options?: StandardControlOptions<Value>): Control<Value>;
67
+ export declare function useControl(options: CheckedControlOptions): Control<boolean, string>;
227
68
  /**
228
69
  * A React hook that lets you subscribe to the current `FormData` of a form and derive a custom value from it.
229
70
  * The selector runs whenever the form's structure or data changes, and the hook re-renders only when the result is deeply different.
@@ -231,8 +72,9 @@ export declare function useControl(options?: {
231
72
  * Returns `undefined` when the form element is not available (e.g., on SSR or initial client render),
232
73
  * unless a `fallback` is provided.
233
74
  *
234
- * @see https://conform.guide/api/react/future/useFormData
235
- * @example
75
+ * See https://conform.guide/api/react/future/useFormData
76
+ *
77
+ * **Example:**
236
78
  * ```ts
237
79
  * const value = useFormData(
238
80
  * formRef,
@@ -268,4 +110,37 @@ export declare const useSafeLayoutEffect: typeof useEffect;
268
110
  * Useful to avoid stale closures in event handlers or async callbacks.
269
111
  */
270
112
  export declare function useLatest<Value>(value: Value): import("react").MutableRefObject<Value>;
113
+ /**
114
+ * A component that renders hidden base control(s) based on the shape of defaultValue.
115
+ * Used with useControl to sync complex values with form data.
116
+ *
117
+ * **Example:**
118
+ * ```tsx
119
+ * const control = useControl<{ street: string; city: string }>({
120
+ * defaultValue: { street: '123 Main St', city: 'Anytown' },
121
+ * parse(payload) {
122
+ * if (
123
+ * typeof payload === 'object' &&
124
+ * payload !== null &&
125
+ * 'street' in payload &&
126
+ * 'city' in payload &&
127
+ * typeof payload.street === 'string' &&
128
+ * typeof payload.city === 'string'
129
+ * ) {
130
+ * return payload;
131
+ * }
132
+ *
133
+ * throw new Error('Unexpected payload shape');
134
+ * },
135
+ * });
136
+ *
137
+ * <BaseControl
138
+ * type="fieldset"
139
+ * name="address"
140
+ * ref={control.register}
141
+ * defaultValue={control.defaultValue}
142
+ * />
143
+ * ```
144
+ */
145
+ export declare const BaseControl: import("react").ForwardRefExoticComponent<BaseControlProps & import("react").RefAttributes<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | HTMLFieldSetElement>>;
271
146
  //# sourceMappingURL=hooks.d.ts.map