@conform-to/react 0.1.0 → 0.3.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/hooks.d.ts CHANGED
@@ -1,64 +1,143 @@
1
- import { type FieldConfig, type Schema } from '@conform-to/dom';
2
- import { type ButtonHTMLAttributes, type FormEventHandler, type FormHTMLAttributes, type RefObject, type ReactElement } from 'react';
3
- interface FormConfig {
1
+ import { type FieldConfig, type FieldError, type FieldValue, type FieldElement, type FieldsetConstraint, type ListCommand, type Primitive } from '@conform-to/dom';
2
+ import { type InputHTMLAttributes, type FormEvent, type RefObject } from 'react';
3
+ export interface FormConfig {
4
4
  /**
5
- * Decide when the error should be reported initially.
6
- * Default to `onSubmit`
5
+ * Define when the error should be reported initially.
6
+ * Support "onSubmit", "onChange", "onBlur".
7
+ *
8
+ * Default to `onSubmit`.
7
9
  */
8
10
  initialReport?: 'onSubmit' | 'onChange' | 'onBlur';
9
11
  /**
10
- * Native browser report will be used before hydation if it is set to `true`.
11
- * Default to `false`
12
+ * Enable native validation before hydation.
13
+ *
14
+ * Default to `false`.
12
15
  */
13
16
  fallbackNative?: boolean;
14
17
  /**
15
- * The form could be submitted even if there is invalid input control if it is set to `true`.
16
- * Default to `false`
18
+ * Accept form submission regardless of the form validity.
19
+ *
20
+ * Default to `false`.
17
21
  */
18
22
  noValidate?: boolean;
19
23
  /**
20
- * The submit handler will be triggered only when the form is valid.
21
- * Or when noValidate is set to `true`
24
+ * A function to be called when the form should be (re)validated.
22
25
  */
23
- onSubmit?: FormHTMLAttributes<HTMLFormElement>['onSubmit'];
24
- onReset?: FormHTMLAttributes<HTMLFormElement>['onReset'];
26
+ validate?: (form: HTMLFormElement, submitter?: HTMLInputElement | HTMLButtonElement | null) => void;
27
+ /**
28
+ * The submit event handler of the form. It will be called
29
+ * only when the form is considered valid.
30
+ */
31
+ onSubmit?: (event: FormEvent<HTMLFormElement>) => void;
25
32
  }
33
+ /**
34
+ * Properties to be applied to the form element
35
+ */
26
36
  interface FormProps {
27
37
  ref: RefObject<HTMLFormElement>;
28
- onSubmit: Required<FormHTMLAttributes<HTMLFormElement>>['onSubmit'];
29
- onReset: Required<FormHTMLAttributes<HTMLFormElement>>['onReset'];
30
- noValidate: Required<FormHTMLAttributes<HTMLFormElement>>['noValidate'];
38
+ onSubmit: (event: FormEvent<HTMLFormElement>) => void;
39
+ noValidate: boolean;
31
40
  }
32
- export declare function useForm({ onReset, onSubmit, noValidate, fallbackNative, initialReport, }?: FormConfig): FormProps;
33
- interface FieldsetConfig<Type> extends Partial<FieldConfig<Type>> {
41
+ /**
42
+ * Returns properties required to hook into form events.
43
+ * Applied custom validation and define when error should be reported.
44
+ *
45
+ * @see https://github.com/edmundhung/conform/tree/v0.3.0-pre.0/packages/conform-react/README.md#useform
46
+ */
47
+ export declare function useForm(config?: FormConfig): FormProps;
48
+ /**
49
+ * All the information of the field, including state and config.
50
+ */
51
+ export declare type Field<Schema> = {
52
+ config: FieldConfig<Schema>;
53
+ error?: string;
54
+ };
55
+ /**
56
+ * A set of field information.
57
+ */
58
+ export declare type Fieldset<Schema extends Record<string, any>> = {
59
+ [Key in keyof Schema]-?: Field<Schema[Key]>;
60
+ };
61
+ export interface FieldsetConfig<Schema extends Record<string, any>> {
62
+ /**
63
+ * The prefix used to generate the name of nested fields.
64
+ */
65
+ name?: string;
66
+ /**
67
+ * An object representing the initial value of the fieldset.
68
+ */
69
+ defaultValue?: FieldValue<Schema>;
70
+ /**
71
+ * An object describing the initial error of each field
72
+ */
73
+ initialError?: FieldError<Schema>['details'];
74
+ /**
75
+ * An object describing the constraint of each field
76
+ */
77
+ constraint?: FieldsetConstraint<Schema>;
78
+ /**
79
+ * The id of the form, connecting each field to a form remotely.
80
+ */
81
+ form?: string;
34
82
  }
35
- interface FieldsetProps {
36
- ref: RefObject<HTMLFieldSetElement>;
83
+ /**
84
+ * Returns all the information about the fieldset.
85
+ *
86
+ * @see https://github.com/edmundhung/conform/tree/v0.3.0-pre.0/packages/conform-react/README.md#usefieldset
87
+ */
88
+ export declare function useFieldset<Schema extends Record<string, any>>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config?: FieldsetConfig<Schema>): Fieldset<Schema>;
89
+ export declare function useFieldset<Schema extends Record<string, any>>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config?: FieldConfig<Schema>): Fieldset<Schema>;
90
+ interface ControlButtonProps {
37
91
  name?: string;
92
+ value?: string;
38
93
  form?: string;
39
- onInput: FormEventHandler<HTMLFieldSetElement>;
40
- onReset: FormEventHandler<HTMLFieldSetElement>;
41
- onInvalid: FormEventHandler<HTMLFieldSetElement>;
94
+ formNoValidate: true;
42
95
  }
43
- export declare function useFieldset<Type extends Record<string, any>>(schema: Schema<Type>, config?: FieldsetConfig<Type>): [FieldsetProps, {
44
- [Key in keyof Type]-?: FieldConfig<Type[Key]>;
45
- }];
46
- interface FieldListControl {
47
- prepend(): ButtonHTMLAttributes<HTMLButtonElement>;
48
- append(): ButtonHTMLAttributes<HTMLButtonElement>;
49
- remove(index: number): ButtonHTMLAttributes<HTMLButtonElement>;
96
+ declare type CommandPayload<Schema, Type extends ListCommand<FieldValue<Schema>>['type']> = Extract<ListCommand<FieldValue<Schema>>, {
97
+ type: Type;
98
+ }>['payload'];
99
+ /**
100
+ * A group of helpers for configuring a list control button
101
+ */
102
+ interface ListControl<Schema> {
103
+ prepend(payload?: CommandPayload<Schema, 'prepend'>): ControlButtonProps;
104
+ append(payload?: CommandPayload<Schema, 'append'>): ControlButtonProps;
105
+ replace(payload: CommandPayload<Schema, 'replace'>): ControlButtonProps;
106
+ remove(payload: CommandPayload<Schema, 'remove'>): ControlButtonProps;
107
+ reorder(payload: CommandPayload<Schema, 'reorder'>): ControlButtonProps;
50
108
  }
51
- export declare function useFieldList<Type extends Array<any>>(config: FieldConfig<Type>): [
109
+ /**
110
+ * Returns a list of key and config, with a group of helpers
111
+ * configuring buttons for list manipulation
112
+ *
113
+ * @see https://github.com/edmundhung/conform/tree/v0.3.0-pre.0/packages/conform-react/README.md#usefieldlist
114
+ */
115
+ export declare function useFieldList<Payload = any>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config: FieldConfig<Array<Payload>>): [
52
116
  Array<{
53
117
  key: string;
54
- config: FieldConfig<Type extends Array<infer InnerType> ? InnerType : never>;
118
+ config: FieldConfig<Payload>;
55
119
  }>,
56
- FieldListControl
120
+ ListControl<Payload>
57
121
  ];
122
+ interface ShadowInputProps extends InputHTMLAttributes<HTMLInputElement> {
123
+ ref: RefObject<HTMLInputElement>;
124
+ }
58
125
  interface InputControl {
59
126
  value: string;
60
- onChange: (value: string) => void;
127
+ onChange: (eventOrValue: {
128
+ target: {
129
+ value: string;
130
+ };
131
+ } | string) => void;
61
132
  onBlur: () => void;
133
+ onInvalid: (event: FormEvent<FieldElement>) => void;
62
134
  }
63
- export declare function useControlledInput<T extends string | number | Date | undefined>(field: FieldConfig<T>): [ReactElement, InputControl];
135
+ /**
136
+ * Returns the properties required to configure a shadow input for validation.
137
+ * This is particular useful when integrating dropdown and datepicker whichs
138
+ * introduces custom input mode.
139
+ *
140
+ * @see https://github.com/edmundhung/conform/tree/v0.3.0-pre.0/packages/conform-react/README.md#usecontrolledinput
141
+ */
142
+ export declare function useControlledInput<Schema extends Primitive = Primitive>(field: FieldConfig<Schema>): [ShadowInputProps, InputControl];
64
143
  export {};