Skip to content

Commit

Permalink
馃悶 fix #5234 should render error logic for valid state (#5235)
Browse files Browse the repository at this point in the history
* fix #5234 should render error logic for valid state

* Revert "fix #5234 should render error logic for valid state"

This reverts commit 082dd3d.

* isValid check for render

* code reducation

* fix cypress test

* more code reducation

* combine logic

* remove if

* improve logic

* Revert "improve logic"

This reverts commit a5f2c5f.

* Revert "Revert "improve logic""

This reverts commit b1438cc.

* fix tests
  • Loading branch information
bluebill1049 committed May 18, 2021
1 parent 3af1ad1 commit 5a44263
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cypress/integration/isValid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('isValid', () => {
cy.get('#isValid').contains('false');
cy.get('input[name="lastName"]').type('test');
cy.get('#isValid').contains('true');
cy.get('#renderCount').contains('4');
cy.get('#renderCount').contains('3');
cy.get('#toggle').click();
cy.get('#isValid').contains('false');
cy.get('#toggle').click();
Expand Down
28 changes: 9 additions & 19 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,34 +190,24 @@ export function useForm<
): boolean | void => {
const previousError = get(formStateRef.current.errors, name);

let shouldReRender =
shouldRender ||
!deepEqual(previousError, error, true) ||
(readFormStateRef.current.isValid &&
isUndefined(error) &&
get(fieldsWithValidationRef.current, name) &&
!get(validFieldsRef.current, name));

if (error) {
unset(validFieldsRef.current, name);
shouldReRender =
shouldReRender ||
!previousError ||
!deepEqual(previousError, error, true);
set(formStateRef.current.errors, name, error);
} else {
if (get(fieldsWithValidationRef.current, name) || resolverRef.current) {
(get(fieldsWithValidationRef.current, name) || resolverRef.current) &&
set(validFieldsRef.current, name, true);
shouldReRender = shouldReRender || previousError;
}

unset(formStateRef.current.errors, name);
}

if (
(shouldReRender && !isNullOrUndefined(shouldRender)) ||
!isEmptyObject(state) ||
isWatched
(shouldRender ||
isWatched ||
(error ? !deepEqual(previousError, error, true) : previousError) ||
!isEmptyObject(state) ||
(readFormStateRef.current.isValid &&
formStateRef.current.isValid !==
(resolverRef.current ? !!isValid : getIsValid()))) &&
!isNullOrUndefined(shouldRender)
) {
const updatedFormState = {
...state,
Expand Down

0 comments on commit 5a44263

Please sign in to comment.