@conform-to/react 0.6.0 → 0.6.2

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 CHANGED
@@ -42,12 +42,20 @@ function LoginForm() {
42
42
  id: undefined,
43
43
 
44
44
  /**
45
- * Define when the error should be reported initially.
45
+ * Define when conform should start validation.
46
46
  * Support "onSubmit", "onChange", "onBlur".
47
47
  *
48
48
  * Default to `onSubmit`.
49
49
  */
50
- initialReport: 'onBlur',
50
+ shouldValidate: 'onSubmit',
51
+
52
+ /**
53
+ * Define when conform should revalidate again.
54
+ * Support "onSubmit", "onChange", "onBlur".
55
+ *
56
+ * Default to `onInput`.
57
+ */
58
+ shouldRevalidate: 'onInput',
51
59
 
52
60
  /**
53
61
  * An object representing the initial value of the form.
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;
@@ -34,23 +35,21 @@ interface TextareaProps extends FormControlProps {
34
35
  maxLength?: number;
35
36
  defaultValue?: string;
36
37
  }
37
- type InputOptions = {
38
- type: 'checkbox' | 'radio';
38
+ type BaseOptions = {
39
+ description?: boolean;
39
40
  hidden?: boolean;
41
+ };
42
+ type InputOptions = BaseOptions & ({
43
+ type: 'checkbox' | 'radio';
40
44
  value?: string;
41
45
  } | {
42
46
  type?: Exclude<HTMLInputTypeAttribute, 'button' | 'submit' | 'hidden'>;
43
- hidden?: boolean;
44
47
  value?: never;
45
- };
46
- export declare function input<Schema extends File | File[]>(config: FieldConfig<Schema>, options: {
48
+ });
49
+ export declare function input<Schema extends File | File[]>(config: FieldConfig<Schema>, options: InputOptions & {
47
50
  type: 'file';
48
51
  }): InputProps<Schema>;
49
52
  export declare function input<Schema extends Primitive>(config: FieldConfig<Schema>, options?: InputOptions): InputProps<Schema>;
50
- export declare function select(config: FieldConfig<Primitive | Primitive[]>, options?: {
51
- hidden?: boolean;
52
- }): SelectProps;
53
- export declare function textarea(config: FieldConfig<Primitive>, options?: {
54
- hidden?: boolean;
55
- }): TextareaProps;
53
+ export declare function select(config: FieldConfig<Primitive | Primitive[]>, options?: BaseOptions): SelectProps;
54
+ export declare function textarea(config: FieldConfig<Primitive>, options?: BaseOptions): TextareaProps;
56
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
@@ -30,10 +31,13 @@ function getFormControlProps(config, options) {
30
31
  };
31
32
  if (config.id) {
32
33
  props.id = config.id;
33
- props['aria-describedby'] = config.errorId;
34
+ }
35
+ if (config.descriptionId && options !== null && options !== void 0 && options.description) {
36
+ props['aria-describedby'] = config.descriptionId;
34
37
  }
35
38
  if (config.errorId && (_config$error = config.error) !== null && _config$error !== void 0 && _config$error.length) {
36
39
  props['aria-invalid'] = true;
40
+ props['aria-describedby'] = config.descriptionId && options !== null && options !== void 0 && options.description ? "".concat(config.errorId, " ").concat(config.descriptionId) : config.errorId;
37
41
  }
38
42
  if (config.initialError && Object.entries(config.initialError).length > 0) {
39
43
  props.autoFocus = true;
@@ -86,14 +90,8 @@ Object.defineProperty(exports, 'INTENT', {
86
90
  enumerable: true,
87
91
  get: function () { return dom.INTENT; }
88
92
  });
89
- Object.defineProperty(exports, 'VALIDATION_SKIPPED', {
90
- enumerable: true,
91
- get: function () { return dom.VALIDATION_SKIPPED; }
92
- });
93
- Object.defineProperty(exports, 'VALIDATION_UNDEFINED', {
94
- enumerable: true,
95
- get: function () { return dom.VALIDATION_UNDEFINED; }
96
- });
93
+ exports.VALIDATION_SKIPPED = hooks.VALIDATION_SKIPPED;
94
+ exports.VALIDATION_UNDEFINED = hooks.VALIDATION_UNDEFINED;
97
95
  exports.input = input;
98
96
  exports.select = select;
99
97
  exports.textarea = textarea;
package/hooks.d.ts CHANGED
@@ -1,22 +1,58 @@
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>> : Schema extends Record<string, any> ? {
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.
7
28
  */
8
29
  id?: string;
9
30
  /**
10
- * Define when the error should be reported initially.
31
+ * A form ref object. Conform will fallback to its own ref object if it is not provided.
32
+ */
33
+ ref?: RefObject<HTMLFormElement>;
34
+ /**
35
+ * @deprecated Use `shouldValidate` and `shouldRevalidate` instead.
36
+ */
37
+ initialReport?: 'onSubmit' | 'onChange' | 'onBlur';
38
+ /**
39
+ * Define when conform should start validation.
11
40
  * Support "onSubmit", "onChange", "onBlur".
12
41
  *
13
42
  * Default to `onSubmit`.
14
43
  */
15
- initialReport?: 'onSubmit' | 'onChange' | 'onBlur';
44
+ shouldValidate?: 'onSubmit' | 'onBlur' | 'onInput';
45
+ /**
46
+ * Define when conform should revalidate again.
47
+ * Support "onSubmit", "onChange", "onBlur".
48
+ *
49
+ * Default to `onInput`.
50
+ */
51
+ shouldRevalidate?: 'onSubmit' | 'onBlur' | 'onInput';
16
52
  /**
17
53
  * An object representing the initial value of the form.
18
54
  */
19
- defaultValue?: FieldValue<Schema>;
55
+ defaultValue?: FieldValue<Input>;
20
56
  /**
21
57
  * An object describing the result of the last submission
22
58
  */
@@ -24,7 +60,7 @@ export interface FormConfig<Schema extends Record<string, any>, ClientSubmission
24
60
  /**
25
61
  * An object describing the constraint of each field
26
62
  */
27
- constraint?: FieldsetConstraint<Schema>;
63
+ constraint?: FieldsetConstraint<Input>;
28
64
  /**
29
65
  * Enable native validation before hydation.
30
66
  *
@@ -43,17 +79,17 @@ export interface FormConfig<Schema extends Record<string, any>, ClientSubmission
43
79
  onValidate?: ({ form, formData, }: {
44
80
  form: HTMLFormElement;
45
81
  formData: FormData;
46
- }) => ClientSubmission;
82
+ }) => Submission | Submission<Output>;
47
83
  /**
48
84
  * The submit event handler of the form. It will be called
49
85
  * only when the form is considered valid.
50
86
  */
51
87
  onSubmit?: (event: FormEvent<HTMLFormElement>, context: {
52
88
  formData: FormData;
53
- submission: ClientSubmission;
89
+ submission: Submission;
54
90
  action: string;
55
- encType: FormEncType;
56
- method: FormMethod;
91
+ encType: ReturnType<typeof getFormEncType>;
92
+ method: ReturnType<typeof getFormMethod>;
57
93
  }) => void;
58
94
  }
