1
1
import * as React from "react" ;
2
2
import { useEffect , useState } from 'react' ;
3
3
import styles from './AnimatedDialog.module.scss' ;
4
- import { Dialog , IDialogProps } from 'office-ui-fabric-react/lib/Dialog' ;
4
+ import { Dialog , IDialogProps , IDialogContentProps } from 'office-ui-fabric-react/lib/Dialog' ;
5
5
import { Icon } from 'office-ui-fabric-react/lib/Icon' ;
6
6
import { DefaultButton , PrimaryButton } from 'office-ui-fabric-react/lib/Button' ;
7
7
import { Spinner , SpinnerSize } from 'office-ui-fabric-react/lib/Spinner' ;
@@ -22,32 +22,56 @@ export interface IAnimatedDialogProps extends IDialogProps {
22
22
onError ?: ( ) => void ;
23
23
}
24
24
25
+ const animationPrefix : string = `animate__` ;
26
+ const mainAnimationClass : string = `${ animationPrefix } animated` ;
27
+ const defaultDialogAnimationClass : string = `${ animationPrefix } bounceIn` ;
28
+ const defaultIconAnimationClass : string = `${ animationPrefix } zoomIn` ;
29
+
25
30
export function AnimatedDialog ( props : React . PropsWithChildren < IAnimatedDialogProps > ) {
26
31
27
32
const [ dialogProps , setDialogProps ] = useState < IDialogProps > ( props ) ;
33
+ const [ animatedDialogContentProps , setAnimatedDialogContentProps ] = useState < IDialogContentProps > ( props . dialogContentProps ) ;
34
+ const [ animatedDialogFooter , setAnimatedDialogFooter ] = useState < JSX . Element > ( null ) ;
28
35
const [ loading , setLoading ] = useState < boolean > ( false ) ;
29
36
30
37
const { dialogAnimationInType, dialogAnimationOutType,
31
38
iconName, iconAnimationType,
32
39
modalProps, dialogContentProps,
33
40
showAnimatedDialogFooter, okButtonText, cancelButtonText } = props ;
34
41
35
-
36
- const animationPrefix : string = `animate__` ;
37
- const mainAnimationClass : string = `${ animationPrefix } animated` ;
38
42
const currentContainerClass : string = modalProps && modalProps . containerClassName ;
39
43
const containerAnimationClass : string = `${ currentContainerClass } ${ mainAnimationClass } ${ animationPrefix } fast` ;
40
- const defaultDialogAnimationClass : string = `${ animationPrefix } bounceIn` ;
41
- const defaultIconAnimationClass : string = `${ animationPrefix } zoomIn` ;
42
44
43
- useEffect ( ( ) => {
45
+ const getAnimatedDialogFooter = ( ) : JSX . Element => {
46
+ return showAnimatedDialogFooter ?
47
+ < div className = { styles . animatedDialogFooter } >
48
+ < PrimaryButton
49
+ onClick = { async ( ) => {
50
+ setLoading ( true ) ;
51
+ try {
52
+ await props . onOkClick ( ) ;
53
+ props . onSuccess ( ) ;
54
+ } catch ( error ) {
55
+ props . onError ( ) ;
56
+ }
57
+ setLoading ( false ) ;
58
+ } }
59
+ disabled = { loading }
60
+ text = { ! loading && ( okButtonText ? okButtonText : "Ok" ) }
61
+ iconProps = { ! loading && { iconName : 'CheckMark' } } >
62
+ { loading && < Spinner size = { SpinnerSize . medium } /> }
63
+ </ PrimaryButton >
44
64
45
- let containerClassName : string = `${ containerAnimationClass } ${ defaultDialogAnimationClass } ` ;
46
- let title : string | JSX . Element = dialogContentProps && dialogContentProps . title ;
65
+ < DefaultButton
66
+ onClick = { props . onDismiss }
67
+ text = { cancelButtonText ? cancelButtonText : "Cancel" }
68
+ disabled = { loading }
69
+ iconProps = { { iconName : 'Cancel' } } />
70
+ </ div > : null ;
71
+ } ;
47
72
48
- if ( props . dialogAnimationInType ) {
49
- containerClassName = `${ containerAnimationClass } ${ animationPrefix } ${ dialogAnimationInType } ` ;
50
- }
73
+ useEffect ( ( ) => {
74
+ let title : string | JSX . Element = dialogContentProps && dialogContentProps . title ;
51
75
52
76
if ( iconName ) {
53
77
title =
@@ -62,6 +86,21 @@ export function AnimatedDialog(props: React.PropsWithChildren<IAnimatedDialogPro
62
86
</ div > ;
63
87
}
64
88
89
+ setAnimatedDialogContentProps ( { ...dialogContentProps , title } ) ;
90
+ } , [ ] ) ;
91
+
92
+ useEffect ( ( ) => {
93
+ setAnimatedDialogFooter ( getAnimatedDialogFooter ( ) ) ;
94
+ } , [ loading ] ) ;
95
+
96
+ useEffect ( ( ) => {
97
+
98
+ let containerClassName : string = `${ containerAnimationClass } ${ defaultDialogAnimationClass } ` ;
99
+
100
+ if ( props . dialogAnimationInType ) {
101
+ containerClassName = `${ containerAnimationClass } ${ animationPrefix } ${ dialogAnimationInType } ` ;
102
+ }
103
+
65
104
if ( props . hidden ) {
66
105
containerClassName = `${ containerAnimationClass } ` ;
67
106
containerClassName += dialogAnimationOutType ?
@@ -71,47 +110,17 @@ export function AnimatedDialog(props: React.PropsWithChildren<IAnimatedDialogPro
71
110
72
111
setDialogProps ( {
73
112
...props ,
74
- modalProps : { ... modalProps , containerClassName } ,
75
- dialogContentProps : { ...dialogContentProps , title }
113
+ dialogContentProps : animatedDialogContentProps ,
114
+ modalProps : { ...modalProps , containerClassName }
76
115
} ) ;
77
116
78
117
79
118
} , [ props . hidden ] ) ;
80
119
81
- const animatedDialogFooter : JSX . Element =
82
- showAnimatedDialogFooter ?
83
- < div className = { styles . animatedDialogFooter } >
84
- < PrimaryButton
85
- onClick = { async ( ) => {
86
- setLoading ( true ) ;
87
- try {
88
- await props . onOkClick ( ) ;
89
- props . onSuccess ( ) ;
90
- } catch ( error ) {
91
- props . onError ( ) ;
92
- }
93
- setLoading ( false ) ;
94
- } }
95
- disabled = { loading }
96
- text = { ! loading && ( okButtonText ? okButtonText : "Ok" ) }
97
- iconProps = { ! loading && { iconName : 'CheckMark' } } >
98
- { loading && < Spinner size = { SpinnerSize . medium } /> }
99
- </ PrimaryButton >
100
-
101
- < DefaultButton
102
- onClick = { props . onDismiss }
103
- text = { cancelButtonText ? cancelButtonText : "Cancel" }
104
- disabled = { loading }
105
- iconProps = { { iconName : 'Cancel' } } />
106
- </ div > :
107
- null ;
108
-
109
120
return (
110
121
< Dialog { ...dialogProps } >
111
122
{ props . children }
112
- {
113
- showAnimatedDialogFooter && animatedDialogFooter
114
- }
123
+ { animatedDialogFooter }
115
124
</ Dialog >
116
125
) ;
117
126
}
0 commit comments