Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setFields/registerField支持triggerDependenciesUpdate #516

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,9 @@ export class FormStore {
});
});

// Dependencies update
this.triggerDependenciesUpdate(prevStore, namePathList);

this.notifyWatch(namePathList);
};

Expand Down Expand Up @@ -623,6 +626,9 @@ export class FormStore {
type: 'valueUpdate',
source: 'internal',
});

// Dependencies update
this.triggerDependenciesUpdate(prevStore, [namePath]);
}

// un-register field callback
Expand All @@ -649,7 +655,7 @@ export class FormStore {
this.notifyObservers(prevStore, [namePath], { type: 'remove' });

// Dependencies update
this.triggerDependenciesUpdate(prevStore, namePath);
this.triggerDependenciesUpdate(prevStore, [namePath]);
}
}

Expand Down Expand Up @@ -695,16 +701,17 @@ export class FormStore {
/**
* Notify dependencies children with parent update
* We need delay to trigger validate in case Field is under render props
* Pass namePath array to calculate childrenFields together to avoid repeatedly trigger notifyObservers
*/
private triggerDependenciesUpdate = (prevStore: Store, namePath: InternalNamePath) => {
private triggerDependenciesUpdate = (prevStore: Store, namePath: InternalNamePath[]) => {
const childrenFields = this.getDependencyChildrenFields(namePath);
if (childrenFields.length) {
this.validateFields(childrenFields);
}

this.notifyObservers(prevStore, childrenFields, {
type: 'dependenciesUpdate',
relatedFields: [namePath, ...childrenFields],
relatedFields: [...namePath, ...childrenFields],
});

return childrenFields;
Expand All @@ -722,7 +729,7 @@ export class FormStore {
this.notifyWatch([namePath]);

// Dependencies update
const childrenFields = this.triggerDependenciesUpdate(prevStore, namePath);
const childrenFields = this.triggerDependenciesUpdate(prevStore, [namePath]);

// trigger callback function
const { onValuesChange } = this.callbacks;
Expand Down Expand Up @@ -762,7 +769,7 @@ export class FormStore {
]);
};

private getDependencyChildrenFields = (rootNamePath: InternalNamePath): InternalNamePath[] => {
private getDependencyChildrenFields = (rootNamePath: InternalNamePath[]): InternalNamePath[] => {
const children: Set<FieldEntity> = new Set();
const childrenFields: InternalNamePath[] = [];

Expand Down Expand Up @@ -798,7 +805,7 @@ export class FormStore {
});
};

fillChildren(rootNamePath);
rootNamePath.forEach(namePath => fillChildren(namePath));

return childrenFields;
};
Expand Down