-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmessage.tsx
More file actions
executable file
·87 lines (79 loc) · 2.52 KB
/
message.tsx
File metadata and controls
executable file
·87 lines (79 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import React, { useEffect, useRef, useState, useContext } from 'react';
import classNames from 'classnames';
import Transition from '../transition';
import { ConfigContext } from '../config-provider/config-context';
import { getPrefixCls } from '../_utils/general';
import {
CheckCircle,
CloseCircle,
InfoCircle,
LoadingCircle,
WarningCircle,
} from '../_utils/components';
import { MessageProps } from './types';
const Message = (props: MessageProps): JSX.Element => {
const {
type,
icon,
content,
duration,
willUnmount,
extra,
className,
style,
prefixCls: customisedCls,
} = props;
const configContext = useContext(ConfigContext);
const prefixCls = getPrefixCls('message', configContext.prefixCls, customisedCls);
const cls = classNames(prefixCls, className);
const ref = useRef<HTMLDivElement | null>(null);
const [visible, setVisible] = useState(true);
const renderIcon = (): React.ReactNode => {
if (React.isValidElement(icon)) {
return icon;
} else if (typeof type === 'string') {
switch (type) {
case 'success':
return <CheckCircle size={16} className={`${prefixCls}__icon`} />;
case 'info':
return <InfoCircle size={16} className={`${prefixCls}__icon`} />;
case 'warning':
return <WarningCircle size={16} className={`${prefixCls}__icon`} />;
case 'error':
return <CloseCircle size={16} className={`${prefixCls}__icon`} />;
case 'loading':
return (
<LoadingCircle
size={16}
className={classNames(`${prefixCls}__icon`, {
[`${prefixCls}__icon_loading`]: type === 'loading',
})}
/>
);
}
}
return null;
};
useEffect(() => {
const node = ref.current;
const height = (node && node.offsetHeight) || 0;
const timer = window.setTimeout(() => {
setVisible(false);
willUnmount(height);
}, duration);
return (): void => {
window.clearTimeout(timer);
};
}, [duration, willUnmount]);
return (
<Transition nodeRef={ref} in={visible} appear={true} timeout={0} unmountOnExit={false} classNames={`${prefixCls}_fade-slide`}>
<div role="alert" className={cls} style={style} ref={ref}>
{renderIcon()}
<span className={`${prefixCls}__content`}>{content}</span>
{extra && <div className={`${prefixCls}__extra`}>{extra}</div>}
</div>
</Transition>
);
};
Message.displayName = 'Message';
export default Message;