react-hook-form 7.44.0-next.0 → 7.44.0-next.1

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.
@@ -59,8 +59,7 @@ const INPUT_VALIDATION_RULES = {
59
59
  pattern: 'pattern',
60
60
  required: 'required',
61
61
  validate: 'validate',
62
- };
63
- const SERVER_ERROR_TYPE = 'root.server';
62
+ };
64
63
 
65
64
  const HookFormContext = React__default.createContext(null);
66
65
  /**
@@ -523,55 +522,69 @@ const POST_REQUEST = 'post';
523
522
  function Form(props) {
524
523
  const methods = useFormContext();
525
524
  const [mounted, setMounted] = React.useState(false);
526
- const { control = methods.control, onSubmit, children, action, method = POST_REQUEST, headers, encType, onError, render, onSuccess, validateStatus, ...rest } = props;
525
+ const { control = methods.control, onSubmit, children, action, method = POST_REQUEST, headers, encType, onError, render, onSuccess, validateStatus, fetcher, ...rest } = props;
527
526
  const isPostRequest = method === POST_REQUEST;
528
- const submit = control.handleSubmit(async (data) => {
529
- onSubmit && onSubmit(data);
530
- if (action) {
531
- const formData = new FormData();
532
- let shouldStringifySubmissionData = false;
533
- if (headers) {
534
- shouldStringifySubmissionData =
535
- headers['Content-Type'].includes('json');
536
- }
537
- else {
538
- control._names.mount.forEach((name) => formData.append(name, get(data, name)));
539
- }
540
- try {
541
- const response = await fetch(action, {
542
- method,
543
- headers: {
544
- ...headers,
545
- ...(encType ? { 'Content-Type': encType } : {}),
546
- },
547
- ...(isPostRequest
548
- ? {
549
- body: shouldStringifySubmissionData
550
- ? JSON.stringify(data)
551
- : formData,
527
+ const submit = async (event) => {
528
+ let serverError = false;
529
+ await control.handleSubmit(async (values) => {
530
+ onSubmit && onSubmit(values);
531
+ if (action) {
532
+ const formData = new FormData();
533
+ let shouldStringifySubmissionData = false;
534
+ if (headers) {
535
+ shouldStringifySubmissionData =
536
+ headers['Content-Type'].includes('json');
537
+ }
538
+ else {
539
+ control._names.mount.forEach((name) => formData.append(name, get(values, name)));
540
+ }
541
+ if (!fetcher) {
542
+ try {
543
+ const response = await fetch(action, {
544
+ method,
545
+ headers: {
546
+ ...headers,
547
+ ...(encType ? { 'Content-Type': encType } : {}),
548
+ },
549
+ ...(isPostRequest
550
+ ? {
551
+ body: shouldStringifySubmissionData
552
+ ? JSON.stringify(values)
553
+ : formData,
554
+ }
555
+ : {}),
556
+ });
557
+ if (response &&
558
+ (validateStatus
559
+ ? !validateStatus(response.status)
560
+ : response.status < 200 || response.status >= 300)) {
561
+ serverError = true;
562
+ onError && onError({ response });
552
563
  }
553
- : {}),
554
- });
555
- if (validateStatus
556
- ? !validateStatus(response.status)
557
- : response.status < 200 || response.status >= 300) {
558
- control.setError(SERVER_ERROR_TYPE, {
559
- type: String(response.status),
560
- });
561
- onError && onError({ response });
564
+ else {
565
+ onSuccess && onSuccess({ response });
566
+ }
567
+ }
568
+ catch (error) {
569
+ serverError = true;
570
+ onError && onError({ error });
571
+ }
562
572
  }
563
573
  else {
564
- onSuccess && onSuccess({ response });
574
+ await fetcher(action, {
575
+ method,
576
+ values,
577
+ event,
578
+ });
565
579
  }
566
580
  }
567
- catch (error) {
568
- control.setError(SERVER_ERROR_TYPE, {
569
- type: 'error',
570
- });
571
- onError && onError({ error });
572
- }
573
- }
574
- });
581
+ })(event);
582
+ serverError &&
583
+ props.control &&
584
+ props.control._subjects.state.next({
585
+ isSubmitSuccessful: false,
586
+ });
587
+ };
575
588
  React.useEffect(() => {
576
589
  setMounted(true);
577
590
  }, []);
@@ -688,7 +701,7 @@ var isHTMLElement = (value) => {
688
701
  (owner && owner.defaultView ? owner.defaultView.HTMLElement : HTMLElement));
689
702
  };
690
703
 
691
- var isMessage = (value) => isString(value) || React__default.isValidElement(value);
704
+ var isMessage = (value) => isString(value);
692
705
 
693
706
  var isRadioInput = (element) => element.type === 'radio';
694
707
 
@@ -1950,9 +1963,6 @@ function createFormControl(props = {}, flushRootRender) {
1950
1963
  name,
1951
1964
  errors: _formState.errors,
1952
1965
  isValid: false,
1953
- ...(error.type === SERVER_ERROR_TYPE
1954
- ? { isSubmitSuccessful: false }
1955
- : {}),
1956
1966
  });
1957
1967
  options && options.shouldFocus && ref && ref.focus && ref.focus();
1958
1968
  };