ant-design
ant-design copied to clipboard
Support PrefixNameContext in Form
What problem does this feature solve?
业务中使用Form组件开发大型表单。比如用来创建K8s资源对象(Application等)有大量字段和多层嵌套层级。比如:
application.container.lifecycle.postStart.exec.command
application.container.lifecycle.preStop.exec.command
application.container.metadata.annotations.lbcinstance
application.container.metadata.annotations.bound
...
手动配置每个Form.Item的name字段会有大量重复的prefix,比如:
['application', 'xxx1']
, ['application', 'xxx2']
, ['application', 'xxx3']
...
(同一级最多可能10-20个字段)
What does the proposed API look like?
<PrefixContext.Provider value={["application", "container"]}>
<PrefixContext.Provider value={["lifecycle"]}>
<Form.Item name="postStart">
{/* 实际name:["application", "container", "lifecycle", "postStart" ] */}
</Form.Item>
<Form.Item name="preStop">
{/* 实际name:["application", "container", "lifecycle", "preStop" ] */}
</Form.Item>
</PrefixContext.Provider>
<PrefixContext.Provider value={["metadata", "annotations"]}>
<Form.Item name="lbcinstance">
{/* 实际name:["application", "container", "metadata", "annotations", "lbcinstance" ] */}
</Form.Item>
<Form.Item name="bound">
{/* 实际name:["application", "container", "metadata", "annotations", "bound" ] */}
</Form.Item>
</PrefixContext.Provider>
</PrefixContext.Provider>
目前rc-field-form
中已经通过FieldContext
实现。可以单独实现一个PrefixContext
并且暴露出来
PR:https://github.com/react-component/field-form/pull/541
感觉有点裸,需要讨论一下。
mark,我在想这个功能是不是业务同学实现 Context
比较好 ,而非组件库去做
API 添加会比较慎重,和几个 Collaborator 聊了一下。感觉可以先做个 Demo 出来,让开发者进行封装。
业务自行实现的问题在于List
组件已经添加了prefixName
,在List
中的Item
会出现问题
比如:https://codesandbox.io/s/prefixnamecontext-demo-m7l7pz
业务自行实现的问题在于
List
组件已经添加了prefixName
,在List
中的Item
会出现问题比如:https://codesandbox.io/s/prefixnamecontext-demo-m7l7pz
@zombieJ 麻烦确认一下这个demo。List
中的Item
会同时接收到自定义的prefix和List
内部的prefix
嗯,看起来 List 需要其他方式封装。
https://codesandbox.io/s/prefixnamecontext-demo-forked-qmthgl?file=/FormItem.tsx
业务上在List
中可能还会使用PrefixProvider
目前业务上的实现是:https://codesandbox.io/s/prefixnamecontext-demo-forked-kw8rub?file=/demo.tsx
虽然能实现,但是觉得为了避免和List
的重复,实现起来和理解起来会很绕
感觉如果能够原生暴露出来会很方便
这样如何? https://codesandbox.io/s/prefixnamecontext-demo-forked-ojcoy3?file=/FormList.tsx
这样很清晰👍
不过业务上还有一个需求🤣。因为字段嵌套层级太深,如果字段之间有依赖关系时目前是使用相对路径的。
比如:application.containers[0].metadata.annotations.lbcinstance
字段依赖application.containers[0].metadata.annotations.bound
字段。
目前是通过useContext(PrefixContext).fullpath
获取到完整前缀,比如["application", "containers", 0, "metadata", "annotations"]
,然后dependencies=[[...prefix, "bound"]]
。按照大佬上边的demo,containers
是一个List
,获取到的prefix只是[0, "metadata", "annotations"]
这一段?
这个就是你上面的例子,Context 搞两条路径,一个存要传导的,一个存全量的。只要分割清楚就好了哈~