UNPKG

linkmore-design

Version:

🌈 πŸš€lmη»„δ»ΆεΊ“γ€‚πŸš€

165 lines (154 loc) β€’ 7.64 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _typeof from "@babel/runtime/helpers/esm/typeof"; import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; var _excluded = ["prefixCls", "scrollNumberPrefixCls", "children", "status", "text", "color", "count", "overflowCount", "dot", "size", "title", "offset", "style", "className", "showZero"]; import classNames from 'classnames'; import CSSMotion from 'rc-motion'; import * as React from 'react'; import { useMemo, useRef } from 'react'; import { ConfigContext } from "../config-provider"; import { cloneElement } from "../_util/reactNode"; import Ribbon from "./Ribbon"; import ScrollNumber from "./ScrollNumber"; import { isPresetColor } from "./utils"; var Badge = function Badge(_ref) { var _classNames, _classNames2; 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 _React$useContext = React.useContext(ConfigContext), getPrefixCls = _React$useContext.getPrefixCls, direction = _React$useContext.direction; var prefixCls = getPrefixCls('badge', customizePrefixCls); // ================================ Misc ================================ var numberedDisplayCount = count > overflowCount ? "".concat(overflowCount, "+") : count; var isZero = numberedDisplayCount === '0' || numberedDisplayCount === 0; var ignoreCount = count === null || isZero; var hasStatus = (status !== null && status !== undefined || color !== null && color !== undefined) && ignoreCount; 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; } // =============================== Styles =============================== var mergedStyle = useMemo(function () { if (!offset) { return _objectSpread({}, style); } var offsetStyle = { marginTop: offset[1] }; if (direction === 'rtl') { offsetStyle.left = parseInt(offset[0], 10); } else { offsetStyle.right = -parseInt(offset[0], 10); } return _objectSpread(_objectSpread({}, offsetStyle), style); }, [direction, offset, style]); // =============================== Render =============================== // >>> Title var titleNode = title !== null && title !== void 0 ? 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 statusStyle = {}; if (color && !isPresetColor(color)) { statusStyle.background = color; } var badgeClassName = classNames(prefixCls, (_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-status"), hasStatus), _defineProperty(_classNames2, "".concat(prefixCls, "-not-a-wrapper"), !children), _defineProperty(_classNames2, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _classNames2), className); // <Badge status="success" /> 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 }), text && /*#__PURE__*/React.createElement("span", { style: { color: statusTextColor }, className: "".concat(prefixCls, "-status-text") }, text)); } // <Badge status="success" count={<Icon type="xxx" />}></Badge> return /*#__PURE__*/React.createElement("span", _extends({}, restProps, { className: badgeClassName }), children, /*#__PURE__*/React.createElement(CSSMotion, { visible: !isHidden, motionName: "".concat(prefixCls, "-zoom"), motionAppear: false, motionDeadline: 1000 }, function (_ref2) { var _classNames3; var motionClassName = _ref2.className; var scrollNumberPrefixCls = getPrefixCls('scroll-number', customizeScrollNumberPrefixCls); var isDot = isDotRef.current; var scrollNumberCls = classNames((_classNames3 = {}, _defineProperty(_classNames3, "".concat(prefixCls, "-dot"), isDot), _defineProperty(_classNames3, "".concat(prefixCls, "-count"), !isDot), _defineProperty(_classNames3, "".concat(prefixCls, "-count-sm"), size === 'small'), _defineProperty(_classNames3, "".concat(prefixCls, "-multiple-words"), !isDot && displayCount && displayCount.toString().length > 1), _defineProperty(_classNames3, "".concat(prefixCls, "-status-").concat(status), !!status), _defineProperty(_classNames3, "".concat(prefixCls, "-status-").concat(color), isPresetColor(color)), _classNames3)); var scrollNumberStyle = _objectSpread({}, mergedStyle); if (color && !isPresetColor(color)) { scrollNumberStyle = scrollNumberStyle || {}; scrollNumberStyle.background = color; } return /*#__PURE__*/React.createElement(ScrollNumber, { prefixCls: scrollNumberPrefixCls, show: !isHidden, motionClassName: motionClassName, className: scrollNumberCls, count: displayCount, title: titleNode, style: scrollNumberStyle, key: "scrollNumber" }, displayNode); }), statusTextNode); }; Badge.Ribbon = Ribbon; export default Badge;