UNPKG

8.42 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _extends2 = require('babel-runtime/helpers/extends');
8
9var _extends3 = _interopRequireDefault(_extends2);
10
11var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
12
13var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
14
15var _createClass2 = require('babel-runtime/helpers/createClass');
16
17var _createClass3 = _interopRequireDefault(_createClass2);
18
19var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
20
21var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
22
23var _inherits2 = require('babel-runtime/helpers/inherits');
24
25var _inherits3 = _interopRequireDefault(_inherits2);
26
27var _react = require('react');
28
29var _react2 = _interopRequireDefault(_react);
30
31var _rcAnimate = require('rc-animate');
32
33var _rcAnimate2 = _interopRequireDefault(_rcAnimate);
34
35var _LazyRenderBox = require('./LazyRenderBox');
36
37var _LazyRenderBox2 = _interopRequireDefault(_LazyRenderBox);
38
39function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
40
41function noop() {}
42
43var Dialog = function (_React$Component) {
44 (0, _inherits3['default'])(Dialog, _React$Component);
45
46 function Dialog() {
47 (0, _classCallCheck3['default'])(this, Dialog);
48
49 var _this = (0, _possibleConstructorReturn3['default'])(this, (Dialog.__proto__ || Object.getPrototypeOf(Dialog)).apply(this, arguments));
50
51 _this.getDialogElement = function () {
52 var props = _this.props;
53 var closable = props.closable;
54 var prefixCls = props.prefixCls;
55 var footer = void 0;
56 if (props.footer) {
57 footer = _react2['default'].createElement("div", { className: prefixCls + '-footer', ref: function ref(el) {
58 return _this.footerRef = el;
59 } }, props.footer);
60 }
61 var header = void 0;
62 if (props.title) {
63 header = _react2['default'].createElement("div", { className: prefixCls + '-header', ref: function ref(el) {
64 return _this.headerRef = el;
65 } }, _react2['default'].createElement("div", { className: prefixCls + '-title' }, props.title));
66 }
67 var closer = void 0;
68 if (closable) {
69 closer = _react2['default'].createElement("button", { onClick: _this.close, "aria-label": "Close", className: prefixCls + '-close' }, _react2['default'].createElement("span", { className: prefixCls + '-close-x' }));
70 }
71 var transitionName = _this.getTransitionName();
72 var dialogElement = _react2['default'].createElement(_LazyRenderBox2['default'], { key: "dialog-element", role: "document", ref: function ref(el) {
73 return _this.dialogRef = el;
74 }, style: props.style || {}, className: prefixCls + ' ' + (props.className || ''), visible: props.visible }, _react2['default'].createElement("div", { className: prefixCls + '-content' }, closer, header, _react2['default'].createElement("div", { className: prefixCls + '-body', style: props.bodyStyle, ref: function ref(el) {
75 return _this.bodyRef = el;
76 } }, props.children), footer));
77 return _react2['default'].createElement(_rcAnimate2['default'], { key: "dialog", showProp: "visible", onAppear: _this.onAnimateAppear, onLeave: _this.onAnimateLeave, transitionName: transitionName, component: "", transitionAppear: true }, dialogElement);
78 };
79 _this.onAnimateAppear = function () {
80 document.body.style.overflow = 'hidden';
81 };
82 _this.onAnimateLeave = function () {
83 document.body.style.overflow = '';
84 if (_this.wrapRef) {
85 _this.wrapRef.style.display = 'none';
86 }
87 if (_this.props.onAnimateLeave) {
88 _this.props.onAnimateLeave();
89 }
90 if (_this.props.afterClose) {
91 _this.props.afterClose();
92 }
93 };
94 _this.close = function (e) {
95 if (_this.props.onClose) {
96 _this.props.onClose(e);
97 }
98 };
99 _this.onMaskClick = function (e) {
100 if (e.target === e.currentTarget) {
101 _this.close(e);
102 }
103 };
104 return _this;
105 }
106
107 (0, _createClass3['default'])(Dialog, [{
108 key: 'componentWillUnmount',
109 value: function componentWillUnmount() {
110 // fix: react@16 no dismissing animation
111 document.body.style.overflow = '';
112 if (this.wrapRef) {
113 this.wrapRef.style.display = 'none';
114 }
115 }
116 }, {
117 key: 'getZIndexStyle',
118 value: function getZIndexStyle() {
119 var style = {};
120 var props = this.props;
121 if (props.zIndex !== undefined) {
122 style.zIndex = props.zIndex;
123 }
124 return style;
125 }
126 }, {
127 key: 'getWrapStyle',
128 value: function getWrapStyle() {
129 var wrapStyle = this.props.wrapStyle || {};
130 return (0, _extends3['default'])({}, this.getZIndexStyle(), wrapStyle);
131 }
132 }, {
133 key: 'getMaskStyle',
134 value: function getMaskStyle() {
135 var maskStyle = this.props.maskStyle || {};
136 return (0, _extends3['default'])({}, this.getZIndexStyle(), maskStyle);
137 }
138 }, {
139 key: 'getMaskTransitionName',
140 value: function getMaskTransitionName() {
141 var props = this.props;
142 var transitionName = props.maskTransitionName;
143 var animation = props.maskAnimation;
144 if (!transitionName && animation) {
145 transitionName = props.prefixCls + '-' + animation;
146 }
147 return transitionName;
148 }
149 }, {
150 key: 'getTransitionName',
151 value: function getTransitionName() {
152 var props = this.props;
153 var transitionName = props.transitionName;
154 var animation = props.animation;
155 if (!transitionName && animation) {
156 transitionName = props.prefixCls + '-' + animation;
157 }
158 return transitionName;
159 }
160 }, {
161 key: 'getMaskElement',
162 value: function getMaskElement() {
163 var props = this.props;
164 var maskElement = void 0;
165 if (props.mask) {
166 var maskTransition = this.getMaskTransitionName();
167 maskElement = _react2['default'].createElement(_LazyRenderBox2['default'], (0, _extends3['default'])({ style: this.getMaskStyle(), key: "mask-element", className: props.prefixCls + '-mask', hiddenClassName: props.prefixCls + '-mask-hidden', visible: props.visible }, props.maskProps));
168 if (maskTransition) {
169 maskElement = _react2['default'].createElement(_rcAnimate2['default'], { key: "mask", showProp: "visible", transitionAppear: true, component: "", transitionName: maskTransition }, maskElement);
170 }
171 }
172 return maskElement;
173 }
174 }, {
175 key: 'render',
176 value: function render() {
177 var _this2 = this;
178
179 var props = this.props;
180 var prefixCls = props.prefixCls,
181 maskClosable = props.maskClosable;
182
183 var style = this.getWrapStyle();
184 if (props.visible) {
185 style.display = null;
186 }
187 return _react2['default'].createElement("div", null, this.getMaskElement(), _react2['default'].createElement("div", (0, _extends3['default'])({ className: prefixCls + '-wrap ' + (props.wrapClassName || ''), ref: function ref(el) {
188 return _this2.wrapRef = el;
189 }, onClick: maskClosable ? this.onMaskClick : undefined, role: "dialog", "aria-labelledby": props.title, style: style }, props.wrapProps), this.getDialogElement()));
190 }
191 }]);
192 return Dialog;
193}(_react2['default'].Component);
194
195exports['default'] = Dialog;
196
197Dialog.defaultProps = {
198 afterClose: noop,
199 className: '',
200 mask: true,
201 visible: false,
202 closable: true,
203 maskClosable: true,
204 prefixCls: 'rmc-dialog',
205 onClose: noop
206};
207module.exports = exports['default'];
\No newline at end of file