1
+ import * as classNames from 'classnames' ;
2
+ import * as Modal from 'react-modal' ;
1
3
import * as React from 'react' ;
2
4
import * as ReactDOM from 'react-dom' ;
3
5
import { Provider } from 'react-redux' ;
4
- import * as Modal from 'react-modal' ;
5
6
import { Router } from 'react-router-dom' ;
6
7
import { CompatRouter } from 'react-router-dom-v5-compat' ;
7
- import * as classNames from 'classnames' ;
8
- import * as _ from 'lodash-es' ;
9
- import { ActionGroup , Button , Text , TextContent , TextVariants } from '@patternfly/react-core' ;
10
8
import { useTranslation } from 'react-i18next' ;
11
- import i18next from 'i18next' ;
9
+ import { ActionGroup , Button , Text , TextContent , TextVariants } from '@patternfly/react-core' ;
10
+ import { ModalWrapperProps } from '@console/dynamic-plugin-sdk/src/api/internal-types' ;
12
11
import CloseButton from '@console/shared/src/components/close-button' ;
13
-
14
12
import store from '../../redux' ;
15
13
import { ButtonBar } from '../utils/button-bar' ;
16
14
import { history } from '../utils/router' ;
17
15
18
- export const createModal : CreateModal = ( getModalContainer ) => {
19
- const modalContainer = document . getElementById ( 'modal-container' ) ;
16
+ export const createModal : CreateModal = ( getModalElement ) => {
17
+ const containerElement = document . getElementById ( 'modal-container' ) ;
20
18
const result = new Promise < void > ( ( resolve ) => {
21
19
const closeModal = ( e ?: React . SyntheticEvent ) => {
22
20
if ( e && e . stopPropagation ) {
23
21
e . stopPropagation ( ) ;
24
22
}
25
- ReactDOM . unmountComponentAtNode ( modalContainer ) ;
23
+ ReactDOM . unmountComponentAtNode ( containerElement ) ;
26
24
resolve ( ) ;
27
25
} ;
28
26
Modal . setAppElement ( document . getElementById ( 'app-content' ) ) ;
29
- modalContainer && ReactDOM . render ( getModalContainer ( closeModal ) , modalContainer ) ;
27
+ containerElement && ReactDOM . render ( getModalElement ( closeModal ) , containerElement ) ;
30
28
} ) ;
31
29
return { result } ;
32
30
} ;
33
31
34
- export const createModalLauncher : CreateModalLauncher = ( Component , modalWrapper = true ) => (
35
- props ,
36
- ) => {
32
+ export const ModalWrapper : React . FC < ModalWrapperProps > = ( {
33
+ blocking,
34
+ className,
35
+ children,
36
+ onClose,
37
+ } ) => {
38
+ const { t } = useTranslation ( ) ;
39
+ const parentSelector = React . useCallback ( ( ) => document . querySelector ( '#modal-container' ) , [ ] ) ;
40
+ return (
41
+ < Modal
42
+ className = { classNames ( 'modal-dialog' , className ) }
43
+ contentLabel = { t ( 'public~Modal' ) }
44
+ isOpen
45
+ onRequestClose = { onClose }
46
+ overlayClassName = "co-overlay"
47
+ parentSelector = { parentSelector }
48
+ shouldCloseOnOverlayClick = { ! blocking }
49
+ >
50
+ { children }
51
+ </ Modal >
52
+ ) ;
53
+ } ;
54
+
55
+ export const createModalLauncher : CreateModalLauncher = ( Component , modalWrapper = true ) => ( {
56
+ blocking,
57
+ modalClassName,
58
+ close,
59
+ cancel,
60
+ ...props
61
+ } ) => {
37
62
const getModalContainer : GetModalContainer = ( onClose ) => {
38
- const _handleClose = ( e : React . SyntheticEvent ) => {
39
- onClose && onClose ( e ) ;
40
- props . close && props . close ( ) ;
63
+ const handleClose = ( e : React . SyntheticEvent ) => {
64
+ onClose ?. ( e ) ;
65
+ close ?. ( ) ;
41
66
} ;
42
- const _handleCancel = ( e : React . SyntheticEvent ) => {
43
- props . cancel && props . cancel ( ) ;
44
- _handleClose ( e ) ;
67
+ const handleCancel = ( e : React . SyntheticEvent ) => {
68
+ cancel ?. ( ) ;
69
+ handleClose ( e ) ;
45
70
} ;
46
71
47
72
return (
48
73
< Provider store = { store } >
49
74
< Router { ...{ history, basename : window . SERVER_FLAGS . basePath } } >
50
75
< CompatRouter >
51
76
{ modalWrapper ? (
52
- < Modal
53
- isOpen = { true }
54
- contentLabel = { i18next . t ( 'public~Modal' ) }
55
- onRequestClose = { _handleClose }
56
- className = { classNames ( 'modal-dialog' , props . modalClassName ) }
57
- overlayClassName = "co-overlay"
58
- shouldCloseOnOverlayClick = { ! props . blocking }
59
- parentSelector = { ( ) => document . getElementById ( 'modal-container' ) }
60
- >
61
- < Component
62
- { ...( _ . omit ( props , 'blocking' , 'modalClassName' ) as any ) }
63
- cancel = { _handleCancel }
64
- close = { _handleClose }
65
- />
66
- </ Modal >
77
+ < ModalWrapper blocking = { blocking } className = { modalClassName } onClose = { handleClose } >
78
+ < Component { ...( props as any ) } cancel = { handleCancel } close = { handleClose } />
79
+ </ ModalWrapper >
67
80
) : (
68
- < Component
69
- { ...( _ . omit ( props , 'blocking' , 'modalClassName' ) as any ) }
70
- cancel = { _handleCancel }
71
- close = { _handleClose }
72
- />
81
+ < Component { ...( props as any ) } cancel = { handleCancel } close = { handleClose } />
73
82
) }
74
83
</ CompatRouter >
75
84
</ Router >
@@ -79,7 +88,7 @@ export const createModalLauncher: CreateModalLauncher = (Component, modalWrapper
79
88
return createModal ( getModalContainer ) ;
80
89
} ;
81
90
82
- export const ModalTitle : React . SFC < ModalTitleProps > = ( {
91
+ export const ModalTitle : React . FC < ModalTitleProps > = ( {
83
92
children,
84
93
className = 'modal-header' ,
85
94
close,
@@ -102,13 +111,13 @@ export const ModalTitle: React.SFC<ModalTitleProps> = ({
102
111
</ div >
103
112
) ;
104
113
105
- export const ModalBody : React . SFC < ModalBodyProps > = ( { children } ) => (
114
+ export const ModalBody : React . FC < ModalBodyProps > = ( { children } ) => (
106
115
< div className = "modal-body" >
107
116
< div className = "modal-body-content" > { children } </ div >
108
117
</ div >
109
118
) ;
110
119
111
- export const ModalFooter : React . SFC < ModalFooterProps > = ( {
120
+ export const ModalFooter : React . FC < ModalFooterProps > = ( {
112
121
message,
113
122
errorMessage,
114
123
inProgress,
@@ -126,7 +135,7 @@ export const ModalFooter: React.SFC<ModalFooterProps> = ({
126
135
) ;
127
136
} ;
128
137
129
- export const ModalSubmitFooter : React . SFC < ModalSubmitFooterProps > = ( {
138
+ export const ModalSubmitFooter : React . FC < ModalSubmitFooterProps > = ( {
130
139
message,
131
140
errorMessage,
132
141
inProgress,
@@ -137,7 +146,7 @@ export const ModalSubmitFooter: React.SFC<ModalSubmitFooterProps> = ({
137
146
submitDisabled,
138
147
submitDanger,
139
148
buttonAlignment = 'right' ,
140
- resetText = i18next . t ( 'public~Reset' ) ,
149
+ resetText,
141
150
reset,
142
151
} ) => {
143
152
const { t } = useTranslation ( ) ;
@@ -163,31 +172,21 @@ export const ModalSubmitFooter: React.SFC<ModalSubmitFooterProps> = ({
163
172
</ Button >
164
173
) ;
165
174
166
- const submitButton = submitDanger ? (
175
+ const submitButton = (
167
176
< Button
168
- type = "submit"
169
- variant = "danger"
170
- isDisabled = { submitDisabled }
171
177
data-test = "confirm-action"
172
178
id = "confirm-action"
173
- >
174
- { submitText }
175
- </ Button >
176
- ) : (
177
- < Button
178
- type = "submit"
179
- variant = "primary"
180
179
isDisabled = { submitDisabled }
181
- data-test = "confirm-action "
182
- id = "confirm-action"
180
+ type = "submit "
181
+ variant = { submitDanger ? 'danger' : 'primary' }
183
182
>
184
- { submitText }
183
+ { submitText || t ( 'public~Submit' ) }
185
184
</ Button >
186
185
) ;
187
186
188
187
const resetButton = (
189
188
< Button variant = "link" isInline onClick = { onResetClick } id = "reset-action" >
190
- { resetText }
189
+ { resetText || t ( 'public~Reset' ) }
191
190
</ Button >
192
191
) ;
193
192
0 commit comments