import React, { Component } from 'react';
import { Form } from 'antd';

export default class extends Component {
  constructor(props) {
    super(props);
    this.state = { C: null };
  }

  componentWillMount() {
    if (!this.state.C && this.props.component) {
      this.props.component().then(({ default: component }) => {
        this.setState({ C: this.props.isForm ? Form.create()(component) : component });
      });
    }
  }

  componentWillUnmount() {
    this.setState({ Component: null });
  }

  render() {
    const { C } = this.state;
    if (C) {
      return <C {...this.props}>{this.props.children || ''}</C>;
    }
    return <div />;
  }
}
