Skip to content

Commit 0c8b4e5

Browse files
committed
feat: add delayFrame option to validateFields
- Add delayFrame option to InternalValidateOptions interface - Conditionally call delayFrame when delayFrame option is true in Field.validateRules - Use delayFrame: true in triggerDependenciesUpdate to avoid useWatch rule timing issues - Adjust test files (jest.mock formatting cleanup)
1 parent f914c41 commit 0c8b4e5

6 files changed

Lines changed: 12 additions & 6 deletions

File tree

src/Field.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
392392
const namePath = this.getNamePath();
393393
const currentValue = this.getValue();
394394

395-
const { triggerName, validateOnly = false } = options || {};
395+
const { triggerName, validateOnly = false, delayFrame: showDelayFrame } = options || {};
396396

397397
// Force change to async to avoid rule OOD under renderProps field
398398
const rootPromise = Promise.resolve().then(async (): Promise<any[]> => {
@@ -404,7 +404,9 @@ class Field extends React.Component<InternalFieldProps, FieldState> implements F
404404

405405
// Should wait for the frame render,
406406
// since developer may `useWatch` value in the rules.
407-
await delayFrame();
407+
if (showDelayFrame) {
408+
await delayFrame();
409+
}
408410

409411
// Start validate
410412
let filteredRules = this.getRules();

src/hooks/useForm.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,10 @@ export class FormStore {
747747
private triggerDependenciesUpdate = (prevStore: Store, namePath: InternalNamePath) => {
748748
const childrenFields = this.getDependencyChildrenFields(namePath);
749749
if (childrenFields.length) {
750-
this.validateFields(childrenFields);
750+
this.validateFields(childrenFields, {
751+
// Delay to avoid `useWatch` dynamic adjust rules that deps not get latest one
752+
delayFrame: true,
753+
});
751754
}
752755

753756
this.notifyObservers(prevStore, childrenFields, {

src/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ export type ValidateFields<Values = any> = {
157157
export interface InternalValidateOptions extends ValidateOptions {
158158
triggerName?: string;
159159
validateMessages?: ValidateMessages;
160+
delayFrame?: boolean;
160161
}
161162

162163
export type InternalValidateFields<Values = any> = {

tests/index.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { changeValue, getInput, matchError } from './common';
77
import InfoField, { Input } from './common/InfoField';
88
import timeout, { waitFakeTime } from './common/timeout';
99

10+
1011
jest.mock('../src/utils/delayUtil');
11-
import type { FormRef, Meta } from '@/interface';
1212

1313
describe('Form.Basic', () => {
1414
describe('create form', () => {

tests/list.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import InfoField, { Input } from './common/InfoField';
1010
import { changeValue, getInput } from './common';
1111
import timeout from './common/timeout';
1212

13-
jest.mock('../src/utils/delayUtil');
1413

14+
jest.mock('../src/utils/delayUtil');
1515
describe('Form.List', () => {
1616
const form = React.createRef<FormInstance>();
1717

tests/validate-warning.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { changeValue, getInput, matchError } from './common';
55
import type { FormInstance, Rule } from '../src/interface';
66
import { render } from '@testing-library/react';
77

8-
jest.mock('../src/utils/delayUtil');
98

9+
jest.mock('../src/utils/delayUtil');
1010
describe('Form.WarningValidate', () => {
1111
it('required', async () => {
1212
const form = React.createRef<FormInstance>();

0 commit comments

Comments
 (0)