[英]Ant Design Dynamic Form Collection
By Following this example, I have been struggling whole day for creating a dynamic form with multiple-input elements in each row. 通过遵循此示例,我整天都在努力创建动态表格,每行中都有多个输入元素。 https://ant.design/components/form/#components-form-demo-dynamic-form-item
https://ant.design/components/form/#components-form-demo-dynamic-form-item
I am getting following error on trying the code below: Objects are not valid as a React child (found: object with keys {name, email}) 我在尝试以下代码时遇到以下错误: 对象作为React子对象无效(找到:带有键{name,email}的对象)
const formItems = keys.map((k, index) => {
return (
<React.Fragment>
<FormItem
{...(index === 0 ? formItemLayout : formItemLayoutWithOutLabel)}
label={index === 0 ? 'Passengers' : ''}
required={false}
key={`${k}-name`}
>
{getFieldDecorator(`passengers[${k}].name`, {
validateTrigger: ['onChange', 'onBlur'],
rules: [{
required: true,
whitespace: true,
message: "Please input passenger's name or delete this field.",
}],
})(
<Input placeholder="passenger name" style={{ width: '60%', marginRight: 8 }} />
)}
<FormItem
{...(index === 0 ? formItemLayout : formItemLayoutWithOutLabel)}
label={index === 0 ? 'Passengers' : ''}
required={false}
key={`${k}-email`}
>
{getFieldDecorator(`passengers[${k}].email`, {
validateTrigger: ['onChange', 'onBlur'],
rules: [{
required: true,
whitespace: true,
message: "Please input passenger's name or delete this field.",
}],
})(
<Input placeholder="passenger name" style={{ width: '60%', marginRight: 8 }} />
)}
</FormItem>
</React.Fragment>
);
});
In the 2 places where you're doing: 在您做的2个地方:
<FormItem
{...(index === 0 ? formItemLayout : formItemLayoutWithOutLabel)}
This will only work if those objects have a style
key that all your layout is under, otherwise perhaps you are unintentionally writing a key here? 仅当这些对象具有所有布局都位于其下的
style
键时,这才起作用,否则,您可能无意在此处编写了一个键? It may be safer to explicitly set the style like this: 像这样显式设置样式可能更安全:
<FormItem
style={index === 0 ? formItemLayout : formItemLayoutWithOutLabel}
This way the formItemLayout
object can contain a flat style, no danger of overwriting other FormItem props. 这样,
formItemLayout
对象可以包含平面样式,没有覆盖其他FormItem道具的危险。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.