@nutui/nutui-react
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
61 lines (60 loc) • 3.51 kB
JavaScript
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _type_of } from "@swc/helpers/_/_type_of";
import React from "react";
import classNames from "classnames";
import { ComponentDefaults } from "../../utils/typings";
import { useRtl } from "../configprovider";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
value: '',
dot: false,
max: 99,
top: 0,
right: 0,
fill: 'solid',
size: 'large',
disabled: false
});
export var Badge = function(props) {
var isIcon = function isIcon() {
if ((typeof value === "undefined" ? "undefined" : _type_of(value)) === 'object' && value) return value;
};
var isNumber = function isNumber() {
if (typeof value === 'number' && value) return value;
};
var isString = function isString() {
if (typeof value === 'string' && value) return value;
};
var _this;
var rtl = useRtl();
var _$_object_spread = _object_spread({}, defaultProps, props), className = _$_object_spread.className, style = _$_object_spread.style, value = _$_object_spread.value, max = _$_object_spread.max, children = _$_object_spread.children, dot = _$_object_spread.dot, top = _$_object_spread.top, right = _$_object_spread.right, fill = _$_object_spread.fill, size = _$_object_spread.size, disabled = _$_object_spread.disabled;
var classPrefix = 'nut-badge';
var classes = classNames(classPrefix, className);
var getContent = function() {
if (dot || value === 0) return null;
if (typeof value === 'number') {
return max < value ? "".concat(max, "+") : "".concat(value);
}
return value;
};
var _obj;
var contentClasses = classNames((_obj = {}, _define_property(_obj, "".concat(classPrefix, "-sup"), isNumber() || isString() || dot), _define_property(_obj, "".concat(classPrefix, "-number"), isNumber()), _define_property(_obj, "".concat(classPrefix, "-one"), typeof getContent() === 'string' && ((_this = "".concat(getContent())) === null || _this === void 0 ? void 0 : _this.length) === 1), _define_property(_obj, "".concat(classPrefix, "-dot"), dot), _define_property(_obj, "".concat(classPrefix, "-dot-").concat(size), dot), _define_property(_obj, "".concat(classPrefix, "-").concat(fill), fill === 'outline'), _define_property(_obj, "".concat(classPrefix, "-content"), children), _define_property(_obj, "".concat(classPrefix, "-disabled"), disabled), _obj));
var getPositionStyle = function() {
return _define_property({
top: "".concat(Number(top) || 0, "px")
}, rtl ? 'left' : 'right', "".concat(Number(right) || 0, "px"));
};
var _obj1;
return /*#__PURE__*/ React.createElement("div", {
className: classes,
style: style
}, isIcon() && /*#__PURE__*/ React.createElement("div", {
className: classNames("".concat(classPrefix, "-content"), (_obj1 = {}, _define_property(_obj1, "".concat(classPrefix, "-icon"), true), _define_property(_obj1, "".concat(classPrefix, "-icon-rtl"), rtl), _obj1)),
style: getPositionStyle()
}, value), children, !isIcon() && (getContent() || dot) && /*#__PURE__*/ React.createElement("div", {
className: contentClasses,
style: getPositionStyle()
}, getContent()));
};
Badge.displayName = 'NutBadge';