@@ -14,6 +14,14 @@ interface State {
1414 loading : boolean ;
1515}
1616
17+ interface AskModalOptions {
18+ title ?: string ;
19+ message ?: string ;
20+ onDone ?: ( success : boolean ) => Promise < void > ;
21+ yesText ?: string ;
22+ noText ?: string ;
23+ }
24+
1725@autoBindMethodsForReact ( AUTOBIND_CFG )
1826class AskModal extends PureComponent < { } , State > {
1927 state : State = {
@@ -26,8 +34,9 @@ class AskModal extends PureComponent<{}, State> {
2634
2735 modal : Modal | null = null ;
2836 yesButton : HTMLButtonElement | null = null ;
29- _doneCallback : Function = ( ) => { } ;
30- _promiseCallback : Function = ( ) => { } ;
37+
38+ _doneCallback : AskModalOptions [ 'onDone' ] ;
39+ _promiseCallback : ( value : boolean | PromiseLike < boolean > ) => void = ( ) => { } ;
3140
3241 _setModalRef ( m : Modal ) {
3342 this . modal = m ;
@@ -42,19 +51,17 @@ class AskModal extends PureComponent<{}, State> {
4251 loading : true ,
4352 } ) ;
4453
45- if ( this . _doneCallback ) {
46- // Wait for the callback to finish before closing
47- await this . _doneCallback ( true ) ;
48- }
54+ // Wait for the callback to finish before closing
55+ await this . _doneCallback ?.( true ) ;
4956
5057 this . _promiseCallback ( true ) ;
5158
5259 this . hide ( ) ;
5360 }
5461
55- _handleNo ( ) {
62+ async _handleNo ( ) {
5663 this . hide ( ) ;
57- this ? ._doneCallback ( false ) ;
64+ await this . _doneCallback ?. ( false ) ;
5865
5966 this . _promiseCallback ( false ) ;
6067 }
@@ -63,8 +70,7 @@ class AskModal extends PureComponent<{}, State> {
6370 this . modal ?. hide ( ) ;
6471 }
6572
66- show ( options : any = { } ) {
67- const { title, message, onDone, yesText, noText } = options ;
73+ show ( { title, message, onDone, yesText, noText } : AskModalOptions = { } ) {
6874 this . _doneCallback = onDone ;
6975 this . setState ( {
7076 title : title || 'Confirm' ,
@@ -74,10 +80,12 @@ class AskModal extends PureComponent<{}, State> {
7480 loading : false ,
7581 } ) ;
7682 this . modal ?. show ( ) ;
83+
7784 setTimeout ( ( ) => {
7885 this . yesButton && this . yesButton . focus ( ) ;
7986 } , 100 ) ;
80- return new Promise ( resolve => {
87+
88+ return new Promise < boolean > ( resolve => {
8189 this . _promiseCallback = resolve ;
8290 } ) ;
8391 }
0 commit comments