Skip to content

Commit

Permalink
👩‍🌾 close react-hook-form#12168 optimise re-render with validating fi…
Browse files Browse the repository at this point in the history
…elds subscription (react-hook-form#12192)

* 👩‍🌾 close react-hook-form#12168 optimise re-render with validating fields subscription

* revert back the check

* optimise the logic
  • Loading branch information
bluebill1049 authored and oskarski committed Oct 1, 2024
1 parent 9bf2436 commit 364174d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/logic/createFormControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import getFieldValueAs from './getFieldValueAs';
import getResolverOptions from './getResolverOptions';
import getRuleValue from './getRuleValue';
import getValidationModes from './getValidationModes';
import hasPromiseValidation from './hasPromiseValidation';
import hasValidation from './hasValidation';
import isNameInFieldArray from './isNameInFieldArray';
import isWatched from './isWatched';
Expand Down Expand Up @@ -452,15 +453,23 @@ export function createFormControl<

if (_f) {
const isFieldArrayRoot = _names.array.has(_f.name);
_updateIsValidating([name], true);
const isPromiseFunction = field._f && hasPromiseValidation(field._f);

if (isPromiseFunction && _proxyFormState.validatingFields) {
_updateIsValidating([name], true);
}

const fieldError = await validateField(
field,
_formValues,
shouldDisplayAllAssociatedErrors,
_options.shouldUseNativeValidation && !shouldOnlyCheckValid,
isFieldArrayRoot,
);
_updateIsValidating([name]);

if (isPromiseFunction && _proxyFormState.validatingFields) {
_updateIsValidating([name]);
}

if (fieldError[_f.name]) {
context.valid = false;
Expand Down
17 changes: 17 additions & 0 deletions src/logic/hasPromiseValidation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Field, Validate } from '../types';
import isFunction from '../utils/isFunction';
import isObject from '../utils/isObject';

const ASYNC_FUNCTION = 'AsyncFunction';

export default (fieldReference: Field['_f']) =>
(!fieldReference || !fieldReference.validate) &&
!!(
(isFunction(fieldReference.validate) &&
fieldReference.validate.constructor.name === ASYNC_FUNCTION) ||
(isObject(fieldReference.validate) &&
Object.values(fieldReference.validate).find(
(validateFunction: Validate<unknown, unknown>) =>
validateFunction.constructor.name === ASYNC_FUNCTION,
))
);

0 comments on commit 364174d

Please sign in to comment.