React 筛选器组件:更新表单功能实现
import React, { PureComponent } from 'react';\nimport { connect } from 'dva';\nimport { Form, Input, Modal, Select, Row, Col } from 'antd';\n\nconst { Option } = Select;\nconst FormItem = Form.Item;\n\n@Form.create()\n@connect(({ filterBlock }) => ({\n filterBlock,\n}))\nclass UpdateForm extends PureComponent {\n constructor(props) {\n super(props);\n this.state = {\n visible: false,\n formVals: props.record || {},\n filterSelect: [],\n keyId: '',\n };\n\n this.formLayout = {\n labelCol: { span: 4 },\n wrapperCol: { span: 18 },\n };\n\n this.formLayout1 = {\n labelCol: { span: 10 },\n wrapperCol: { span: 14 },\n };\n this.formLayout2 = {\n labelCol: { span: 6 },\n wrapperCol: { span: 18 },\n };\n }\n\n componentDidMount() {\n const { dispatch } = this.props;\n // eslint-disable-next-line no-unused-vars\n const { formVals, filterSelect } = this.state;\n if (formVals.key) {\n dispatch({\n type: 'filterBlock/fetchItemsList',\n payload: formVals.key,\n callback: res => {\n if (res && res.status === 200) {\n const centerData = res.data.map(item => {\n return {\n label: item,\n value: ${item},\n };\n });\n this.setState({ filterSelect: centerData });\n }\n },\n });\n }\n }\n\n handleSave = () => {\n const { form, okChange, index, record, filterList } = this.props;\n const { formVals, keyId } = this.state;\n form.validateFields((err, fieldsValue) => {\n if (err) return;\n\n const arrData = fieldsValue.content.reduce((arr, next) => {\n const typeList = next.type.map(type => {\n return { type, name: next.name };\n });\n return arr.concat(typeList);\n }, []);\n\n const nameValue = (filterList || []).find(item => item.id === keyId)?.title;\n const formItem = {\n key: keyId || formVals?.key,\n value: fieldsValue?.value || nameValue,\n typeList: arrData,\n };\n console.log(formItem, 80);\n okChange(formItem, index);\n this.handleVisible();\n });\n };\n\n handleVisible = (flag, record) => {\n this.setState({\n visible: !!flag,\n formVals: record || {},\n });\n\n };\n\n // 添加\n add = () => {\n // const { form } = this.props;\n // const list = form.getFieldValue('list');\n // const nextList = list?.concat({});\n // form.setFieldsValue({\n // list: nextList,\n // });\n const { form } = this.props;\n const list = form.getFieldValue('list') || [];\n const nextList = [...list, {}];\n form.setFieldsValue({\n list: nextList,\n });\n form.getFieldDecorator('list', { initialValue: nextList });\n }\n\n // 删除\n deleteRow = index => {\n const { form } = this.props;\n const list = form.getFieldValue('list');\n const content = form.getFieldValue('content');\n\n if (list.length === 1) {\n return;\n }\n\n form.setFieldsValue({\n list: list.filter((item, key) => key !== index),\n content: content.filter((item, key) => key !== index),\n });\n };\n\n projectChange = keys => {\n const { dispatch } = this.props;\n // eslint-disable-next-line no-unused-vars\n const { filterSelect, keyId } = this.state;\n dispatch({\n type: 'filterBlock/fetchItemsList',\n payload: keys,\n callback: res => {\n if (res && res.status === 200) {\n const centerData = res.data.map(item => {\n return {\n label: item,\n value: ${item},\n };\n });\n this.setState({ filterSelect: centerData, keyId: keys });\n }\n },\n });\n\n };\n\n renderContent = formVals => {\n // console.log(formVals, 144);\n const { filterSelect } = this.state;\n const { formLayout, formLayout1, formLayout2 } = this\n const {\n form: { getFieldDecorator, getFieldValue },\n filterList,\n } = this.props;\n ;\n const list = formVals?.typeList || [{}];\n getFieldDecorator('list', { initialValue: list });\n console.log(list, 155);\n const selectProps = {\n placeholder: '请选择',\n optionFilterProp: 'children',\n style: {\n width: '100%',\n },\n allowClear: true,\n showSearch: true,\n };\n\n return (\n