@nutui/nutui-react-taro
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
93 lines (92 loc) • 4.78 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 { View } from "@tarojs/components";
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 TabbarItem(props) {
var ctx = useContext(TabbarContext);
var _$_object_spread = _object_spread({}, defaultProps, props), className = _$_object_spread.className, style = _$_object_spread.style, title = _$_object_spread.title, icon = _$_object_spread.icon, value = _$_object_spread.value, dot = _$_object_spread.dot, max = _$_object_spread.max, top = _$_object_spread.top, right = _$_object_spread.right, // @ts-ignore
index = _$_object_spread.index, direction = _$_object_spread.direction, onActiveClick = _$_object_spread.onActiveClick, rest = _object_without_properties(_$_object_spread, [
"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 renderNodeWithActive(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 renderTitleText() {
return title && /*#__PURE__*/ React.createElement(View, {
className: "".concat(classPrefix, "-text"),
style: {
color: active ? ctx === null || ctx === void 0 ? void 0 : ctx.activeColor : ctx === null || ctx === void 0 ? void 0 : ctx.inactiveColor
}
}, renderNodeWithActive(title));
};
var renderTitle = function renderTitle() {
return /*#__PURE__*/ React.createElement(Badge, _object_spread({
size: "normal"
}, badgeProps), renderTitleText());
};
var renderIcon = function renderIcon() {
var distIcon = renderNodeWithActive(icon);
// 鸿蒙差异处理,需要手动给icon一个color
return /*#__PURE__*/ React.isValidElement(distIcon) ? /*#__PURE__*/ React.cloneElement(distIcon, _object_spread_props(_object_spread({}, distIcon.props), {
color: active ? ctx === null || ctx === void 0 ? void 0 : ctx.activeColor : ctx === null || ctx === void 0 ? void 0 : ctx.inactiveColor
})) : null;
};
var renderIconAndTitle = function renderIconAndTitle() {
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 renderDualItem() {
return dot ? null : /*#__PURE__*/ React.createElement(React.Fragment, null, renderIcon(), renderTitleText(), /*#__PURE__*/ React.createElement(Badge, badgeProps));
};
return /*#__PURE__*/ React.createElement(View, _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 onClick() {
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()));
};