1 | import * as React from 'react';
|
2 | import * as PropTypes from 'prop-types';
|
3 |
|
4 | import _omit from 'lodash/omit';
|
5 |
|
6 | import { Box } from '../primitives';
|
7 | import { ButtonProps, buttonPropTypes } from '../Button/Button';
|
8 | import { LocalPaneProps, PaneProps, panePropTypes, paneDefaultProps } from '../Pane/Pane';
|
9 | import { Omit } from '../types';
|
10 |
|
11 | import ToastClose, { ToastCloseProps } from './ToastClose';
|
12 | import ToastContainer, { ToastContainerProps } from './ToastContainer';
|
13 | import ToastIcon from './ToastIcon';
|
14 | import ToastTitle from './ToastTitle';
|
15 | import _Toast, { CountdownBar, Content } from './styled';
|
16 |
|
17 | export type LocalToastProps = LocalPaneProps & {
|
18 | autoDismissTimeout?: number;
|
19 | children?: React.ReactNode;
|
20 | closeButtonProps?: Omit<ButtonProps, 'children'>;
|
21 | hasHorizontalBar?: boolean;
|
22 | hasIcon?: boolean;
|
23 | hasTint?: boolean;
|
24 | hideCloseButton?: boolean;
|
25 | onClickClose?: ToastCloseProps['onClickClose'];
|
26 | showCountdownBar?: boolean;
|
27 | title?: string;
|
28 | type?: string;
|
29 | };
|
30 | export type ToastProps = PaneProps & LocalToastProps;
|
31 | export type ToastComponents = {
|
32 | Container: React.FunctionComponent<ToastContainerProps>;
|
33 | };
|
34 |
|
35 | export const Toast: React.FunctionComponent<LocalToastProps> & ToastComponents = ({
|
36 | autoDismissTimeout,
|
37 | children,
|
38 | closeButtonProps,
|
39 | hasHorizontalBar,
|
40 | hasIcon,
|
41 | hideCloseButton,
|
42 | onClickClose,
|
43 | showCountdownBar,
|
44 | title,
|
45 | type,
|
46 | ...props
|
47 | }) => (
|
48 | <_Toast type={type} {...props}>
|
49 | {showCountdownBar && (
|
50 | <React.Fragment>
|
51 | <CountdownBar isHorizontal={hasHorizontalBar} type={type} isBackground />
|
52 | <CountdownBar isHorizontal={hasHorizontalBar} type={type} autoDismissTimeout={autoDismissTimeout} />
|
53 | </React.Fragment>
|
54 | )}
|
55 | <Content showCountdownBar={showCountdownBar}>
|
56 | {hasIcon && type && <ToastIcon type={type} size={children ? '300' : undefined} />}
|
57 | <Box>
|
58 | {title && <ToastTitle marginBottom={children ? 'minor-2' : undefined}>{title}</ToastTitle>}
|
59 | {children}
|
60 | </Box>
|
61 | </Content>
|
62 | {!hideCloseButton && (
|
63 | <ToastClose isAbsolute={Boolean(children)} onClickClose={onClickClose} {...closeButtonProps} />
|
64 | )}
|
65 | </_Toast>
|
66 | );
|
67 |
|
68 | Toast.Container = ToastContainer;
|
69 |
|
70 | export const toastPropTypes = {
|
71 | autoDismissTimeout: PropTypes.number,
|
72 | children: PropTypes.node,
|
73 | closeButtonProps: PropTypes.shape(_omit(buttonPropTypes, 'children')),
|
74 | hasHorizontalBar: PropTypes.bool,
|
75 | hasIcon: PropTypes.bool,
|
76 | hasTint: PropTypes.bool,
|
77 | hideCloseButton: PropTypes.bool,
|
78 | onClickClose: PropTypes.func,
|
79 | showCountdownBar: PropTypes.bool,
|
80 | type: PropTypes.string,
|
81 | title: PropTypes.string,
|
82 | ...panePropTypes
|
83 | };
|
84 | Toast.propTypes = toastPropTypes;
|
85 |
|
86 | export const toastDefaultProps = {
|
87 | ...paneDefaultProps,
|
88 | autoDismissTimeout: undefined,
|
89 | children: undefined,
|
90 | closeButtonProps: {},
|
91 | elevation: '300',
|
92 | hasHorizontalBar: false,
|
93 | hasIcon: true,
|
94 | hasTint: false,
|
95 | hideCloseButton: false,
|
96 | onClickClose: undefined,
|
97 | showCountdownBar: true,
|
98 | title: undefined,
|
99 | type: 'info'
|
100 | };
|
101 | Toast.defaultProps = toastDefaultProps;
|
102 |
|
103 | const C: React.FunctionComponent<ToastProps> & ToastComponents = Toast;
|
104 | export default C;
|