UNPKG

4.75 kBJavaScriptView Raw
1import React from 'react';
2import Notification from 'bee-notification';
3import classnames from 'classnames';
4
5let defaultDuration = 1.5;
6let defaultTop = 0;
7let defaultBottom = 48;
8let bottom = 90;
9let padding = 30;
10let width =200;
11let messageInstance;
12let key = 1;
13let clsPrefix = 'u-message';
14const noop = () => {};
15
16let positionObj = {
17 "top": {
18 messageStyle: {
19 width: "100%"
20 },
21 notificationStyle: {
22 top: defaultTop,
23 width: "100%"
24 },
25 transitionName: 'top'
26 },
27 "bottom": {
28 messageStyle: {
29 width: "100%"
30 },
31 notificationStyle: {
32 bottom: defaultBottom,
33 width: "100%"
34 },
35 transitionName: 'bottom'
36 },
37 "topRight": {
38 messageStyle: {
39 width: width
40 },
41 notificationStyle: {
42 top: padding,
43 right: padding,
44 width: width
45 },
46 transitionName: 'right'
47 },
48 "bottomRight": {
49 messageStyle: {
50 width: width
51 },
52 notificationStyle: {
53 bottom: bottom,
54 right: padding,
55 width: width
56 },
57 transitionName: 'right'
58 },
59 "topLeft": {
60 messageStyle: {
61 width: width
62 },
63 notificationStyle: {
64 top: padding,
65 left: padding,
66 width: width
67 },
68 transitionName: 'left'
69 },
70 "bottomLeft": {
71 messageStyle: {
72 width: width
73 },
74 notificationStyle: {
75 bottom: bottom,
76 left: padding,
77 width: width
78 },
79 transitionName: 'left'
80 }
81}
82
83function getMessageInstance(position = 'top', callback, keyboard, onEscapeKeyUp) {
84 if (messageInstance) {
85 callback(messageInstance);
86 return;
87 }
88 var style = positionObj[position].notificationStyle;
89 let instanceObj = {
90 clsPrefix,
91 transitionName: `${clsPrefix}-${positionObj[position].transitionName}`,
92 style: style, // 覆盖原来的样式
93 position: '',
94 }
95 if (typeof keyboard === 'boolean') {
96 instanceObj.keyboard = keyboard;
97 }
98 if (typeof onEscapeKeyUp === 'function') {
99 instanceObj.onEscapeKeyUp = onEscapeKeyUp;
100 }
101 Notification.newInstance(instanceObj, instance => {
102 messageInstance = instance;
103 callback(instance);
104 });
105}
106
107
108
109function notice(content, duration, type, onClose, position, style, keyboard, onEscapeKeyUp) {
110 let iconType = ({
111 info: 'uf uf-i-c-2',
112 success: 'uf uf-correct',
113 danger: 'uf uf-close-c',
114 warning: 'uf uf-exc-t',
115 light: 'uf uf-notification',
116 dark: 'uf uf-bubble',
117 news: 'uf uf-bell',
118 infolight: 'uf uf-i-c-2',
119 successlight: 'uf uf-correct',
120 dangerlight: 'uf uf-close-c',
121 warninglight: 'uf uf-exc-t',
122 })[type];
123
124 let positionStyle = positionObj[position].messageStyle;
125
126 getMessageInstance(position, instance => {
127 instance.notice({
128 key,
129 duration,
130 color: type,
131 style: Object.assign({}, positionStyle, style),
132 content: (
133 <div>
134 <div className={`${clsPrefix}-notice-description-icon`}>
135 <i className= { classnames(iconType) } />
136 </div>
137 <div className={`${clsPrefix}-notice-description-content`}>{content}</div>
138 </div>
139 ),
140 onClose,
141 });
142 }, keyboard, onEscapeKeyUp)
143 return (function () {
144 let target = key++;
145 return function () {
146 if (messageInstance) {
147 messageInstance.removeNotice(target);
148 }
149 };
150 }());
151}
152
153export default {
154 create(obj) {
155 let content = obj.content || '';
156 let duration = typeof obj.duration == undefined ? defaultDuration : obj.duration;
157 let color = obj.color || 'dark';
158 let onClose = obj.onClose || noop;
159 let position = obj.position || "top";
160 let style = obj.style || {};
161 return notice(content, duration, color, onClose, position, style, obj.keyboard, obj.onEscapeKeyUp);
162 },
163 config(options) {
164 if (options.top !== undefined) {
165 defaultTop = options.top;
166 }
167 if (options.duration !== undefined) {
168 defaultDuration = options.duration;
169 }
170 if (options.clsPrefix !== undefined) {
171 clsPrefix = options.clsPrefix;
172 }
173 if (options.defaultBottom !== undefined) {
174 defaultBottom = options.defaultBottom;
175 }
176 if (options.bottom !== undefined) {
177 bottom = options.bottom;
178 }
179 if (options.width !== undefined) {
180 bottom = options.width;
181 }
182 },
183 destroy() {
184 if (messageInstance) {
185 messageInstance.destroy();
186 messageInstance = null;
187 }
188 },
189};