index.js 1.24 KB
Newer Older
1 2 3
import React, { Component } from 'react'
import './index.scss'
import classnames from 'classnames'
4
import propTypes from 'prop-types'
5 6 7 8

class ClosablePopup extends Component {

  state = {
zhanghaozhe committed
9
    visible: this.props.visible
10 11 12 13 14 15 16
  }

  close = () => {
    const {close} = this.props
    close ? close() : this.setState({visible: false})
  }

zhanghaozhe committed
17
  componentDidUpdate(prevProps, prevState) {
zhanghaozhe committed
18
    const {visible} = this.props
zhanghaozhe committed
19
    if (prevState.visible !== this.props.visible) {
zhanghaozhe committed
20 21 22 23 24 25 26
      this.setState({
        visible
      })
    }
  }


27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  render() {
    const {title, className, children, closable = true} = this.props
    return (
      this.state.visible
        ?
        <div className={'closable-popup-mask'}>
          <div className={classnames(['popup-container', className])}>
            <div className="title">{title}</div>
            <div className="content">
              {children}
            </div>
            {
              closable && <i className={'close iconfont iconiconfront-2'} onClick={this.close}/>
            }
          </div>
        </div>
        : null
    )
  }
}

48 49 50 51 52 53 54
ClosablePopup.propTypes = {
  title: propTypes.string.isRequired,
  visible: propTypes.bool,
  closable: propTypes.string,
  close: propTypes.func
}

55
export default ClosablePopup