1 | 'use strict';
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.withContext = exports.confirm = exports.help = exports.warning = exports.notice = exports.error = exports.success = exports.alert = exports.show = exports.ModalInner = undefined;
|
5 |
|
6 | var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
|
7 |
|
8 | var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
|
9 |
|
10 | var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
11 |
|
12 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
13 |
|
14 | var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
|
15 |
|
16 | var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
|
17 |
|
18 | var _inherits2 = require('babel-runtime/helpers/inherits');
|
19 |
|
20 | var _inherits3 = _interopRequireDefault(_inherits2);
|
21 |
|
22 | var _extends2 = require('babel-runtime/helpers/extends');
|
23 |
|
24 | var _extends3 = _interopRequireDefault(_extends2);
|
25 |
|
26 | var _class, _temp2;
|
27 |
|
28 | var _react = require('react');
|
29 |
|
30 | var _react2 = _interopRequireDefault(_react);
|
31 |
|
32 | var _reactDom = require('react-dom');
|
33 |
|
34 | var _reactDom2 = _interopRequireDefault(_reactDom);
|
35 |
|
36 | var _propTypes = require('prop-types');
|
37 |
|
38 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
39 |
|
40 | var _classnames = require('classnames');
|
41 |
|
42 | var _classnames2 = _interopRequireDefault(_classnames);
|
43 |
|
44 | var _configProvider = require('../config-provider');
|
45 |
|
46 | var _configProvider2 = _interopRequireDefault(_configProvider);
|
47 |
|
48 | var _message = require('../message');
|
49 |
|
50 | var _message2 = _interopRequireDefault(_message);
|
51 |
|
52 | var _zhCn = require('../locale/zh-cn');
|
53 |
|
54 | var _zhCn2 = _interopRequireDefault(_zhCn);
|
55 |
|
56 | var _dialog = require('./dialog');
|
57 |
|
58 | var _dialog2 = _interopRequireDefault(_dialog);
|
59 |
|
60 | var _dialogV = require('./dialog-v2');
|
61 |
|
62 | var _dialogV2 = _interopRequireDefault(_dialogV);
|
63 |
|
64 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
65 |
|
66 | var Dialog = _configProvider2.default.config(_dialog2.default);
|
67 | var Dialog2 = _configProvider2.default.config(_dialogV2.default);
|
68 |
|
69 | var noop = function noop() {};
|
70 | var MESSAGE_TYPE = {
|
71 | alert: 'warning',
|
72 | confirm: 'help',
|
73 |
|
74 | success: 'success',
|
75 | error: 'error',
|
76 | warning: 'warning',
|
77 | notice: 'notice',
|
78 | help: 'help'
|
79 | };
|
80 |
|
81 | var ModalInner = exports.ModalInner = function ModalInner(_ref) {
|
82 | var type = _ref.type,
|
83 | _ref$messageProps = _ref.messageProps,
|
84 | messageProps = _ref$messageProps === undefined ? {} : _ref$messageProps,
|
85 | title = _ref.title,
|
86 | rtl = _ref.rtl,
|
87 | _ref$prefix = _ref.prefix,
|
88 | prefix = _ref$prefix === undefined ? 'next-' : _ref$prefix,
|
89 | content = _ref.content;
|
90 |
|
91 | return _react2.default.createElement(
|
92 | _message2.default,
|
93 | (0, _extends3.default)({
|
94 | size: 'large',
|
95 | shape: 'addon',
|
96 | type: MESSAGE_TYPE[type]
|
97 | }, messageProps, {
|
98 | title: title,
|
99 | rtl: rtl,
|
100 | className: (0, _classnames2.default)(prefix + 'dialog-message', messageProps.className)
|
101 | }),
|
102 | content
|
103 | );
|
104 | };
|
105 |
|
106 | var Modal = (_temp2 = _class = function (_Component) {
|
107 | (0, _inherits3.default)(Modal, _Component);
|
108 |
|
109 | function Modal() {
|
110 | var _temp, _this, _ret;
|
111 |
|
112 | (0, _classCallCheck3.default)(this, Modal);
|
113 |
|
114 | for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
|
115 | args[_key] = arguments[_key];
|
116 | }
|
117 |
|
118 | return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.state = {
|
119 | visible: true,
|
120 | loading: false
|
121 | }, _this.close = function () {
|
122 | _this.setState({
|
123 | visible: false
|
124 | });
|
125 | }, _this.loading = function (loading) {
|
126 | _this.setState({
|
127 | loading: loading
|
128 | });
|
129 | }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
|
130 | }
|
131 |
|
132 | Modal.prototype.wrapper = function wrapper(fn, callback) {
|
133 | var _this2 = this;
|
134 |
|
135 | return function () {
|
136 | var res = fn.apply(undefined, arguments);
|
137 | if (res && res.then) {
|
138 | _this2.loading(true);
|
139 |
|
140 | res.then(function (result) {
|
141 | _this2.loading(false);
|
142 |
|
143 | if (result !== false) {
|
144 | return callback();
|
145 | }
|
146 | }).catch(function (e) {
|
147 | _this2.loading(false);
|
148 | throw e;
|
149 | });
|
150 | } else if (res !== false) {
|
151 | return callback();
|
152 | }
|
153 | };
|
154 | };
|
155 |
|
156 | Modal.prototype.render = function render() {
|
157 | var _props = this.props,
|
158 | prefix = _props.prefix,
|
159 | type = _props.type,
|
160 | title = _props.title,
|
161 | content = _props.content,
|
162 | messageProps = _props.messageProps,
|
163 | footerActions = _props.footerActions,
|
164 | onOk = _props.onOk,
|
165 | onCancel = _props.onCancel,
|
166 | onClose = _props.onClose,
|
167 | okProps = _props.okProps,
|
168 | needWrapper = _props.needWrapper,
|
169 | rtl = _props.rtl,
|
170 | className = _props.className,
|
171 | v2 = _props.v2,
|
172 | _props$width = _props.width,
|
173 | width = _props$width === undefined ? 420 : _props$width,
|
174 | others = (0, _objectWithoutProperties3.default)(_props, ['prefix', 'type', 'title', 'content', 'messageProps', 'footerActions', 'onOk', 'onCancel', 'onClose', 'okProps', 'needWrapper', 'rtl', 'className', 'v2', 'width']);
|
175 |
|
176 | var newTitle = needWrapper && type ? null : title;
|
177 |
|
178 | var newContent = needWrapper && type ? _react2.default.createElement(ModalInner, {
|
179 | type: type,
|
180 | messageProps: messageProps,
|
181 | title: title,
|
182 | rtl: rtl,
|
183 | prefix: prefix,
|
184 | content: content
|
185 | }) : content;
|
186 |
|
187 | var newFooterActions = footerActions || (type === 'confirm' ? ['ok', 'cancel'] : ['alert', 'success', 'error', 'notice', 'warning', 'help'].indexOf(type) > -1 ? ['ok'] : undefined);
|
188 | var newOnOk = this.wrapper(onOk, this.close);
|
189 | var newOnCancel = this.wrapper(onCancel, this.close);
|
190 | var newOnClose = this.wrapper(onClose, this.close);
|
191 |
|
192 | var _state = this.state,
|
193 | visible = _state.visible,
|
194 | loading = _state.loading;
|
195 |
|
196 |
|
197 |
|
198 | var newOkProps = (0, _extends3.default)({}, okProps);
|
199 | if (!('loading' in okProps)) {
|
200 | newOkProps.loading = loading;
|
201 | }
|
202 |
|
203 | var classNames = (0, _classnames2.default)(prefix + 'dialog-quick', className);
|
204 |
|
205 | var Tag = v2 ? Dialog2 : Dialog;
|
206 |
|
207 | return _react2.default.createElement(
|
208 | Tag,
|
209 | (0, _extends3.default)({
|
210 | prefix: prefix,
|
211 | role: 'alertdialog'
|
212 | }, others, {
|
213 | visible: visible,
|
214 | title: newTitle,
|
215 | rtl: rtl,
|
216 | footerActions: newFooterActions,
|
217 | onOk: this.state.loading ? noop : newOnOk,
|
218 | onCancel: newOnCancel,
|
219 | onClose: newOnClose,
|
220 | okProps: newOkProps,
|
221 | className: classNames,
|
222 | width: v2 ? width : undefined
|
223 | }),
|
224 | newContent
|
225 | );
|
226 | };
|
227 |
|
228 | return Modal;
|
229 | }(_react.Component), _class.propTypes = {
|
230 | prefix: _propTypes2.default.string,
|
231 | pure: _propTypes2.default.bool,
|
232 | rtl: _propTypes2.default.bool,
|
233 | type: _propTypes2.default.oneOf(['alert', 'confirm', 'success', 'error', 'notice', 'warning', 'help']),
|
234 | title: _propTypes2.default.node,
|
235 | content: _propTypes2.default.node,
|
236 | messageProps: _propTypes2.default.object,
|
237 | footerActions: _propTypes2.default.array,
|
238 | |
239 |
|
240 |
|
241 |
|
242 |
|
243 | onOk: _propTypes2.default.func,
|
244 | |
245 |
|
246 |
|
247 |
|
248 |
|
249 | onCancel: _propTypes2.default.func,
|
250 | |
251 |
|
252 |
|
253 |
|
254 |
|
255 | onClose: _propTypes2.default.func,
|
256 | okProps: _propTypes2.default.object,
|
257 | locale: _propTypes2.default.object,
|
258 | needWrapper: _propTypes2.default.bool,
|
259 | className: _propTypes2.default.string
|
260 | }, _class.defaultProps = {
|
261 | prefix: 'next-',
|
262 | pure: false,
|
263 | messageProps: {},
|
264 | onOk: noop,
|
265 | onCancel: noop,
|
266 | onClose: noop,
|
267 | okProps: {},
|
268 | locale: _zhCn2.default.Dialog,
|
269 | needWrapper: true
|
270 | }, _temp2);
|
271 | Modal.displayName = 'Modal';
|
272 |
|
273 |
|
274 | var ConfigModal = _configProvider2.default.config(Modal, { componentName: 'Dialog' });
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 | var _show = function _show() {
|
283 | var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
284 |
|
285 | var container = document.createElement('div');
|
286 | var unmount = function unmount() {
|
287 | if (config.afterClose) {
|
288 | config.afterClose();
|
289 | }
|
290 | _reactDom2.default.unmountComponentAtNode(container);
|
291 | container.parentNode.removeChild(container);
|
292 | };
|
293 |
|
294 | document.body.appendChild(container);
|
295 | var newContext = config.contextConfig;
|
296 | if (!newContext) newContext = _configProvider2.default.getContext();
|
297 |
|
298 | var instance = void 0,
|
299 | myRef = void 0;
|
300 |
|
301 | _reactDom2.default.render(_react2.default.createElement(
|
302 | _configProvider2.default,
|
303 | newContext,
|
304 | _react2.default.createElement(ConfigModal, (0, _extends3.default)({}, config, {
|
305 | afterClose: unmount,
|
306 | ref: function ref(_ref2) {
|
307 | myRef = _ref2;
|
308 | }
|
309 | }))
|
310 | ), container, function () {
|
311 | instance = myRef;
|
312 | });
|
313 | return {
|
314 | hide: function hide() {
|
315 | var inc = instance && instance.getInstance();
|
316 | inc && inc.close();
|
317 | }
|
318 | };
|
319 | };
|
320 |
|
321 | exports.show = _show;
|
322 | var methodFactory = function methodFactory(type) {
|
323 | return function () {
|
324 | var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
325 |
|
326 | config.type = type;
|
327 | return _show(config);
|
328 | };
|
329 | };
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 | var _alert = methodFactory('alert');
|
338 |
|
339 | exports.alert = _alert;
|
340 | var _success = methodFactory('success');
|
341 | exports.success = _success;
|
342 | var _error = methodFactory('error');
|
343 | exports.error = _error;
|
344 | var _notice = methodFactory('notice');
|
345 | exports.notice = _notice;
|
346 | var _warning = methodFactory('warning');
|
347 | exports.warning = _warning;
|
348 | var _help = methodFactory('help');
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 | exports.help = _help;
|
357 | var _confirm = methodFactory('confirm');
|
358 |
|
359 | exports.confirm = _confirm;
|
360 | var withContext = exports.withContext = function withContext(WrappedComponent) {
|
361 | var HOC = function HOC(props) {
|
362 | return _react2.default.createElement(
|
363 | _configProvider2.default.Consumer,
|
364 | null,
|
365 | function (contextConfig) {
|
366 | return _react2.default.createElement(WrappedComponent, (0, _extends3.default)({}, props, {
|
367 | contextDialog: {
|
368 | show: function show() {
|
369 | var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
370 | return _show((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
|
371 | },
|
372 | alert: function alert() {
|
373 | var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
374 | return _alert((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
|
375 | },
|
376 | confirm: function confirm() {
|
377 | var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
378 | return _confirm((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
|
379 | },
|
380 | success: function success() {
|
381 | var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
382 | return _success((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
|
383 | },
|
384 | error: function error() {
|
385 | var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
386 | return _error((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
|
387 | },
|
388 | warning: function warning() {
|
389 | var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
390 | return _warning((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
|
391 | },
|
392 | notice: function notice() {
|
393 | var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
394 | return _notice((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
|
395 | },
|
396 | help: function help() {
|
397 | var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
398 | return _help((0, _extends3.default)({}, config, { contextConfig: contextConfig }));
|
399 | }
|
400 | }
|
401 | }));
|
402 | }
|
403 | );
|
404 | };
|
405 | return HOC;
|
406 | }; |
\ | No newline at end of file |