提交 7fdd18a2 编写于 作者: D DCloud_LXH

feat: support dynamic tabbarItem hidden

上级 df9fc217
...@@ -20,9 +20,10 @@ export function setTabBarItem ({ ...@@ -20,9 +20,10 @@ export function setTabBarItem ({
text, text,
iconPath, iconPath,
selectedIconPath, selectedIconPath,
pagePath pagePath,
visible
}) { }) {
tabBar.setTabBarItem(index, text, iconPath, selectedIconPath) tabBar.setTabBarItem(index, text, iconPath, selectedIconPath, visible)
const route = pagePath && __uniRoutes.find(({ path }) => path === pagePath) const route = pagePath && __uniRoutes.find(({ path }) => path === pagePath)
if (route) { if (route) {
const meta = route.meta const meta = route.meta
......
...@@ -49,7 +49,7 @@ function setTabBarBadge (type, index, text) { ...@@ -49,7 +49,7 @@ function setTabBarBadge (type, index, text) {
/** /**
* 动态设置 tabBar 某一项的内容 * 动态设置 tabBar 某一项的内容
*/ */
function setTabBarItem (index, text, iconPath, selectedIconPath) { function setTabBarItem (index, text, iconPath, selectedIconPath, visible) {
const item = { const item = {
index index
} }
...@@ -62,7 +62,17 @@ function setTabBarItem (index, text, iconPath, selectedIconPath) { ...@@ -62,7 +62,17 @@ function setTabBarItem (index, text, iconPath, selectedIconPath) {
if (selectedIconPath) { if (selectedIconPath) {
item.selectedIconPath = getRealPath(selectedIconPath) item.selectedIconPath = getRealPath(selectedIconPath)
} }
tabBar && tabBar.setTabBarItem(item) if (visible !== undefined) {
item.visible = config.list[index].visible = visible
delete item.index
const tabbarItems = config.list.map(item => ({ visible: item.visible }))
tabbarItems[index] = item
tabBar && tabBar.setTabBarItems({ list: tabbarItems })
} else {
tabBar && tabBar.setTabBarItem(item)
}
} }
/** /**
* 动态设置 tabBar 的整体样式 * 动态设置 tabBar 的整体样式
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
class="uni-tabbar-border" class="uni-tabbar-border"
/> />
<div <div
v-for="(item,index) in list" v-for="(item,index) in visibleList"
:key="item.isMidButton ? index : item.pagePath" :key="item.isMidButton ? index : item.pagePath"
:style="item.isMidButton ? {flex:'0 0 ' + item.width,position:'relative'} : {}" :style="item.isMidButton ? {flex:'0 0 ' + item.width,position:'relative'} : {}"
class="uni-tabbar__item" class="uni-tabbar__item"
...@@ -36,14 +36,14 @@ ...@@ -36,14 +36,14 @@
:style="{height:height}" :style="{height:height}"
> >
<div <div
v-if="item.iconPath || item.isMidButton" v-if="getIconPath(item,index) || item.iconPath || item.isMidButton"
:class="{'uni-tabbar__icon__diff':!item.text}" :class="{'uni-tabbar__icon__diff':!item.text}"
class="uni-tabbar__icon" class="uni-tabbar__icon"
:style="{width: iconWidth,height:iconWidth}" :style="{width: iconWidth,height:iconWidth}"
> >
<img <img
v-if="!item.isMidButton" v-if="!item.isMidButton"
:src="_getRealPath(selectedIndex===index?item.selectedIconPath:item.iconPath)" :src="_getRealPath(getIconPath(item,index))"
> >
<div <div
v-if="item.redDot" v-if="item.redDot"
...@@ -287,7 +287,8 @@ export default { ...@@ -287,7 +287,8 @@ export default {
}, },
data () { data () {
return { return {
selectedIndex: 0 selectedIndex: 0,
visibleList: []
} }
}, },
computed: { computed: {
...@@ -319,25 +320,45 @@ export default { ...@@ -319,25 +320,45 @@ export default {
watch: { watch: {
$route: { $route: {
immediate: true, immediate: true,
handler (to) { handler () {
if (to.meta.isTabBar) { // 只在此做一次 visibleList 的初始化
this.__path__ = to.path if (!this.visibleList.length) this._initVisibleList()
const index = this.list.findIndex(item => to.meta.pagePath === item.pagePath) this.setSelectedIndex()
if (index > -1) { }
this.selectedIndex = index },
} list: {
} deep: true,
handler () {
this._initVisibleList()
this.setSelectedIndex()
} }
} }
}, },
created () { created () {
this._initMidButton() this.list.forEach(item => {
if (item.visible === undefined) {
this.$set(item, 'visible', true)
}
})
}, },
beforeCreate () { beforeCreate () {
this.__path__ = this.$route.path this.__path__ = this.$route.path
}, },
methods: { methods: {
_getRealPath (filePath) { getIconPath (item, index) {
return (this.selectedIndex === index ? item.selectedIconPath || item.iconPath : item.iconPath) || ''
},
setSelectedIndex () {
if (this.$route.meta.isTabBar) {
this.__path__ = this.$route.path
const index = this.visibleList.findIndex(item => this.$route.meta.pagePath === item.pagePath)
this.selectedIndex = index
}
},
_initVisibleList () {
this.visibleList = this._initMidButton(this.list.filter(item => item.visible !== false))
},
_getRealPath (filePath = '') {
const SCHEME_RE = /^([a-z-]+:)?\/\//i const SCHEME_RE = /^([a-z-]+:)?\/\//i
const DATA_RE = /^data:.*,.*/ const DATA_RE = /^data:.*,.*/
if (!(SCHEME_RE.test(filePath) || DATA_RE.test(filePath)) && filePath.indexOf('/') !== 0) { if (!(SCHEME_RE.test(filePath) || DATA_RE.test(filePath)) && filePath.indexOf('/') !== 0) {
...@@ -375,8 +396,8 @@ export default { ...@@ -375,8 +396,8 @@ export default {
UniServiceJSBridge.emit('onTabItemTap', detail) UniServiceJSBridge.emit('onTabItemTap', detail)
} }
}, },
_initMidButton () { _initMidButton (list) {
const listLength = this.list.length const listLength = list.length
// 偶数则添加midButton // 偶数则添加midButton
if (listLength % 2 === 0 && isPlainObject(this.midButton)) { if (listLength % 2 === 0 && isPlainObject(this.midButton)) {
// 给midButton赋值默认值 // 给midButton赋值默认值
...@@ -388,8 +409,9 @@ export default { ...@@ -388,8 +409,9 @@ export default {
for (const key in DefaultMidButton) { for (const key in DefaultMidButton) {
this.midButton[key] = this.midButton[key] || DefaultMidButton[key] this.midButton[key] = this.midButton[key] || DefaultMidButton[key]
} }
this.list.splice(~~(listLength / 2), 0, Object.assign({}, this.midButton, { isMidButton: true })) list.splice(~~(listLength / 2), 0, Object.assign({}, this.midButton, { isMidButton: true }))
} }
return list
}, },
_uniTabbarBdStyle (item) { _uniTabbarBdStyle (item) {
return Object.assign({}, { return Object.assign({}, {
......
...@@ -2,7 +2,7 @@ import { ...@@ -2,7 +2,7 @@ import {
setProperties setProperties
} from 'uni-shared' } from 'uni-shared'
const setTabBarItemProps = ['text', 'iconPath', 'selectedIconPath'] const setTabBarItemProps = ['text', 'iconPath', 'selectedIconPath', 'visible']
const setTabBarStyleProps = ['color', 'selectedColor', 'backgroundColor', 'borderStyle'] const setTabBarStyleProps = ['color', 'selectedColor', 'backgroundColor', 'borderStyle']
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册