@conform-to/react 0.5.1 → 0.6.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
@@ -2,6 +2,9 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js');
6
+ var dom = require('@conform-to/dom');
7
+
5
8
  /**
6
9
  * Style to make the input element visually hidden
7
10
  * Based on the `sr-only` class from tailwindcss
@@ -17,89 +20,80 @@ var hiddenStyle = {
17
20
  whiteSpace: 'nowrap',
18
21
  border: 0
19
22
  };
20
- function input(config) {
21
- var _config$initialError;
22
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
23
- var attributes = {
23
+ function getFormControlProps(config, options) {
24
+ var _config$error;
25
+ var props = {
24
26
  id: config.id,
25
- type: options.type,
26
27
  name: config.name,
27
28
  form: config.form,
28
- required: config.required,
29
+ required: config.required
30
+ };
31
+ if (config.id) {
32
+ props.id = config.id;
33
+ props['aria-describedby'] = config.errorId;
34
+ }
35
+ if (config.errorId && (_config$error = config.error) !== null && _config$error !== void 0 && _config$error.length) {
36
+ props['aria-invalid'] = true;
37
+ }
38
+ if (config.initialError && Object.entries(config.initialError).length > 0) {
39
+ props.autoFocus = true;
40
+ }
41
+ if (options !== null && options !== void 0 && options.hidden) {
42
+ props.style = hiddenStyle;
43
+ props.tabIndex = -1;
44
+ props['aria-hidden'] = true;
45
+ }
46
+ return props;
47
+ }
48
+ function input(config) {
49
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
50
+ var props = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, getFormControlProps(config, options)), {}, {
51
+ type: options.type,
29
52
  minLength: config.minLength,
30
53
  maxLength: config.maxLength,
31
54
  min: config.min,
32
55
  max: config.max,
33
56
  step: config.step,
34
57
  pattern: config.pattern,
35
- multiple: config.multiple,
36
- 'aria-invalid': Boolean((_config$initialError = config.initialError) === null || _config$initialError === void 0 ? void 0 : _config$initialError.length),
37
- 'aria-describedby': config.errorId
38
- };
39
- if (options !== null && options !== void 0 && options.hidden) {
40
- attributes.style = hiddenStyle;
41
- attributes.tabIndex = -1;
42
- attributes['aria-hidden'] = true;
43
- }
44
- if (config.initialError && config.initialError.length > 0) {
45
- attributes.autoFocus = true;
46
- }
58
+ multiple: config.multiple
59
+ });
47
60
  if (options.type === 'checkbox' || options.type === 'radio') {
48
61
  var _options$value;
49
- attributes.value = (_options$value = options.value) !== null && _options$value !== void 0 ? _options$value : 'on';
50
- attributes.defaultChecked = config.defaultValue === attributes.value;
62
+ props.value = (_options$value = options.value) !== null && _options$value !== void 0 ? _options$value : 'on';
63
+ props.defaultChecked = config.defaultValue === props.value;
51
64
  } else if (options.type !== 'file') {
52
- attributes.defaultValue = config.defaultValue;
65
+ props.defaultValue = config.defaultValue;
53
66
  }
54
- return attributes;
67
+ return props;
55
68
  }
56
69
  function select(config, options) {
57
- var _config$defaultValue, _config$initialError2;
58
- var attributes = {
59
- id: config.id,
60
- name: config.name,
61
- form: config.form,
62
- defaultValue: config.multiple ? Array.isArray(config.defaultValue) ? config.defaultValue : [] : "".concat((_config$defaultValue = config.defaultValue) !== null && _config$defaultValue !== void 0 ? _config$defaultValue : ''),
63
- required: config.required,
64
- multiple: config.multiple,
65
- 'aria-invalid': Boolean((_config$initialError2 = config.initialError) === null || _config$initialError2 === void 0 ? void 0 : _config$initialError2.length),
66
- 'aria-describedby': config.errorId
67
- };
68
- if (options !== null && options !== void 0 && options.hidden) {
69
- attributes.style = hiddenStyle;
70
- attributes.tabIndex = -1;
71
- attributes['aria-hidden'] = true;
72
- }
73
- if (config.initialError && config.initialError.length > 0) {
74
- attributes.autoFocus = true;
75
- }
76
- return attributes;
70
+ var props = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, getFormControlProps(config, options)), {}, {
71
+ defaultValue: config.defaultValue,
72
+ multiple: config.multiple
73
+ });
74
+ return props;
77
75
  }
78
76
  function textarea(config, options) {
79
- var _config$defaultValue2, _config$initialError3;
80
- var attributes = {
81
- id: config.id,
82
- name: config.name,
83
- form: config.form,
84
- defaultValue: "".concat((_config$defaultValue2 = config.defaultValue) !== null && _config$defaultValue2 !== void 0 ? _config$defaultValue2 : ''),
85
- required: config.required,
77
+ var props = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, getFormControlProps(config, options)), {}, {
78
+ defaultValue: config.defaultValue,
86
79
  minLength: config.minLength,
87
- maxLength: config.maxLength,
88
- autoFocus: Boolean(config.initialError),
89
- 'aria-invalid': Boolean((_config$initialError3 = config.initialError) === null || _config$initialError3 === void 0 ? void 0 : _config$initialError3.length),
90
- 'aria-describedby': config.errorId
91
- };
92
- if (options !== null && options !== void 0 && options.hidden) {
93
- attributes.style = hiddenStyle;
94
- attributes.tabIndex = -1;
95
- attributes['aria-hidden'] = true;
96
- }
97
- if (config.initialError && config.initialError.length > 0) {
98
- attributes.autoFocus = true;
99
- }
100
- return attributes;
80
+ maxLength: config.maxLength
81
+ });
82
+ return props;
101
83
  }
102
84
 
85
+ Object.defineProperty(exports, 'INTENT', {
86
+ enumerable: true,
87
+ get: function () { return dom.INTENT; }
88
+ });
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
+ });
103
97
  exports.input = input;
104
98
  exports.select = select;
105
99
  exports.textarea = textarea;
package/hooks.d.ts CHANGED
@@ -1,15 +1,11 @@
1
- import { type FieldConfig, type FieldElement, type FieldValue, type FieldsetConstraint, type Primitive, type Submission } from '@conform-to/dom';
2
- import { type InputHTMLAttributes, type FormEvent, type RefObject } from 'react';
3
- export interface FormConfig<Schema extends Record<string, any>> {
1
+ import { type FieldConfig, type FieldElement, type FieldValue, type FieldsetConstraint, type FormMethod, type FormEncType, type Submission } from '@conform-to/dom';
2
+ import { type FormEvent, type RefObject } from 'react';
3
+ export interface FormConfig<Schema extends Record<string, any>, ClientSubmission extends Submission | Submission<Schema> = Submission> {
4
4
  /**
5
5
  * If the form id is provided, Id for label,
6
6
  * input and error elements will be derived.
7
7
  */