59
95
  /**
@@ -81,12 +117,12 @@ interface Form {
81
117
  *
82
118
  * @see https://conform.guide/api/react#useform
83
119
  */
84
- 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>];
85
121
  /**
86
122
  * A set of field configuration
87
123
  */
88
124
  export type Fieldset<Schema extends Record<string, any>> = {
89
- [Key in keyof Schema]-?: FieldConfig<Schema[Key]>;
125
+ [Key in KeysOf<Schema>]-?: FieldConfig<ResolveType<Schema, Key>>;
90
126
  };
91
127
  export interface FieldsetConfig<Schema extends Record<string, any>> {
92
128
  /**
@@ -118,8 +154,7 @@ export interface FieldsetConfig<Schema extends Record<string, any>> {
118
154
  export declare function useFieldset<Schema extends Record<string, any>>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config: FieldsetConfig<Schema>): Fieldset<Schema>;
119
155
  export declare function useFieldset<Schema extends Record<string, any>>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config: FieldConfig<Schema>): Fieldset<Schema>;
120
156
  /**
121
- * Returns a list of key and config, with a group of helpers
122
- * configuring buttons for list manipulation
157
+ * Returns a list of key and field config.
123
158
  *
124
159
  * @see https://conform.guide/api/react#usefieldlist
125
160
  */
@@ -149,4 +184,35 @@ export declare function useInputEvent<RefShape extends Exclude<any, FieldElement
149
184
  onSubmit?: (event: SubmitEvent) => void;
150
185
  onReset?: (event: Event) => void;
151
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;
152
218
  export {};