antd
Version:
An enterprise-class UI design language and React components implementation
187 lines (150 loc) • 8.63 kB
JavaScript
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
import * as React from 'react';
import CloseOutlined from '@ant-design/icons/CloseOutlined';
import CheckCircleOutlined from '@ant-design/icons/CheckCircleOutlined';
import ExclamationCircleOutlined from '@ant-design/icons/ExclamationCircleOutlined';
import InfoCircleOutlined from '@ant-design/icons/InfoCircleOutlined';
import CloseCircleOutlined from '@ant-design/icons/CloseCircleOutlined';
import CheckCircleFilled from '@ant-design/icons/CheckCircleFilled';
import ExclamationCircleFilled from '@ant-design/icons/ExclamationCircleFilled';
import InfoCircleFilled from '@ant-design/icons/InfoCircleFilled';
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
import Animate from 'rc-animate';
import classNames from 'classnames';
import { ConfigContext } from '../config-provider';
import getDataOrAriaProps from '../_util/getDataOrAriaProps';
import ErrorBoundary from './ErrorBoundary';
import { replaceElement } from '../_util/reactNode';
var iconMapFilled = {
success: CheckCircleFilled,
info: InfoCircleFilled,
error: CloseCircleFilled,
warning: ExclamationCircleFilled
};
var iconMapOutlined = {
success: CheckCircleOutlined,
info: InfoCircleOutlined,
error: CloseCircleOutlined,
warning: ExclamationCircleOutlined
};
var Alert = function Alert(_a) {
var _classNames2;
var description = _a.description,
customizePrefixCls = _a.prefixCls,
message = _a.message,
banner = _a.banner,
_a$className = _a.className,
className = _a$className === void 0 ? '' : _a$className,
style = _a.style,
onMouseEnter = _a.onMouseEnter,
onMouseLeave = _a.onMouseLeave,
onClick = _a.onClick,
showIcon = _a.showIcon,
closable = _a.closable,
closeText = _a.closeText,
props = __rest(_a, ["description", "prefixCls", "message", "banner", "className", "style", "onMouseEnter", "onMouseLeave", "onClick", "showIcon", "closable", "closeText"]);
var _React$useState = React.useState(false),
_React$useState2 = _slicedToArray(_React$useState, 2),
closing = _React$useState2[0],
setClosing = _React$useState2[1];
var _React$useState3 = React.useState(false),
_React$useState4 = _slicedToArray(_React$useState3, 2),
closed = _React$useState4[0],
setClosed = _React$useState4[1];
var ref = React.useRef();
var _React$useContext = React.useContext(ConfigContext),
getPrefixCls = _React$useContext.getPrefixCls,
direction = _React$useContext.direction;
var prefixCls = getPrefixCls('alert', customizePrefixCls);
var handleClose = function handleClose(e) {
var _a;
e.preventDefault();
var dom = ref.current;
dom.style.height = "".concat(dom.offsetHeight, "px"); // Magic code
// 重复一次后才能正确设置 height
dom.style.height = "".concat(dom.offsetHeight, "px");
setClosing(true);
(_a = props.onClose) === null || _a === void 0 ? void 0 : _a.call(props, e);
};
var animationEnd = function animationEnd() {
var _a;
setClosing(false);
setClosed(true);
(_a = props.afterClose) === null || _a === void 0 ? void 0 : _a.call(props);
};
var getType = function getType() {
var type = props.type;
if (type !== undefined) {
return type;
} // banner 模式默认为警告
return banner ? 'warning' : 'info';
}; // closeable when closeText is assigned
var isClosable = closeText ? true : closable;
var type = getType();
var renderIconNode = function renderIconNode() {
var icon = props.icon; // use outline icon in alert with description
var iconType = (description ? iconMapOutlined : iconMapFilled)[type] || null;
if (icon) {
return replaceElement(icon, /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-icon")
}, icon), function () {
return {
className: classNames("".concat(prefixCls, "-icon"), _defineProperty({}, icon.props.className, icon.props.className))
};
});
}
return /*#__PURE__*/React.createElement(iconType, {
className: "".concat(prefixCls, "-icon")
});
};
var renderCloseIcon = function renderCloseIcon() {
return isClosable ? /*#__PURE__*/React.createElement("button", {
type: "button",
onClick: handleClose,
className: "".concat(prefixCls, "-close-icon"),
tabIndex: 0
}, closeText ? /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-close-text")
}, closeText) : /*#__PURE__*/React.createElement(CloseOutlined, null)) : null;
}; // banner 模式默认有 Icon
var isShowIcon = banner && showIcon === undefined ? true : showIcon;
var alertCls = classNames(prefixCls, "".concat(prefixCls, "-").concat(type), (_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-closing"), closing), _defineProperty(_classNames2, "".concat(prefixCls, "-with-description"), !!description), _defineProperty(_classNames2, "".concat(prefixCls, "-no-icon"), !isShowIcon), _defineProperty(_classNames2, "".concat(prefixCls, "-banner"), !!banner), _defineProperty(_classNames2, "".concat(prefixCls, "-closable"), isClosable), _defineProperty(_classNames2, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _classNames2), className);
var dataOrAriaProps = getDataOrAriaProps(props);
return closed ? null : /*#__PURE__*/React.createElement(Animate, {
component: "",
showProp: "data-show",
transitionName: "".concat(prefixCls, "-slide-up"),
onEnd: animationEnd
}, /*#__PURE__*/React.createElement("div", _extends({
ref: ref,
"data-show": !closing,
className: alertCls,
style: style,
onMouseEnter: onMouseEnter,
onMouseLeave: onMouseLeave,
onClick: onClick
}, dataOrAriaProps), isShowIcon ? renderIconNode() : null, /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-message")
}, message), /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-description")
}, description), renderCloseIcon()));
};
Alert.ErrorBoundary = ErrorBoundary;
export default Alert;