8
8
  id?: string;
9
- /**
10
- * Validation mode. Default to `client-only`.
11
- */
12
- mode?: 'client-only' | 'server-validation';
13
9
  /**
14
10
  * Define when the error should be reported initially.
15
11
  * Support "onSubmit", "onChange", "onBlur".
@@ -22,9 +18,9 @@ export interface FormConfig<Schema extends Record<string, any>> {
22
18
  */
23
19
  defaultValue?: FieldValue<Schema>;
24
20
  /**
25
- * An object describing the state from the last submission
21
+ * An object describing the result of the last submission
26
22
  */
27
- state?: Submission<Schema>;
23
+ lastSubmission?: Submission;
28
24
  /**
29
25
  * An object describing the constraint of each field
30
26
  */
@@ -47,31 +43,37 @@ export interface FormConfig<Schema extends Record<string, any>> {
47
43
  onValidate?: ({ form, formData, }: {
48
44
  form: HTMLFormElement;
49
45
  formData: FormData;
50
- }) => Submission<Schema>;
46
+ }) => ClientSubmission;
51
47
  /**
52
48
  * The submit event handler of the form. It will be called
53
49
  * only when the form is considered valid.
54
50
  */
55
51
  onSubmit?: (event: FormEvent<HTMLFormElement>, context: {
56
52
  formData: FormData;
57
- submission: Submission<Schema>;
53
+ submission: ClientSubmission;
54
+ action: string;
55
+ encType: FormEncType;
56
+ method: FormMethod;
58
57
  }) => void;
59
58
  }
60
59
  /**
61
60
  * Properties to be applied to the form element
62
61
  */
