react和antd3 实现动态表单

import React, { Component } from 'react';
import { Spin, Form, Row, Col, Card,Icon } from 'antd';
import { OcirfButton,OcirfInput } from '@/components';
import { handleNewFilter, getStringLength } from '@/utils/utils';
import styles from './index.less';
import { connect } from 'dva';

const FormItem = Form.Item;
const createForm = Form.create;
const infoColProps = {
    xs: 24,
    sm: 24,
    md: 8,
    lg: 8,
    xl: 8,
};
@connect(({ litigationList, loading }) => ({
  litigationList,
  detailLoading: loading.effects['litigationList/litigationBasicInformationDetails'],
}))
@createForm()
class OcirfFilterForm extends Component {
    constructor(props) {
        super(props);
        this.state = {
            extend: false,
            otherDefendantPeope:[{name:'',nameId:'',id:new Date().getTime()}],//其他被告人,默认为1
            thirdPeope:[{name:'',nameId:'',id:new Date().getTime()}],//第三人,默认为1
            // allLitigationData:{},//掉接口获取的数据
            // reallyOptions:{},//处理后用于 提交诉讼基本信息 的入参数
        }
    }

    componentDidMount() {
      const { onRef } = this.props;
      onRef&&this.props.onRef(this);
      this.getBasicsLitigationInfo()
    }

    //判断是否为必填---其他被告人和第三人
    getIsRequired = (type) => {
      const { otherDefendantPeope ,thirdPeope} = this.state
      const { form } = this.props;
      form.validateFields((err, values)=>{
        if(err) {return;}
      })
    }


    //查询诉讼基本信息
    getBasicsLitigationInfo = () => {
      const { dispatch,litigationId, } = this.props
      // console.log('litigationId===========',litigationId)
      let otherDefendantPeopeArr = []
      let thirdPeopeArr = []
      dispatch({
        type: 'litigationList/litigationBasicInformationDetails',
        payload: {
          litigationId,
        },
        callback: (res) => {
          
          if(res.otherDefendantEntities){
            res.otherDefendantEntities.map((item,index)=>{
              otherDefendantPeopeArr.push({name:item.name,nameId:item.id,id:new Date().getTime()+index})
            })
            // console.log('otherDefendantPeopeArr',otherDefendantPeopeArr)
            if(otherDefendantPeopeArr.length>0){
              this.setState({
                otherDefendantPeope:otherDefendantPeopeArr
              })
            }
          }
    
          if(res.thirdPartyEntities){
            res.thirdPartyEntities.map((item,index)=>{
              thirdPeopeArr.push({name:item.name,nameId:item.id,id:new Date().getTime()+index})
            })
            if(thirdPeopeArr.length>0){
              this.setState({
                thirdPeope:thirdPeopeArr
              })
            }
          }

        }
      })
    } 

