react-toolbox/components/snackbar/index.jsx

70 lines
1.6 KiB
React
Raw Normal View History

2015-10-20 06:25:35 +03:00
import React from 'react';
import style from './style';
2015-10-20 18:27:22 +03:00
import Button from '../button';
2015-10-20 06:25:35 +03:00
import FontIcon from '../font_icon';
2015-10-22 02:59:20 +03:00
class Snackbar extends React.Component {
2015-10-21 05:58:11 +03:00
static propTypes = {
action: React.PropTypes.string,
2015-11-01 17:19:45 +03:00
className: React.PropTypes.string,
2015-10-20 06:25:35 +03:00
icon: React.PropTypes.string,
2015-11-01 17:19:45 +03:00
label: React.PropTypes.string.isRequired,
onClick: React.PropTypes.func,
2015-10-20 06:25:35 +03:00
timeout: React.PropTypes.number,
type: React.PropTypes.string
2015-10-21 05:58:11 +03:00
};
2015-10-20 06:25:35 +03:00
2015-10-21 05:58:11 +03:00
state = {
active: false
};
2015-10-20 06:25:35 +03:00
2015-10-22 02:31:17 +03:00
handleClick = (event) => {
this.setState({active: false});
if (this.props.onClick) {
this.props.onClick(event, this);
}
2015-10-22 02:31:17 +03:00
};
2015-10-20 06:25:35 +03:00
renderButton () {
if (this.props.action) {
return (
<Button
className={style.button}
kind='flat'
2015-10-21 01:48:24 +03:00
label={this.props.action}
2015-10-21 09:13:24 +03:00
onClick={this.handleClick}
/>
);
}
2015-10-21 05:58:11 +03:00
}
2015-10-20 18:27:22 +03:00
2015-10-20 06:25:35 +03:00
render () {
let className = `${style.root} ${style[this.props.type]}`;
if (this.state.active) className += ` ${style.active}`;
2015-10-20 06:25:35 +03:00
if (this.props.className) className += ` ${this.props.className}`;
return (
<div data-react-toolbox='snackbar' className={className}>
2015-10-20 18:27:22 +03:00
{ this.props.icon ? <FontIcon value={this.props.icon} className={style.icon} /> : null }
<span className={style.label}>{this.props.label}</span>
{ this.renderButton() }
2015-10-20 06:25:35 +03:00
</div>
);
2015-10-21 05:58:11 +03:00
}
hide () {
this.setState({active: false});
2015-10-21 05:58:11 +03:00
}
show () {
this.setState({active: true});
if (this.props.timeout) {
setTimeout(() => {
this.setState({ active: false });
}, this.props.timeout * 1000);
}
2015-10-20 06:25:35 +03:00
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
2015-10-22 02:59:20 +03:00
export default Snackbar;