@conform-to/react 0.3.1 → 0.4.0-pre.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.
package/helpers.js CHANGED
@@ -22,7 +22,7 @@ function input(config) {
22
22
  multiple: config.multiple
23
23
  };
24
24
 
25
- if (typeof config.initialError !== 'undefined') {
25
+ if (config.initialError && config.initialError.length > 0) {
26
26
  attributes.autoFocus = true;
27
27
  }
28
28
 
@@ -43,11 +43,10 @@ function select(config) {
43
43
  form: config.form,
44
44
  defaultValue: config.multiple ? Array.isArray(config.defaultValue) ? config.defaultValue : [] : "".concat((_config$defaultValue = config.defaultValue) !== null && _config$defaultValue !== void 0 ? _config$defaultValue : ''),
45
45
  required: config.required,
46
- multiple: config.multiple,
47
- autoFocus: typeof config.initialError !== 'undefined' ? Boolean(config.initialError) : undefined
46
+ multiple: config.multiple
48
47
  };
49
48
 
50
- if (typeof config.initialError !== 'undefined') {
49
+ if (config.initialError && config.initialError.length > 0) {
51
50
  attributes.autoFocus = true;
52
51
  }
53
52
 
@@ -66,7 +65,7 @@ function textarea(config) {
66
65
  autoFocus: Boolean(config.initialError)
67
66
  };
68
67
 
69
- if (typeof config.initialError !== 'undefined') {
68
+ if (config.initialError && config.initialError.length > 0) {
70
69
  attributes.autoFocus = true;
71
70
  }
72
71
 
package/hooks.d.ts CHANGED
@@ -1,6 +1,15 @@
1
- import { type FieldConfig, type FieldError, type FieldValue, type FieldElement, type FieldsetConstraint, type ListCommand, type Primitive } from '@conform-to/dom';
1
+ import { type FieldConfig, type FieldElement, type FieldValue, type FieldsetConstraint, type ListCommand, type Primitive, type Submission } from '@conform-to/dom';
2
2
  import { type InputHTMLAttributes, type FormEvent, type RefObject } from 'react';
3
- export interface FormConfig {
3
+ interface FormContext<Schema extends Record<string, any>> {
4
+ form: HTMLFormElement;
5
+ formData: FormData;
6
+ submission: Submission<Schema>;
7
+ }
8
+ export interface FormConfig<Schema extends Record<string, any>> {
9
+ /**
10
+ * Validation mode. Default to `client-only`.
11
+ */
12
+ mode?: 'client-only' | 'server-validation';
4
13
  /**
5
14
  * Define when the error should be reported initially.
6
15
  * Support "onSubmit", "onChange", "onBlur".
@@ -8,6 +17,14 @@ export interface FormConfig {
8
17
  * Default to `onSubmit`.
9
18
  */
10
19
  initialReport?: 'onSubmit' | 'onChange' | 'onBlur';
20
+ /**
21
+ * An object representing the initial value of the form.
22
+ */
23
+ defaultValue?: FieldValue<Schema>;
24
+ /**
25
+ * An object describing the state from the last submission
26
+ */
27
+ state?: Submission<Schema>;
11
28
  /**
12
29
  * Enable native validation before hydation.
13
30
  *
@@ -23,12 +40,12 @@ export interface FormConfig {
23
40
  /**
24
41
  * A function to be called when the form should be (re)validated.
25
42
  */
26
- validate?: (form: HTMLFormElement, submitter?: HTMLInputElement | HTMLButtonElement | null) => void;
43
+ onValidate?: (context: FormContext<Schema>) => void;
27
44
  /**
28
45
  * The submit event handler of the form. It will be called
29
46
  * only when the form is considered valid.
30
47
  */
31
- onSubmit?: (event: FormEvent<HTMLFormElement>) => void;
48
+ onSubmit?: (event: FormEvent<HTMLFormElement>, context: FormContext<Schema>) => void;
32
49
  }
33
50
  /**
34
51
  * Properties to be applied to the form element
@@ -38,13 +55,19 @@ interface FormProps {
38
55
  onSubmit: (event: FormEvent<HTMLFormElement>) => void;
39
56
  noValidate: boolean;
40
57
  }
58
+ interface Form<Schema extends Record<string, any>> {
59
+ ref: RefObject<HTMLFormElement>;
60
+ error: string;
61
+ props: FormProps;
62
+ config: FieldsetConfig<Schema>;
63
+ }
41
64
  /**
42
65
  * Returns properties required to hook into form events.
43
66
  * Applied custom validation and define when error should be reported.
44
67
  *
45
- * @see https://github.com/edmundhung/conform/tree/v0.3.1/packages/conform-react/README.md#useform
68
+ * @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.1/packages/conform-react/README.md#useform
46
69
  */
47
- export declare function useForm(config?: FormConfig): FormProps;
70
+ export declare function useForm<Schema extends Record<string, any>>(config?: FormConfig<Schema>): Form<Schema>;
48
71
  /**
49
72
  * All the information of the field, including state and config.
50
73
  */
@@ -70,7 +93,7 @@ export interface FieldsetConfig<Schema extends Record<string, any>> {
70
93
  /**
71
94
  * An object describing the initial error of each field
72
95
  */
73
- initialError?: FieldError<Schema>['details'];
96
+ initialError?: Array<[string, string]>;
74
97
  /**
75
98
  * An object describing the constraint of each field
76
99
  */
@@ -83,7 +106,7 @@ export interface FieldsetConfig<Schema extends Record<string, any>> {
83
106
  /**
84
107
  * Returns all the information about the fieldset.
85
108
  *
86
- * @see https://github.com/edmundhung/conform/tree/v0.3.1/packages/conform-react/README.md#usefieldset
109
+ * @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.1/packages/conform-react/README.md#usefieldset
87
110
  */
88
111
  export declare function useFieldset<Schema extends Record<string, any>>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config?: FieldsetConfig<Schema>): Fieldset<Schema>;
89
112
  export declare function useFieldset<Schema extends Record<string, any>>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config?: FieldConfig<Schema>): Fieldset<Schema>;
@@ -110,7 +133,7 @@ interface ListControl<Schema> {
110
133
  * Returns a list of key and config, with a group of helpers
111
134
  * configuring buttons for list manipulation
112
135
  *
113
- * @see https://github.com/edmundhung/conform/tree/v0.3.1/packages/conform-react/README.md#usefieldlist
136
+ * @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.1/packages/conform-react/README.md#usefieldlist
114
137
  */
115
138
  export declare function useFieldList<Payload = any>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config: FieldConfig<Array<Payload>>): [
116
139
  Array<{
@@ -140,9 +163,9 @@ interface InputControl<Element extends {
140
163
  * This is particular useful when integrating dropdown and datepicker whichs
141
164
  * introduces custom input mode.
142
165
  *
143
- * @see https://github.com/edmundhung/conform/tree/v0.3.1/packages/conform-react/README.md#usecontrolledinput
166
+ * @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.1/packages/conform-react/README.md#usecontrolledinput
144
167
  */
145
168
  export declare function useControlledInput<Element extends {
146
169
  focus: () => void;
147
- } = HTMLInputElement, Schema extends Primitive = Primitive>(field: FieldConfig<Schema>): [ShadowInputProps, InputControl<Element>];
170
+ } = HTMLInputElement, Schema extends Primitive = Primitive>(config: FieldConfig<Schema>): [ShadowInputProps, InputControl<Element>];
148
171
  export {};