2017-04-17 17:14:17 +03:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2016-05-25 01:41:26 +03:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import { themr } from 'react-css-themr';
|
2017-01-26 20:05:32 +03:00
|
|
|
import { SNACKBAR } from '../identifiers';
|
|
|
|
import ActivableRenderer from '../hoc/ActivableRenderer';
|
|
|
|
import InjectButton from '../button/Button';
|
|
|
|
import Portal from '../hoc/Portal';
|
2015-11-22 23:41:28 +03:00
|
|
|
|
2017-01-05 04:42:18 +03:00
|
|
|
const factory = (Button) => {
|
2016-05-30 07:59:44 +03:00
|
|
|
class Snackbar extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
action: PropTypes.string,
|
|
|
|
active: PropTypes.bool,
|
2016-10-08 13:27:57 +03:00
|
|
|
children: PropTypes.node,
|
2016-05-30 07:59:44 +03:00
|
|
|
className: PropTypes.string,
|
2016-10-08 13:27:57 +03:00
|
|
|
label: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
2017-01-26 20:05:32 +03:00
|
|
|
PropTypes.element,
|
2016-10-08 13:27:57 +03:00
|
|
|
]),
|
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,
|
|
|
|
label: PropTypes.string,
|
|
|
|
snackbar: PropTypes.string,
|
2017-01-26 20:05:32 +03:00
|
|
|
warning: PropTypes.string,
|
2016-05-30 07:59:44 +03:00
|
|
|
}),
|
|
|
|
timeout: PropTypes.number,
|
2017-01-26 20:05:32 +03:00
|
|
|
type: PropTypes.oneOf(['accept', 'cancel', 'warning']),
|
2016-05-30 07:59:44 +03:00
|
|
|
};
|
2015-11-22 23:41:28 +03:00
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
componentDidMount() {
|
2016-11-18 20:35:20 +03:00
|
|
|
if (this.props.active && this.props.timeout) {
|
|
|
|
this.scheduleTimeout(this.props);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2016-05-30 07:59:44 +03:00
|
|
|
if (nextProps.active && nextProps.timeout) {
|
2016-11-18 20:35:20 +03:00
|
|
|
this.scheduleTimeout(nextProps);
|
2016-05-30 07:59:44 +03:00
|
|
|
}
|
2015-11-22 23:41:28 +03:00
|
|
|
}
|
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
componentWillUnmount() {
|
2016-06-13 23:50:48 +03:00
|
|
|
clearTimeout(this.curTimeout);
|
|
|
|
}
|
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
scheduleTimeout = (props) => {
|
2016-11-18 20:35:20 +03:00
|
|
|
const { onTimeout, timeout } = props;
|
|
|
|
if (this.curTimeout) clearTimeout(this.curTimeout);
|
|
|
|
this.curTimeout = setTimeout(() => {
|
|
|
|
if (onTimeout) onTimeout();
|
|
|
|
this.curTimeout = null;
|
|
|
|
}, timeout);
|
|
|
|
}
|
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
render() {
|
|
|
|
const { action, active, children, label, onClick, theme, type } = this.props;
|
2016-05-30 07:59:44 +03:00
|
|
|
const className = classnames([theme.snackbar, theme[type]], {
|
2017-01-26 20:05:32 +03:00
|
|
|
[theme.active]: active,
|
2016-05-30 07:59:44 +03:00
|
|
|
}, this.props.className);
|
2015-11-22 23:41:28 +03:00
|
|
|
|
2016-05-30 07:59:44 +03:00
|
|
|
return (
|
2017-01-05 04:42:18 +03:00
|
|
|
<Portal className={theme.portal}>
|
2017-01-26 20:05:32 +03:00
|
|
|
<div data-react-toolbox="snackbar" className={className}>
|
2016-10-08 13:27:57 +03:00
|
|
|
<span className={theme.label}>
|
|
|
|
{label}
|
|
|
|
{children}
|
|
|
|
</span>
|
2017-01-26 20:05:32 +03:00
|
|
|
{action ? <Button className={theme.button} label={action} onClick={onClick} /> : null}
|
2016-05-30 07:59:44 +03:00
|
|
|
</div>
|
2017-01-05 04:42:18 +03:00
|
|
|
</Portal>
|
2016-05-30 07:59:44 +03:00
|
|
|
);
|
|
|
|
}
|
2015-11-22 23:41:28 +03:00
|
|
|
}
|
|
|
|
|
2016-05-30 07:59:44 +03:00
|
|
|
return ActivableRenderer()(Snackbar);
|
|
|
|
};
|
|
|
|
|
2017-01-05 04:42:18 +03:00
|
|
|
const Snackbar = factory(InjectButton);
|
2016-05-30 07:59:44 +03:00
|
|
|
export default themr(SNACKBAR)(Snackbar);
|
|
|
|
export { factory as snackbarFactory };
|
|
|
|
export { Snackbar };
|