File tree 5 files changed +37
-6
lines changed
5 files changed +37
-6
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,8 @@ Accessible modal dialog component for React.JS
7
7
``` xml
8
8
<Modal
9
9
isOpen ={bool}
10
- onRequestClose ={fn}
10
+ onAfterOpen ={afterOpenFn}
11
+ onRequestClose ={requestOpenFn}
11
12
closeTimeoutMS ={n}
12
13
style ={customStyle}
13
14
>
@@ -104,6 +105,11 @@ var App = React.createClass({
104
105
this .setState ({modalIsOpen: true });
105
106
},
106
107
108
+ afterOpenModal : function () {
109
+ // references are now sync'd and can be accessed.
110
+ this .refs .subtitle .style .color = ' #f00' ;
111
+ },
112
+
107
113
closeModal : function () {
108
114
this .setState ({modalIsOpen: false });
109
115
},
@@ -114,10 +120,11 @@ var App = React.createClass({
114
120
< button onClick= {this .openModal }> Open Modal< / button>
115
121
< Modal
116
122
isOpen= {this .state .modalIsOpen }
123
+ onAfterOpen= {this .afterOpenModal }
117
124
onRequestClose= {this .closeModal }
118
125
style= {customStyles} >
119
126
120
- < h2> Hello< / h2>
127
+ < h2 ref = " subtitle " > Hello< / h2>
121
128
< button onClick= {this .closeModal }> close< / button>
122
129
< div> I am a modal< / div>
123
130
< form>
@@ -146,7 +153,8 @@ pass the 'shouldCloseOnOverlayClick' prop with 'false' value.
146
153
``` xml
147
154
<Modal
148
155
isOpen ={bool}
149
- onRequestClose ={fn}
156
+ onAfterOpen ={afterOpenFn}
157
+ onRequestClose ={requestCloseFn}
150
158
closeTimeoutMS ={n}
151
159
shouldCloseOnOverlayClick ={false}
152
160
style ={customStyle}>
Original file line number Diff line number Diff line change @@ -60,17 +60,23 @@ var App = React.createClass({
60
60
this . setState ( { foo : 'bar' } ) ;
61
61
} ,
62
62
63
+ handleOnAfterOpenModal : function ( ) {
64
+ // when ready, we can access the available refs.
65
+ this . refs . title . style . color = '#F00' ;
66
+ } ,
67
+
63
68
render : function ( ) {
64
69
return (
65
70
< div >
66
71
< button onClick = { this . openModal } > Open Modal</ button >
67
72
< button onClick = { this . openModal2 } > Open Modal 2</ button >
68
73
< Modal
74
+ ref = "mymodal"
69
75
closeTimeoutMS = { 150 }
70
76
isOpen = { this . state . modalIsOpen }
71
- onRequestClose = { this . handleModalCloseRequest }
72
- >
73
- < h1 > Hello</ h1 >
77
+ onAfterOpen = { this . handleOnAfterOpenModal }
78
+ onRequestClose = { this . handleModalCloseRequest } >
79
+ < h1 ref = "title" > Hello</ h1 >
74
80
< button onClick = { this . closeModal } > close</ button >
75
81
< div > I am a modal</ div >
76
82
< form >
Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ var Modal = React.createClass({
31
31
overlay : React . PropTypes . object
32
32
} ) ,
33
33
appElement : React . PropTypes . instanceOf ( SafeHTMLElement ) ,
34
+ onAfterOpen : React . PropTypes . func ,
34
35
onRequestClose : React . PropTypes . func ,
35
36
closeTimeoutMS : React . PropTypes . number ,
36
37
ariaHideApp : React . PropTypes . bool ,
Original file line number Diff line number Diff line change @@ -76,6 +76,10 @@ var ModalPortal = module.exports = React.createClass({
76
76
focusManager . markForFocusLater ( ) ;
77
77
this . setState ( { isOpen : true } , function ( ) {
78
78
this . setState ( { afterOpen : true } ) ;
79
+
80
+ if ( this . props . isOpen && this . props . onAfterOpen ) {
81
+ this . props . onAfterOpen ( ) ;
82
+ }
79
83
} . bind ( this ) ) ;
80
84
} ,
81
85
Original file line number Diff line number Diff line change @@ -201,6 +201,18 @@ describe('Modal', function () {
201
201
unmountModal ( ) ;
202
202
} ) ;
203
203
204
+ it ( 'should trigger the onAfterOpen callback' , function ( ) {
205
+ var afterOpenCallback = sinon . spy ( ) ;
206
+ var modal = renderModal ( {
207
+ isOpen : true ,
208
+ onAfterOpen : function ( ) {
209
+ afterOpenCallback ( ) ;
210
+ }
211
+ } ) ;
212
+ ok ( afterOpenCallback . called ) ;
213
+ unmountModal ( ) ;
214
+ } ) ;
215
+
204
216
describe ( 'should close on overlay click' , function ( ) {
205
217
afterEach ( 'Unmount modal' , function ( ) {
206
218
unmountModal ( ) ;
You can’t perform that action at this time.
0 commit comments