UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

160 lines (133 loc) 7.18 kB
import _extends from "@babel/runtime/helpers/extends"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _typeof from "@babel/runtime/helpers/typeof"; import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; var _excluded = ["prefixCls", "scrollNumberPrefixCls", "children", "status", "text", "color", "count", "overflowCount", "dot", "size", "title", "offset", "style", "className", "showZero"]; import React, { memo, useContext, useMemo, useRef } from 'react'; import classNames from 'classnames'; import defaultTo from 'lodash/defaultTo'; import ScrollNumber from './ScrollNumber'; import { isPresetColor } from '../_util/colors'; import { cloneElement } from '../_util/reactNode'; import ConfigContext from '../config-provider/ConfigContext'; import Animate from '../animate'; var Badge = function Badge(_ref) { var _classNames, _classNames2, _classNames3; var customizePrefixCls = _ref.prefixCls, customizeScrollNumberPrefixCls = _ref.scrollNumberPrefixCls, children = _ref.children, status = _ref.status, text = _ref.text, color = _ref.color, _ref$count = _ref.count, count = _ref$count === void 0 ? null : _ref$count, _ref$overflowCount = _ref.overflowCount, overflowCount = _ref$overflowCount === void 0 ? 99 : _ref$overflowCount, _ref$dot = _ref.dot, dot = _ref$dot === void 0 ? false : _ref$dot, _ref$size = _ref.size, size = _ref$size === void 0 ? 'default' : _ref$size, title = _ref.title, offset = _ref.offset, style = _ref.style, className = _ref.className, _ref$showZero = _ref.showZero, showZero = _ref$showZero === void 0 ? false : _ref$showZero, restProps = _objectWithoutProperties(_ref, _excluded); var _useContext = useContext(ConfigContext), getPrefixCls = _useContext.getPrefixCls; var prefixCls = getPrefixCls('badge', customizePrefixCls); var scrollNumberPrefixCls = getPrefixCls('scroll-number', customizeScrollNumberPrefixCls); // ================================ Misc ================================ var numberedDisplayCount = count > overflowCount ? "".concat(overflowCount, "+") : count; var hasStatus = status !== null && status !== undefined || color !== null && color !== undefined; var isZero = numberedDisplayCount === '0' || numberedDisplayCount === 0; var showAsDot = dot && !isZero; var mergedCount = showAsDot ? '' : numberedDisplayCount; var isHidden = useMemo(function () { var isEmpty = mergedCount === null || mergedCount === undefined || mergedCount === ''; return (isEmpty || isZero && !showZero) && !showAsDot; }, [mergedCount, isZero, showZero, showAsDot]); // Count should be cache in case hidden change it var countRef = useRef(count); if (!isHidden) { countRef.current = count; } var livingCount = countRef.current; // We need cache count since remove motion should not change count display var displayCountRef = useRef(mergedCount); if (!isHidden) { displayCountRef.current = mergedCount; } var displayCount = displayCountRef.current; // We will cache the dot status to avoid shaking on leaved motion var isDotRef = useRef(showAsDot); if (!isHidden) { isDotRef.current = showAsDot; } var isDot = isDotRef.current; // =============================== Styles =============================== var mergedStyle = useMemo(function () { if (!offset) { return _objectSpread({}, style); } var offsetStyle = { marginTop: offset[0], marginLeft: offset[1] }; return _objectSpread(_objectSpread({}, offsetStyle), style); }, [offset, style]); // =============================== Render =============================== // >>> Title var titleNode = defaultTo(title, typeof livingCount === 'string' || typeof livingCount === 'number' ? livingCount : undefined); // >>> Status Text var statusTextNode = isHidden || !text ? null : /*#__PURE__*/React.createElement("span", { className: "".concat(prefixCls, "-status-text") }, text); // >>> Display Component var displayNode = !livingCount || _typeof(livingCount) !== 'object' ? undefined : cloneElement(livingCount, function (oriProps) { return { style: _objectSpread(_objectSpread({}, mergedStyle), oriProps.style) }; }); // Shared styles var statusCls = classNames((_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-status-dot"), hasStatus), _defineProperty(_classNames, "".concat(prefixCls, "-status-").concat(status), !!status), _defineProperty(_classNames, "".concat(prefixCls, "-status-").concat(color), isPresetColor(color)), _classNames)); var scrollNumberCls = classNames((_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-dot"), isDot), _defineProperty(_classNames2, "".concat(prefixCls, "-count"), !isDot), _defineProperty(_classNames2, "".concat(prefixCls, "-count-sm"), size === 'small'), _defineProperty(_classNames2, "".concat(prefixCls, "-multiple-words"), !isDot && displayCount && displayCount.toString().length > 1), _defineProperty(_classNames2, "".concat(prefixCls, "-status-").concat(status), !!status), _defineProperty(_classNames2, "".concat(prefixCls, "-status-").concat(color), isPresetColor(color)), _classNames2)); var badgeClassName = classNames(prefixCls, (_classNames3 = {}, _defineProperty(_classNames3, "".concat(prefixCls, "-status"), hasStatus), _defineProperty(_classNames3, "".concat(prefixCls, "-not-a-wrapper"), !children), _classNames3), className); var statusStyle = {}; if (color && !isPresetColor(color)) { statusStyle.background = color; } if (!children && hasStatus) { var statusTextColor = mergedStyle.color; return /*#__PURE__*/React.createElement("span", _extends({}, restProps, { className: badgeClassName, style: mergedStyle }), /*#__PURE__*/React.createElement("span", { className: statusCls, style: statusStyle }), /*#__PURE__*/React.createElement("span", { style: { color: statusTextColor }, className: "".concat(prefixCls, "-status-text") }, text)); } var scrollNumberStyle = _objectSpread({}, mergedStyle); if (color && !isPresetColor(color)) { scrollNumberStyle = scrollNumberStyle || {}; scrollNumberStyle.background = color; } var scrollNumber = isHidden ? null : /*#__PURE__*/React.createElement(ScrollNumber, { prefixCls: scrollNumberPrefixCls, hidden: isHidden, className: scrollNumberCls, count: displayCount, title: titleNode, style: scrollNumberStyle, key: "scrollNumber" }, displayNode); return /*#__PURE__*/React.createElement("span", _extends({}, restProps, { className: badgeClassName }), children, /*#__PURE__*/React.createElement(Animate, { component: "", hiddenProp: "hidden", transitionName: children ? "".concat(prefixCls, "-zoom") : '', transitionAppear: true }, scrollNumber, ";"), statusTextNode); }; Badge.displayName = 'Badge'; export default /*#__PURE__*/memo(Badge); //# sourceMappingURL=index.js.map