react实现列表增删改查的小demo(class组件版)

前言

react的语法上就是比vue麻烦不少,既然要开手动挡,那就开吧,一个基础的demo

效果图

列表

新增弹窗

react实现列表增删改查的小demo(class组件版)_第1张图片

编辑弹框

react实现列表增删改查的小demo(class组件版)_第2张图片

 新增一条数据后的效果

react实现列表增删改查的小demo(class组件版)_第3张图片

代码

根组件  index.jsx

import React, { Component,createRef} from 'react'
import withRouter from '../../utils/withRouter'
import GlobalFormCom from './globalForm'
import GlobalTable from './globalTable'
import './index.less'
import { cloneDeep } from 'lodash'
import { Input, Select , Button , Modal } from 'antd';
import PubSub from 'pubsub-js'

export default withRouter(class index extends Component {
  state = {
    searchForm:{
      searchName:'王惊涛',
      selectValue:'jsCoder'
    },
    modalInfo:{
      isModalOpen:false,
      modalTitle:'新建数据',
    },
    doneType:'add',
    addFormData:{},
    editFormData:{},
    editRecord:{}
  }
  searchDom = {
    width: '200px',
    marginRight:'16px'
  }
  
  selectOptions = [
    {
      label:'前端',
      value:'jsCoder'
    },
    {
      label:'后端',
      value:'pythonCoder'
    }
  ]
  changeNameInput = (e,name) => {
    this.setValue('searchForm',this.state.searchForm,name,e.target.value)
  }
  changeSelectValue = (e,name) =>{
    this.setValue('searchForm',this.state.searchForm,name,e)
  }
  setValue = (name,data,key,value,fn)=>{
    let _data = cloneDeep(data)
    _data[key] = value
    this.setState({[name]:_data},()=>{
      if(fn){
        fn()
      }
    })
  }
  openModal = (flag) =>{
    this.setState({doneType:flag})
    let fn = ()=>{
      this.setValue('modalInfo',this.state.modalInfo,'modalTitle','新建数据')
    }
    this.setValue('modalInfo',this.state.modalInfo,'isModalOpen',true,fn)
  }
  editHandler = (editData)=>{
    this.setState({doneType:'edit'},()=>{
      this.setValue('modalInfo',this.state.modalInfo,'modalTitle','编辑数据')
    })
    let data = cloneDeep(editData)
    this.setState({editRecord:data})
    this.setValue('modalInfo',this.state.modalInfo,'isModalOpen',true,()=>{
      setTimeout(()=>{
        PubSub.publish('editForm',this.state.editRecord)
      })

    })
  }
  handleOk = ()=>{
    // this.handleCancel()
    if(this.state.doneType === 'add'){
      PubSub.publish('getFormData','add')
    }else if(this.state.doneType === 'edit'){
      PubSub.publish('getFormData','edit')
    }
    
  }
  handleCancel = ()=>{
    let fn = ()=>{

    }
    this.setValue('modalInfo',this.state.modalInfo,'isModalOpen',false,fn)
  }
  globalFormStyle = ()=>{
    if(this.state.isModalOpen === false){
      return {
        display:none
      }
    }
  }
  
  sendFormData = (formData,flag)=>{
    if(flag === 'add'){
      this.setState({addFormData:formData},()=>{
        PubSub.publish('addTableData',this.state.addFormData)
       })
    }else if(flag === 'edit'){
      this.setState({editFormData:formData},()=>{
        PubSub.publish('editTableData',this.state.editFormData)
      })
    }

  }
  reqList = ()=>{
    setTimeout(()=>{
      PubSub.publish('reqList')
    })

  }
  render() {
    return (
      
姓名: this.changeNameInput(e,'searchName')} value={this.state.searchForm.searchName} style={this.searchDom} /> 类型: this.changeFormItem(e, 'name')} value={this.state.formData.name}> this.changeFormItem(e, 'gender')}> this.changeDate(date, dateString)} defaultValue={dayjs(this.state.formData.birthDate, 'YYYY/MM/DD')} format='YYYY/MM/DD' />