UNPKG

@txdfe/at

Version:

一个设计体系组件库

253 lines 10.7 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } import React from 'react'; import PropTypes from 'prop-types'; import cx from 'classnames'; import { ToastStatus } from './toast.schema'; import Icon from '../icon'; import Button from '../button'; import { func } from '../util'; import ConfigProvider from '../config-provider'; import { Transition, motion } from './base/transition'; var ToastItem = /*#__PURE__*/function (_React$Component) { function ToastItem() { var _this; _classCallCheck(this, ToastItem); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _callSuper(this, ToastItem, [].concat(args)); _defineProperty(_this, "handleClose", function () { if (func.isFunction(_this.props.onClose)) { _this.props.onClose(); } }); _defineProperty(_this, "handlePause", function () { if (func.isFunction(_this.props.onPause)) { _this.props.onPause(); } }); _defineProperty(_this, "handleStart", function () { if (func.isFunction(_this.props.onStart)) { _this.props.onStart(); } }); return _this; } _inherits(ToastItem, _React$Component); return _createClass(ToastItem, [{ key: "componentWillUnmount", value: function componentWillUnmount() { this.props.onRemove(); } }, { key: "getSuffix", value: function getSuffix() { switch (this.props.status) { case ToastStatus.Error: return 'error'; case ToastStatus.Success: return 'success'; case ToastStatus.Info: return 'info'; case ToastStatus.Warning: return 'warning'; default: return 'info'; } } }, { key: "getStatusClassName", value: function getStatusClassName(classNamePrefix) { var suffix = this.getSuffix(); return cx(classNamePrefix, suffix ? "".concat(classNamePrefix, "-is-").concat(suffix) : null); } }, { key: "getStatusIconName", value: function getStatusIconName() { switch (this.props.status) { case ToastStatus.Success: return 'tick-o-fill'; case ToastStatus.Warning: return 'warning-o-fill'; case ToastStatus.Error: return 'remove-o-fill'; default: return 'info-o-fill'; } } }, { key: "renderContent", value: function renderContent(toastPrefix) { var cls = "".concat(toastPrefix, "-content"); return /*#__PURE__*/React.createElement("div", { className: cls }, this.renderTitle(toastPrefix)); } }, { key: "renderTitle", value: function renderTitle(toastPrefix) { var titleCls = "".concat(toastPrefix, "-title"); var textCls = "".concat(toastPrefix, "-title-text"); return /*#__PURE__*/React.createElement("div", { className: titleCls }, this.renderIcon(toastPrefix), /*#__PURE__*/React.createElement("div", { className: textCls }, this.renderMessage(toastPrefix), this.renderHandlers(toastPrefix), this.renderDescription(toastPrefix)), this.renderCloseHandler(toastPrefix)); } }, { key: "renderHandlers", value: function renderHandlers(toastPrefix) { var _this$props = this.props, handlers = _this$props.handlers, onClickHandlers = _this$props.onClickHandlers; if (!handlers) { return null; } var handlerCls = "".concat(toastPrefix, "-handlers"); if (typeof handlers !== 'string') { return /*#__PURE__*/React.createElement("span", { className: handlerCls }, handlers); } return /*#__PURE__*/React.createElement(Button, { className: handlerCls, type: "primary", text: true, onClick: onClickHandlers }, handlers); } }, { key: "renderCloseHandler", value: function renderCloseHandler(toastPrefix) { var closeable = this.props.closeable; if (!closeable) { return null; } var iconCls = "".concat(toastPrefix, "-close-handler"); return /*#__PURE__*/React.createElement(Icon, { className: iconCls, type: "remove", onClick: this.handleClose, size: "small" }); } }, { key: "renderStatusBar", value: function renderStatusBar(toastPrefix) { var cls = "".concat(toastPrefix, "-solid-bar"); return /*#__PURE__*/React.createElement("hr", { className: this.getStatusClassName(cls) }); } }, { key: "renderIcon", value: function renderIcon(toastPrefix) { if (this.props.icon !== undefined) { return this.props.icon; } var iconName = this.getStatusIconName(); if (typeof iconName !== 'string') { return null; } var iconCls = "".concat(toastPrefix, "-symbol"); return /*#__PURE__*/React.createElement(Icon, { className: this.getStatusClassName(iconCls), type: iconName }); } }, { key: "renderMessage", value: function renderMessage(toastPrefix) { var title = this.props.title; if ((typeof title !== 'string' || title.length === 0) && _typeof(title) !== 'object') { return null; } var msgCls = "".concat(toastPrefix, "-message"); return /*#__PURE__*/React.createElement("h4", { className: msgCls }, title); } }, { key: "renderDescription", value: function renderDescription(toastPrefix) { var content = this.props.content; if ((typeof content !== 'string' || content.length === 0) && _typeof(content) !== 'object') { return null; } var descCls = "".concat(toastPrefix, "-description"); return /*#__PURE__*/React.createElement("p", { className: descCls }, content); } }, { key: "render", value: function render() { var _this$props2 = this.props, prefix = _this$props2.prefix, className = _this$props2.className, style = _this$props2.style; var toastPrefix = "".concat(prefix, "toast"); var classes = cx(_defineProperty(_defineProperty(_defineProperty({}, "".concat(toastPrefix, "-component"), true), toastPrefix, true), className, !!className)); return /*#__PURE__*/React.createElement("div", { className: classes, onMouseEnter: this.handlePause, onMouseLeave: this.handleStart, style: style }, this.renderStatusBar(toastPrefix), this.renderContent(toastPrefix)); } }]); }(React.Component); _defineProperty(ToastItem, "propTypes", { prefix: PropTypes.string, className: PropTypes.string, style: PropTypes.object, title: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), content: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), handlers: PropTypes.array, status: PropTypes.any, icon: PropTypes.string, closeable: PropTypes.bool, onClose: PropTypes.func, onPause: PropTypes.func, onStart: PropTypes.func, onRemove: PropTypes.func, onClickHandlers: PropTypes.func }); _defineProperty(ToastItem, "defaultProps", { prefix: 'next-' }); var CSSMotion = { timeout: 320, enter: motion('.32s cubic-bezier(0, 0.63, 0.26, 1.12)', { height: '0', opacity: '0', transform: 'scale(0.3) translateY(60px)' }, { height: '*', opacity: '1', transform: 'scale(1) translateY(0px)' }), exit: motion('.32s cubic-bezier(0.8, 0, 0.4, 1)', { opacity: '1', height: '*', transform: 'scale(1)' }, { opacity: '0', height: '0', transform: 'scale(0.5)' }) }; export default ConfigProvider.config(Transition(CSSMotion, true)(ToastItem));