-
Notifications
You must be signed in to change notification settings - Fork 100
/
Notice.tsx
133 lines (118 loc) · 3.27 KB
/
Notice.tsx
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import * as React from 'react';
import { Component } from 'react';
import ReactDOM from 'react-dom';
import classNames from 'classnames';
interface DivProps extends React.HTMLProps<HTMLDivElement> {
// Ideally we would allow all data-* props but this would depend on https://github.com/microsoft/TypeScript/issues/28960
'data-testid'?: string;
}
export interface NoticeProps {
prefixCls: string;
style?: React.CSSProperties;
className?: string;
duration?: number | null;
children?: React.ReactNode;
updateMark?: string;
/** Mark as final key since set maxCount may keep the key but user pass key is different */
noticeKey: React.Key;
closeIcon?: React.ReactNode;
closable?: boolean;
props?: DivProps;
onClick?: React.MouseEventHandler<HTMLDivElement>;
onClose?: (key: React.Key) => void;
/** @private Only for internal usage. We don't promise that we will refactor this */
holder?: HTMLDivElement;
}
export default class Notice extends Component<NoticeProps> {
static defaultProps = {
onClose() {},
duration: 1.5,
};
closeTimer: number | null = null;
componentDidMount() {
this.startCloseTimer();
}
componentDidUpdate(prevProps: NoticeProps) {
if (
this.props.duration !== prevProps.duration ||
this.props.updateMark !== prevProps.updateMark
) {
this.restartCloseTimer();
}
}
componentWillUnmount() {
this.clearCloseTimer();
}
close = (e?: React.MouseEvent<HTMLAnchorElement>) => {
if (e) {
e.stopPropagation();
}
this.clearCloseTimer();
const { onClose, noticeKey } = this.props;
if (onClose) {
onClose(noticeKey);
}
};
startCloseTimer = () => {
if (this.props.duration) {
this.closeTimer = window.setTimeout(() => {
this.close();
}, this.props.duration * 1000);
}
};
clearCloseTimer = () => {
if (this.closeTimer) {
clearTimeout(this.closeTimer);
this.closeTimer = null;
}
};
restartCloseTimer() {
this.clearCloseTimer();
this.startCloseTimer();
}
render() {
const {
prefixCls,
className,
closable,
closeIcon,
style,
onClick,
children,
holder,
} = this.props;
const componentClass = `${prefixCls}-notice`;
const dataOrAriaAttributeProps = Object.keys(this.props).reduce(
(acc: Record<string, string>, key: string) => {
if (key.substr(0, 5) === 'data-' || key.substr(0, 5) === 'aria-' || key === 'role') {
acc[key] = (this.props as any)[key];
}
return acc;
},
{},
);
const node = (
<div
className={classNames(componentClass, className, {
[`${componentClass}-closable`]: closable,
})}
style={style}
onMouseEnter={this.clearCloseTimer}
onMouseLeave={this.startCloseTimer}
onClick={onClick}
{...dataOrAriaAttributeProps}
>
<div className={`${componentClass}-content`}>{children}</div>
{closable ? (
<a tabIndex={0} onClick={this.close} className={`${componentClass}-close`}>
{closeIcon || <span className={`${componentClass}-close-x`} />}
</a>
) : null}
</div>
);
if (holder) {
return ReactDOM.createPortal(node, holder);
}
return node;
}
}