-
Notifications
You must be signed in to change notification settings - Fork 442
/
Copy pathToast.jsx
87 lines (71 loc) · 2.03 KB
/
Toast.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* @flow */
import React from 'react';
import { Component, PropTypes, Transition, View } from '../../libs';
import icons from './assets';
type State = {
visible: boolean
};
export default class Toast extends Component {
state: State;
constructor(props: Object) {
super(props);
this.state = {
visible: false
};
}
componentDidMount() {
this.setState({
visible: true
})
this.startTimer();
}
componentWillUnmount() {
this.stopTimer();
}
onClose() {
this.stopTimer();
this.setState({
visible: false
});
}
startTimer() {
if (this.props.duration > 0) {
this.timeout = setTimeout(() => {
this.onClose();
}, this.props.duration)
}
}
stopTimer() {
clearTimeout(this.timeout);
}
render() {
const { iconClass, customClass } = this.props;
return (
<Transition name="el-message-fade" onAfterLeave={() => { this.props.willUnmount(); }}>
<View show={this.state.visible}>
<div className={this.classNames('el-message', customClass)} onMouseEnter={this.stopTimer.bind(this)} onMouseLeave={this.startTimer.bind(this)}>
{ !iconClass && <img className="el-message__img" src={icons[this.props.type]} /> }
<div className={this.classNames('el-message__group', { 'is-with-icon': iconClass })}>
{ iconClass && <i className={this.classNames('el-message__icon', iconClass)}></i> }
<p>{this.props.message}</p>
{ this.props.showClose && <div className="el-message__closeBtn el-icon-close" onClick={this.onClose.bind(this)}></div> }
</div>
</div>
</View>
</Transition>
)
}
}
Toast.propTypes = {
type: PropTypes.oneOf(['success', 'warning', 'info', 'error']),
message: PropTypes.oneOfType([PropTypes.string, PropTypes.element]).isRequired,
duration: PropTypes.number,
showClose: PropTypes.bool,
customClass: PropTypes.string,
iconClass: PropTypes.string
}
Toast.defaultProps = {
type: 'info',
duration: 3000,
showClose: false
}