    renderNewFilterForm = (type) => {
        const { form: { getFieldDecorator }, formList,basicFormList,litigationFormList } = this.props;
        return (<Row gutter={32} className="filter-wrap">{
            ((type==='basicInfo'?basicFormList:litigationFormList)||[]).map((item,index)=>{
                const len = getStringLength(item.label);
                return <Col {...infoColProps} className={`filter-item ${index>2?'hide-filter-item':''}`} key={index}>
                    {len < 13 && (<label>{item.label}</label>)}
                    {12 < len && len < 25 && (<label className={item.labelSingleline ? '' : 'new-label-lines'}>{item.label}</label>)}
                    {len > 24 && (<label className="new-label-lines"><ChannelEllipsis lines={2} title={item.label}>{item.label}</ChannelEllipsis></label>)}
                    <span className="filter-opera">
                        {!item.filedType && (<FormItem>
                            {getFieldDecorator(item.getField, {
                                initialValue: item.initialValue,
                                ...item.getFieldDecorator
                            })(
                                React.createElement(
                                item.dom,
                                {...item.props},
                                item.children
                                )
                            )}
                        </FormItem>)}
                        {item.filedType == 'line' && (<FormItem className={styles.middle_line_box}>
                            <FormItem  className={styles.middle_line_item}>{getFieldDecorator(item.getField1, {
                                initialValue: item.initialValue,
                                ...item.getFieldDecorator
                            })(
                                React.createElement(
                                item.dom,
                                item.props1?{...item.props1}:{...item.props},
                                item.children
                                )
                            )}</FormItem>
                            <span className={styles.middle_line_txt}>-</span>
                            <FormItem className={styles.middle_line_item}>
                                {getFieldDecorator(item.getField2, {
                                    initialValue: item.initialValue,
                                    ...item.getFieldDecorator
                                })(
                                    React.createElement(
                                    item.dom,
                                    item.props2?{...item.props2}:{...item.props},
                                    item.children
                                    )
                                )}
                            </FormItem>
                        </FormItem>)}
                    </span>
                </Col>
            })}
        </Row>)
    }
    //点击确定按钮 数据处理
    searchInfo = () => {
        const { otherDefendantPeope,thirdPeope } = this.state
        const { form, getData } = this.props;
        
        const otherDefendantPeopeArr = []
        const thirdPeopeArr = []
        // const tagOtherDefendantPeopeArr = []
        // const reallyOptions = {}
        form.validateFields((err, values)=>{
          // console.log('values',values)
          if(err) {return;}

          otherDefendantPeope.map((item,index)=>{
            otherDefendantPeopeArr.push({
              name:values[`otherDefendantName${item.id}`],
              id:values[`otherDefendantId${item.id}`]
            })
            // if(values[`otherDefendantName${item.id}`]&&values[`otherDefendantName${item.id}`]!=''){
            //   otherDefendantPeope.splice(index,1,{...item,isTageOtherDefendantId:true})
            // }
            // if(values[`otherDefendantId${item.id}`]&&values[`otherDefendantId${item.id}`]!=''){
            //   otherDefendantPeope.splice(index,1,{...item,isTageOtherDefendantName:true})
            // }
          })
          thirdPeope.map((item,index)=>{
            thirdPeopeArr.push({
              name:values[`thirdPartyName${item.id}`],
              id:values[`thirdPartyId${item.id}`]
            })
            // if(values[`thirdPartyName${item.id}`]&&values[`thirdPartyName${item.id}`]!=''){
            //   thirdPeope.splice(index,1,{...item,isTageThirdPartyId:true})
            // }
            // if(values[`thirdPartyId${item.id}`]&&values[`thirdPartyId${item.id}`]!=''){
            //   thirdPeope.splice(index,1,{...item,isTageThirdPartyName:true})
            // }
          })
          //确定时执行函数
          getData(JSON.parse(JSON.stringify({
                ...values,
                otherDefendantEntities:otherDefendantPeopeArr,
                thirdPartyEntities:thirdPeopeArr
              }
          )));
        })

    }

    resetMyForm = () => {
        const { form, resetFormCallback } = this.props;
        form.resetFields();
        resetFormCallback&&resetFormCallback();
    }

    //点击增加一列
    addListInfo = (index,value) => {
      const { thirdPeope,otherDefendantPeope} = this.state
      switch(value){
        case 'otherDefendantPeope' :
          return this.setState(()=>({
            otherDefendantPeope:otherDefendantPeope.concat({name:'',nameId:'',id:new Date().getTime()})
          }))
        case 'thirdPeope' :
          return this.setState(()=>({
            thirdPeope:thirdPeope.concat({name:'',nameId:'',id:new Date().getTime()})
          }))
      }
    }
    //删除一列
    reduceListInfo = (item,type) => {
      const { thirdPeope,otherDefendantPeope} = this.state
      switch(type){
        case 'otherDefendantPeope' :
          return this.setState(()=>({
            otherDefendantPeope:otherDefendantPeope.filter((valueItem)=>{
                  return valueItem.id != item.id
                }
              )
          }))
        case 'thirdPeope' :
          return this.setState(()=>({
            thirdPeope:thirdPeope.filter((valueItem)=>{
                  return valueItem.id != item.id
                }
              )
          }))
      }
    }
  


