import React, { Component } from 'react' import { Form, Input, Radio, InputNumber, TreeSelect, Spin } from 'antd' import { AntIcon } from 'components' import { cloneDeep } from 'lodash' import getDictData from 'util/dic' import { EMPTY_ID } from 'util/global' import { api } from 'common/api' const initialValues = { type: 1, sort: 100 } export default class form extends Component { state = { // 加载状态 loading: true, options: { dicTreeData: [] } } // 表单实例 form = React.createRef() // 初始化数据 record = {} /** * mount后回调 */ componentDidMount() { this.props.created && this.props.created(this) } /** * 填充数据 * 可以在设置this.record之后对其作出数据结构调整 * [异步,必要] * @param {*} params */ async fillData(params) { this.record = cloneDeep(params.record) const treeData = await this.loadDicTreeData() this.setState({ options: { dicTreeData: treeData } }) this.record = { pid: params.pid, ...this.record } if (this.record.code) { this.record.type = 2; this.setState({ type: 2 }) } this.form.current.setFieldsValue(this.record) this.setState({ loading: false }) } /** * 获取数据 * 可以对postData进行数据结构调整 * [异步,必要] * @returns */ async getData() { const form = this.form.current const valid = await form.validateFields() if (valid) { const postData = form.getFieldsValue() if (this.record) { postData.id = this.record.id } //#region 从前段转换后端所需格式 //#endregion return postData } } //#region 自定义方法 async loadDicTreeData() { const { data } = await api.sysDictTypeTree() return [{ id: EMPTY_ID, parentId: undefined, title: '顶级', value: EMPTY_ID, pid: undefined, children: data, }] } //#endregion render() { return (
{ if (changeValues.hasOwnProperty('type')) { this.setState({ type: changeValues.type }) } }} > }>
{/* 表单控件 */} 目录 字典类型 {this.state.type == 2 && <> } {/* ... */}
) } }