antd Form.Item 只接受一个子项
我创建了一个小小提琴来说明这个问题: https:// stackblitz.com/edit/react-avejvc-mmhqda?file=index.js
此表单有效:
<Form initialValues={{ surname: 'Mouse'}}>
<Form.Item name="surname">
<Input />
</Form.Item>
</Form>
此表单无效:
<Form initialValues={{ surname: 'Mouse'}}>
<Form.Item name="surname">
<Input />
{null}
</Form.Item>
</Form>
唯一的区别是第二个中的 Form.Item
形式有两个孩子。
这背后是否有意图?
如果有人想知道我为什么问。所以像这样的东西破坏了形式:
<Form.Item name={name}>
{type==="string" && <Input />}
{type==="integer" && <InputNumber />}
</Form.Item>
I've created a little Fiddle to illustrate the issue: https://stackblitz.com/edit/react-avejvc-mmhqda?file=index.js
This form works:
<Form initialValues={{ surname: 'Mouse'}}>
<Form.Item name="surname">
<Input />
</Form.Item>
</Form>
This form doesn't:
<Form initialValues={{ surname: 'Mouse'}}>
<Form.Item name="surname">
<Input />
{null}
</Form.Item>
</Form>
The only difference is that the Form.Item
in the second form has two children.
Is there an intention behind this?
In case anyone wonders why I am asking. So sth like this is breaking the form:
<Form.Item name={name}>
{type==="string" && <Input />}
{type==="integer" && <InputNumber />}
</Form.Item>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
官方文档此处给出了在一个
Form.Item
。您在
Form.Item
中放入的内容似乎有问题,即。可能不允许{null}
。The official documentation here gives examples of using multiple children in one
Form.Item
.You appear to have a problem with what you are putting in the
Form.Item
, ie.{null}
may not be allowed.我找到了解决方案,现在对发生的事情有了更好的了解。
从文档(https://ant.design/components/form/#Form.Item< /a>):
被Form.Item用name属性包裹后,value(或valuePropName定义的其他属性)onChange(或trigger定义的其他属性)props将被添加到表单控件中,表单数据的流动将由 Form 处理
有一个文档中的工作示例也是如此,这里是codepen: https://codepen.io/pen? &编辑=001
I found a solution and have a better understanding now of what is going on.
From the docs (https://ant.design/components/form/#Form.Item):
After wrapped by Form.Item with name property, value(or other property defined by valuePropName) onChange(or other property defined by trigger) props will be added to form controls, the flow of form data will be handled by Form
There is a working example in the docs too, here is the codepen: https://codepen.io/pen?&editors=001
将元素包装在
内可以很好地适应上述场景。检查下面的代码Wrapping elements inside
<Input.Group>
works fine with above scenario. Check below code