Skip to content

Commit

Permalink
fix(form): fix form setFields rerender
Browse files Browse the repository at this point in the history
  • Loading branch information
HaixingOoO committed Dec 30, 2024
1 parent 1a4d630 commit ab0b19b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
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

0 comments on commit ab0b19b

Please sign in to comment.