Skip to content

Commit b4b0ad5

Browse files
committed
fix: return validation result on form validation
1 parent 56533e2 commit b4b0ad5

File tree

3 files changed

+1787
-1687
lines changed

3 files changed

+1787
-1687
lines changed

src/__tests__/forms.spec.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,23 @@ describe('Unit Test cases for use-form hook', () => {
359359
expect(result.current[0].name.value).toBe(text);
360360
expect(result.current[0].name.isValid).toBe(false);
361361
expect(result.current[1].isValid).toBe(false);
362+
let returnedValue;
362363
await act(async () => {
363-
await result.current[1].validate();
364+
returnedValue = await result.current[1].validate();
364365
});
365366
expect(result.current[0].name.isValid).toBe(true);
366367
expect(result.current[1].isValid).toBe(true);
368+
expect(returnedValue).toMatchInlineSnapshot(`
369+
Array [
370+
true,
371+
Object {
372+
"name": Object {
373+
"errorMsg": "",
374+
"isValid": true,
375+
},
376+
},
377+
]
378+
`);
367379
});
368380
});
369381
describe('Cases of use-form on demand config change', () => {

src/use-form.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,12 @@ export default function useNewForm(fieldConfig) {
9797
);
9898

9999
const validateForm = useCallback(async () => {
100-
const [, errors] = await getFormErrors(
100+
const [isValid, errors] = await getFormErrors(
101101
fieldConfigRef.current,
102102
formValuesAsRef.current,
103103
);
104104
setFormErrors(errors);
105+
return [isValid, errors];
105106
}, []);
106107

107108
const updateFieldConfig = useCallback(

0 commit comments

Comments
 (0)