index.js 1.94 KB
Newer Older
FE committed
1 2
import React from 'react'
import ReactDOM from 'react-dom'
3 4 5
import './index.scss'
import classnames from 'classnames'

FE committed
6 7
const re = /(https?|ftp):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/

FE committed
8
function ClosablePopup({
FE committed
9 10 11 12 13 14 15 16 17 18 19 20 21
                         title,
                         content,
                         className,
                         closable = true,
                         close = function () {
                         },
                         clickMaskClose = true,
                         closeIcon = 'iconiconfront-2',
                         afterClose = function () {
                         },
                         remove = function () {

                         }
FE committed
22
                       } = {}) {
23

FE committed
24

FE committed
25 26 27 28 29
  function unmountComponent() {
    ReactDOM.unmountComponentAtNode(div)
    if (div && div.parentNode) {
      div.parentNode.removeChild(div)
    }
30 31
  }

FE committed
32 33 34 35 36
  function _close() {
    let _c = close()
    if (_c && _c.then) {
      _c.then(() => {
        unmountComponent()
FE committed
37
        afterClose()
zhanghaozhe committed
38
      })
FE committed
39 40
    } else {
      unmountComponent()
FE committed
41
      afterClose()
zhanghaozhe committed
42 43 44
    }
  }

FE committed
45 46 47 48 49 50 51 52 53
  function clickMask() {
    if (closable) {
      return
    }
    if (!clickMaskClose) {
      return
    }
    _close()
  }
zhanghaozhe committed
54

FE committed
55
  const closablePopup = (
FE committed
56
    <div className={'closable-popup-mask'} onClick={clickMask}>
FE committed
57 58 59 60
      <div className={classnames(['popup-container', className])}>
        <div className="title">{title}</div>
        <div className="content">
          {content}
61
        </div>
FE committed
62
        {
FE committed
63 64 65 66
          closable &&
          (re.test(closeIcon)
            ? <img src={closeIcon} alt="" className={'close-icon'} onClick={_close}/>
            : <i className={`close iconfont ${closeIcon}`} onClick={_close}/>)
FE committed
67 68 69 70 71 72 73
        }
      </div>
    </div>
  )
  const div = document.createElement('div')
  document.body.appendChild(div)

74

FE committed
75
  ReactDOM.render(closablePopup, div)
zhanghaozhe committed
76 77

  return {
FE committed
78 79
    close: _close,
    remove: unmountComponent
zhanghaozhe committed
80
  }
81 82
}

83
export default ClosablePopup