@@ -6,28 +6,70 @@ import assign from 'object-assign'
6
6
7
7
import {
8
8
containerStyle ,
9
+ containerTopStyle ,
10
+ containerBottomStyle ,
11
+ containerDefaultStyle ,
9
12
contentBaseStyle ,
10
13
contentSuccessStyle ,
11
14
contentErrorStyle ,
12
15
contentWarningStyle ,
13
- contentDefaultStyle ,
14
- transformShowStyle ,
15
- transformHideStyle
16
+ contentInfoStyle ,
17
+ animateDownStyleToHide ,
18
+ animateDownStyleToShow ,
19
+ animateUpStyleToHide ,
20
+ animateUpStyleToShow ,
21
+ animateFadeStyleToHide ,
22
+ animateFadeStyleToShow
16
23
} from './styles'
17
24
import {
25
+ wrapper ,
26
+ duration ,
18
27
options
19
28
} from './config'
20
29
21
30
class Toast extends React . Component {
22
- constructor ( ) {
23
- super ( )
31
+ constructor ( props ) {
32
+ super ( props )
24
33
this . state = {
25
- contentStyleExt : null
34
+ containerStyleExt : this . setContainerStyle ( ) . container
26
35
}
27
36
}
28
-
37
+ // set container style
38
+ setContainerStyle ( ) {
39
+ // create style object
40
+ let style = { }
41
+ style . animate = { }
42
+ // switch position
43
+ switch ( this . props . position ) {
44
+ case 'top' :
45
+ style . container = assign ( { } , containerStyle , containerTopStyle )
46
+ style . animate . show = assign ( { } , animateDownStyleToShow )
47
+ style . animate . hide = assign ( { } , animateDownStyleToHide )
48
+ break
49
+ case 'bottom' :
50
+ style . container = assign ( { } , containerStyle , containerBottomStyle )
51
+ style . animate . show = assign ( { } , animateUpStyleToShow )
52
+ style . animate . hide = assign ( { } , animateUpStyleToHide )
53
+ break
54
+ case 'default' :
55
+ style . container = assign ( { } , containerStyle , containerDefaultStyle )
56
+ style . animate . show = assign ( { } , animateFadeStyleToShow )
57
+ style . animate . hide = assign ( { } , animateFadeStyleToHide )
58
+ break
59
+ default :
60
+ style . container = assign ( { } , containerStyle , containerDefaultStyle )
61
+ style . animate . show = assign ( { } , animateFadeStyleToShow )
62
+ style . animate . hide = assign ( { } , animateFadeStyleToHide )
63
+ break
64
+ }
65
+ // return style
66
+ return style
67
+ }
68
+ // set content style
29
69
setContentStyle ( ) {
70
+ // create style object
30
71
let style = { }
72
+ // switch type
31
73
switch ( this . props . type ) {
32
74
case 'success' :
33
75
style = assign ( { } , contentBaseStyle , contentSuccessStyle )
@@ -38,50 +80,56 @@ class Toast extends React.Component {
38
80
case 'warning' :
39
81
style = assign ( { } , contentBaseStyle , contentWarningStyle )
40
82
break
83
+ case 'info' :
84
+ style = assign ( { } , contentBaseStyle , contentInfoStyle )
85
+ break
41
86
default :
42
87
style = assign ( { } , contentBaseStyle )
43
88
break
44
89
}
90
+ // if set backgroundColor attr
91
+ if ( this . props . backgroundColor ) {
92
+ style = assign ( { } , style , { backgroundColor : this . props . backgroundColor } )
93
+ }
94
+ // if set textColor attr
95
+ if ( this . props . textColor ) {
96
+ style = assign ( { } , style , { color : this . props . textColor } )
97
+ }
98
+ // return style
45
99
return style
46
100
}
47
-
48
101
// after component mount
49
102
componentDidMount ( ) {
50
- // set container base style
51
- this . setState ( {
52
- contentStyleExt : containerStyle
53
- } )
54
- // show toast effect style
103
+ let _containerStyle = this . setContainerStyle ( )
104
+ // // show toast effect style
55
105
setTimeout ( ( ) => {
56
106
this . setState ( {
57
- contentStyleExt : assign ( { } , containerStyle , transformShowStyle )
107
+ containerStyleExt : assign ( { } , _containerStyle . container , _containerStyle . animate . show )
58
108
} )
59
109
} , 100 )
60
110
// hide toast effect style, do it after timeout
61
111
setTimeout ( ( ) => {
62
112
this . setState ( {
63
- contentStyleExt : assign ( { } , containerStyle , transformHideStyle )
113
+ containerStyleExt : assign ( { } , _containerStyle . container , _containerStyle . animate . hide )
64
114
} )
65
- } , options . defaultTimeout )
115
+ } , this . props . timeout )
66
116
}
67
117
68
118
// render component
69
119
render ( ) {
70
120
let { text} = this . props
71
- let contentStyle = this . setContentStyle ( )
121
+ let _contentStyle = this . setContentStyle ( )
72
122
return (
73
- < div style = { this . state . contentStyleExt } >
74
- < span style = { contentStyle } > { text } </ span >
123
+ < div style = { this . state . containerStyleExt } >
124
+ < span style = { _contentStyle } > { text } </ span >
75
125
</ div >
76
126
)
77
127
}
78
128
}
79
129
// component prop types
80
130
Toast . PropTypes = {
81
131
text : PropTypes . string ,
82
- timeout : PropTypes . number ,
83
132
type : PropTypes . string ,
84
- color : PropTypes . object ,
85
133
style : PropTypes . oneOfType ( [
86
134
PropTypes . object ,
87
135
PropTypes . bool
@@ -92,33 +140,34 @@ Toast.PropTypes = {
92
140
export default class extends React . Component {
93
141
render ( ) {
94
142
return (
95
- < div id = { options . wrapperId } > </ div >
143
+ < div id = { wrapper } > </ div >
96
144
) ;
97
145
}
98
146
}
99
147
100
148
// mount toast to wrapper dom
101
- function mountToast ( text , type ) {
149
+ function mountToast ( text , type , config ) {
102
150
ReactDOM . render (
103
- < Toast text = { text } type = { type } /> ,
104
- document . getElementById ( options . wrapperId )
151
+ < Toast text = { text } type = { type } { ... config } /> ,
152
+ document . getElementById ( wrapper )
105
153
) ;
106
154
}
107
155
108
156
// un mount toast to wrapper dom
109
157
function umountToast ( ) {
110
- ReactDOM . unmountComponentAtNode ( document . getElementById ( options . wrapperId ) )
158
+ ReactDOM . unmountComponentAtNode ( document . getElementById ( wrapper ) )
111
159
}
112
160
113
161
// show animated toast
114
- function show ( text , type ) {
115
- if ( ! document . getElementById ( options . wrapperId ) . hasChildNodes ( ) ) {
162
+ function show ( text , type , config ) {
163
+ let newConfig = assign ( { } , options , config )
164
+ if ( ! document . getElementById ( wrapper ) . hasChildNodes ( ) ) {
116
165
// mount toast
117
- mountToast ( text , type )
166
+ mountToast ( text , type , newConfig )
118
167
// un mount after timeout
119
- setTimeout ( function ( ) {
168
+ setTimeout ( ( ) => {
120
169
umountToast ( )
121
- } , options . defaultTimeout + options . animationDuration )
170
+ } , newConfig . timeout + duration )
122
171
return true
123
172
}
124
173
return false
0 commit comments