@txdfe/at
Version:
一个设计体系组件库
302 lines (254 loc) • 11.2 kB
JavaScript
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); }
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; }
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) {
_inherits(ToastItem, _React$Component);
var _super = _createSuper(ToastItem);
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 = _super.call.apply(_super, [this].concat(args));
_defineProperty(_assertThisInitialized(_this), "handleClose", function () {
if (func.isFunction(_this.props.onClose)) {
_this.props.onClose();
}
});
_defineProperty(_assertThisInitialized(_this), "handlePause", function () {
if (func.isFunction(_this.props.onPause)) {
_this.props.onPause();
}
});
_defineProperty(_assertThisInitialized(_this), "handleStart", function () {
if (func.isFunction(_this.props.onStart)) {
_this.props.onStart();
}
});
return _this;
}
_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 _cx;
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((_cx = {}, _defineProperty(_cx, "".concat(toastPrefix, "-component"), true), _defineProperty(_cx, toastPrefix, true), _defineProperty(_cx, className, !!className), _cx));
return /*#__PURE__*/React.createElement("div", {
className: classes,
onMouseEnter: this.handlePause,
onMouseLeave: this.handleStart,
style: style
}, this.renderStatusBar(toastPrefix), this.renderContent(toastPrefix));
}
}]);
return ToastItem;
}(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));