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