CustomTabBar 自定义标签栏

自定义标签栏高阶组件

自动从 app.config 中获取 tabBar 配置,内部全面管理标签状态(显示、激活、颜色、图标、红点等),支持 TabBar 相关 API。

示例

custom-tab-bar/index.js
import { CustomTabBar, TabBar } from '@tarojsx/ui'
export default () => {
return (
<CustomTabBar>
{({
hidden,
current,
list,
switchTabIndex,
style: { backgroundColor, color, selectedColor, borderStyle, position },
}) => (
<TabBar
style={{ display: hidden ? 'none' : 'flex' }}
backgroundColor={backgroundColor}
color={color}
selectedColor={selectedColor}
tabList={list.map(({ key, text, iconPath, selectedIconPath, badge, redDot }, index) => ({
key,
title: text,
image: iconPath,
selectedImage: selectedIconPath,
text: badge,
dot: redDot,
}))}
current={current}
onClick={onClick}
/>
)}
</CustomTabBar>
)
}

以下为 app 配置代码片段:

app.config.js
export default {
tabBar: {
custom: true,
color: '#888888',
selectedColor: '#3B7CF7',
list: [
{
pagePath: 'pages/todo',
text: '待办事项',
iconPath: 'assets/todo.png',
selectedIconPath: 'assets/todo2.png',
},
{
pagePath: 'pages/shot',
text: '拍照',
iconPath: 'assets/shot.png',
selectedIconPath: 'assets/shot2.png',
},
{
pagePath: 'pages/contact',
text: '通讯录',
iconPath: 'assets/contact.png',
selectedIconPath: 'assets/contact2.png',
},
],
},
}

支持 API:

参数说明
Taro.hideTabBar隐藏标签栏,子组件属性 hidden 变成 true
Taro.showTabBar显示标签栏,子组件属性 hidden 变成 false
Taro.setTabBarStyle设置标签栏样式,子组件属性 style 随之更新。
Taro.setTabBarItem设置标签,子组件属性 list 随之更新。
Taro.setTabBarBadge设置标签 badge,子组件属性 list 随之更新。
Taro.removeTabBarBadge移除标签 badge,子组件属性 list 随之更新。
Taro.showTabBarRedDot显示标签红点,子组件属性 list 随之更新。
Taro.hideTabBarRedDot隐藏标签红点,子组件属性 list 随之更新。

子组件属性:

参数说明类型默认值
hidden是否隐藏标签栏boolean
current当前索引number
list标签列表object (参见 标签列表项)
style标签栏样式object (参见 标签栏样式)
switchTabIndex切换激活标签(index:number) => void

标签列表项:

参数说明类型默认值
keyReact 组件 keystring
text源自 app.configstring
iconPath源自 app.configstring
selectedIconPath源自 app.configstring
badgebadge 文字string
redDot小红点boolean

标签栏样式:

参数说明类型默认值
backgroundColor源自 app.configstring
color源自 app.configstring
selectedColor源自 app.configstring
borderStyle源自 app.configstring
position源自 app.configstring

API