Skip to content

Commit f2c50c7

Browse files
committed
fix(manager): do silent update only once
1 parent 21a98d0 commit f2c50c7

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

packages/form-state-manager/src/files/form-state-manager.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ const FormStateManager: React.ComponentType<FormStateManagerProps> = ({
5757
...fieldArrayApi
5858
};
5959

60+
const finalRender = render ? render : children;
61+
6062
return (
6163
<FormManagerContext.Provider value={managerState}>
62-
<FormManagerContext.Consumer>{() => (render ? render(managerState) : children(managerState))}</FormManagerContext.Consumer>
64+
<FormManagerContext.Consumer>{() => finalRender && finalRender(managerState)}</FormManagerContext.Consumer>
6365
</FormManagerContext.Provider>
6466
);
6567
};

packages/form-state-manager/src/types/form-state-manager.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { Debug } from './manager-api';
55

66
export interface FormStateManagerProps {
77
onSubmit: (values: AnyObject) => void;
8-
children: (props: AnyObject) => React.ReactNode;
9-
render: (props: AnyObject) => React.ReactNode;
8+
children?: (props: AnyObject) => React.ReactNode;
9+
render?: (props: AnyObject) => React.ReactNode;
1010
clearOnUnmount?: boolean;
1111
subscription?: Subscription;
1212
clearedValue?: any;

packages/form-state-manager/src/utils/manager-api.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ const createManagerApi: CreateManagerApi = ({
251251
let runFormValidation = false;
252252
let revalidatedFields: Array<string> = [];
253253
let registeringField: string | number | undefined;
254-
let isSilent = false;
254+
let isSilent = 0;
255255
let silentRender: string[] = [];
256256
let runningValidators = 0;
257257
let flatSubmitErrors: AnyObject = {};
@@ -528,7 +528,7 @@ const createManagerApi: CreateManagerApi = ({
528528
return (subscribeTo: Array<string> = []) => {
529529
const changedAttributes = [...findDifference(snapshot, state), ...subscribeTo];
530530

531-
if (isSilent) {
531+
if (isSilent > 0) {
532532
changedAttributes.forEach((attr) => addIfUnique(silentRender, attr));
533533
} else if (changedAttributes.length > 0) {
534534
rerender(changedAttributes);
@@ -745,7 +745,7 @@ const createManagerApi: CreateManagerApi = ({
745745
}
746746

747747
function registerField(field: FieldConfig): void {
748-
isSilent = !!field.silent;
748+
isSilent = field.silent ? isSilent + 1 : isSilent;
749749
registeringField = field.internalId || field.name;
750750
batch(() => {
751751
const render = prepareRerender();
@@ -799,18 +799,18 @@ const createManagerApi: CreateManagerApi = ({
799799

800800
render();
801801
});
802-
isSilent = false;
802+
isSilent = field.silent ? Math.min(isSilent - 1, 0) : isSilent;
803803
registeringField = undefined;
804804
}
805805

806806
function afterSilentRegistration(field: Omit<FieldConfig, 'render'>) {
807-
revalidateFields([field.name, ...(state.fieldListeners[field.name]?.validateFields || state.registeredFields.filter((n) => n !== field.name))]);
807+
if (isSilent === 0 && silentRender.length > 0) {
808+
revalidateFields([field.name, ...(state.fieldListeners[field.name]?.validateFields || state.registeredFields.filter((n) => n !== field.name))]);
808809

809-
if (config.validate) {
810-
validateForm(config.validate);
811-
}
810+
if (config.validate) {
811+
validateForm(config.validate);
812+
}
812813

813-
if (silentRender.length > 0) {
814814
registeringField = field.internalId || field.name;
815815
rerender(silentRender);
816816
silentRender = [];

0 commit comments

Comments
 (0)