2019-01-03 16:00:55 +03:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2020-08-09 04:18:45 +03:00
|
|
|
import { MdClose } from 'react-icons/md';
|
|
|
|
import { MdWarning } from 'react-icons/md';
|
|
|
|
import { MdError } from 'react-icons/md';
|
|
|
|
import { MdCheckCircle } from 'react-icons/md';
|
2020-09-10 17:10:53 +03:00
|
|
|
import { Base16Theme } from 'base16';
|
2019-01-03 16:00:55 +03:00
|
|
|
import createStyledComponent from '../utils/createStyledComponent';
|
|
|
|
import styles from './styles';
|
|
|
|
|
|
|
|
const NotificationWrapper = createStyledComponent(styles);
|
|
|
|
|
2020-09-09 17:35:22 +03:00
|
|
|
export type Type = 'info' | 'success' | 'warning' | 'error';
|
|
|
|
|
|
|
|
export interface NotificationProps {
|
|
|
|
children?: React.ReactNode;
|
|
|
|
type: Type;
|
|
|
|
onClose?: React.MouseEventHandler<HTMLButtonElement>;
|
2020-09-10 17:10:53 +03:00
|
|
|
theme?: Base16Theme;
|
2020-09-09 17:35:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class Notification extends Component<NotificationProps> {
|
|
|
|
shouldComponentUpdate(nextProps: NotificationProps) {
|
2019-01-10 21:51:14 +03:00
|
|
|
return (
|
|
|
|
nextProps.children !== this.props.children ||
|
|
|
|
nextProps.type !== this.props.type
|
|
|
|
);
|
2019-01-03 16:00:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getIcon = () => {
|
|
|
|
switch (this.props.type) {
|
2019-01-10 21:51:14 +03:00
|
|
|
case 'warning':
|
2020-08-09 04:18:45 +03:00
|
|
|
return <MdWarning />;
|
2019-01-10 21:51:14 +03:00
|
|
|
case 'error':
|
2020-08-09 04:18:45 +03:00
|
|
|
return <MdError />;
|
2019-01-10 21:51:14 +03:00
|
|
|
case 'success':
|
2020-08-09 04:18:45 +03:00
|
|
|
return <MdCheckCircle />;
|
2019-01-10 21:51:14 +03:00
|
|
|
default:
|
|
|
|
return null;
|
2019-01-03 16:00:55 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<NotificationWrapper type={this.props.type} theme={this.props.theme}>
|
|
|
|
{this.getIcon()}
|
|
|
|
<span>{this.props.children}</span>
|
2019-01-10 21:51:14 +03:00
|
|
|
{this.props.onClose && (
|
|
|
|
<button onClick={this.props.onClose}>
|
2020-08-09 04:18:45 +03:00
|
|
|
<MdClose />
|
2019-01-10 21:51:14 +03:00
|
|
|
</button>
|
|
|
|
)}
|
2019-01-03 16:00:55 +03:00
|
|
|
</NotificationWrapper>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-09 17:35:22 +03:00
|
|
|
static propTypes = {
|
|
|
|
children: PropTypes.any.isRequired,
|
|
|
|
type: PropTypes.oneOf(['info', 'success', 'warning', 'error']),
|
|
|
|
onClose: PropTypes.func,
|
|
|
|
theme: PropTypes.object,
|
|
|
|
};
|
2019-01-03 16:00:55 +03:00
|
|
|
|
2020-09-09 17:35:22 +03:00
|
|
|
static defaultProps = {
|
|
|
|
type: 'info',
|
|
|
|
};
|
|
|
|
}
|