Skip to content

Commit 7693dbc

Browse files
authored
fix(FormList): nested items not showing dynamically added data (#3881)
* fix(FormList): nested items not showing dynamically added data * fix(FormList): ensure initial data is consistently
1 parent 66f1d2e commit 7693dbc

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

packages/components/form/FormList.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import React, { useEffect, useImperativeHandle, useRef, useState } from 'react';
22
import { flattenDeep, get, merge, set, unset } from 'lodash-es';
33
import log from '@tdesign/common-js/log/index';
44
import { FormListContext, useFormContext } from './FormContext';
5-
import type { FormItemInstance } from './FormItem';
65
import { HOOK_MARK } from './hooks/useForm';
7-
import type { FormListField, FormListFieldOperation, TdFormListProps } from './type';
86
import { calcFieldValue } from './utils';
97

8+
import type { FormItemInstance } from './FormItem';
9+
import type { FormListField, FormListFieldOperation, TdFormListProps } from './type';
10+
1011
let key = 0;
1112

1213
const FormList: React.FC<TdFormListProps> = (props) => {
@@ -19,7 +20,9 @@ const FormList: React.FC<TdFormListProps> = (props) => {
1920
} = useFormContext();
2021
const { name, rules, children } = props;
2122

22-
const initialData = props.initialData || get(initialDataFromForm, name) || [];
23+
const originalInitialData = props.initialData || get(initialDataFromForm, name) || [];
24+
const storeValue = get(form?.store, name);
25+
const initialData = storeValue ?? originalInitialData;
2326

2427
const [formListValue, setFormListValue] = useState(initialData);
2528
const [fields, setFields] = useState<Array<FormListField>>(() =>
@@ -64,8 +67,7 @@ const FormList: React.FC<TdFormListProps> = (props) => {
6467
nextFormListValue[index] = defaultValue;
6568
setFormListValue(nextFormListValue);
6669
}
67-
68-
set(form?.store, flattenDeep([name, index]), nextFormListValue);
70+
set(form?.store, flattenDeep([name]), nextFormListValue);
6971

7072
const fieldValue = calcFieldValue(name, nextFormListValue);
7173
requestAnimationFrame(() => {
@@ -149,7 +151,7 @@ const FormList: React.FC<TdFormListProps> = (props) => {
149151
Promise.resolve().then(() => {
150152
if (!fieldsTaskQueueRef.current.length) return;
151153

152-
// fix multiple formlist stuck
154+
// fix multiple FormList stuck
153155
const currentQueue = fieldsTaskQueueRef.current.pop();
154156
const { fieldData, callback, originData } = currentQueue;
155157

@@ -235,22 +237,22 @@ const FormList: React.FC<TdFormListProps> = (props) => {
235237
const resetType = type || resetTypeFromContext;
236238

237239
if (resetType === 'initial') {
238-
setFormListValue(initialData);
240+
setFormListValue(originalInitialData);
239241

240-
const newFields = initialData.map((data, index) => ({
242+
const newFields = originalInitialData.map((data, index) => ({
241243
data: { ...data },
242244
key: (key += 1),
243245
name: index,
244246
isListField: true,
245247
}));
246248
setFields(newFields);
247-
set(form?.store, flattenDeep([name]), initialData);
249+
set(form?.store, flattenDeep([name]), originalInitialData);
248250

249251
requestAnimationFrame(() => {
250252
[...formListMapRef.current.values()].forEach((formItemRef) => {
251253
if (!formItemRef.current) return;
252254
const { name: itemName } = formItemRef.current;
253-
const itemValue = get(initialData, itemName);
255+
const itemValue = get(originalInitialData, itemName);
254256
if (itemValue !== undefined) {
255257
formItemRef.current.setField({ value: itemValue, status: 'not' });
256258
}

0 commit comments

Comments
 (0)