249 lines
10 KiB
JavaScript
249 lines
10 KiB
JavaScript
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
|
|
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
|
|
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
|
|
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
|
|
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
|
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
|
|
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
|
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
|
|
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
|
|
|
|
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
|
|
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
|
|
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
|
|
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
|
|
import React, { Component } from 'react';
|
|
import ReactDOM from 'react-dom';
|
|
import Animate from 'rc-animate';
|
|
import createChainedFunction from "rc-util/es/createChainedFunction";
|
|
import classnames from 'classnames';
|
|
import Notice from './Notice';
|
|
import _useNotification from './useNotification';
|
|
var seed = 0;
|
|
var now = Date.now();
|
|
|
|
function getUuid() {
|
|
var id = seed;
|
|
seed += 1;
|
|
return "rcNotification_".concat(now, "_").concat(id);
|
|
}
|
|
|
|
var Notification =
|
|
/*#__PURE__*/
|
|
function (_Component) {
|
|
_inherits(Notification, _Component);
|
|
|
|
function Notification() {
|
|
var _this;
|
|
|
|
_classCallCheck(this, Notification);
|
|
|
|
_this = _possibleConstructorReturn(this, _getPrototypeOf(Notification).apply(this, arguments));
|
|
_this.state = {
|
|
notices: []
|
|
};
|
|
_this.hookRefs = new Map();
|
|
|
|
_this.add = function (notice, holderCallback) {
|
|
notice.key = notice.key || getUuid();
|
|
var key = notice.key;
|
|
var maxCount = _this.props.maxCount;
|
|
|
|
_this.setState(function (previousState) {
|
|
var notices = previousState.notices;
|
|
var noticeIndex = notices.map(function (v) {
|
|
return v.notice.key;
|
|
}).indexOf(key);
|
|
var updatedNotices = notices.concat();
|
|
|
|
if (noticeIndex !== -1) {
|
|
updatedNotices.splice(noticeIndex, 1, {
|
|
notice: notice,
|
|
holderCallback: holderCallback
|
|
});
|
|
} else {
|
|
if (maxCount && notices.length >= maxCount) {
|
|
// XXX, use key of first item to update new added (let React to move exsiting
|
|
// instead of remove and mount). Same key was used before for both a) external
|
|
// manual control and b) internal react 'key' prop , which is not that good.
|
|
notice.updateKey = updatedNotices[0].notice.updateKey || updatedNotices[0].notice.key;
|
|
updatedNotices.shift();
|
|
}
|
|
|
|
updatedNotices.push({
|
|
notice: notice,
|
|
holderCallback: holderCallback
|
|
});
|
|
}
|
|
|
|
return {
|
|
notices: updatedNotices
|
|
};
|
|
});
|
|
};
|
|
|
|
_this.remove = function (key) {
|
|
_this.setState(function (previousState) {
|
|
return {
|
|
notices: previousState.notices.filter(function (_ref) {
|
|
var notice = _ref.notice;
|
|
return notice.key !== key;
|
|
})
|
|
};
|
|
});
|
|
};
|
|
|
|
return _this;
|
|
}
|
|
|
|
_createClass(Notification, [{
|
|
key: "getTransitionName",
|
|
value: function getTransitionName() {
|
|
var _this$props = this.props,
|
|
prefixCls = _this$props.prefixCls,
|
|
animation = _this$props.animation;
|
|
var transitionName = this.props.transitionName;
|
|
|
|
if (!transitionName && animation) {
|
|
transitionName = "".concat(prefixCls, "-").concat(animation);
|
|
}
|
|
|
|
return transitionName;
|
|
}
|
|
}, {
|
|
key: "render",
|
|
value: function render() {
|
|
var _this2 = this;
|
|
|
|
var notices = this.state.notices;
|
|
var _this$props2 = this.props,
|
|
prefixCls = _this$props2.prefixCls,
|
|
className = _this$props2.className,
|
|
closeIcon = _this$props2.closeIcon,
|
|
style = _this$props2.style;
|
|
var noticeNodes = notices.map(function (_ref2, index) {
|
|
var notice = _ref2.notice,
|
|
holderCallback = _ref2.holderCallback;
|
|
var update = Boolean(index === notices.length - 1 && notice.updateKey);
|
|
var key = notice.updateKey ? notice.updateKey : notice.key;
|
|
var onClose = createChainedFunction(_this2.remove.bind(_this2, notice.key), notice.onClose);
|
|
|
|
var noticeProps = _objectSpread({
|
|
prefixCls: prefixCls,
|
|
closeIcon: closeIcon
|
|
}, notice, {
|
|
key: key,
|
|
update: update,
|
|
onClose: onClose,
|
|
onClick: notice.onClick,
|
|
children: notice.content
|
|
});
|
|
|
|
if (holderCallback) {
|
|
return React.createElement("div", {
|
|
key: key,
|
|
className: "".concat(prefixCls, "-hook-holder"),
|
|
ref: function ref(div) {
|
|
if (div) {
|
|
_this2.hookRefs.set(key, div);
|
|
|
|
holderCallback(div, noticeProps);
|
|
} else {
|
|
_this2.hookRefs.delete(key);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
return React.createElement(Notice, Object.assign({}, noticeProps));
|
|
});
|
|
return React.createElement("div", {
|
|
className: classnames(prefixCls, className),
|
|
style: style
|
|
}, React.createElement(Animate, {
|
|
transitionName: this.getTransitionName()
|
|
}, noticeNodes));
|
|
}
|
|
}]);
|
|
|
|
return Notification;
|
|
}(Component);
|
|
|
|
Notification.defaultProps = {
|
|
prefixCls: 'rc-notification',
|
|
animation: 'fade',
|
|
style: {
|
|
top: 65,
|
|
left: '50%'
|
|
}
|
|
};
|
|
|
|
Notification.newInstance = function newNotificationInstance(properties, callback) {
|
|
var _ref3 = properties || {},
|
|
getContainer = _ref3.getContainer,
|
|
props = _objectWithoutProperties(_ref3, ["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);
|
|
},
|
|
// Hooks
|
|
useNotification: function useNotification() {
|
|
return _useNotification(notification);
|
|
}
|
|
});
|
|
} // Only used for test case usage
|
|
|
|
|
|
if (process.env.NODE_ENV === 'test' && properties.TEST_RENDER) {
|
|
properties.TEST_RENDER(React.createElement(Notification, Object.assign({}, props, {
|
|
ref: ref
|
|
})));
|
|
return;
|
|
}
|
|
|
|
ReactDOM.render(React.createElement(Notification, Object.assign({}, props, {
|
|
ref: ref
|
|
})), div);
|
|
};
|
|
|
|
export default Notification; |