@conform-to/react 0.6.1 → 0.6.3-pre.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/base.d.ts ADDED
File without changes
package/helpers.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { type FieldConfig, type Primitive, VALIDATION_UNDEFINED, VALIDATION_SKIPPED, INTENT } from '@conform-to/dom';
1
+ import { INTENT } from '@conform-to/dom';
2
+ import { type FieldConfig, type Primitive, VALIDATION_UNDEFINED, VALIDATION_SKIPPED } from './hooks';
2
3
  import type { CSSProperties, HTMLInputTypeAttribute } from 'react';
3
4
  interface FormControlProps {
4
5
  id?: string;
@@ -45,10 +46,10 @@ type InputOptions = BaseOptions & ({
45
46
  type?: Exclude<HTMLInputTypeAttribute, 'button' | 'submit' | 'hidden'>;
46
47
  value?: never;
47
48
  });
49
+ export declare function input<Schema extends Primitive | unknown>(config: FieldConfig<Schema>, options?: InputOptions): InputProps<Schema>;
48
50
  export declare function input<Schema extends File | File[]>(config: FieldConfig<Schema>, options: InputOptions & {
49
51
  type: 'file';
50
52
  }): InputProps<Schema>;
51
- export declare function input<Schema extends Primitive>(config: FieldConfig<Schema>, options?: InputOptions): InputProps<Schema>;
52
- export declare function select(config: FieldConfig<Primitive | Primitive[]>, options?: BaseOptions): SelectProps;
53
- export declare function textarea(config: FieldConfig<Primitive>, options?: BaseOptions): TextareaProps;
53
+ export declare function select<Schema extends Primitive | Primitive[] | undefined | unknown>(config: FieldConfig<Schema>, options?: BaseOptions): SelectProps;
54
+ export declare function textarea<Schema extends Primitive | undefined | unknown>(config: FieldConfig<Schema>, options?: BaseOptions): TextareaProps;
54
55
  export { INTENT, VALIDATION_UNDEFINED, VALIDATION_SKIPPED };
package/helpers.js CHANGED
@@ -4,6 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js');
6
6
  var dom = require('@conform-to/dom');
7
+ var hooks = require('./hooks.js');
7
8
 
8
9
  /**
9
10
  * Style to make the input element visually hidden
@@ -89,14 +90,8 @@ Object.defineProperty(exports, 'INTENT', {
89
90
  enumerable: true,
90
91
  get: function () { return dom.INTENT; }
91
92
  });
92
- Object.defineProperty(exports, 'VALIDATION_SKIPPED', {
93
- enumerable: true,
94
- get: function () { return dom.VALIDATION_SKIPPED; }
95
- });
96
- Object.defineProperty(exports, 'VALIDATION_UNDEFINED', {
97
- enumerable: true,
98
- get: function () { return dom.VALIDATION_UNDEFINED; }
99
- });
93
+ exports.VALIDATION_SKIPPED = hooks.VALIDATION_SKIPPED;
94
+ exports.VALIDATION_UNDEFINED = hooks.VALIDATION_UNDEFINED;
100
95
  exports.input = input;
101
96
  exports.select = select;
102
97
  exports.textarea = textarea;
package/hooks.d.ts CHANGED
@@ -1,6 +1,27 @@
1
- import { type FieldConfig, type FieldElement, type FieldValue, type FieldsetConstraint, type FormMethod, type FormEncType, type Submission } from '@conform-to/dom';
1
+ import { type FieldConstraint, type FieldElement, type FieldsetConstraint, type Submission, type KeysOf, type ResolveType, getFormEncType, getFormMethod } from '@conform-to/dom';
2
2
  import { type FormEvent, type RefObject } from 'react';
3
- export interface FormConfig<Schema extends Record<string, any>, ClientSubmission extends Submission | Submission<Schema> = Submission> {
3
+ export type Primitive = null | undefined | string | number | boolean | Date;
4
+ export interface FieldConfig<Schema> extends FieldConstraint<Schema> {
5
+ id?: string;
6
+ name: string;
7
+ defaultValue?: FieldValue<Schema>;
8
+ initialError?: Record<string, string | string[]>;
9
+ form?: string;
10
+ descriptionId?: string;
11
+ errorId?: string;
12
+ /**
13
+ * The frist error of the field
14
+ */
15
+ error?: string;
16
+ /**
17
+ * All of the field errors
18
+ */
19
+ errors?: string[];
20
+ }
21
+ export type FieldValue<Schema> = Schema extends Primitive ? string : Schema extends File ? File : Schema extends Array<infer InnerType> ? Array<FieldValue<InnerType>> : unknown extends Schema ? any : Record<string, any> extends Schema ? {
22
+ [Key in KeysOf<Schema>]?: FieldValue<ResolveType<Schema, Key>>;
23
+ } : any;
24
+ export interface FormConfig<Output extends Record<string, any>, Input extends Record<string, any> = Output> {
4
25
  /**
5
26
  * If the form id is provided, Id for label,
6
27
  * input and error elements will be derived.
@@ -31,7 +52,7 @@ export interface FormConfig<Schema extends Record<string, any>, ClientSubmission
31
52
  /**
32
53
  * An object representing the initial value of the form.
33
54
  */
34
- defaultValue?: FieldValue<Schema>;
55
+ defaultValue?: FieldValue<Input>;
35
56
  /**
36
57
  * An object describing the result of the last submission
37
58
  */
@@ -39,7 +60,7 @@ export interface FormConfig<Schema extends Record<string, any>, ClientSubmission
39
60
  /**
40
61
  * An object describing the constraint of each field
41
62
  */
42
- constraint?: FieldsetConstraint<Schema>;
63
+ constraint?: FieldsetConstraint<Input>;
43
64
  /**
44
65
  * Enable native validation before hydation.
45
66
  *
@@ -58,17 +79,17 @@ export interface FormConfig<Schema extends Record<string, any>, ClientSubmission
58
79
  onValidate?: ({ form, formData, }: {
59
80
  form: HTMLFormElement;
60
81
  formData: FormData;
61
- }) => ClientSubmission;
82
+ }) => Submission | Submission<Output>;
62
83
  /**
63
84
  * The submit event handler of the form. It will be called
64
85
  * only when the form is considered valid.
65
86
  */
66
87
  onSubmit?: (event: FormEvent<HTMLFormElement>, context: {
67
88
  formData: FormData;
68
- submission: ClientSubmission;
89
+ submission: Submission;
69
90
  action: string;
70
- encType: FormEncType;
71
- method: FormMethod;
91
+ encType: ReturnType<typeof getFormEncType>;
92
+ method: ReturnType<typeof getFormMethod>;
72
93
  }) => void;
73
94
  }
