@conform-to/react 0.7.1 → 0.7.2

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.
Files changed (4) hide show
  1. package/hooks.d.ts +7 -2
  2. package/hooks.js +18 -17
  3. package/hooks.mjs +19 -18
  4. package/package.json +2 -2
package/hooks.d.ts CHANGED
@@ -21,6 +21,11 @@ export interface FieldConfig<Schema> extends FieldConstraint<Schema> {
21
21
  export type FieldValue<Schema> = Schema extends Primitive ? string : Schema extends File ? File : Schema extends Array<infer InnerType> ? Array<FieldValue<InnerType>> : unknown extends Schema ? any : Record<string, any> extends Schema ? {
22
22
  [Key in KeysOf<Schema>]?: FieldValue<ResolveType<Schema, Key>>;
23
23
  } : any;
24
+ type SubmissionResult = {
25
+ intent: Submission['intent'];
26
+ payload: Submission['payload'] | null;
27
+ error: Submission['error'];
28
+ };
24
29
  export interface FormConfig<Output extends Record<string, any>, Input extends Record<string, any> = Output> {
25
30
  /**
26
31
  * If the form id is provided, Id for label,
@@ -52,7 +57,7 @@ export interface FormConfig<Output extends Record<string, any>, Input extends Re
52
57
  /**
53
58
  * An object describing the result of the last submission
54
59
  */
55
- lastSubmission?: Submission | null;
60
+ lastSubmission?: SubmissionResult;
56
61
  /**
57
62
  * An object describing the constraint of each field
58
63
  */
@@ -203,6 +208,6 @@ export declare function validateConstraint(options: {
203
208
  defaultErrors: string[];
204
209
  }) => string[];
205
210
  }): Submission;
206
- export declare function reportSubmission(form: HTMLFormElement, submission: Submission): void;
211
+ export declare function reportSubmission(form: HTMLFormElement, submission: SubmissionResult): void;
207
212
  export declare function getScope(intent: ReturnType<typeof parseIntent>): string | null;
208
213
  export {};
package/hooks.js CHANGED
@@ -48,6 +48,14 @@ function useFormReporter(ref, lastSubmission) {
48
48
  if (!form || !lastSubmission) {
49
49
  return;
50
50
  }
51
+ if (!lastSubmission.payload) {
52
+ // If the default value is empty, we can safely reset the form.
53
+ // This ensure the behavior is consistent with and without JS.
54
+ form.reset();
55
+
56
+ // There is no need to report the submission anymore.
57
+ return;
58
+ }
51
59
  report(form, lastSubmission);
52
60
  }, [ref, lastSubmission, report]);
