import React, { Component } from 'react' import ReactDOM from 'react-dom' import { Anchor, Card, Col, Divider, Row, Spin } from 'antd' import { AntIcon, ComponentDynamic, Container } from 'components' import { isEqual, merge } from 'lodash' const parts = [ { // title: '标题', component: () => import('./part'), }, ] export default class index extends Component { // 子表单实例集合 children = [] // 整合提交数据 formData = {} // 锚点挂载DOM container = window /** * 阻止外部组件引发的渲染,提升性能 * 可自行添加渲染条件 * [必要] * @param {*} props * @param {*} state * @returns */ shouldComponentUpdate(props, state) { return !isEqual(this.state, state) || this.props.loading !== props.loading } /** * 加载完成,通知父级组件并传递自身 */ call(child, index) { this.children[index] = child if (this.children.filter(p => p).length === parts.length) { const { onRef } = this.props if (onRef) onRef(this) } } /** * 从下级组件获取表单数据,并传递给更上级组件 * [异步,必要] * @returns */ async getData() { for (const child of this.children) { const data = await child.getData() merge(this.formData, data) } return this.formData } /** * 设置锚点容器 * [非必要] * @param {*} container */ setContainer = container => { this.container = (ReactDOM.findDOMNode(container) || {}).parentNode } /** * 渲染 * 当前渲染结构已完善,非必要可以不用修改 * [必要] * @returns */ render() { const { id, loading } = this.props return (
{parts.map((item, i) => (
{item.title &&
{item.title}
} } wrapperClassName={loading && 'h-400-min'} > {!loading && ( this.call(child, i)} /> )}
{i < parts.length - 1 && }
))}
{/* 锚点,如果不需要可以删除以下节点 */} this.container} offsetTop={24} targetOffset={100} wrapperStyle={{ backgroundColor: 'transparent' }} onClick={e => e.preventDefault()} > {parts.map((part, i) => ( ))} ) } }