@@ -6,28 +6,70 @@ import assign from 'object-assign'
66
77import {
88 containerStyle ,
9+ containerTopStyle ,
10+ containerBottomStyle ,
11+ containerDefaultStyle ,
912 contentBaseStyle ,
1013 contentSuccessStyle ,
1114 contentErrorStyle ,
1215 contentWarningStyle ,
13- contentDefaultStyle ,
14- transformShowStyle ,
15- transformHideStyle
16+ contentInfoStyle ,
17+ animateDownStyleToHide ,
18+ animateDownStyleToShow ,
19+ animateUpStyleToHide ,
20+ animateUpStyleToShow ,
21+ animateFadeStyleToHide ,
22+ animateFadeStyleToShow
1623} from './styles'
1724import {
25+ wrapper ,
26+ duration ,
1827 options
1928} from './config'
2029
2130class Toast extends React . Component {
22- constructor ( ) {
23- super ( )
31+ constructor ( props ) {
32+ super ( props )
2433 this . state = {
25- contentStyleExt : null
34+ containerStyleExt : this . setContainerStyle ( ) . container
2635 }
2736 }
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
2969 setContentStyle ( ) {
70+ // create style object
3071 let style = { }
72+ // switch type
3173 switch ( this . props . type ) {
3274 case 'success' :
3375 style = assign ( { } , contentBaseStyle , contentSuccessStyle )
@@ -38,50 +80,56 @@ class Toast extends React.Component {
3880 case 'warning' :
3981 style = assign ( { } , contentBaseStyle , contentWarningStyle )
4082 break
83+ case 'info' :
84+ style = assign ( { } , contentBaseStyle , contentInfoStyle )
85+ break
4186 default :
4287 style = assign ( { } , contentBaseStyle )
4388 break
4489 }
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
4599 return style
46100 }
47-
48101 // after component mount
49102 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
55105 setTimeout ( ( ) => {
56106 this . setState ( {
57- contentStyleExt : assign ( { } , containerStyle , transformShowStyle )
107+ containerStyleExt : assign ( { } , _containerStyle . container , _containerStyle . animate . show )
58108 } )
59109 } , 100 )
60110 // hide toast effect style, do it after timeout
61111 setTimeout ( ( ) => {
62112 this . setState ( {
63- contentStyleExt : assign ( { } , containerStyle , transformHideStyle )
113+ containerStyleExt : assign ( { } , _containerStyle . container , _containerStyle . animate . hide )
64114 } )
65- } , options . defaultTimeout )
115+ } , this . props . timeout )
66116 }
67117
68118 // render component
69119 render ( ) {
70120 let { text} = this . props
71- let contentStyle = this . setContentStyle ( )
121+ let _contentStyle = this . setContentStyle ( )
72122 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 >
75125 </ div >
76126 )
77127 }
78128}
79129// component prop types
80130Toast . PropTypes = {
81131 text : PropTypes . string ,
82- timeout : PropTypes . number ,
83132 type : PropTypes . string ,
84- color : PropTypes . object ,
85133 style : PropTypes . oneOfType ( [
86134 PropTypes . object ,
87135 PropTypes . bool
@@ -92,33 +140,34 @@ Toast.PropTypes = {
92140export default class extends React . Component {
93141 render ( ) {
94142 return (
95- < div id = { options . wrapperId } > </ div >
143+ < div id = { wrapper } > </ div >
96144 ) ;
97145 }
98146}
99147
100148// mount toast to wrapper dom
101- function mountToast ( text , type ) {
149+ function mountToast ( text , type , config ) {
102150 ReactDOM . render (
103- < Toast text = { text } type = { type } /> ,
104- document . getElementById ( options . wrapperId )
151+ < Toast text = { text } type = { type } { ... config } /> ,
152+ document . getElementById ( wrapper )
105153 ) ;
106154}
107155
108156// un mount toast to wrapper dom
109157function umountToast ( ) {
110- ReactDOM . unmountComponentAtNode ( document . getElementById ( options . wrapperId ) )
158+ ReactDOM . unmountComponentAtNode ( document . getElementById ( wrapper ) )
111159}
112160
113161// 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 ( ) ) {
116165 // mount toast
117- mountToast ( text , type )
166+ mountToast ( text , type , newConfig )
118167 // un mount after timeout
119- setTimeout ( function ( ) {
168+ setTimeout ( ( ) => {
120169 umountToast ( )
121- } , options . defaultTimeout + options . animationDuration )
170+ } , newConfig . timeout + duration )
122171 return true
123172 }
124173 return false
0 commit comments