53
61
  react.useEffect(() => {
@@ -129,7 +137,7 @@ function useFormError(ref, config) {
129
137
  * @see https://conform.guide/api/react#useform
130
138
  */
131
139
  function useForm() {
132
- var _config$lastSubmissio2;
140
+ var _config$lastSubmissio2, _config$lastSubmissio3;
133
141
  var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
134
142
  var configRef = useConfigRef(config);
135
143
  var ref = useFormRef(config.ref);
@@ -152,21 +160,14 @@ function useForm() {
152
160
  }, [config.lastSubmission]);
153
161
  // This payload from lastSubmission is only useful before hydration
154
162
  // After hydration, any new payload on lastSubmission will be ignored
155
- var [lastSubmissionPayload, setLastSubmissionPayload] = react.useState((_config$lastSubmissio2 = config.lastSubmission) === null || _config$lastSubmissio2 === void 0 ? void 0 : _config$lastSubmissio2.payload);
163
+ var [defaultValueFromLastSubmission, setDefaultValueFromLastSubmission] = react.useState( // @ts-expect-error defaultValue is not in Submission type
164
+ (_config$lastSubmissio2 = (_config$lastSubmissio3 = config.lastSubmission) === null || _config$lastSubmissio3 === void 0 ? void 0 : _config$lastSubmissio3.payload) !== null && _config$lastSubmissio2 !== void 0 ? _config$lastSubmissio2 : null);
156
165
  var fieldset = useFieldset(ref, {
157
- defaultValue: lastSubmissionPayload !== null && lastSubmissionPayload !== void 0 ? lastSubmissionPayload : config.defaultValue,
166
+ defaultValue: defaultValueFromLastSubmission !== null && defaultValueFromLastSubmission !== void 0 ? defaultValueFromLastSubmission : config.defaultValue,
158
167
  initialError,
159
168
  constraint: config.constraint,
160
169
  form: config.id
161
170
  });
162
- var lastSubmissionRef = react.useRef(config.lastSubmission);
163
- useSafeLayoutEffect(() => {
164
- if (lastSubmissionRef.current !== config.lastSubmission && config.lastSubmission === null) {
165
- var _ref$current;
166
- (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.reset();
167
- }
168
- lastSubmissionRef.current = config.lastSubmission;
169
- }, [ref, config.lastSubmission]);
170
171
  react.useEffect(() => {
171
172
  // custom validate handler
172
173
  var createValidateHandler = type => event => {
@@ -206,7 +207,7 @@ function useForm() {
206
207
  _element.setCustomValidity('');
207
208
  }
208
209
  setErrors([]);
209
- setLastSubmissionPayload(undefined);
210
+ setDefaultValueFromLastSubmission(null);
210
211
  };
211
212
  var handleInput = createValidateHandler('onInput');
212
213
  var handleBlur = createValidateHandler('onBlur');
@@ -478,7 +479,7 @@ function useInputEvent(options) {
478
479
  onBlur: false
479
480
  });
480
481
  useSafeLayoutEffect(() => {
481
- var createEventListner = listener => {
482
+ var createEventListener = listener => {
482
483
  return event => {
483
484
  var _optionsRef$current, _optionsRef$current2, _optionsRef$current3;
484
485
  var element = typeof ((_optionsRef$current = optionsRef.current) === null || _optionsRef$current === void 0 ? void 0 : _optionsRef$current.ref) === 'function' ? (_optionsRef$current2 = optionsRef.current) === null || _optionsRef$current2 === void 0 ? void 0 : _optionsRef$current2.ref() : (_optionsRef$current3 = optionsRef.current) === null || _optionsRef$current3 === void 0 ? void 0 : _optionsRef$current3.ref.current;
@@ -491,10 +492,10 @@ function useInputEvent(options) {
491
492
  }
492
493
  };
493
494
  };
494
- var inputHandler = createEventListner('onInput');
495
- var focusHandler = createEventListner('onFocus');
496
- var blurHandler = createEventListner('onBlur');
497
- var resetHandler = createEventListner('onReset');
495
+ var inputHandler = createEventListener('onInput');
496
+ var focusHandler = createEventListener('onFocus');
497
+ var blurHandler = createEventListener('onBlur');
498
+ var resetHandler = createEventListener('onReset');
498
499
 
499
500
  // focus/blur event does not bubble
500
501
  document.addEventListener('input', inputHandler, true);
package/hooks.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.mjs';
2
2
  import { parseIntent, getFormData, parse, VALIDATION_UNDEFINED, VALIDATION_SKIPPED, getFormAction, getFormEncType, getFormMethod, getPaths, getName, isFieldElement, getErrors, getFormControls, getFormElement, updateList, getValidationMessage, focusFirstInvalidControl, isFocusableFormControl, requestIntent, validate } from '@conform-to/dom';
3
- import { useState, useMemo, useRef, useEffect, useLayoutEffect, useCallback } from 'react';
3
+ import { useState, useMemo, useEffect, useRef, useCallback, useLayoutEffect } from 'react';
4
4
 
5
5
  /**
6
6
  * Normalize error to an array of string.
@@ -44,6 +44,14 @@ function useFormReporter(ref, lastSubmission) {
44
44
  if (!form || !lastSubmission) {
45
45
  return;
46
46
  }
47
+ if (!lastSubmission.payload) {
48
+ // If the default value is empty, we can safely reset the form.
49
+ // This ensure the behavior is consistent with and without JS.
50
+ form.reset();
51
+
52
+ // There is no need to report the submission anymore.
53
+ return;
54
+ }
47
55
  report(form, lastSubmission);
48
56
  }, [ref, lastSubmission, report]);
49
57
  useEffect(() => {
@@ -125,7 +133,7 @@ function useFormError(ref, config) {
125
133
  * @see https://conform.guide/api/react#useform
126
134
  */
127
135
  function useForm() {
128
- var _config$lastSubmissio2;
136
+ var _config$lastSubmissio2, _config$lastSubmissio3;
129
137
  var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
130
138
  var configRef = useConfigRef(config);
131
139
  var ref = useFormRef(config.ref);
@@ -148,21 +156,14 @@ function useForm() {
148
156
  }, [config.lastSubmission]);
149
157
  // This payload from lastSubmission is only useful before hydration
150
158
  // After hydration, any new payload on lastSubmission will be ignored
151
- var [lastSubmissionPayload, setLastSubmissionPayload] = useState((_config$lastSubmissio2 = config.lastSubmission) === null || _config$lastSubmissio2 === void 0 ? void 0 : _config$lastSubmissio2.payload);
159
+ var [defaultValueFromLastSubmission, setDefaultValueFromLastSubmission] = useState( // @ts-expect-error defaultValue is not in Submission type
160
+ (_config$lastSubmissio2 = (_config$lastSubmissio3 = config.lastSubmission) === null || _config$lastSubmissio3 === void 0 ? void 0 : _config$lastSubmissio3.payload) !== null && _config$lastSubmissio2 !== void 0 ? _config$lastSubmissio2 : null);
152
161
  var fieldset = useFieldset(ref, {
153
- defaultValue: lastSubmissionPayload !== null && lastSubmissionPayload !== void 0 ? lastSubmissionPayload : config.defaultValue,
162
+ defaultValue: defaultValueFromLastSubmission !== null && defaultValueFromLastSubmission !== void 0 ? defaultValueFromLastSubmission : config.defaultValue,
154
163
  initialError,
155
164
  constraint: config.constraint,
156
165
  form: config.id
157
166
  });
158
- var lastSubmissionRef = useRef(config.lastSubmission);
159
- useSafeLayoutEffect(() => {
160
- if (lastSubmissionRef.current !== config.lastSubmission && config.lastSubmission === null) {
161
- var _ref$current;
162
- (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.reset();
163
- }
164
- lastSubmissionRef.current = config.lastSubmission;
165
- }, [ref, config.lastSubmission]);
166
167
  useEffect(() => {
167
168
  // custom validate handler
168
169
  var createValidateHandler = type => event => {
@@ -202,7 +203,7 @@ function useForm() {
202
203
  _element.setCustomValidity('');
203
204
  }
204
205
  setErrors([]);
205
- setLastSubmissionPayload(undefined);
206
+ setDefaultValueFromLastSubmission(null);
206
207
  };
207
208
  var handleInput = createValidateHandler('onInput');
208
209
  var handleBlur = createValidateHandler('onBlur');
@@ -474,7 +475,7 @@ function useInputEvent(options) {
474
475
  onBlur: false
475
476
  });
476
477
  useSafeLayoutEffect(() => {
477
- var createEventListner = listener => {
478
+ var createEventListener = listener => {
478
479
  return event => {
479
480
  var _optionsRef$current, _optionsRef$current2, _optionsRef$current3;
480
481
  var element = typeof ((_optionsRef$current = optionsRef.current) === null || _optionsRef$current === void 0 ? void 0 : _optionsRef$current.ref) === 'function' ? (_optionsRef$current2 = optionsRef.current) === null || _optionsRef$current2 === void 0 ? void 0 : _optionsRef$current2.ref() : (_optionsRef$current3 = optionsRef.current) === null || _optionsRef$current3 === void 0 ? void 0 : _optionsRef$current3.ref.current;
@@ -487,10 +488,10 @@ function useInputEvent(options) {
487
488
  }
488
489
  };
489
490
  };
490
- var inputHandler = createEventListner('onInput');
491
- var focusHandler = createEventListner('onFocus');
492
- var blurHandler = createEventListner('onBlur');
493
- var resetHandler = createEventListner('onReset');
491
+ var inputHandler = createEventListener('onInput');
492
+ var focusHandler = createEventListener('onFocus');
493
+ var blurHandler = createEventListener('onBlur');
494
+ var resetHandler = createEventListener('onReset');
494
495
 
495
496
  // focus/blur event does not bubble
496
497
  document.addEventListener('input', inputHandler, true);
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Conform view adapter for react",
4
4
  "homepage": "https://conform.guide",
5
5
  "license": "MIT",
6
- "version": "0.7.1",
6
+ "version": "0.7.2",
7
7
  "main": "index.js",
8
8
  "module": "index.mjs",
9
9
  "types": "index.d.ts",
@@ -30,7 +30,7 @@
30
30
  "url": "https://github.com/edmundhung/conform/issues"
31
31
  },
32
32
  "dependencies": {
33
- "@conform-to/dom": "0.7.1"
33
+ "@conform-to/dom": "0.7.2"
34
34
  },
35
35
  "peerDependencies": {
36
36
  "react": ">=16.8"