UNPKG

@nutui/nutui-react-taro

Version:

京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序

85 lines (84 loc) 4.56 kB
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator"; 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 _sliced_to_array } from "@swc/helpers/_/_sliced_to_array"; import { _ as _type_of } from "@swc/helpers/_/_type_of"; import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator"; import React, { useEffect, useRef, useState } from "react"; import classNames from "classnames"; import { View } from "@tarojs/components"; import { ComponentDefaults } from "../../utils/typings"; import { useRtl } from "../configprovider/index"; import { pxTransform } from "../../utils/taro/px-transform"; import { harmony } from "../../utils/taro/platform"; 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 getContent = function getContent() { if (dot || value === 0) return null; if (typeof value === 'number') { return max < value ? "".concat(max, "+") : "".concat(value); } return value; }; 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 isHarmony = harmony(); var classes = classNames(classPrefix, className); var badgeRef = useRef(null); var _useState = _sliced_to_array(useState({}), 2), contentStyle = _useState[0], setContentStyle = _useState[1]; 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)); useEffect(function() { if (badgeRef.current) { getPositionStyle(); } }, []); var getPositionStyle = function() { return _async_to_generator(function() { var style; return _ts_generator(this, function(_state) { style = _define_property({ top: pxTransform(Number(top) || 0) }, rtl ? 'left' : 'right', isHarmony ? pxTransform(Number(right)) : "".concat(Number(right) || 0, "px")); setContentStyle(style); return [ 2 ]; }); })(); }; var _obj1; return /*#__PURE__*/ React.createElement(View, { className: classes, style: style, ref: badgeRef }, isIcon() && /*#__PURE__*/ React.createElement(View, { className: classNames("".concat(classPrefix, "-content"), (_obj1 = {}, _define_property(_obj1, "".concat(classPrefix, "-icon"), true), _define_property(_obj1, "".concat(classPrefix, "-icon-rtl"), rtl), _obj1)), style: contentStyle }, value), children, !isIcon() && (getContent() || dot) && /*#__PURE__*/ React.createElement(View, { className: contentClasses, style: contentStyle }, getContent())); }; Badge.displayName = 'NutBadge';