import React, { Component } from 'react' import { Form, Input, InputNumber, Radio, Select, Spin, Switch, TreeSelect } from 'antd' import { AntIcon, IconSelector } from 'components' import { cloneDeep } from 'lodash' import getDictData from 'util/dic' import { api } from 'common/api' import { EMPTY_ID } from 'util/global' const initialValues = { type: '1', openType: '1', visible: true, sort: 100 } export default class form extends Component { state = { // 加载状态 loading: true, codes: { menuType: [], openType: [] }, options: { appList: [], parentTreeData: [] }, type: initialValues.type, openType: initialValues.openType, icon: '' } // 表单实例 form = React.createRef() iconSelector = React.createRef() // 初始化数据 record = {} /** * mount后回调 */ componentDidMount() { this.props.created && this.props.created(this) } /** * 填充数据 * 可以在设置this.record之后对其作出数据结构调整 * [异步,必要] * @param {*} params */ async fillData(params) { this.record = cloneDeep(params.record) //#region 从后端转换成前段所需格式 const { menuType, openType } = await getDictData('menu_type', 'open_type') const appList = await this.onLoadSysApplist() let parentTreeData = [] if (params.isParent) { parentTreeData = await this.onLoadMenuTree(params.parent.application) } else if (params.record) { parentTreeData = await this.onLoadMenuTree(params.record.application) } const icon = params.record && params.record.icon this.setState({ codes: { menuType, openType }, options: { appList, parentTreeData }, icon }) //#endregion const form = this.form.current if (params.isParent) { form.setFieldsValue({ pid: params.parent.id, application: params.parent.application }) } else { form.setFieldsValue(this.record) } this.setState({ loading: false }) this.onTypeChange() } /** * 获取数据 * 可以对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 onLoadSysApplist() { const { data } = await api.getAppList() return data } async onLoadMenuTree(application) { const { data } = await api.getMenuTree({ application }) return [{ id: EMPTY_ID, parentId: undefined, title: '顶级', value: EMPTY_ID, pid: undefined, children: data, }] } onTypeChange() { this.onTypeChangeGroup() const form = this.form.current const { type } = form.getFieldsValue() if (['0', '2'].includes(type)) { form.setFieldsValue({ openType: '0' }) } else { form.setFieldsValue({ openType: '1' }) } } onOpenTypeChange() { this.onTypeChangeGroup() } onTypeChangeGroup() { const form = this.form.current const { type, openType } = form.getFieldsValue() // if (type == 1 && openType == 2) { // form.setFieldsValue({ // component: 'iframe' // }) // } else { // form.setFieldsValue({ // component: '' // }) // } this.setState({ type, openType }) } async onApplicationChange(value) { this.setState({ loading: true }) const parentTreeData = await this.onLoadMenuTree(value) this.setState({ loading: false, options: { ...this.state.options, parentTreeData } }) this.form.current.setFieldsValue({ pid: undefined }) } onSelectIcon(icon) { this.form.current.setFieldsValue({ icon }) this.setState({ icon }) } //#endregion render() { return (
) } }