Skip to content

Commit 544ad94

Browse files
authored
✌🏻 close #6124 move isValid state into seperate function to unblock validation
* close #6124 when isValid block buildin valdiation * close #6124 move vald state into seperate function to unblock validation
1 parent 1ac324d commit 544ad94

File tree

6 files changed

+25
-15
lines changed

6 files changed

+25
-15
lines changed

cypress/integration/conditionalField.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,6 @@ describe('ConditionalField', () => {
160160
}),
161161
);
162162

163-
cy.get('#renderCount').contains('30');
163+
cy.get('#renderCount').contains('38');
164164
});
165165
});

cypress/integration/formState.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe('form state', () => {
9090
isValid: true,
9191
}),
9292
);
93-
cy.get('#renderCount').contains('14');
93+
cy.get('#renderCount').contains('15');
9494
});
9595

9696
it('should return correct form state with onChange mode', () => {
@@ -184,7 +184,7 @@ describe('form state', () => {
184184
isValid: true,
185185
}),
186186
);
187-
cy.get('#renderCount').contains('14');
187+
cy.get('#renderCount').contains('17');
188188
});
189189

190190
it('should return correct form state with onBlur mode', () => {
@@ -278,7 +278,7 @@ describe('form state', () => {
278278
isValid: true,
279279
}),
280280
);
281-
cy.get('#renderCount').contains('15');
281+
cy.get('#renderCount').contains('17');
282282
});
283283

284284
it('should reset dirty value when inputs reset back to default with onSubmit mode', () => {
@@ -554,6 +554,6 @@ describe('form state', () => {
554554
}),
555555
);
556556

557-
cy.get('#renderCount').contains('13');
557+
cy.get('#renderCount').contains('16');
558558
});
559559
});

cypress/integration/formStateWithNestedFields.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ describe('form state with nested fields', () => {
9292
isValid: true,
9393
}),
9494
);
95-
cy.get('#renderCount').contains('14');
95+
cy.get('#renderCount').contains('15');
9696
});
9797

9898
it('should return correct form state with onChange mode', () => {
@@ -186,7 +186,7 @@ describe('form state with nested fields', () => {
186186
isValid: true,
187187
}),
188188
);
189-
cy.get('#renderCount').contains('14');
189+
cy.get('#renderCount').contains('17');
190190
});
191191

192192
it('should return correct form state with onBlur mode', () => {
@@ -280,7 +280,7 @@ describe('form state with nested fields', () => {
280280
isValid: true,
281281
}),
282282
);
283-
cy.get('#renderCount').contains('15');
283+
cy.get('#renderCount').contains('17');
284284
});
285285

286286
it('should reset dirty value when inputs reset back to default with onSubmit mode', () => {
@@ -417,6 +417,6 @@ describe('form state with nested fields', () => {
417417
}),
418418
);
419419

420-
cy.get('#renderCount').contains('13');
420+
cy.get('#renderCount').contains('16');
421421
});
422422
});

cypress/integration/formStateWithSchema.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ describe('form state with schema validation', () => {
185185
isValid: true,
186186
}),
187187
);
188-
cy.get('#renderCount').contains('14');
188+
cy.get('#renderCount').contains('17');
189189
});
190190

191191
it('should return correct form state with onBlur mode', () => {
@@ -279,7 +279,7 @@ describe('form state with schema validation', () => {
279279
isValid: true,
280280
}),
281281
);
282-
cy.get('#renderCount').contains('15');
282+
cy.get('#renderCount').contains('17');
283283
});
284284

285285
it('should reset dirty value when inputs reset back to default with onSubmit mode', () => {
@@ -553,6 +553,6 @@ describe('form state with schema validation', () => {
553553
isValid: false,
554554
}),
555555
);
556-
cy.get('#renderCount').contains('13');
556+
cy.get('#renderCount').contains('16');
557557
});
558558
});

cypress/integration/useFieldArrayUnregister.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,6 @@ describe('useFieldArrayUnregister', () => {
176176
}),
177177
);
178178

179-
cy.get('#renderCount').contains('33');
179+
cy.get('#renderCount').contains('36');
180180
});
181181
});

src/useForm.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,16 @@ export function useForm<
162162
});
163163
};
164164

165+
const shouldRenderBaseOnValid = async () => {
166+
const isValid = await validateForm(fieldsRef.current, true);
167+
if (isValid !== formStateRef.current.isValid) {
168+
formStateRef.current.isValid = isValid;
169+
subjectsRef.current.state.next({
170+
isValid,
171+
});
172+
}
173+
};
174+
165175
const shouldRenderBaseOnError = React.useCallback(
166176
async (
167177
shouldSkipRender: boolean,
@@ -179,7 +189,7 @@ export function useForm<
179189
const isValid = readFormStateRef.current.isValid
180190
? resolver
181191
? isValidFromResolver
182-
: await validateForm(fieldsRef.current, true)
192+
: shouldRenderBaseOnValid()
183193
: false;
184194

185195
if (delayError && error) {
@@ -202,7 +212,7 @@ export function useForm<
202212
) {
203213
const updatedFormState = {
204214
...fieldState,
205-
isValid: !!isValid,
215+
...(resolver ? { isValid: !!isValid } : {}),
206216
errors: formStateRef.current.errors,
207217
name,
208218
};

0 commit comments

Comments
 (0)