UNPKG

@jdcfe/yep-react

Version:

一套移动端的React组件库

155 lines (138 loc) 5.35 kB
import _extends from "@babel/runtime/helpers/extends"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; 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"; function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } import * as React from 'react'; import * as ReactDOM from 'react-dom'; import { CSSTransition } from 'react-transition-group'; import classNames from 'classnames'; import Mask from '../mask'; var Notification = /*#__PURE__*/function (_React$PureComponent) { _inherits(Notification, _React$PureComponent); var _super = _createSuper(Notification); function Notification(props) { var _this; _classCallCheck(this, Notification); _this = _super.call(this, props); _this.close = _this.close.bind(_assertThisInitialized(_this)); _this.startCloseTimer = _this.startCloseTimer.bind(_assertThisInitialized(_this)); _this.clearCloseTimer = _this.clearCloseTimer.bind(_assertThisInitialized(_this)); _this.close = _this.close.bind(_assertThisInitialized(_this)); _this.state = { show: true }; return _this; } _createClass(Notification, [{ key: "close", value: function close() { this.setState({ show: false }); this.clearCloseTimer(); this.props.onClose(); } }, { key: "startCloseTimer", value: function startCloseTimer() { var _this2 = this; var duration = this.props.duration; if (duration) { this.closeTimer = window.setTimeout(function () { _this2.close(); }, duration * 1000); } } }, { key: "clearCloseTimer", value: function clearCloseTimer() { if (this.closeTimer) { clearTimeout(this.closeTimer); this.closeTimer = -1; } } }, { key: "componentDidMount", value: function componentDidMount() { this.startCloseTimer(); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.clearCloseTimer(); } }, { key: "render", value: function render() { var _this$props = this.props, className = _this$props.className, prefixCls = _this$props.prefixCls, style = _this$props.style, icon = _this$props.icon, message = _this$props.message, bottom = _this$props.bottom, mask = _this$props.mask; var show = this.state.show; var cls = classNames(prefixCls, className, "".concat(prefixCls, "-mask"), _defineProperty({}, "".concat(prefixCls, "-bottom"), bottom)); return /*#__PURE__*/React.createElement(React.Fragment, null, mask && /*#__PURE__*/React.createElement(CSSTransition, { in: show, timeout: 300, classNames: 'fade', unmountOnExit: true }, /*#__PURE__*/React.createElement(Mask, { transparent: true })), /*#__PURE__*/React.createElement(CSSTransition, { in: show, timeout: 300, classNames: "fade", unmountOnExit: true }, /*#__PURE__*/React.createElement("div", { className: cls, style: style }, /*#__PURE__*/React.createElement("div", { className: "".concat(prefixCls, "-notice") }, icon && /*#__PURE__*/React.createElement("div", { className: "".concat(prefixCls, "-notice-icon") }, icon), /*#__PURE__*/React.createElement("div", { className: "".concat(prefixCls, "-notice-message") }, message))))); } }]); return Notification; }(React.PureComponent); export { Notification as default }; Notification.defaultProps = { prefixCls: 'Yep-toast', style: {}, //show:false, bottom: false, duration: 1.5, mask: false }; Notification.newInstance = function (properties, callback) { var div = document.createElement('div'); document.body.appendChild(div); var called = false; function ref(instance) { if (called) { return; } called = true; callback({ component: instance, destroy: function destroy() { ReactDOM.unmountComponentAtNode(div); div && div.parentNode && div.parentNode.removeChild(div); } }); } ReactDOM.render( /*#__PURE__*/React.createElement(Notification, _extends({}, properties, { ref: ref })), div); };