数据驱动展示表单dom, antd formItem封装

外部调用 

        // const dataSourceTest = [
        //     {
        //         label: 'label1',
        //         id: 'label1',
        //         type: 'isInput',
        //         placeholder: '请输入指标分类',
        //     },
        //     {
        //         label: 'label2',
        //         id: 'label2',
        //         type: 'isSelect',
        //         showSearch: 'true',
        //         selectKey: 'id',
        //         selectLabel: 'show',
        //         selectList: [ { id: 1, show: '哈哈哈' } ],
        //         placeholder: '请输入指标分类',
        //     },
        //     {
        //         label: 'label3',
        //         id: 'label3',
        //         type: 'isRangePicker',
        //         rules: [
        //             {
        //                 required: true,
        //                 message: '请输入',
        //             },
        //         ],
        //     },
        // ]
<Form>
     <GetFormItem
        dataSource={dataSourceTest}
         form={this.props.form}
      />
</Form>

 GetFormItem.js文件

import React from 'react'
import { Select, Input, Form, DatePicker, Radio } from 'antd'

const { RangePicker } = DatePicker
const { Option } = Select

// 返回指定类型的dom
const getDomType = (item) => {
    const {
        type, // dom类型
        placeholder,
        list = [],
        disabled,
        customKey, // 自定义指定key和value
        customLabel, // 自定义指定显示的label
        selectMode, // 下拉类型
        allowClear = true,
        showSearch = true,
        optionFilterProp = 'children',
    } = item
    const strategy = {
        isInput: () => <Input
            allowClear={allowClear}
            placeholder={placeholder}
            disabled={disabled} />,
        isSelect: () => (
            <Select
                allowClear={allowClear}
                showSearch={showSearch}
                optionFilterProp={optionFilterProp}
                placeholder={placeholder}
                mode={selectMode}
                disabled={disabled}
            >
                {list.map((el) => (
                    <Option
                        value={el[customKey] || el.id}
                        key={el[customKey] || el.id}>
                        {el[customLabel] || el.label}
                    </Option>
                ))}
            </Select>
        ),
        isRangePicker: () => (
            <RangePicker
                format='YYYY-MM-DD'
                allowClear={allowClear}/>
        ),
        isRadio: () => (
            <Radio.Group>
                {list.map((el) => (
                    <Radio
                        value={el[customKey] || el.id}
                        key={el[customKey] || el.id}>{el[customLabel] || el.label}</Radio>
                ))}
            </Radio.Group>
        ),
    }
    return strategy[type]()
}

const GetFormItem = (props) => {
    const { form, dataSource, formItemLayout, editVal = {} } = props
    const { getFieldDecorator } = form

    return (
        <>
            {
                dataSource.map(item =>
                    <Form.Item
                        label={item.label}
                        key={item.id}
                        {...formItemLayout}>
                        {getFieldDecorator(item.id, {
                            rules: item.rules,
                            initialValue: editVal[item.id],
                        })(getDomType(item))}
                    </Form.Item>,
                )
            }
        </>

    )
}

export default GetFormItem

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值