linkmore-design
Version:
π πlmη»δ»ΆεΊγπ
98 lines (97 loc) β’ 4.47 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["prefixCls", "className", "style", "children", "icon", "color", "onClose", "closeIcon", "closable"];
import CloseOutlined from '@ant-design/icons/CloseOutlined';
import classNames from 'classnames';
import omit from 'rc-util/lib/omit';
import * as React from 'react';
import { ConfigContext } from "../config-provider";
import { PresetColorTypes, PresetStatusColorTypes } from "../_util/colors";
import Wave from "../_util/wave";
import warning from "../_util/warning";
import CheckableTag from "./CheckableTag";
var PresetColorRegex = new RegExp("^(".concat(PresetColorTypes.join('|'), ")(-inverse)?$"));
var PresetStatusColorRegex = new RegExp("^(".concat(PresetStatusColorTypes.join('|'), ")$"));
var InternalTag = function InternalTag(_ref, ref) {
var _classNames;
var customizePrefixCls = _ref.prefixCls,
className = _ref.className,
style = _ref.style,
children = _ref.children,
icon = _ref.icon,
color = _ref.color,
onClose = _ref.onClose,
closeIcon = _ref.closeIcon,
_ref$closable = _ref.closable,
closable = _ref$closable === void 0 ? false : _ref$closable,
props = _objectWithoutProperties(_ref, _excluded);
var _React$useContext = React.useContext(ConfigContext),
getPrefixCls = _React$useContext.getPrefixCls,
direction = _React$useContext.direction;
var _React$useState = React.useState(true),
_React$useState2 = _slicedToArray(_React$useState, 2),
visible = _React$useState2[0],
setVisible = _React$useState2[1];
// Warning for deprecated usage
if (process.env.NODE_ENV !== 'production') {
warning(!('visible' in props), 'Tag', '`visible` will be removed in next major version, please use `visible && <Tag />` instead.');
}
React.useEffect(function () {
if ('visible' in props) {
setVisible(props.visible);
}
}, [props.visible]);
var isPresetColor = function isPresetColor() {
if (!color) {
return false;
}
return PresetColorRegex.test(color) || PresetStatusColorRegex.test(color);
};
var tagStyle = _objectSpread({
backgroundColor: color && !isPresetColor() ? color : undefined
}, style);
var presetColor = isPresetColor();
var prefixCls = getPrefixCls('tag', customizePrefixCls);
var tagClassName = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-").concat(color), presetColor), _defineProperty(_classNames, "".concat(prefixCls, "-has-color"), color && !presetColor), _defineProperty(_classNames, "".concat(prefixCls, "-hidden"), !visible), _defineProperty(_classNames, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _classNames), className);
var handleCloseClick = function handleCloseClick(e) {
e.stopPropagation();
onClose === null || onClose === void 0 ? void 0 : onClose(e);
if (e.defaultPrevented) {
return;
}
if (!('visible' in props)) {
setVisible(false);
}
};
var renderCloseIcon = function renderCloseIcon() {
if (closable) {
return closeIcon ? /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-close-icon"),
onClick: handleCloseClick
}, closeIcon) : /*#__PURE__*/React.createElement(CloseOutlined, {
className: "".concat(prefixCls, "-close-icon"),
onClick: handleCloseClick
});
}
return null;
};
var isNeedWave = 'onClick' in props || children && children.type === 'a';
var tagProps = omit(props, ['visible']);
var iconNode = icon || null;
var kids = iconNode ? /*#__PURE__*/React.createElement(React.Fragment, null, iconNode, /*#__PURE__*/React.createElement("span", null, children)) : children;
var tagNode = /*#__PURE__*/React.createElement("span", _extends({}, tagProps, {
ref: ref,
className: tagClassName,
style: tagStyle
}), kids, renderCloseIcon());
return isNeedWave ? /*#__PURE__*/React.createElement(Wave, null, tagNode) : tagNode;
};
var Tag = /*#__PURE__*/React.forwardRef(InternalTag);
if (process.env.NODE_ENV !== 'production') {
Tag.displayName = 'Tag';
}
Tag.CheckableTag = CheckableTag;
export default Tag;