1
1
import React , {
2
2
FunctionComponent ,
3
3
MouseEvent ,
4
- MouseEventHandler ,
5
4
ReactElement ,
6
5
ReactNode ,
7
6
ReactPortal ,
@@ -12,7 +11,6 @@ import { createPortal } from 'react-dom'
12
11
import { CSSTransition } from 'react-transition-group'
13
12
import classNames from 'classnames'
14
13
import { Close } from '@nutui/icons-react'
15
- import { EnterHandler , ExitHandler } from 'react-transition-group/Transition'
16
14
import { defaultOverlayProps , OverlayProps } from '@/packages/overlay/overlay'
17
15
import Overlay from '@/packages/overlay'
18
16
import { ComponentDefaults } from '@/utils/typings'
@@ -98,45 +96,30 @@ export const Popup: FunctionComponent<
98
96
afterClose,
99
97
onClick,
100
98
} = { ...defaultProps , ...props }
101
- const nodeRef = React . useRef ( null )
99
+ const nodeRef = React . useRef < HTMLDivElement | null > ( null )
102
100
let innerIndex = zIndex || _zIndex
103
101
const [ index , setIndex ] = useState ( innerIndex )
104
102
const [ innerVisible , setInnerVisible ] = useState ( visible )
105
103
const [ showChildren , setShowChildren ] = useState ( true )
106
104
const [ transitionName , setTransitionName ] = useState ( '' )
107
105
108
- const shouldLockScroll = ! innerVisible ? false : lockScroll
109
- useLockScroll ( nodeRef , shouldLockScroll )
106
+ useLockScroll ( nodeRef , innerVisible && lockScroll )
110
107
111
108
const classPrefix = 'nut-popup'
112
- const baseStyle = {
113
- zIndex : index ,
114
- }
115
-
116
109
const overlayStyles = {
117
110
...overlayStyle ,
118
111
'--nutui-overlay-zIndex' : index ,
119
112
}
120
-
121
- const popStyles = {
122
- ...style ,
123
- ...baseStyle ,
124
- }
125
-
113
+ const popStyles = { ...style , zIndex : index }
126
114
const popClassName = classNames (
115
+ classPrefix ,
127
116
{
128
- [ `${ classPrefix } ` ] : true ,
129
117
[ `${ classPrefix } -round` ] : round || position === 'bottom' ,
130
118
[ `${ classPrefix } -${ position } ` ] : true ,
131
119
} ,
132
120
className
133
121
)
134
122
135
- const closeClasses = classNames ( {
136
- [ `${ classPrefix } -title-right` ] : true ,
137
- [ `${ classPrefix } -title-right-${ closeIconPosition } ` ] : true ,
138
- } )
139
-
140
123
const open = ( ) => {
141
124
if ( ! innerVisible ) {
142
125
setInnerVisible ( true )
@@ -160,42 +143,26 @@ export const Popup: FunctionComponent<
160
143
}
161
144
}
162
145
163
- const onHandleClickOverlay = ( e : MouseEvent ) => {
146
+ const handleOverlayClick = ( e : MouseEvent ) => {
164
147
e . stopPropagation ( )
165
- if ( closeOnOverlayClick ) {
166
- const closed = onOverlayClick && onOverlayClick ( e )
167
- closed && close ( )
148
+ if ( closeOnOverlayClick && onOverlayClick ( e ) ) {
149
+ close ( )
168
150
}
169
151
}
170
152
171
- const onHandleClick : MouseEventHandler < HTMLDivElement > = ( e : MouseEvent ) => {
172
- onClick && onClick ( e )
173
- }
174
-
175
- const onHandleClickCloseIcon : MouseEventHandler < HTMLDivElement > = (
176
- e : MouseEvent
177
- ) => {
178
- const closed = onCloseIconClick && onCloseIconClick ( e )
179
- closed && close ( )
180
- }
181
-
182
- const onHandleOpened : EnterHandler < HTMLElement | undefined > | undefined = (
183
- e : HTMLElement
184
- ) => {
185
- afterShow && afterShow ( )
186
- }
187
-
188
- const onHandleClosed : ExitHandler < HTMLElement | undefined > | undefined = (
189
- e : HTMLElement
190
- ) => {
191
- afterClose && afterClose ( )
153
+ const handleCloseIconClick = ( e : MouseEvent ) => {
154
+ onCloseIconClick ( e ) && close ( )
192
155
}
193
156
194
157
const renderCloseIcon = ( ) => {
158
+ const closeClasses = classNames (
159
+ `${ classPrefix } -title-right` ,
160
+ `${ classPrefix } -title-right-${ closeIconPosition } `
161
+ )
195
162
return (
196
163
< >
197
164
{ closeable && (
198
- < div className = { closeClasses } onClick = { onHandleClickCloseIcon } >
165
+ < div className = { closeClasses } onClick = { handleCloseIconClick } >
199
166
{ React . isValidElement ( closeIcon ) ? closeIcon : < Close /> }
200
167
</ div >
201
168
) }
@@ -245,17 +212,17 @@ export const Popup: FunctionComponent<
245
212
unmountOnExit = { destroyOnClose }
246
213
timeout = { duration }
247
214
in = { innerVisible }
248
- onEntered = { onHandleOpened }
249
- onExited = { onHandleClosed }
215
+ onEntered = { afterShow }
216
+ onExited = { afterClose }
250
217
>
251
218
< div
252
219
ref = { nodeRef }
253
220
style = { popStyles }
254
221
className = { popClassName }
255
- onClick = { onHandleClick }
222
+ onClick = { onClick }
256
223
>
257
224
{ renderTitle ( ) }
258
- { showChildren ? children : '' }
225
+ { showChildren ? children : null }
259
226
</ div >
260
227
</ CSSTransition >
261
228
)
@@ -264,17 +231,17 @@ export const Popup: FunctionComponent<
264
231
const renderNode = ( ) => {
265
232
return (
266
233
< >
267
- { overlay ? (
234
+ { overlay && (
268
235
< Overlay
269
236
style = { overlayStyles }
270
237
className = { overlayClassName }
271
238
visible = { innerVisible }
272
239
closeOnOverlayClick = { closeOnOverlayClick }
273
240
lockScroll = { lockScroll }
274
241
duration = { duration }
275
- onClick = { onHandleClickOverlay }
242
+ onClick = { handleOverlayClick }
276
243
/>
277
- ) : null }
244
+ ) }
278
245
{ renderPop ( ) }
279
246
</ >
280
247
)
@@ -288,11 +255,9 @@ export const Popup: FunctionComponent<
288
255
setTransitionName ( transition || `${ classPrefix } -slide-${ position } ` )
289
256
} , [ position , transition ] )
290
257
291
- const resolveContainer = ( getContainer : Teleport | undefined ) => {
292
- const container =
293
- typeof getContainer === 'function' ? getContainer ( ) : getContainer
294
- return container || document . body
295
- }
258
+ const resolveContainer = ( getContainer : Teleport | undefined ) =>
259
+ ( typeof getContainer === 'function' ? getContainer ( ) : getContainer ) ||
260
+ document . body
296
261
297
262
const renderToContainer = ( getContainer : Teleport , node : ReactElement ) => {
298
263
if ( getContainer ) {
0 commit comments