choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
211 lines (175 loc) • 6.01 kB
JavaScript
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function _createSuper(Derived) {
function isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
return true;
} catch (e) {
return false;
}
}
return function () {
var Super = _getPrototypeOf(Derived),
result;
if (isNativeReflectConstruct()) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import Animate from '../../animate';
import createChainedFunction from '../util/createChainedFunction';
import classnames from 'classnames';
import Notice from './Notice';
var seed = 0;
var now = Date.now();
function getUuid() {
return "rcNotification_".concat(now, "_").concat(seed++);
}
var Notification =
/*#__PURE__*/
function (_Component) {
_inherits(Notification, _Component);
var _super = _createSuper(Notification);
function Notification() {
var _this;
_classCallCheck(this, Notification);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
_defineProperty(_assertThisInitialized(_this), "state", {
notices: []
});
_defineProperty(_assertThisInitialized(_this), "add", function (notice) {
var key = notice.key = notice.key || getUuid();
_this.setState(function (previousState) {
var notices = previousState.notices;
if (!notices.filter(function (v) {
return v.key === key;
}).length) {
return {
notices: notices.concat(notice)
};
}
});
});
_defineProperty(_assertThisInitialized(_this), "remove", function (key) {
_this.setState(function (previousState) {
return {
notices: previousState.notices.filter(function (notice) {
return notice.key !== key;
})
};
});
});
return _this;
}
_createClass(Notification, [{
key: "getTransitionName",
value: function getTransitionName() {
var props = this.props;
var transitionName = props.transitionName;
if (!transitionName && props.animation) {
transitionName = "".concat(props.prefixCls, "-").concat(props.animation);
}
return transitionName;
}
}, {
key: "render",
value: function render() {
var _this2 = this,
_className;
var props = this.props;
var contentClassName = props.contentClassName;
var noticeNodes = this.state.notices.map(function (notice) {
var onClose = createChainedFunction(_this2.remove.bind(_this2, notice.key), notice.onClose);
return React.createElement(Notice, _extends({
prefixCls: props.prefixCls,
contentClassName: contentClassName
}, notice, {
onClose: onClose,
closeIcon: props.closeIcon
}), notice.content);
});
var className = (_className = {}, _defineProperty(_className, props.prefixCls, 1), _defineProperty(_className, props.className, !!props.className), _className);
return React.createElement("div", {
className: classnames(className),
style: props.style
}, React.createElement(Animate, {
transitionName: this.getTransitionName()
}, noticeNodes));
}
}]);
return Notification;
}(Component);
_defineProperty(Notification, "propTypes", {
prefixCls: PropTypes.string,
transitionName: PropTypes.string,
animation: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
style: PropTypes.object,
closeIcon: PropTypes.node,
contentClassName: PropTypes.string
});
_defineProperty(Notification, "defaultProps", {
prefixCls: 'rc-notification',
animation: 'fade',
style: {
top: 65,
left: '50%'
}
});
_defineProperty(Notification, "newInstance", function newNotificationInstance(properties, callback) {
var _ref = properties || {},
getContainer = _ref.getContainer,
props = _objectWithoutProperties(_ref, ["getContainer"]);
var div = document.createElement('div');
if (getContainer) {
var root = getContainer();
root.appendChild(div);
} else {
document.body.appendChild(div);
}
var called = false;
function ref(notification) {
if (called) {
return;
}
called = true;
callback({
notice: function notice(noticeProps) {
notification.add(noticeProps);
},
removeNotice: function removeNotice(key) {
notification.remove(key);
},
component: notification,
destroy: function destroy() {
ReactDOM.unmountComponentAtNode(div);
div.parentNode.removeChild(div);
}
});
}
ReactDOM.render(React.createElement(Notification, _extends({}, props, {
ref: ref
})), div);
});
export { Notification as default };
//# sourceMappingURL=Notification.js.map