-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
167 lines (118 loc) · 3.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* @flow */
import React, { PureComponent } from 'react'
const defaultProps = {
onClose: null,
onOpen: null,
openMs: 1000,
closeMs: 500,
showBtn: true,
btnClassName: 'fa fa-close',
outsideClickClose: false,
children: [],
defaultOpen: false
}
type Props = {
onClose?: Function,
onOpen?: Function,
openMs?: number,
closeMs?: number,
showBtn?: boolean,
btnClassName?: string,
outsideClickClose?: boolean,
defaultOpen?: boolean,
children?: any,
}
type State = {
stage: string,
ready: boolean,
}
// It's an decorator
export default ( Comp: any ) => {
return class Modal extends PureComponent<Props, State> {
state: State = {
stage: 'closed',
ready: false
}
openMs: number = 0
closeMs: number = 0
onClose: null | Function = null
onOpen: null | Function = null
openPopup ( initialStage?: string = '' ) {
if ( initialStage && typeof initialStage === 'string' )
return this.setState( {
stage: initialStage,
} )
const { stage } = this.state
if ( stage !== 'closed' )
return null
return this.setState( {
stage: 'opening',
} , () => {
setTimeout( () => {
return this.setState( {
stage: 'open',
}, () => {
if ( this.onOpen )
this.onOpen()
} )
} , this.openMs )
} )
}
closePopup () {
const { stage } = this.state
return this.setState( {
stage: 'closing'
} , () => {
return setTimeout( () => {
this.setState( {
stage: 'closed',
}, () => {
if ( this.onClose )
this.onClose()
} )
}, this.closeMs )
} )
}
componentDidMount() {
return this.setState( {
ready: true,
} )
}
clickOutside = ( e: Object ) => {
if ( e.target.classList.contains( 'popModal' ) )
return this.closePopup()
}
_modal = ( props: Props ) => {
const { ready } = this.state
const newProps = { ...defaultProps, ...props }
const stage = ( !ready && newProps.defaultOpen ) ? 'open' : this.state.stage
if ( stage === 'closed' )
return null
this.onClose = newProps.onClose || null
this.onOpen = newProps.onOpen || null
return (
<div className={ `popModal ${stage}` } onClick={ ( newProps.outsideClickClose && stage === 'open' ) ? this.clickOutside : null }>
{
( newProps.showBtn && stage === 'open' ) ? (
<button onClick={ this.closePopup.bind( this ) } className='popModalClose' type='button'>
<span className={ newProps.btnClassName } />
</button>
) : null
}
<div className='popModalContainer'>
{ newProps.children }
</div>
</div>
)
}
render () {
return <Comp
{ ...this.props }
Modal={ this._modal.bind( this ) }
modalStage={ this.state.stage }
openPopup={ this.openPopup.bind( this ) }
closePopup={ this.closePopup.bind( this ) }
/>
}
}
}