forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotification.jsx
166 lines (156 loc) · 4.62 KB
/
Notification.jsx
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import Vue from 'vue';
import PropTypes from '../_util/vue-types';
import { getStyle, getComponentFromProp } from '../_util/props-util';
import BaseMixin from '../_util/BaseMixin';
import createChainedFunction from '../_util/createChainedFunction';
import getTransitionProps from '../_util/getTransitionProps';
import Notice from './Notice';
function noop() {}
let seed = 0;
const now = Date.now();
function getUuid() {
return `rcNotification_${now}_${seed++}`;
}
const Notification = {
mixins: [BaseMixin],
props: {
prefixCls: PropTypes.string.def('rc-notification'),
transitionName: PropTypes.string,
animation: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).def('fade'),
maxCount: PropTypes.number,
closeIcon: PropTypes.any,
},
data() {
return {
notices: [],
};
},
methods: {
getTransitionName() {
const props = this.$props;
let transitionName = props.transitionName;
if (!transitionName && props.animation) {
transitionName = `${props.prefixCls}-${props.animation}`;
}
return transitionName;
},
add(notice) {
const key = (notice.key = notice.key || getUuid());
const { maxCount } = this.$props;
this.setState(previousState => {
const notices = previousState.notices;
const noticeIndex = notices.map(v => v.key).indexOf(key);
const updatedNotices = notices.concat();
if (noticeIndex !== -1) {
updatedNotices.splice(noticeIndex, 1, notice);
} else {
if (maxCount && notices.length >= maxCount) {
// XXX, use key of first item to update new added (let React to move exsiting
// instead of remove and mount). Same key was used before for both a) external
// manual control and b) internal react 'key' prop , which is not that good.
notice.updateKey = updatedNotices[0].updateKey || updatedNotices[0].key;
updatedNotices.shift();
}
updatedNotices.push(notice);
}
return {
notices: updatedNotices,
};
});
},
remove(key) {
this.setState(previousState => {
return {
notices: previousState.notices.filter(notice => notice.key !== key && notice.updateKey !== key),
};
});
},
},
render(h) {
const { prefixCls, notices, remove, getTransitionName } = this;
const transitionProps = getTransitionProps(getTransitionName());
const noticeNodes = notices.map((notice, index) => {
const update = Boolean(index === notices.length - 1 && notice.updateKey);
const key = notice.updateKey ? notice.updateKey : notice.key;
const { content, duration, closable, onClose, style, class: className } = notice;
const close = createChainedFunction(remove.bind(this, key), onClose);
const noticeProps = {
props: {
prefixCls,
duration,
closable,
update,
closeIcon: getComponentFromProp(this, 'closeIcon'),
},
on: {
close,
click: notice.onClick || noop,
},
style,
class: className,
key,
};
return (
<Notice {...noticeProps}>{typeof content === 'function' ? content(h) : content}</Notice>
);
});
const className = {
[prefixCls]: 1,
};
const style = getStyle(this);
return (
<div
class={className}
style={
style || {
top: '65px',
left: '50%',
}
}
>
<transition-group {...transitionProps}>{noticeNodes}</transition-group>
</div>
);
},
};
Notification.newInstance = function newNotificationInstance(properties, callback) {
const { getContainer, style, class: className, ...props } = properties || {};
const div = document.createElement('div');
if (getContainer) {
const root = getContainer();
root.appendChild(div);
} else {
document.body.appendChild(div);
}
new Vue({
el: div,
mounted() {
const self = this;
this.$nextTick(() => {
callback({
notice(noticeProps) {
self.$refs.notification.add(noticeProps);
},
removeNotice(key) {
self.$refs.notification.remove(key);
},
component: self,
destroy() {
self.$destroy();
self.$el.parentNode.removeChild(self.$el);
},
});
});
},
render() {
const p = {
props,
ref: 'notification',
style,
class: className,
};
return <Notification {...p} />;
},
});
};
export default Notification;