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

[Form] 动态设置级联表单无法数据预填 #3076

Open
DevQiao opened this issue Sep 2, 2024 · 5 comments
Open

[Form] 动态设置级联表单无法数据预填 #3076

DevQiao opened this issue Sep 2, 2024 · 5 comments
Labels
🐞 bug Something isn't working

Comments

@DevQiao
Copy link

DevQiao commented Sep 2, 2024

tdesign-react 版本

1.8

重现链接

https://stackblitz.com/edit/react-mdcobx-3txpta?file=src%2Fdemo.tsx

重现步骤

No response

期望结果

看下 Antd 的效果 https://stackblitz.com/edit/react-xqhb4t?file=demo.tsx

实际结果

动态级联渲染的 FormItem 无法预填数据,每多一层级,就需要重新 set 一次

框架版本

No response

浏览器版本

No response

系统版本

No response

Node版本

No response

补充说明

Pretty Critical!

Copy link
Contributor

github-actions bot commented Sep 2, 2024

👋 @DevQiao,感谢给 TDesign 提出了 issue。
请根据 issue 模版确保背景信息的完善,我们将调查并尽快回复你。

@HaixingOoO HaixingOoO added the 🐞 bug Something isn't working label Sep 18, 2024
@l123wx
Copy link

l123wx commented Oct 14, 2024

导致这个问题的原因是,tdesign 不是跟 antd 一样在 Form 组件中维护一个统一的 formData 来储存整个表单的值,而是在 FormItem 中使用 state 存储各自的值。

使用 setFieldsValue 设置表单值时,会直接调用 FormItem 的 setValue 方法。所以当组件还没渲染时,没办法调用组件的 setValue 方法

function setFieldsValue(fields = {}) {
	travelMapFromObject(fields, formMapRef, (formItemRef, fieldValue) => {
		formItemRef?.current?.setValue?.(fieldValue, fields);
	});
}

@l123wx
Copy link

l123wx commented Oct 14, 2024

如果要改成在 Form 中使用统一的 formData 来管理表单的值,这个改动会有点大。

我的想法是:

使用 useRef 创建一个 游离(临时)字段值对象记录这种数据,当调用 setFieldsValue 时,如果目标字段组件还不存在,就记录到游离对象中。如果组件不存在,并且对应字段的游离值已经存在,更新游离值。

在组件渲染时,判断是否存在该字段的游离值,如果存在就作为 initialValue。当组件使用过这个游离值后,将这个值从游离对象中删除。

当 Form 重置或卸载时,将游离对象清空

不知道这个方法有没有什么问题 @HaixingOoO

@HaixingOoO
Copy link
Collaborator

如果要改成在 Form 中使用统一的 formData 来管理表单的值,这个改动会有点大。

我的想法是:

使用 useRef 创建一个 游离(临时)字段值对象记录这种数据,当调用 setFieldsValue 时,如果目标字段组件还不存在,就记录到游离对象中。如果组件不存在,并且对应字段的游离值已经存在,更新游离值。

在组件渲染时,判断是否存在该字段的游离值,如果存在就作为 initialValue。当组件使用过这个游离值后,将这个值从游离对象中删除。

当 Form 重置或卸载时,将游离对象清空

不知道这个方法有没有什么问题 @HaixingOoO

需要測試一下,欢迎PR!!!

@l123wx
Copy link

l123wx commented Oct 14, 2024

好,那我往这个方向试一下~

l123wx added a commit to l123wx/tdesign-react that referenced this issue Oct 15, 2024
l123wx added a commit to l123wx/tdesign-react that referenced this issue Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants