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

fix(form): fix form setFields rerender #3304

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const Form = forwardRefWithStatics(
formItemRef?.current.resetField();
});
form?.getInternalHooks?.(HOOK_MARK)?.notifyWatch?.([]);
form.store = {};
onReset?.({ e });
}

Expand Down
33 changes: 24 additions & 9 deletions src/form/FormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
CloseCircleFilledIcon as TdCloseCircleFilledIcon,
ErrorCircleFilledIcon as TdErrorCircleFilledIcon,
} from 'tdesign-icons-react';
import isEqual from 'lodash/isEqual';
import set from 'lodash/set';
import { calcFieldValue } from './utils';
import useConfig from '../hooks/useConfig';
import useGlobalIcon from '../hooks/useGlobalIcon';
Expand Down Expand Up @@ -110,12 +110,16 @@ const FormItem = forwardRef<FormItemInstance, FormItemProps>((originalProps, ref
const [verifyStatus, setVerifyStatus] = useState('validating');
const [resetValidating, setResetValidating] = useState(false);
const [needResetField, setNeedResetField] = useState(false);
const [formValue, setFormValue] = useState(() =>
getDefaultInitialData({
children,
initialData,
}),
);
const [formValue, setFormValue] = useState(() => {
const fieldName = [].concat(formListName, name).filter((item) => item !== undefined);
return (
get(form.store, fieldName) ??
getDefaultInitialData({
children,
initialData,
})
);
});

const formItemRef = useRef<FormItemInstance>(); // 当前 formItem 实例
const innerFormItemsRef = useRef([]);
Expand Down Expand Up @@ -166,9 +170,20 @@ const FormItem = forwardRef<FormItemInstance, FormItemProps>((originalProps, ref
isUpdatedRef.current = true;
shouldValidate.current = validate;
valueRef.current = newVal;
if (!isEqual(formValue, newVal)) {
setFormValue(newVal);

if (formListName) {
const fieldName = [].concat(formListName, name).filter((item) => item !== undefined);
if (get(form.store, fieldName)) {
const fieldValue = get(form.store, fieldName);
if (fieldValue !== newVal) {
setFormValue(newVal);
set(form.store, fieldName, newVal);
}
return;
}
set(form.store, fieldName, newVal);
}
setFormValue(newVal);
};

// 初始化 rules,最终以 formItem 上优先级最高
Expand Down
2 changes: 1 addition & 1 deletion src/form/hooks/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export interface InternalHooks {

export interface InternalFormInstance extends FormInstanceFunctions {
_init?: boolean;

store?: Store;
getInternalHooks?: (secret: string) => InternalHooks | null;
}
2 changes: 1 addition & 1 deletion src/form/hooks/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class FormStore {
getFieldValue: null,
getFieldsValue: null,
_init: true,

store: this.store,
getInternalHooks: this.getInternalHooks,
});

Expand Down
Loading