@nutui/nutui-react
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
85 lines (84 loc) • 3.86 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 _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import React, { useContext } from "react";
import classNames from "classnames";
import { ComponentDefaults } from "../../utils/typings";
import Badge from "../badge/index";
import TabbarContext from "../tabbar/context";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
title: '',
icon: null,
value: '',
dot: false,
max: 99,
top: '0',
right: '0',
direction: 'vertical'
});
export var TabbarItem = function(props) {
var ctx = useContext(TabbarContext);
var _ref = _object_spread({}, defaultProps, props), className = _ref.className, style = _ref.style, title = _ref.title, icon = _ref.icon, value = _ref.value, dot = _ref.dot, max = _ref.max, top = _ref.top, right = _ref.right, // @ts-ignore
index = _ref.index, direction = _ref.direction, onActiveClick = _ref.onActiveClick, rest = _object_without_properties(_ref, [
"className",
"style",
"title",
"icon",
"value",
"dot",
"max",
"top",
"right",
"index",
"direction",
"onActiveClick"
]);
var active = index === (ctx === null || ctx === void 0 ? void 0 : ctx.selectIndex);
var classPrefix = 'nut-tabbar-item';
var _obj;
var tabbarItemClass = classNames(classPrefix, (_obj = {}, _define_property(_obj, "".concat(classPrefix, "-active"), active), _define_property(_obj, "".concat(classPrefix, "-large"), !icon || !title), _obj), className);
var renderNodeWithActive = function(node) {
return node && typeof node === 'function' ? node(active) : node;
};
var badgeProps = {
value: renderNodeWithActive(value),
dot: dot,
max: max,
top: top,
right: right,
color: ctx === null || ctx === void 0 ? void 0 : ctx.activeColor
};
var renderTitleText = function() {
return title && /*#__PURE__*/ React.createElement("div", {
className: "".concat(classPrefix, "-text")
}, renderNodeWithActive(title));
};
var renderTitle = function() {
return /*#__PURE__*/ React.createElement(Badge, _object_spread({
size: "normal"
}, badgeProps), renderTitleText());
};
var renderIcon = function() {
return renderNodeWithActive(icon);
};
var renderIconAndTitle = function() {
return /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(Badge, _object_spread_props(_object_spread({
size: "normal"
}, badgeProps), {
top: 3
}), renderIcon()), renderTitleText());
};
var renderDualItem = function() {
return dot ? null : /*#__PURE__*/ React.createElement(React.Fragment, null, renderIcon(), renderTitleText(), /*#__PURE__*/ React.createElement(Badge, badgeProps));
};
return /*#__PURE__*/ React.createElement("div", _object_spread({
className: tabbarItemClass,
style: _object_spread({
color: active ? ctx === null || ctx === void 0 ? void 0 : ctx.activeColor : ctx === null || ctx === void 0 ? void 0 : ctx.inactiveColor
}, style),
onClick: function() {
return active ? onActiveClick === null || onActiveClick === void 0 ? void 0 : onActiveClick() : ctx === null || ctx === void 0 ? void 0 : ctx.handleClick(index);
}
}, rest), direction === 'horizontal' && !dot ? renderDualItem() : /*#__PURE__*/ React.createElement(React.Fragment, null, icon && renderIconAndTitle(), !icon && renderTitle()));
};