需求:
新建投票信息,点击添加按钮,新增一组问题表单,表单字段名一样(需要Ant的form表单中循环动态渲染formItem),此时getFieldDecorator(id,options)中的id不能在循环中写死,那要如何解决呢?
解决方案:
1、添加索引区分 form.getFieldDecorator(questionName${index}
, {…
{questionList.map((question, index) => (
<FormItem label="问题名称">
{form.getFieldDecorator(`questionName${index}`, {
initialValue: '',
rules: [{required: true, message: '请输入问题名称'}]
})(<Input placeholder="不超过20个字符" type="text" style={{ width: 300 }} maxLength={20} />)}
</FormItem>
<FormItem label="问题描述">
{form.getFieldDecorator(`questionDescrib${index}`, {
initialValue: '',
rules: [{required: true, message: '请输入问题描述'}]
})(<Input placeholder="不超过20个字符" type="text" style={{ width: 300 }} maxLength={20} />)}
</FormItem>
))}
2、字段变为数组
form.getFieldDecorator(questionName${index}
, {…
改为 form.getFieldDecorator(questionName[${index]}
, {…