63
62
  interface FormProps {
64
- ref: RefObject<HTMLFormElement>;
65
63
  id?: string;
64
+ ref: RefObject<HTMLFormElement>;
66
65
  onSubmit: (event: FormEvent<HTMLFormElement>) => void;
67
66
  noValidate: boolean;
67
+ 'aria-invalid'?: 'true';
68
+ 'aria-describedby'?: string;
68
69
  }
69
- interface Form<Schema extends Record<string, any>> {
70
+ interface Form {
70
71
  id?: string;
71
- ref: RefObject<HTMLFormElement>;
72
+ errorId?: string;
72
73
  error: string;
74
+ errors: string[];
75
+ ref: RefObject<HTMLFormElement>;
73
76
  props: FormProps;
74
- config: FieldsetConfig<Schema>;
75
77
  }
76
78
  /**
77
79
  * Returns properties required to hook into form events.
@@ -79,19 +81,12 @@ interface Form<Schema extends Record<string, any>> {
79
81
  *
80
82
  * @see https://conform.guide/api/react#useform
81
83
  */
82
- export declare function useForm<Schema extends Record<string, any>>(config?: FormConfig<Schema>): [Form<Schema>, Fieldset<Schema>];
84
+ export declare function useForm<Schema extends Record<string, any>, ClientSubmission extends Submission | Submission<Schema> = Submission>(config?: FormConfig<Schema, ClientSubmission>): [Form, Fieldset<Schema>];
83
85
  /**
84
- * All the information of the field, including state and config.
86
+ * A set of field configuration
85
87
  */
86
- export declare type Field<Schema> = {
87
- config: FieldConfig<Schema>;
88
- error?: string;
89
- };
90
- /**
91
- * A set of field information.
92
- */
93
- export declare type Fieldset<Schema extends Record<string, any>> = {
94
- [Key in keyof Schema]-?: Field<Schema[Key]>;
88
+ export type Fieldset<Schema extends Record<string, any>> = {
89
+ [Key in keyof Schema]-?: FieldConfig<Schema[Key]>;
95
90
  };
96
91
  export interface FieldsetConfig<Schema extends Record<string, any>> {
97
92
  /**
@@ -105,13 +100,13 @@ export interface FieldsetConfig<Schema extends Record<string, any>> {
105
100
  /**
106
101
  * An object describing the initial error of each field
107
102
  */
108
- initialError?: Array<[string, string]>;
103
+ initialError?: Record<string, string | string[]>;
109
104
  /**
110
105
  * An object describing the constraint of each field
111
106
  */
112
107
  constraint?: FieldsetConstraint<Schema>;
113
108
  /**
114
- * The id of the form, connecting each field to a form remotely.
109
+ * The id of the form, connecting each field to a form remotely
115
110
  */
116
111
  form?: string;
117
112
  }
@@ -130,36 +125,7 @@ export declare function useFieldset<Schema extends Record<string, any>>(ref: Ref
130
125
  */
131
126
  export declare function useFieldList<Payload = any>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config: FieldConfig<Array<Payload>>): Array<{
132
127
  key: string;
133
- error: string | undefined;
134
- config: FieldConfig<Payload>;
135
- }>;
136
- interface ShadowInputProps extends InputHTMLAttributes<HTMLInputElement> {
137
- ref: RefObject<HTMLInputElement>;
138
- }
139
- interface LegacyInputControl<Element extends {
140
- focus: () => void;
141
- }> {
142
- ref: RefObject<Element>;
143
- value: string;
144
- onChange: (eventOrValue: {
145
- target: {
146
- value: string;
147
- };
148
- } | string) => void;
149
- onBlur: () => void;
150
- onInvalid: (event: FormEvent<FieldElement>) => void;
151
- }
152
- /**
153
- * Returns the properties required to configure a shadow input for validation.
154
- * This is particular useful when integrating dropdown and datepicker whichs
155
- * introduces custom input mode.
156
- *
157
- * @deprecated Please use the `useInputEvent` hook instead
158
- * @see https://conform.guide/api/react#usecontrolledinput
159
- */
160
- export declare function useControlledInput<Element extends {
161
- focus: () => void;
162
- } = HTMLInputElement, Schema extends Primitive = Primitive>(config: FieldConfig<Schema>): [ShadowInputProps, LegacyInputControl<Element>];
128
+ } & FieldConfig<Payload>>;
163
129
  interface InputControl {
164
130
  change: (eventOrValue: {
165
131
  target: {