UNPKG

@txdfe/at

Version:

一个设计体系组件库

243 lines (180 loc) 11.4 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _classnames = _interopRequireDefault(require("classnames")); var _configProvider = _interopRequireDefault(require("../config-provider")); var _reactTransitionGroup = require("react-transition-group"); var _toastItem = _interopRequireDefault(require("./toast-item.component")); var _util = require("../util"); var _excluded = ["clearTimeout", "className", "key", "startTimeout", "onClose", "onPause", "onStart"]; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } 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 _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); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } 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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); 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); } 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 _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } 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 _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; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } 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; } var ToastContainer = /*#__PURE__*/function (_React$Component) { _inherits(ToastContainer, _React$Component); var _super = _createSuper(ToastContainer); function ToastContainer(props, context) { var _this; _classCallCheck(this, ToastContainer); _this = _super.call(this, props, context); _defineProperty(_assertThisInitialized(_this), "findToast", function (key) { var toasts = _this.props.toasts; if (!Array.isArray(toasts)) { return null; } var toastIds = toasts.map(function (toast) { return toast.key; }); var index = toastIds.indexOf(key); return index > -1 ? toasts[index] : null; }); _defineProperty(_assertThisInitialized(_this), "handleClose", function (key) { var toast = _this.findToast(key); if (toast) { var clearTimeout = toast.clearTimeout, onClose = toast.onClose; clearTimeout(); if (_util.func.isFunction(onClose)) { onClose(key); } } _this.props.onClose(key); }); _defineProperty(_assertThisInitialized(_this), "handlePause", function (key) { var toast = _this.findToast(key); if (toast) { var clearTimeout = toast.clearTimeout, onPause = toast.onPause; clearTimeout(); if (_util.func.isFunction(onPause)) { onPause(key); } } }); _defineProperty(_assertThisInitialized(_this), "handleRemove", function (componentProps) { var key = componentProps.key, onRemove = componentProps.onRemove; _this.props.onClose(key); if (_util.func.isFunction(onRemove)) { onRemove(key); } }); _defineProperty(_assertThisInitialized(_this), "handleStart", function (key) { var toast = _this.findToast(key); if (toast) { var startTimeout = toast.startTimeout, onStart = toast.onStart; startTimeout(); if (_util.func.isFunction(onStart)) { onStart(key); } } }); _defineProperty(_assertThisInitialized(_this), "renderToast", function (componentProps, toastPrefix) { var _cx; var clearTimeout = componentProps.clearTimeout, className = componentProps.className, key = componentProps.key, startTimeout = componentProps.startTimeout, onClose = componentProps.onClose, onPause = componentProps.onPause, onStart = componentProps.onStart, restProps = _objectWithoutProperties(componentProps, _excluded); var cls = (0, _classnames["default"])((_cx = {}, _defineProperty(_cx, "".concat(toastPrefix, "-container-child"), true), _defineProperty(_cx, className, !!className), _defineProperty(_cx, "".concat(toastPrefix, "-item"), true), _cx)); var childProps = _objectSpread(_objectSpread({}, restProps), {}, { className: cls, key: key, onRemove: _this.handleRemove.bind(_assertThisInitialized(_this), componentProps) }); if (componentProps.closeable) { childProps = _objectSpread(_objectSpread({}, childProps), {}, { onClose: _this.handleClose.bind(_assertThisInitialized(_this), key) }); } if (componentProps.pauseable) { childProps = _objectSpread(_objectSpread({}, childProps), {}, { onStart: _this.handleStart.bind(_assertThisInitialized(_this), key), onPause: _this.handlePause.bind(_assertThisInitialized(_this), key) }); } var Tmpl = _this.state.tmpl; return /*#__PURE__*/_react["default"].createElement(Tmpl, childProps); }); _this.state = { tmpl: props.tmpl }; return _this; } _createClass(ToastContainer, [{ key: "replaceTemplate", value: function replaceTemplate(tmpl) { this.setState({ tmpl: tmpl }); } }, { key: "render", value: function render() { var _cx2, _this2 = this; var _this$props = this.props, prefix = _this$props.prefix, toasts = _this$props.toasts, className = _this$props.className, placement = _this$props.placement; var toastPrefix = "".concat(prefix, "toast"); var cls = (0, _classnames["default"])((_cx2 = {}, _defineProperty(_cx2, "".concat(toastPrefix, "-container"), true), _defineProperty(_cx2, "".concat(toastPrefix, "-container-").concat(placement), true), _defineProperty(_cx2, className, !!className), _cx2)); var toastGroup = toasts.map(function (toast) { return _this2.renderToast(toast, toastPrefix); }); return /*#__PURE__*/_react["default"].createElement(_reactTransitionGroup.TransitionGroup, { component: "div", className: cls }, toastGroup); } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(props, state) { if (state && state.tmpl && props.tmpl !== state.tmpl) { return { tmpl: props.tmpl }; } return null; } }]); return ToastContainer; }(_react["default"].Component); _defineProperty(ToastContainer, "propTypes", { prefix: _propTypes["default"].string, toasts: _propTypes["default"].array, placement: _propTypes["default"].string, tmpl: _propTypes["default"].any, onClose: _propTypes["default"].func, className: _propTypes["default"].string }); _defineProperty(ToastContainer, "defaultProps", { prefix: 'next-', placement: 'bottomLeft', tmpl: _toastItem["default"] }); var _default = _configProvider["default"].config(ToastContainer); exports["default"] = _default;