@@ -4,83 +4,94 @@ import {
4
4
StyleSheet ,
5
5
ViewProps ,
6
6
} from 'react-native' ;
7
- import { ModalService } from '../../service' ;
7
+ import {
8
+ ModalPresenting ,
9
+ ModalService ,
10
+ } from '../../service' ;
8
11
import { Modal } from '../../../ui/modal/modal.component' ;
12
+ import { ModalComponentCloseProps } from '@kitten/theme' ;
9
13
10
14
export interface ModalPanelProps {
11
15
children : React . ReactElement < any > | React . ReactElement < any > [ ] ;
12
16
}
13
17
14
18
interface ModalPanelState {
15
- dialogComponents : Map < string , React . ReactElement < any > > ;
16
- backdropValues : Map < string , boolean > ;
19
+ components : Map < string , React . ReactElement < ModalComponentCloseProps > > ;
20
+ backdrops : Map < string , boolean > ;
17
21
}
18
22
19
- export class ModalPanel extends React . Component < ModalPanelProps , ModalPanelState > {
23
+ export class ModalPanel extends React . Component < ModalPanelProps , ModalPanelState > implements ModalPresenting {
20
24
21
25
public state : ModalPanelState = {
22
- dialogComponents : new Map ( ) ,
23
- backdropValues : new Map ( ) ,
26
+ components : new Map ( ) ,
27
+ backdrops : new Map ( ) ,
24
28
} ;
25
29
26
30
public componentDidMount ( ) : void {
27
- ModalService . setComponent ( this ) ;
31
+ ModalService . mount ( this ) ;
28
32
}
29
33
30
34
public componentWillUnmount ( ) : void {
31
- ModalService . setComponent ( null ) ;
35
+ ModalService . unmount ( ) ;
32
36
}
33
37
34
- public onCloseModal = ( identifier : string ) => {
35
- const components : Map < string , React . ReactElement < any > > = this . state . dialogComponents ;
38
+ public hide = ( identifier : string ) : void => {
39
+ const component : React . ReactElement < ModalComponentCloseProps > = this . state . components
40
+ . get ( identifier ) ;
41
+ if ( component ) {
42
+ component . props . onRequestClose && component . props . onRequestClose ( ) ;
43
+ }
44
+ const components : Map < string , React . ReactElement < any > > = this . state . components ;
36
45
components . delete ( identifier ) ;
37
- const backdropValues : Map < string , boolean > = this . state . backdropValues ;
38
- backdropValues . delete ( identifier ) ;
46
+ const backdrops : Map < string , boolean > = this . state . backdrops ;
47
+ backdrops . delete ( identifier ) ;
39
48
this . setState ( {
40
- dialogComponents : components ,
41
- backdropValues : backdropValues ,
49
+ components : components ,
50
+ backdrops : backdrops ,
42
51
} ) ;
43
52
} ;
44
53
45
- public showDialog ( dialogComponent : React . ReactElement < any > , closeOnBackDrop : boolean ) : void {
54
+ public show ( dialogComponent : React . ReactElement < any > , closeOnBackDrop : boolean ) : string {
46
55
const key : string = this . generateUniqueComponentKey ( ) ;
47
- const componentsMap : Map < string , React . ReactElement < any > > = this . state . dialogComponents
56
+ const componentsMap : Map < string , React . ReactElement < any > > = this . state . components
48
57
. set ( key , dialogComponent ) ;
49
- const backdropsMap : Map < string , boolean > = this . state . backdropValues . set ( key , closeOnBackDrop ) ;
58
+ const backdrops : Map < string , boolean > = this . state . backdrops . set ( key , closeOnBackDrop ) ;
50
59
this . setState ( {
51
- dialogComponents : componentsMap ,
52
- backdropValues : backdropsMap ,
60
+ components : componentsMap ,
61
+ backdrops : backdrops ,
53
62
} ) ;
63
+ return key ;
54
64
}
55
65
56
66
private generateUniqueComponentKey = ( ) : string => {
57
67
return Math . random ( ) . toString ( 36 ) . substring ( 2 ) ;
58
68
} ;
59
69
60
70
private areThereAnyComponents ( ) : boolean {
61
- return this . state . dialogComponents && this . state . dialogComponents . size !== 0 ;
71
+ return this . state . components && this . state . components . size !== 0 ;
62
72
}
63
73
64
74
private renderModal ( modal : React . ReactElement < any > , index : number ) {
65
- const allModalKeys : string [ ] = Array . from ( this . state . dialogComponents . keys ( ) ) ;
75
+ const allModalKeys : string [ ] = Array . from ( this . state . components . keys ( ) ) ;
66
76
const identifier : string = allModalKeys
67
- . find ( item => this . state . dialogComponents . get ( item ) === modal ) ;
68
- const closeOnBackdrop : boolean = this . state . backdropValues . get ( identifier ) ;
77
+ . find ( item => this . state . components . get ( item ) === modal ) ;
78
+ const closeOnBackdrop : boolean = this . state . backdrops . get ( identifier ) ;
69
79
return (
70
80
< Modal
81
+ { ...modal . props }
71
82
visible = { true }
72
83
isBackDropAllowed = { closeOnBackdrop }
73
84
key = { index }
74
85
identifier = { identifier }
75
- onCloseModal = { this . onCloseModal }
86
+ onCloseModal = { this . hide }
76
87
>
77
88
{ modal }
78
89
</ Modal >
79
90
) ;
80
91
}
81
92
82
93
private renderModals ( ) {
83
- return Array . from ( this . state . dialogComponents . values ( ) )
94
+ return Array . from ( this . state . components . values ( ) )
84
95
. map ( ( component : React . ReactElement < any > , i : number ) =>
85
96
this . renderModal ( component , i ) ) ;
86
97
}
0 commit comments