-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathalert.tsx
More file actions
executable file
·91 lines (82 loc) · 2.91 KB
/
alert.tsx
File metadata and controls
executable file
·91 lines (82 loc) · 2.91 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
88
89
90
91
import React, { useState, useRef, useContext } from 'react';
import classNames from 'classnames';
import { ConfigContext } from '../config-provider/config-context';
import { getPrefixCls } from '../_utils/general';
import { CheckCircle, CloseCircle, InfoCircle, WarningCircle } from '../_utils/components';
import { AlertProps } from './types';
import Transition from '../transition';
const setClosedStyle = (node: HTMLElement): void => {
node.style.borderTopWidth = '0';
node.style.paddingTop = '0';
node.style.marginTop = '0';
node.style.height = '0';
node.style.paddingBottom = '0';
node.style.borderBottomWidth = '0';
node.style.marginBottom = '0';
};
const Alert = React.forwardRef<HTMLDivElement, AlertProps>((props, forwardedRef) => {
const {
type = 'info',
iconSize = 14,
prefixCls: customisedCls,
title,
icon,
closeText,
closable,
afterClose,
onClose,
children,
className,
style,
...otherProps
} = props;
const [isShow, setShow] = useState(true);
const ref = useRef<HTMLDivElement | null>(null);
const configContext = useContext(ConfigContext);
const prefixCls = getPrefixCls('alert', configContext.prefixCls, customisedCls);
const cls = classNames(prefixCls, className, [`${prefixCls}_${type}`]);
const closeBtnOnClick = (e: React.MouseEvent<HTMLSpanElement>): void => {
ref.current && setClosedStyle(ref.current as HTMLDivElement);
setShow(false);
onClose?.(e);
};
// Setting close text attribute also allows to be closable
const closeIcon = (closable || closeText) && (
<button type="button" className={`${prefixCls}__close-btn`} onClick={closeBtnOnClick} aria-label="Close">
{closeText || '✕'}
</button>
);
const renderIcon = (): React.ReactNode => {
if (typeof icon === 'boolean') {
switch (type) {
case 'success':
return <CheckCircle size={iconSize} className={`${prefixCls}__icon`} />;
case 'info':
return <InfoCircle size={iconSize} className={`${prefixCls}__icon`} />;
case 'warning':
return <WarningCircle size={iconSize} className={`${prefixCls}__icon`} />;
case 'error':
return <CloseCircle size={iconSize} className={`${prefixCls}__icon`} />;
}
}
return icon;
};
return (
<Transition timeout={300} in={isShow} nodeRef={ref} onExited={afterClose}>
<div {...otherProps} role="alert" className={cls} style={style} ref={(node) => {
ref.current = node;
if (typeof forwardedRef === 'function') forwardedRef(node);
else if (forwardedRef) forwardedRef.current = node;
}}>
{icon && renderIcon()}
<div>
{title && <p className={classNames(`${prefixCls}__title`, { [`${prefixCls}__title_has-content`]: children })}>{title}</p>}
{children}
</div>
{closeIcon}
</div>
</Transition>
);
});
Alert.displayName = 'Alert';
export default Alert;