    render() {
        const { children, className, formList,cancel,confirm,allLitigationData,defendantInfo,form} = this.props;
        const { extend,otherDefendantPeope,thirdPeope } = this.state;
        const { getFieldDecorator } = this.props.form;

        // console.log('otherDefendantPeope',otherDefendantPeope)
        // console.log('thirdPeope',thirdPeope)

        return (
            <div className={`ocirf-filter new-filter-wrap`} style={{paddingRight:'0px'}}>
                <div className="new-filter-contant" style={{maxHeight:'none'}}>
                  <div className={styles.content_title}>诉讼基本信息</div>
                  <Form className="ocirf-filter-form">
                    <div style={{borderBottom:'1px dashed #E8E8E8',marginBottom:'16px'}}>
                      <p className={styles.content_node}>
                        <span className={styles.content_node_icon}></span>
                        <span className={styles.content_node_text}>基本信息</span>
                      </p>
                      {this.renderNewFilterForm('basicInfo')}
                    </div>
                    <div style={{borderBottom:'1px dashed #E8E8E8',marginBottom:'16px'}}>
                      <p className={styles.content_node}>
                        <span className={styles.content_node_icon}></span>
                        <span className={styles.content_node_text}>诉讼信息</span>
                      </p>
                      {this.renderNewFilterForm('litigationInfo')}
                    </div>
                    <div>
                      <p className={styles.content_node}>
                        <span className={styles.content_node_icon}></span>
                        <span className={styles.content_node_text}>被告信息</span>
                      </p>
                      <Row gutter={32} className="filter-wrap">
                        <Col span={8} className="filter-item">
                          <label className={styles.pop_label} style={{lineHeight:'normal'}}>被告人</label>
                          <span className="filter-opera">
                            <FormItem style={{lineHeight:'0'}} label={`defendantName`}>
                              {getFieldDecorator(`defendantName` ,{initialValue:defendantInfo.customerName||'',rules: [{ required: true, message: '请输入被告人姓名' }]})(<OcirfInput/>)}
                            </FormItem>
                          </span>
                        </Col>
                        <Col span={8} className="filter-item" >
                          <label className={styles.pop_label} style={{lineHeight:'normal'}}>证件号码</label>
                          <span className="filter-opera">
                            <FormItem style={{lineHeight:'0'}} label={`defendantId`}>
                              {getFieldDecorator(`defendantId`, {initialValue:defendantInfo.certNo||'', rules: [{ required: true, message: '请输入被告人证件号码' }]})(<OcirfInput/>)}
                            </FormItem>
                          </span>
                        </Col>   
                      </Row>
                      {
                        otherDefendantPeope.map((item,index)=>{
                          return <Row gutter={32} className="filter-wrap" key={index}>
                            <Col span={8} className="filter-item" >
                              <label className={styles.pop_label} style={{lineHeight:'normal'}}>其他被告人(姓名/名称)</label>
                              <span className="filter-opera">
                                <FormItem label={`otherDefendantName${item.id}`} style={{lineHeight:'0'}}>
                                  {getFieldDecorator(`otherDefendantName${item.id}`,
                                  {initialValue:item.name||'',rules: [{ required: form.getFieldValue(`otherDefendantId${item.id}`)?true:false, message: '请输入其他被告人姓名' }]})(<OcirfInput onBlur={()=>this.getIsRequired(`otherDefendantName${item.id}`)}/>)}
                                </FormItem>
                              </span>
                            </Col>
                            <Col span={8} className="filter-item" >
                              <label className={styles.pop_label} style={{lineHeight:'normal'}}>证件号码</label>
                              <span className="filter-opera">
                                <FormItem style={{lineHeight:'0'}} label={`otherDefendantId${item.id}`}>
                                  {getFieldDecorator(`otherDefendantId${item.id}`, 
                                  // {initialValue:item.nameId||'',rules: [{ required: form.getFieldValue(`otherDefendantName${item.id}`)?true:false, message: '请输入证件号码' }]})(<OcirfInput onBlur={()=>this.getIsRequired(`otherDefendantId${item.id}`)}/>)}
                                  {initialValue:item.nameId||''})(<OcirfInput onBlur={()=>this.getIsRequired(`thirdPartyId${item.id}`)}/>)}
                                </FormItem>
                              </span>
                            </Col> 
                            <Col span={8} className="filter-item" style={{lineHeight:'36px',fontSize:'20px'}}>
                              {
                                index === 0
                                ?
                                <Icon type="plus-circle" onClick={()=>this.addListInfo(index,'otherDefendantPeope')} style={{color:'#3D83FF'}}/>
                                :
                                <Icon type="minus-circle" onClick={()=>this.reduceListInfo(item,'otherDefendantPeope')} style={{color:'#F5222D'}}/>
                              }
                            </Col>     
                          </Row>
                        })
                      } 
                      {
                        thirdPeope.map((item,index)=>{
                          return <Row gutter={32} className="filter-wrap" key={index}>
                            <Col span={8} className="filter-item" >
                              <label className={styles.pop_label} style={{lineHeight:'normal'}}>第三人(姓名/名称)</label>
                              <span className="filter-opera">
                                <FormItem style={{lineHeight:'0'}} label={`thirdPartyName${item.id}`}>
                                  {getFieldDecorator(`thirdPartyName${item.id}`, {initialValue:item.name||'',rules: [{ required: form.getFieldValue(`thirdPartyId${item.id}`)?true:false, message: '请输入第三人姓名' }]})(<OcirfInput onBlur={()=>this.getIsRequired(`thirdPartyId${item.id}`)}/>)}
                                </FormItem>
                              </span>
                            </Col>
                            <Col span={8} className="filter-item">
                              <label className={styles.pop_label} style={{lineHeight:'normal'}}>证件号码</label>
                              <span className="filter-opera">
                                <FormItem style={{lineHeight:'0'}} label={`thirdPartyId${item.id}`}>
                                  {/* {getFieldDecorator(`thirdPartyId${item.id}`, {initialValue:item.nameId||'',rules: [{ required: form.getFieldValue(`thirdPartyName${item.id}`)?true:false, message: '请输入证件号码' }]})(<OcirfInput onBlur={()=>this.getIsRequired(`thirdPartyName${item.id}`)}/>)} */}
                                  {getFieldDecorator(`thirdPartyId${item.id}`, {initialValue:item.nameId||''})(<OcirfInput onBlur={()=>this.getIsRequired(`thirdPartyId${item.id}`)}/>)}
                                </FormItem>
                              </span>
                            </Col> 
                            <Col span={8} className="filter-item" style={{lineHeight:'36px',fontSize:'20px'}}>
                              {
                                index === 0
                                ?
                                <Icon type="plus-circle" onClick={()=>this.addListInfo(index,'thirdPeope')} style={{color:'#3D83FF'}}/>
                                :
                                <Icon type="minus-circle" onClick={()=>this.reduceListInfo(item,'thirdPeope')} style={{color:'#F5222D'}}/>
                              }
                            </Col>     
                          </Row>
                        })
                      } 
                    </div>
                  </Form>
                  <div style={{paddingBottom:'16px',textAlign:"center"}}>
                      <OcirfButton className="new-ocirf-btn btn-64" type="primary" onClick={this.searchInfo}>{confirm||'查询'}</OcirfButton>
                      <OcirfButton className="new-ocirf-btn-empty btn-64 ml16" onClick={this.resetMyForm}>{cancel||'重置'}</OcirfButton>
                  </div>
                </div>
            </div>
        );
    }
}

export default OcirfFilterForm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值