74
95
  /**
@@ -96,14 +117,14 @@ interface Form {
96
117
  *
97
118
  * @see https://conform.guide/api/react#useform
98
119
  */
99
- export declare function useForm<Schema extends Record<string, any>, ClientSubmission extends Submission | Submission<Schema> = Submission>(config?: FormConfig<Schema, ClientSubmission>): [Form, Fieldset<Schema>];
120
+ export declare function useForm<Output extends Record<string, any>, Input extends Record<string, any> = Output>(config?: FormConfig<Output, Input>): [Form, Fieldset<Input>];
100
121
  /**
101
122
  * A set of field configuration
102
123
  */
103
- export type Fieldset<Schema extends Record<string, any>> = {
104
- [Key in keyof Schema]-?: FieldConfig<Schema[Key]>;
124
+ export type Fieldset<Schema extends Record<string, any> | undefined> = {
125
+ [Key in KeysOf<Schema>]-?: FieldConfig<ResolveType<Schema, Key>>;
105
126
  };
106
- export interface FieldsetConfig<Schema extends Record<string, any>> {
127
+ export interface FieldsetConfig<Schema extends Record<string, any> | undefined> {
107
128
  /**
108
129
  * The prefix used to generate the name of nested fields.
109
130
  */
@@ -130,17 +151,16 @@ export interface FieldsetConfig<Schema extends Record<string, any>> {
130
151
  *
131
152
  * @see https://conform.guide/api/react#usefieldset
132
153
  */
133
- export declare function useFieldset<Schema extends Record<string, any>>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config: FieldsetConfig<Schema>): Fieldset<Schema>;
134
- export declare function useFieldset<Schema extends Record<string, any>>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config: FieldConfig<Schema>): Fieldset<Schema>;
154
+ export declare function useFieldset<Schema extends Record<string, any> | undefined>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config: FieldsetConfig<Schema>): Fieldset<Schema>;
155
+ export declare function useFieldset<Schema extends Record<string, any> | undefined>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config: FieldConfig<Schema>): Fieldset<Schema>;
135
156
  /**
136
- * Returns a list of key and config, with a group of helpers
137
- * configuring buttons for list manipulation
157
+ * Returns a list of key and field config.
138
158
  *
139
159
  * @see https://conform.guide/api/react#usefieldlist
140
160
  */
141
- export declare function useFieldList<Payload = any>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config: FieldConfig<Array<Payload>>): Array<{
161
+ export declare function useFieldList<Schema extends Array<any> | undefined>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config: FieldConfig<Schema>): Array<{
142
162
  key: string;
143
- } & FieldConfig<Payload>>;
163
+ } & FieldConfig<Schema extends Array<infer Item> ? Item : never>>;
144
164
  interface InputControl {
145
165
  change: (eventOrValue: {
146
166
  target: {
@@ -164,4 +184,35 @@ export declare function useInputEvent<RefShape extends Exclude<any, FieldElement
164
184
  onSubmit?: (event: SubmitEvent) => void;
165
185
  onReset?: (event: Event) => void;
166
186
  }): [RefObject<RefShape>, InputControl];
187
+ export declare const VALIDATION_UNDEFINED = "__undefined__";
188
+ export declare const VALIDATION_SKIPPED = "__skipped__";
189
+ export declare const FORM_ERROR_ELEMENT_NAME = "__form__";
190
+ /**
191
+ * Validate the form with the Constraint Validation API
192
+ * @see https://conform.guide/api/react#validateconstraint
193
+ */
194
+ export declare function validateConstraint(options: {
195
+ form: HTMLFormElement;
196
+ formData?: FormData;
197
+ constraint?: Record<Lowercase<string>, (value: string, context: {
198
+ formData: FormData;
199
+ attributeValue: string;
200
+ }) => boolean>;
201
+ acceptMultipleErrors?: ({ name, intent, payload, }: {
202
+ name: string;
203
+ intent: string;
204
+ payload: Record<string, any>;
205
+ }) => boolean;
206
+ formatMessages?: ({ name, validity, constraint, defaultErrors, }: {
207
+ name: string;
208
+ validity: ValidityState;
209
+ constraint: Record<string, boolean>;
210
+ defaultErrors: string[];
211
+ }) => string[];
212
+ }): Submission;
213
+ export declare function reportSubmission(form: HTMLFormElement, submission: Submission): void;
214
+ /**
215
+ * Check if the current focus is on a intent button.
216
+ */
217
+ export declare function isFocusedOnIntentButton(form: HTMLFormElement, intent: string): boolean;
167
218
  export {};