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(FormList): 修复FormList组件使用form setFieldsValue、reset异常 #2406

Merged
merged 2 commits into from
Aug 24, 2023
Merged
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
40 changes: 37 additions & 3 deletions src/form/FormList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ import log from '../_common/js/log';
let key = 0;

const FormList: React.FC<TdFormListProps> = (props) => {
const { formMapRef, form, onFormItemValueChange, initialData: initialDataFromForm } = useFormContext();
const {
formMapRef,
form,
onFormItemValueChange,
initialData: initialDataFromForm,
resetType: resetTypeFromContext,
} = useFormContext();
const { name, rules, children } = props;

const initialData = props.initialData || get(initialDataFromForm, name) || [];

const [formListValue, setFormListValue] = useState(initialData);
const [fields, setFields] = useState<Array<FormListField>>(
const [fields, setFields] = useState<Array<FormListField>>(() =>
initialData.map((data, index) => ({
data: { ...data },
key: (key += 1),
Expand All @@ -33,6 +39,15 @@ const FormList: React.FC<TdFormListProps> = (props) => {
.filter((item) => item !== undefined)
.join('_'); // 转化 name

const isMounted = useRef(false);
honkinglin marked this conversation as resolved.
Show resolved Hide resolved

useEffect(
() => () => {
isMounted.current = false;
},
[],
);

const operation: FormListFieldOperation = {
add(defaultValue?: any, insertIndex?: number) {
const cloneFields = [...fields];
Expand Down Expand Up @@ -119,6 +134,10 @@ const FormList: React.FC<TdFormListProps> = (props) => {
}, [formListValue]);

useEffect(() => {
if (!isMounted.current) {
isMounted.current = true;
return;
}
// fields 变化通知 watch 事件
form?.getInternalHooks?.(HOOK_MARK)?.notifyWatch?.(name);

Expand Down Expand Up @@ -207,11 +226,26 @@ const FormList: React.FC<TdFormListProps> = (props) => {
originData,
);
},
resetField: () => {
resetField: (type: string) => {
const resetType = type || resetTypeFromContext;
[...formListMapRef.current.values()].forEach((formItemRef) => {
formItemRef?.current?.resetField?.();
});
setFormListValue([]);
fieldsTaskQueueRef.current = [];
key = 0;
if (resetType === 'initial') {
setFields(
initialData.map((data, index) => ({
data: { ...data },
key: (key += 1),
name: index,
isListField: true,
})),
);
} else {
setFields([]);
}
},
setValidateMessage: (fieldData) => {
[...formListMapRef.current.values()].forEach((formItemRef) => {
Expand Down
12 changes: 7 additions & 5 deletions src/form/__tests__/form-list.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,12 @@ describe('Form List 组件测试', () => {
fireEvent.click(resetBtn);
fireEvent.click(submitBtn);
await mockTimeout(() => true);
expect(queryByText('省份必填')).toBeTruthy();
expect(queryByText('地区必填')).toBeTruthy();
expect(queryByText('guangdong')).not.toBeTruthy();
expect(queryByText('shenzhen')).not.toBeTruthy();

fireEvent.click(addBtn);
expect(queryByDisplayValue('guangdong')).toBeTruthy();
expect(queryByDisplayValue('shenzhen')).toBeTruthy();
const removeBtn = container.querySelector('.test-remove');
fireEvent.click(removeBtn);
expect(queryByDisplayValue('guangdong')).not.toBeTruthy();
Expand All @@ -121,11 +124,10 @@ describe('Form List 组件测试', () => {
fireEvent.click(queryByText('validateOnly'));
await mockTimeout(() => true);
expect(queryByText('省份必填')).not.toBeTruthy();
fireEvent.click(queryByText('reset'));
fireEvent.click(queryByText('validate'));
await mockTimeout(() => true);
expect(queryByText('省份必填')).toBeTruthy();
expect(queryByText('地区必填')).toBeTruthy();
fireEvent.click(queryByText('clearValidate'));
expect(queryByText('省份必填')).not.toBeTruthy();
expect(queryByText('地区必填')).not.toBeTruthy();
});
});
Loading