import React, { FunctionComponent, useState } from 'react'; import styles from './Notification.scss'; import { transparentizeColour } from '../../utils'; type NotificationType = | 'info' | 'success' | 'warning' | 'error'; export interface NotificationProps { type: NotificationType, closable?: boolean, colour?: string, className?: string, variant?: 'text' | 'outlined' | 'contained' } const DEFAULT_INFO_COLOUR = '#1890ff'; const DEFAULT_SUCCESS_COLOUR = '#52c41a'; const DEFAULT_WARNING_COLOUR = '#faad14'; const DEFAULT_ERROR_COLOUR = '#ff4d4f'; export const Notification: FunctionComponent = (props) => { const variantClass = props.variant ? styles[`${props.variant}-${props.type}`] : ''; const [openClass, setOpenClass] = useState(''); const getInlineStyles = () => { if (props.colour) { let inlineStyles: { [k: string]: string } = {}; if (props.variant && props.variant == 'contained') { inlineStyles['background'] = props.colour; inlineStyles['color'] = '#fff'; return inlineStyles; } if (props.variant && props.variant == 'outlined') { inlineStyles['border'] = `1px solid ${props.colour}`; inlineStyles['background'] = transparentizeColour(props.colour, 0.15); return inlineStyles; } } return {}; } const closeSelf = () => { setOpenClass(styles.closed) } const getIconColour = (defaultColour: string) => { if (props.variant && props.variant == 'contained') { return "#fff"; } return props.colour || defaultColour; } const getInfoIcon = () => { let colour = getIconColour(DEFAULT_INFO_COLOUR); return ( ); } const getErrorIcon = () => { let colour = getIconColour(DEFAULT_ERROR_COLOUR); return ( ); } const getSuccessIcon = () => { let colour = getIconColour(DEFAULT_SUCCESS_COLOUR); return ( ); } const getWarningIcon = () => { let colour = getIconColour(DEFAULT_WARNING_COLOUR); return ( ); } return (
{props.type == "info" && {getInfoIcon()}} {props.type == "error" && {getErrorIcon()}} {props.type == "success" && {getSuccessIcon()}} {props.type == "warning" && {getWarningIcon()}} {props.children} { props.closable && closeSelf()}> }
); }