You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
7.1 KiB

import _extends from 'babel-runtime/helpers/extends';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
import React from 'react';
import Animate from 'rc-animate';
import LazyRenderBox from './LazyRenderBox';
function noop() {}
var Dialog = function (_React$Component) {
_inherits(Dialog, _React$Component);
function Dialog() {
_classCallCheck(this, Dialog);
var _this = _possibleConstructorReturn(this, (Dialog.__proto__ || Object.getPrototypeOf(Dialog)).apply(this, arguments));
_this.getDialogElement = function () {
var props = _this.props;
var closable = props.closable;
var prefixCls = props.prefixCls;
var footer = void 0;
if (props.footer) {
footer = React.createElement("div", { className: prefixCls + '-footer', ref: function ref(el) {
return _this.footerRef = el;
} }, props.footer);
}
var header = void 0;
if (props.title) {
header = React.createElement("div", { className: prefixCls + '-header', ref: function ref(el) {
return _this.headerRef = el;
} }, React.createElement("div", { className: prefixCls + '-title' }, props.title));
}
var closer = void 0;
if (closable) {
closer = React.createElement("button", { onClick: _this.close, "aria-label": "Close", className: prefixCls + '-close' }, React.createElement("span", { className: prefixCls + '-close-x' }));
}
var transitionName = _this.getTransitionName();
var dialogElement = React.createElement(LazyRenderBox, { key: "dialog-element", role: "document", ref: function ref(el) {
return _this.dialogRef = el;
}, style: props.style || {}, className: prefixCls + ' ' + (props.className || ''), visible: props.visible }, React.createElement("div", { className: prefixCls + '-content' }, closer, header, React.createElement("div", { className: prefixCls + '-body', style: props.bodyStyle, ref: function ref(el) {
return _this.bodyRef = el;
} }, props.children), footer));
return React.createElement(Animate, { key: "dialog", showProp: "visible", onAppear: _this.onAnimateAppear, onLeave: _this.onAnimateLeave, transitionName: transitionName, component: "", transitionAppear: true }, dialogElement);
};
_this.onAnimateAppear = function () {
document.body.style.overflow = 'hidden';
};
_this.onAnimateLeave = function () {
document.body.style.overflow = '';
if (_this.wrapRef) {
_this.wrapRef.style.display = 'none';
}
if (_this.props.onAnimateLeave) {
_this.props.onAnimateLeave();
}
if (_this.props.afterClose) {
_this.props.afterClose();
}
};
_this.close = function (e) {
if (_this.props.onClose) {
_this.props.onClose(e);
}
};
_this.onMaskClick = function (e) {
if (e.target === e.currentTarget) {
_this.close(e);
}
};
return _this;
}
_createClass(Dialog, [{
key: 'componentWillUnmount',
value: function componentWillUnmount() {
// fix: react@16 no dismissing animation
document.body.style.overflow = '';
if (this.wrapRef) {
this.wrapRef.style.display = 'none';
}
}
}, {
key: 'getZIndexStyle',
value: function getZIndexStyle() {
var style = {};
var props = this.props;
if (props.zIndex !== undefined) {
style.zIndex = props.zIndex;
}
return style;
}
}, {
key: 'getWrapStyle',
value: function getWrapStyle() {
var wrapStyle = this.props.wrapStyle || {};
return _extends({}, this.getZIndexStyle(), wrapStyle);
}
}, {
key: 'getMaskStyle',
value: function getMaskStyle() {
var maskStyle = this.props.maskStyle || {};
return _extends({}, this.getZIndexStyle(), maskStyle);
}
}, {
key: 'getMaskTransitionName',
value: function getMaskTransitionName() {
var props = this.props;
var transitionName = props.maskTransitionName;
var animation = props.maskAnimation;
if (!transitionName && animation) {
transitionName = props.prefixCls + '-' + animation;
}
return transitionName;
}
}, {
key: 'getTransitionName',
value: function getTransitionName() {
var props = this.props;
var transitionName = props.transitionName;
var animation = props.animation;
if (!transitionName && animation) {
transitionName = props.prefixCls + '-' + animation;
}
return transitionName;
}
}, {
key: 'getMaskElement',
value: function getMaskElement() {
var props = this.props;
var maskElement = void 0;
if (props.mask) {
var maskTransition = this.getMaskTransitionName();
maskElement = React.createElement(LazyRenderBox, _extends({ style: this.getMaskStyle(), key: "mask-element", className: props.prefixCls + '-mask', hiddenClassName: props.prefixCls + '-mask-hidden', visible: props.visible }, props.maskProps));
if (maskTransition) {
maskElement = React.createElement(Animate, { key: "mask", showProp: "visible", transitionAppear: true, component: "", transitionName: maskTransition }, maskElement);
}
}
return maskElement;
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
var props = this.props;
var prefixCls = props.prefixCls,
maskClosable = props.maskClosable;
var style = this.getWrapStyle();
if (props.visible) {
style.display = null;
}
return React.createElement("div", null, this.getMaskElement(), React.createElement("div", _extends({ className: prefixCls + '-wrap ' + (props.wrapClassName || ''), ref: function ref(el) {
return _this2.wrapRef = el;
}, onClick: maskClosable ? this.onMaskClick : undefined, role: "dialog", "aria-labelledby": props.title, style: style }, props.wrapProps), this.getDialogElement()));
}
}]);
return Dialog;
}(React.Component);
export default Dialog;
Dialog.defaultProps = {
afterClose: noop,
className: '',
mask: true,
visible: false,
closable: true,
maskClosable: true,
prefixCls: 'rmc-dialog',
onClose: noop
};