ant Design Form表單,F(xiàn)orm.List/Form.Item實現(xiàn)動態(tài)切換

在Form.List中,動態(tài)新增多個由Space包裹的Form.Item,樣例如下:

const areas = [
  {
    label: 'Beijing',
    value: 'Beijing',
  },
  {
    label: 'Shanghai',
    value: 'Shanghai',
  },
];
const sights = {
  Beijing: ['Tiananmen', 'Great Wall'],
  Shanghai: ['Oriental Pearl', 'The Bund'],
};

<Form.List name="sights">
        {(fields, { add, remove }) => (
          <>
            {fields.map((field) => (
              <Space key={field.key} align="baseline">
                <Form.Item
                  noStyle
                  shouldUpdate={(prevValues, curValues) =>
                    prevValues.area !== curValues.area || prevValues.sights !== curValues.sights
                  }
                >
                  {() => (
                    <Form.Item
                      {...field}
                      label="Sight"
                      name={[field.name, 'sight']}
                      rules={[
                        {
                          required: true,
                          message: 'Missing sight',
                        },
                      ]}
                    >
                      <Select
                        disabled={!form.getFieldValue('area')}
                        style={{
                          width: 130,
                        }}
                      >
                        {(sights[form.getFieldValue('area')] || []).map((item) => (
                          <Option key={item} value={item}>
                            {item}
                          </Option>
                        ))}
                      </Select>
                    </Form.Item>
                  )}
                </Form.Item>
                <Form.Item
                  {...field}
                  label="Price"
                  name={[field.name, 'price']}
                  rules={[
                    {
                      required: true,
                      message: 'Missing price',
                    },
                  ]}
                >
                  <Input />
                </Form.Item>

                <MinusCircleOutlined onClick={() => remove(field.name)} />
              </Space>
            ))}

            <Form.Item>
              <Button type="dashed" onClick={() => add()} block icon={<PlusOutlined />}>
                Add sights
              </Button>
            </Form.Item>
          </>
        )}
      </Form.List>

需求點: 如示例中: 想根據(jù)選擇不同type,其他的Form.Item也不同,進行相應切換, 這里的price Form.Item可以換成其他的例如 size Form.Item, price FormItem則不需要;實現(xiàn)代碼具體如下:

const typeList = ['A', 'B']
 <Form.Item
                {...field}
                  label="Tpye"
                  name={[field.name, 'type']}
                  rules={[
                    {
                      required: true,
                      message: 'Missing type',
                    },
                  ]}
                >
                  <Select
                        style={{
                          width: 130,
                        }}
                      >
                        {(typeList .map((item) => (
                          <Option key={item} value={item}>
                            {item}
                          </Option>
                        ))}
                      </Select>
 </Form.Item>
<Form.Item
           noStyle
           shouldUpdate={(prevValues, curValues) =>
               prevValues.sights[index] !== curValues.sights[index]
                  }
                >
                  {({getFieldValue}) => getFieldValue('sights')[index]&&getFieldValue('sights')[index].type
            &&getFieldValue('sights')[index].type === 'A'  ?  (
      <Space>
        <Form.Item
                      {...field}
                      label="Sight"
                      name={[field.name, 'sight']}
                      ]}
                    >
                      <Select
                        disabled={!form.getFieldValue('area')}
                        style={{
                          width: 130,
                        }}
                      >
                        {(sights[form.getFieldValue('area')] || []).map((item) => (
                          <Option key={item} value={item}>
                            {item}
                          </Option>
                        ))}
                      </Select>
                    </Form.Item>
            </Space> :
      <Form.Item label="Size" name="size">
        ........
      </Form.Item>
          )}
     </Form.Item>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容