-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathNotice.tsx
140 lines (131 loc) · 3.95 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
134
135
136
137
138
139
140
import type { Key } from '../_util/type';
import { Teleport, computed, defineComponent, onMounted, watch, onUnmounted } from 'vue';
import type { HTMLAttributes, CSSProperties } from 'vue';
import type { MouseEventHandler } from '../_util/EventInterface';
import classNames from '../_util/classNames';
interface DivProps extends HTMLAttributes {
// 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;
duration?: number | null;
updateMark?: string;
/** Mark as final key since set maxCount may keep the key but user pass key is different */
noticeKey: Key;
closeIcon?: any;
closable?: boolean;
props?: DivProps;
onClick?: MouseEventHandler;
onClose?: (key: Key) => void;
/** @private Only for internal usage. We don't promise that we will refactor this */
holder?: HTMLDivElement;
/** @private Provided by CSSMotionList */
visible?: boolean;
}
export default defineComponent<NoticeProps>({
name: 'Notice',
inheritAttrs: false,
props: [
'prefixCls',
'duration',
'updateMark',
'noticeKey',
'closeIcon',
'closable',
'props',
'onClick',
'onClose',
'holder',
'visible',
] as any,
setup(props, { attrs, slots }) {
let closeTimer: any;
let isUnMounted = false;
const duration = computed(() => (props.duration === undefined ? 4.5 : props.duration));
const startCloseTimer = () => {
if (duration.value && !isUnMounted) {
closeTimer = setTimeout(() => {
close();
}, duration.value * 1000);
}
};
const clearCloseTimer = () => {
if (closeTimer) {
clearTimeout(closeTimer);
closeTimer = null;
}
};
const close = (e?: MouseEvent) => {
if (e) {
e.stopPropagation();
}
clearCloseTimer();
const { onClose, noticeKey } = props;
if (onClose) {
onClose(noticeKey);
}
};
const restartCloseTimer = () => {
clearCloseTimer();
startCloseTimer();
};
onMounted(() => {
startCloseTimer();
});
onUnmounted(() => {
isUnMounted = true;
clearCloseTimer();
});
watch(
[duration, () => props.updateMark, () => props.visible],
([preDuration, preUpdateMark, preVisible], [newDuration, newUpdateMark, newVisible]) => {
if (
preDuration !== newDuration ||
preUpdateMark !== newUpdateMark ||
(preVisible !== newVisible && newVisible)
) {
restartCloseTimer();
}
},
{ flush: 'post' },
);
return () => {
const { prefixCls, closable, closeIcon = slots.closeIcon?.(), onClick, holder } = props;
const { class: className, style } = attrs;
const componentClass = `${prefixCls}-notice`;
const dataOrAriaAttributeProps = Object.keys(attrs).reduce(
(acc: Record<string, string>, key: string) => {
if (key.startsWith('data-') || key.startsWith('aria-') || key === 'role') {
acc[key] = (attrs as any)[key];
}
return acc;
},
{},
);
const node = (
<div
class={classNames(componentClass, className, {
[`${componentClass}-closable`]: closable,
})}
style={style as CSSProperties}
onMouseenter={clearCloseTimer}
onMouseleave={startCloseTimer}
onClick={onClick}
{...dataOrAriaAttributeProps}
>
<div class={`${componentClass}-content`}>{slots.default?.()}</div>
{closable ? (
<a tabindex={0} onClick={close} class={`${componentClass}-close`}>
{closeIcon || <span class={`${componentClass}-close-x`} />}
</a>
) : null}
</div>
);
if (holder) {
return <Teleport to={holder} v-slots={{ default: () => node }}></Teleport>;
}
return node;
};
},
});