@conform-to/react 1.8.2 → 1.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,49 +1,139 @@
1
- import { type FormRef } from './util';
2
- export type Control = {
3
- /**
4
- * Current value of the base input. Undefined if the registered input
5
- * is a multi-select, file input, or checkbox group.
6
- */
7
- value: string | undefined;
8
- /**
9
- * Selected options of the base input. Defined only when the registered input
10
- * is a multi-select or checkbox group.
11
- */
12
- checked: boolean | undefined;
13
- /**
14
- * Checked state of the base input. Defined only when the registered input
15
- * is a single checkbox or radio input.
16
- */
17
- options: string[] | undefined;
18
- /**
19
- * Selected files of the base input. Defined only when the registered input
20
- * is a file input.
21
- */
22
- files: File[] | undefined;
23
- /**
24
- * Registers the base input element(s). Accepts a single input or an array for groups.
25
- */
26
- register: (element: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | HTMLCollectionOf<HTMLInputElement> | NodeListOf<HTMLInputElement> | null | undefined) => void;
27
- /**
28
- * Programmatically updates the input value and emits
29
- * both [change](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event) and
30
- * [input](https://developer.mozilla.org/en-US/docs/Web/API/Element/input_event) events.
31
- */
32
- change(value: string | string[] | boolean | File | File[] | FileList | null): void;
33
- /**
34
- * Emits [blur](https://developer.mozilla.org/en-US/docs/Web/API/Element/blur_event) and
35
- * [focusout](https://developer.mozilla.org/en-US/docs/Web/API/Element/focusout_event) events.
36
- * Does not actually move focus.
37
- */
38
- focus(): void;
39
- /**
40
- * Emits [focus](https://developer.mozilla.org/en-US/docs/Web/API/Element/focus_event) and
41
- * [focusin](https://developer.mozilla.org/en-US/docs/Web/API/Element/focusin_event) events.
42
- * This does not move the actual keyboard focus to the input. Use `element.focus()` instead
43
- * if you want to move focus to the input.
44
- */
45
- blur(): void;
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
- export {};
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