File tree Expand file tree Collapse file tree 5 files changed +37
-6
lines changed Expand file tree Collapse file tree 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
77``` xml
88<Modal
99 isOpen ={bool}
10- onRequestClose ={fn}
10+ onAfterOpen ={afterOpenFn}
11+ onRequestClose ={requestOpenFn}
1112 closeTimeoutMS ={n}
1213 style ={customStyle}
1314>
@@ -104,6 +105,11 @@ var App = React.createClass({
104105 this .setState ({modalIsOpen: true });
105106 },
106107
108+ afterOpenModal : function () {
109+ // references are now sync'd and can be accessed.
110+ this .refs .subtitle .style .color = ' #f00' ;
111+ },
112+
107113 closeModal : function () {
108114 this .setState ({modalIsOpen: false });
109115 },
@@ -114,10 +120,11 @@ var App = React.createClass({
114120 < button onClick= {this .openModal }> Open Modal< / button>
115121 < Modal
116122 isOpen= {this .state .modalIsOpen }
123+ onAfterOpen= {this .afterOpenModal }
117124 onRequestClose= {this .closeModal }
118125 style= {customStyles} >
119126
120- < h2> Hello< / h2>
127+ < h2 ref = " subtitle " > Hello< / h2>
121128 < button onClick= {this .closeModal }> close< / button>
122129 < div> I am a modal< / div>
123130 < form>
@@ -146,7 +153,8 @@ pass the 'shouldCloseOnOverlayClick' prop with 'false' value.
146153``` xml
147154<Modal
148155 isOpen ={bool}
149- onRequestClose ={fn}
156+ onAfterOpen ={afterOpenFn}
157+ onRequestClose ={requestCloseFn}
150158 closeTimeoutMS ={n}
151159 shouldCloseOnOverlayClick ={false}
152160 style ={customStyle}>
Original file line number Diff line number Diff line change @@ -60,17 +60,23 @@ var App = React.createClass({
6060 this . setState ( { foo : 'bar' } ) ;
6161 } ,
6262
63+ handleOnAfterOpenModal : function ( ) {
64+ // when ready, we can access the available refs.
65+ this . refs . title . style . color = '#F00' ;
66+ } ,
67+
6368 render : function ( ) {
6469 return (
6570 < div >
6671 < button onClick = { this . openModal } > Open Modal</ button >
6772 < button onClick = { this . openModal2 } > Open Modal 2</ button >
6873 < Modal
74+ ref = "mymodal"
6975 closeTimeoutMS = { 150 }
7076 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 >
7480 < button onClick = { this . closeModal } > close</ button >
7581 < div > I am a modal</ div >
7682 < form >
Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ var Modal = React.createClass({
3131 overlay : React . PropTypes . object
3232 } ) ,
3333 appElement : React . PropTypes . instanceOf ( SafeHTMLElement ) ,
34+ onAfterOpen : React . PropTypes . func ,
3435 onRequestClose : React . PropTypes . func ,
3536 closeTimeoutMS : React . PropTypes . number ,
3637 ariaHideApp : React . PropTypes . bool ,
Original file line number Diff line number Diff line change @@ -76,6 +76,10 @@ var ModalPortal = module.exports = React.createClass({
7676 focusManager . markForFocusLater ( ) ;
7777 this . setState ( { isOpen : true } , function ( ) {
7878 this . setState ( { afterOpen : true } ) ;
79+
80+ if ( this . props . isOpen && this . props . onAfterOpen ) {
81+ this . props . onAfterOpen ( ) ;
82+ }
7983 } . bind ( this ) ) ;
8084 } ,
8185
Original file line number Diff line number Diff line change @@ -201,6 +201,18 @@ describe('Modal', function () {
201201 unmountModal ( ) ;
202202 } ) ;
203203
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+
204216 describe ( 'should close on overlay click' , function ( ) {
205217 afterEach ( 'Unmount modal' , function ( ) {
206218 unmountModal ( ) ;
You can’t perform that action at this time.
0 commit comments