基于ant.design的FormList,通过配置生成动态列表

目前只配置了输入框和文本框,需要的可自定义添加type

  • 实现效果如图

在这里插入图片描述

const initValues = [
  {type:'input',name:'start',label:'最小值',initValue:'',},
  {type:'input',name:'end',label:'最大值',initValue:'',},
  {type:'text',name:'unit',label:'采购单位',initValue:'盒',rules:[]},
  {type:'input',name:'unitPrice',label:'单价(含税)',initValue:'',},
  {type:'input',name:'platformUnitPrice',label:'销售单价(含税)',initValue:'',},
]
import { Form, Input, Button, Space, Select } from 'antd';
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';
import React from "react";


const formItemLayout = {
  labelCol: {span: 24},
  wrapperCol: {span: 24},
};

const areas = [
  {label: 'Beijing', value: 'Beijing'},
  {label: 'Shanghai', value: 'Shanghai'},
];

const initRules = [{required: true, message: '字段必填!'}]

interface FormItemProps {
  type: string,
  name: string;
  label: string;
  initValue: string;
  rules: {}[]
}

interface TypeProps {
  initValues: FormItemProps[]
}

const FormList = (props: TypeProps) => {
  const {initValues} = props;
  console.log(initValues)
  const [form] = Form.useForm();

  const onFinish = values => {
    console.log('Received values of form:', values);
  };

  const handleChange = () => {
    form.setFieldsValue({sights: []});
  };

  return (
    <Form form={ form } name="dynamic_form_nest_item" onFinish={ onFinish } autoComplete="off">
      <Form.Item name="area" label="Area" rules={ [{required: true, message: 'Missing area'}] }>
        <Select options={ areas } onChange={ handleChange }/>
      </Form.Item>
      <Form.List name="sights">
        { (fields, {add, remove}) => (
          <>
            { fields.map(field => (
              <Space key={ field.key } align="baseline">
                { initValues.map(item => (
                  <Form.Item
                    { ...field }
                    label={ item.label }
                    { ...formItemLayout }
                    name={ [field.name, item.name] }
                    fieldKey={ [field.fieldKey, item.name] }
                    rules={ item.rules || initRules }
                    initialValue={ item.initValue }
                  >
                    { item.type === 'text' ? item.initValue : <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>
      <Form.Item>
        <Button type="primary" htmlType="submit">
          Submit
        </Button>
      </Form.Item>
    </Form>
  );
};

export default FormList

参考来源

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值