2016-05-30 07:59:44 +03:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
2016-05-25 01:41:26 +03:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import { themr } from 'react-css-themr';
|
2016-05-30 07:59:44 +03:00
|
|
|
import { SNACKBAR } from '../identifiers.js';
|
|
|
|
import ActivableRenderer from '../hoc/ActivableRenderer.js';
|
|
|
|
import FontIcon from '../font_icon/FontIcon.js';
|
|
|
|
import InjectOverlay from '../overlay/Overlay.js';
|
|
|
|
import InjectButton from '../button/Button.js';
|
2015-11-22 23:41:28 +03:00
|
|
|
|
2016-05-30 07:59:44 +03:00
|
|
|
const factory = (Overlay, Button) => {
|
|
|
|
class Snackbar extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
action: PropTypes.string,
|
|
|
|
active: PropTypes.bool,
|
|
|
|
className: PropTypes.string,
|
|
|
|
icon: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.element
|
|
|
|
]),
|
2016-06-04 00:44:33 +03:00
|
|
|
label: PropTypes.string,
|
2016-05-30 07:59:44 +03:00
|
|
|
onClick: PropTypes.func,
|
|
|
|
onTimeout: PropTypes.func,
|
|
|
|
theme: PropTypes.shape({
|
|
|
|
accept: PropTypes.string,
|
|
|
|
active: PropTypes.string,
|
|
|
|
button: PropTypes.string,
|
|
|
|
cancel: PropTypes.string,
|
|
|
|
icon: PropTypes.string,
|
|
|
|
label: PropTypes.string,
|
|
|
|
snackbar: PropTypes.string,
|
|
|
|
warning: PropTypes.string
|
|
|
|
}),
|
|
|
|
timeout: PropTypes.number,
|
|
|
|
type: PropTypes.oneOf([ 'accept', 'cancel', 'warning' ])
|
|
|
|
};
|
2015-11-22 23:41:28 +03:00
|
|
|
|
2016-05-30 07:59:44 +03:00
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
if (nextProps.active && nextProps.timeout) {
|
|
|
|
if (this.curTimeout) clearTimeout(this.curTimeout);
|
|
|
|
this.curTimeout = setTimeout(() => {
|
|
|
|
nextProps.onTimeout();
|
|
|
|
this.curTimeout = null;
|
|
|
|
}, nextProps.timeout);
|
|
|
|
}
|
2015-11-22 23:41:28 +03:00
|
|
|
}
|
|
|
|
|
2016-06-13 23:50:48 +03:00
|
|
|
componentWillUnmount () {
|
|
|
|
clearTimeout(this.curTimeout);
|
|
|
|
}
|
|
|
|
|
2016-05-30 07:59:44 +03:00
|
|
|
render () {
|
|
|
|
const {action, active, icon, label, onClick, theme, type } = this.props;
|
|
|
|
const className = classnames([theme.snackbar, theme[type]], {
|
|
|
|
[theme.active]: active
|
|
|
|
}, this.props.className);
|
2015-11-22 23:41:28 +03:00
|
|
|
|
2016-05-30 07:59:44 +03:00
|
|
|
return (
|
|
|
|
<Overlay invisible>
|
|
|
|
<div data-react-toolbox='snackbar' className={className}>
|
|
|
|
{icon ? <FontIcon value={icon} className={theme.icon} /> : null}
|
|
|
|
<span className={theme.label}>{label}</span>
|
|
|
|
{action ? <Button className={theme.button} label={action} onClick={onClick}/> : null}
|
|
|
|
</div>
|
|
|
|
</Overlay>
|
|
|
|
);
|
|
|
|
}
|
2015-11-22 23:41:28 +03:00
|
|
|
}
|
|
|
|
|
2016-05-30 07:59:44 +03:00
|
|
|
return ActivableRenderer()(Snackbar);
|
|
|
|
};
|
|
|
|
|
|
|
|
const Snackbar = factory(InjectOverlay, InjectButton);
|
|
|
|
export default themr(SNACKBAR)(Snackbar);
|
|
|
|
export { factory as snackbarFactory };
|
|
|
|
export { Snackbar };
|