UNPKG

12.2 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4exports.withContext = undefined;
5
6var _extends2 = require('babel-runtime/helpers/extends');
7
8var _extends3 = _interopRequireDefault(_extends2);
9
10var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
11
12var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
13
14var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
15
16var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
17
18var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
19
20var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
21
22var _inherits2 = require('babel-runtime/helpers/inherits');
23
24var _inherits3 = _interopRequireDefault(_inherits2);
25
26var _class, _temp2;
27
28var _react = require('react');
29
30var _react2 = _interopRequireDefault(_react);
31
32var _reactDom = require('react-dom');
33
34var _reactDom2 = _interopRequireDefault(_reactDom);
35
36var _propTypes = require('prop-types');
37
38var _propTypes2 = _interopRequireDefault(_propTypes);
39
40var _overlay = require('../overlay');
41
42var _overlay2 = _interopRequireDefault(_overlay);
43
44var _configProvider = require('../config-provider');
45
46var _configProvider2 = _interopRequireDefault(_configProvider);
47
48var _util = require('../util');
49
50var _message = require('./message');
51
52var _message2 = _interopRequireDefault(_message);
53
54function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
55
56var config = _configProvider2.default.config;
57
58
59var instance = void 0;
60var timeouts = {};
61
62var Mask = (_temp2 = _class = function (_React$Component) {
63 (0, _inherits3.default)(Mask, _React$Component);
64
65 function Mask() {
66 var _temp, _this, _ret;
67
68 (0, _classCallCheck3.default)(this, Mask);
69
70 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
71 args[_key] = arguments[_key];
72 }
73
74 return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.state = {
75 visible: true
76 }, _this.handleClose = function () {
77 var silent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
78
79 _this.setState({
80 visible: false
81 });
82
83 if (!silent) {
84 _this.props.onClose && _this.props.onClose();
85 }
86 }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
87 }
88
89 Mask.prototype.componentWillUnmount = function componentWillUnmount() {
90 var timeoutId = this.props.timeoutId;
91
92
93 if (timeoutId in timeouts) {
94 var timeout = timeouts[timeoutId];
95 clearTimeout(timeout);
96 delete timeouts[timeoutId];
97 }
98 };
99
100 Mask.prototype.render = function render() {
101 /* eslint-disable no-unused-vars */
102 var _props = this.props,
103 prefix = _props.prefix,
104 type = _props.type,
105 title = _props.title,
106 content = _props.content,
107 align = _props.align,
108 offset = _props.offset,
109 hasMask = _props.hasMask,
110 afterClose = _props.afterClose,
111 animation = _props.animation,
112 overlayProps = _props.overlayProps,
113 timeoutId = _props.timeoutId,
114 className = _props.className,
115 style = _props.style,
116 others = (0, _objectWithoutProperties3.default)(_props, ['prefix', 'type', 'title', 'content', 'align', 'offset', 'hasMask', 'afterClose', 'animation', 'overlayProps', 'timeoutId', 'className', 'style']);
117 /* eslint-enable */
118
119 var visible = this.state.visible;
120
121 return _react2.default.createElement(
122 _overlay2.default,
123 (0, _extends3.default)({}, overlayProps, {
124 prefix: prefix,
125 animation: animation,
126 visible: visible,
127 align: align,
128 offset: offset,
129 hasMask: hasMask,
130 afterClose: afterClose
131 }),
132 _react2.default.createElement(
133 _message2.default,
134 (0, _extends3.default)({}, others, {
135 prefix: prefix,
136 visible: true,
137 type: type,
138 shape: 'toast',
139 title: title,
140 style: style,
141 className: prefix + 'message-wrapper ' + className,
142 onClose: this.handleClose
143 }),
144 content
145 )
146 );
147 };
148
149 return Mask;
150}(_react2.default.Component), _class.contextTypes = {
151 prefix: _propTypes2.default.string
152}, _class.propTypes = {
153 prefix: _propTypes2.default.string,
154 type: _propTypes2.default.string,
155 title: _propTypes2.default.node,
156 content: _propTypes2.default.node,
157 align: _propTypes2.default.string,
158 offset: _propTypes2.default.array,
159 hasMask: _propTypes2.default.bool,
160 afterClose: _propTypes2.default.func,
161 animation: _propTypes2.default.oneOfType([_propTypes2.default.object, _propTypes2.default.bool]),
162 overlayProps: _propTypes2.default.object,
163 onClose: _propTypes2.default.func,
164 timeoutId: _propTypes2.default.string,
165 style: _propTypes2.default.object,
166 className: _propTypes2.default.string
167}, _class.defaultProps = {
168 prefix: 'next-',
169 align: 'tc tc',
170 offset: [0, 30],
171 hasMask: false,
172 animation: {
173 in: 'pulse',
174 out: 'zoomOut'
175 },
176 style: {},
177 className: ''
178}, _temp2);
179Mask.displayName = 'Mask';
180
181
182var NewMask = config(Mask);
183
184var create = function create(props) {
185 /* eslint-disable no-unused-vars */
186 var duration = props.duration,
187 afterClose = props.afterClose,
188 contextConfig = props.contextConfig,
189 others = (0, _objectWithoutProperties3.default)(props, ['duration', 'afterClose', 'contextConfig']);
190 /* eslint-enable no-unused-vars */
191
192 var div = document.createElement('div');
193 document.body.appendChild(div);
194 var closeChain = function closeChain() {
195 _reactDom2.default.unmountComponentAtNode(div);
196 document.body.removeChild(div);
197 afterClose && afterClose();
198 };
199
200 var newContext = contextConfig;
201 if (!newContext) newContext = _configProvider2.default.getContext();
202
203 var mask = void 0,
204 myRef = void 0,
205 destroyed = false;
206 var destroy = function destroy() {
207 var inc = mask && mask.getInstance();
208 inc && inc.handleClose(true);
209 destroyed = true;
210 };
211
212 _reactDom2.default.render(_react2.default.createElement(
213 _configProvider2.default,
214 newContext,
215 _react2.default.createElement(NewMask, (0, _extends3.default)({
216 afterClose: closeChain
217 }, others, {
218 ref: function ref(_ref) {
219 myRef = _ref;
220 }
221 }))
222 ), div, function () {
223 mask = myRef;
224 if (mask && destroyed) {
225 destroy();
226 }
227 });
228
229 return {
230 component: mask,
231 destroy: destroy
232 };
233};
234
235function handleConfig(config, type) {
236 var newConfig = {};
237
238 if (typeof config === 'string' || _react2.default.isValidElement(config)) {
239 newConfig.title = config;
240 } else if (isObject(config)) {
241 newConfig = (0, _extends3.default)({}, config);
242 }
243 if (typeof newConfig.duration !== 'number') {
244 newConfig.duration = 3000;
245 }
246 if (type) {
247 newConfig.type = type;
248 }
249
250 return newConfig;
251}
252
253function isObject(obj) {
254 return {}.toString.call(obj) === '[object Object]';
255}
256
257function open(config, type) {
258 close();
259 config = handleConfig(config, type);
260 var timeoutId = (0, _util.guid)();
261 instance = create((0, _extends3.default)({}, config, { timeoutId: timeoutId }));
262
263 if (config.duration > 0) {
264 var timeout = setTimeout(close, config.duration);
265 timeouts[timeoutId] = timeout;
266 }
267}
268
269function close() {
270 if (instance) {
271 instance.destroy();
272 instance = null;
273 }
274}
275
276/**
277 * 创建提示弹层
278 * @exportName show
279 * @param {Object} props 属性对象
280 */
281function _show(config) {
282 open(config);
283}
284
285/**
286 * 关闭提示弹层
287 * @exportName hide
288 */
289function hide() {
290 close();
291}
292
293/**
294 * 创建成功提示弹层
295 * @exportName success
296 * @param {Object} props 属性对象
297 */
298function _success(config) {
299 open(config, 'success');
300}
301
302/**
303 * 创建警告提示弹层
304 * @exportName warning
305 * @param {Object} props 属性对象
306 */
307function _warning(config) {
308 open(config, 'warning');
309}
310
311/**
312 * 创建错误提示弹层
313 * @exportName error
314 * @param {Object} props 属性对象
315 */
316function _error(config) {
317 open(config, 'error');
318}
319
320/**
321 * 创建帮助提示弹层
322 * @exportName help
323 * @param {Object} props 属性对象
324 */
325function _help(config) {
326 open(config, 'help');
327}
328
329/**
330 * 创建加载中提示弹层
331 * @exportName loading
332 * @param {Object} props 属性对象
333 */
334function _loading(config) {
335 open(config, 'loading');
336}
337
338/**
339 * 创建通知提示弹层
340 * @exportName notice
341 * @param {Object} props 属性对象
342 */
343function _notice(config) {
344 open(config, 'notice');
345}
346
347exports.default = {
348 show: _show,
349 hide: hide,
350 success: _success,
351 warning: _warning,
352 error: _error,
353 help: _help,
354 loading: _loading,
355 notice: _notice
356};
357var withContext = exports.withContext = function withContext(WrappedComponent) {
358 var HOC = function HOC(props) {
359 return _react2.default.createElement(
360 _configProvider2.default.Consumer,
361 null,
362 function (contextConfig) {
363 return _react2.default.createElement(WrappedComponent, (0, _extends3.default)({}, props, {
364 contextMessage: {
365 show: function show() {
366 var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
367 return _show((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
368 },
369 hide: hide,
370 success: function success() {
371 var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
372 return _success((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
373 },
374 warning: function warning() {
375 var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
376 return _warning((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
377 },
378 error: function error() {
379 var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
380 return _error((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
381 },
382 help: function help() {
383 var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
384 return _help((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
385 },
386 loading: function loading() {
387 var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
388 return _loading((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
389 },
390 notice: function notice() {
391 var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
392 return _notice((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
393 }
394 }
395 }));
396 }
397 );
398 };
399 return HOC;
400};
\No newline at end of file