@conform-to/react 0.2.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/helpers.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- import { type FieldProps } from '@conform-to/dom';
1
+ import { type FieldConfig, type Primitive } from '@conform-to/dom';
2
2
  import { type InputHTMLAttributes, type SelectHTMLAttributes, type TextareaHTMLAttributes } from 'react';
3
- export declare function input<Type extends string | number | Date | boolean | undefined>(props: FieldProps<Type>, { type, value }?: {
3
+ export declare function input<Schema extends Primitive>(config: FieldConfig<Schema>, { type, value }?: {
4
4
  type?: string;
5
5
  value?: string;
6
6
  }): InputHTMLAttributes<HTMLInputElement>;
7
- export declare function select<T extends any>(props: FieldProps<T>): SelectHTMLAttributes<HTMLSelectElement>;
8
- export declare function textarea<T extends string | undefined>(props: FieldProps<T>): TextareaHTMLAttributes<HTMLTextAreaElement>;
7
+ export declare function select<Schema extends Primitive | Array<Primitive>>(config: FieldConfig<Schema>): SelectHTMLAttributes<HTMLSelectElement>;
8
+ export declare function textarea<Schema extends Primitive>(config: FieldConfig<Schema>): TextareaHTMLAttributes<HTMLTextAreaElement>;
package/helpers.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- function input(props) {
5
+ function input(config) {
6
6
  var {
7
7
  type,
8
8
  value
@@ -10,50 +10,48 @@ function input(props) {
10
10
  var isCheckboxOrRadio = type === 'checkbox' || type === 'radio';
11
11
  var attributes = {
12
12
  type,
13
- name: props.name,
14
- form: props.form,
15
- required: props.required,
16
- minLength: props.minLength,
17
- maxLength: props.maxLength,
18
- min: props.min,
19
- max: props.max,
20
- step: props.step,
21
- pattern: props.pattern,
22
- multiple: props.multiple
13
+ name: config.name,
14
+ form: config.form,
15
+ required: config.required,
16
+ minLength: config.minLength,
17
+ maxLength: config.maxLength,
18
+ min: config.min,
19
+ max: config.max,
20
+ step: config.step,
21
+ pattern: config.pattern,
22
+ multiple: config.multiple
23
23
  };
24
24
 
25
25
  if (isCheckboxOrRadio) {
26
26
  attributes.value = value !== null && value !== void 0 ? value : 'on';
27
- attributes.defaultChecked = props.defaultValue === attributes.value;
27
+ attributes.defaultChecked = config.defaultValue === attributes.value;
28
28
  } else {
29
- var _props$defaultValue;
30
-
31
- attributes.defaultValue = "".concat((_props$defaultValue = props.defaultValue) !== null && _props$defaultValue !== void 0 ? _props$defaultValue : '');
29
+ attributes.defaultValue = config.defaultValue;
32
30
  }
33
31
 
34
32
  return attributes;
35
33
  }
36
- function select(props) {
37
- var _props$defaultValue2;
34
+ function select(config) {
35
+ var _config$defaultValue;
38
36
 
39
37
  return {
40
- name: props.name,
41
- form: props.form,
42
- defaultValue: props.multiple ? Array.isArray(props.defaultValue) ? props.defaultValue : [] : "".concat((_props$defaultValue2 = props.defaultValue) !== null && _props$defaultValue2 !== void 0 ? _props$defaultValue2 : ''),
43
- required: props.required,
44
- multiple: props.multiple
38
+ name: config.name,
39
+ form: config.form,
40
+ defaultValue: config.multiple ? Array.isArray(config.defaultValue) ? config.defaultValue : [] : "".concat((_config$defaultValue = config.defaultValue) !== null && _config$defaultValue !== void 0 ? _config$defaultValue : ''),
41
+ required: config.required,
42
+ multiple: config.multiple
45
43
  };
46
44
  }
47
- function textarea(props) {
48
- var _props$defaultValue3;
45
+ function textarea(config) {
46
+ var _config$defaultValue2;
49
47
 
50
48
  return {
51
- name: props.name,
52
- form: props.form,
53
- defaultValue: "".concat((_props$defaultValue3 = props.defaultValue) !== null && _props$defaultValue3 !== void 0 ? _props$defaultValue3 : ''),
54
- required: props.required,
55
- minLength: props.minLength,
56
- maxLength: props.maxLength
49
+ name: config.name,
50
+ form: config.form,
51
+ defaultValue: "".concat((_config$defaultValue2 = config.defaultValue) !== null && _config$defaultValue2 !== void 0 ? _config$defaultValue2 : ''),
52
+ required: config.required,
53
+ minLength: config.minLength,
54
+ maxLength: config.maxLength
57
55
  };
58
56
  }
59
57
 
package/hooks.d.ts CHANGED
@@ -1,64 +1,143 @@
1
- import { type FieldProps, type Schema, type FieldsetData } from '@conform-to/dom';
2
- import { type ButtonHTMLAttributes, type FormEventHandler, type FormHTMLAttributes, type RefObject, type ReactElement } from 'react';
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
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;
40
+ }
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;
31
82
  }
32
- export declare function useForm({ onReset, onSubmit, noValidate, fallbackNative, initialReport, }?: FormConfig): FormProps;
33
- export declare type FieldsetConfig<Type> = Partial<Pick<FieldProps<Type>, 'name' | 'form' | 'defaultValue' | 'error'>>;
34
- interface FieldsetProps {
35
- 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 {
36
91
  name?: string;
92
+ value?: string;
37
93
  form?: string;
38
- onInput: FormEventHandler<HTMLFieldSetElement>;
39
- onInvalid: FormEventHandler<HTMLFieldSetElement>;
94
+ formNoValidate: true;
40
95
  }
41
- export declare function useFieldset<Type extends Record<string, any>>(schema: Schema<Type>, config?: FieldsetConfig<Type>): [FieldsetProps, {
42
- [Key in keyof Type]-?: FieldProps<Type[Key]>;
43
- }];
44
- interface FieldListControl<T> {
45
- prepend(defaultValue?: FieldsetData<T, string>): ButtonHTMLAttributes<HTMLButtonElement>;
46
- append(defaultValue?: FieldsetData<T, string>): ButtonHTMLAttributes<HTMLButtonElement>;
47
- replace(index: number, defaultValue: FieldsetData<T, string>): ButtonHTMLAttributes<HTMLButtonElement>;
48
- remove(index: number): ButtonHTMLAttributes<HTMLButtonElement>;
49
- reorder(fromIndex: number, toIndex: 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<Payload>(props: FieldProps<Array<Payload>>): [
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
- props: FieldProps<Payload>;
118
+ config: FieldConfig<Payload>;
55
119
  }>,
56
- FieldListControl<Payload>
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: FieldProps<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 {};