Skip to content

Commit 7f7c4cc

Browse files
authored
Merge pull request #191 from hwaphon/feature/tab-bar-enhanced
feat(plugin-compiler-web & runtime-web): 转 web 时兼容 tabbar 的微信使用场景
2 parents be0cd31 + fa15499 commit 7f7c4cc

File tree

3 files changed

+62
-16
lines changed

3 files changed

+62
-16
lines changed

packages/plugin-compiler-web/src/plugins/generateJSXEntryPlugin.ts

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
WebCompilerUserConfig,
1616
WEB_RUNTIMES
1717
} from '../constants'
18+
import { getValueByOrder } from '../utils'
1819

1920
// Web 的运行时配置,主要用于指定路由和 tabBar 等
2021
interface WebRuntimeConfig {
@@ -290,26 +291,53 @@ export class GenerateJSXEntryPlugin implements Plugin {
290291

291292
// 设置底部导航栏
292293
if (appJson.tabBar) {
293-
// 修复 items 中的路径问题
294-
if (appJson.tabBar.items) {
295-
;(appJson.tabBar as WebRuntimeConfig['tabBar']).items.map((item) => {
296-
if (item.activeIcon || item.selectedIconPath) {
297-
item.activeIcon = addLeadingSlash(
298-
item.activeIcon || item.selectedIconPath
299-
)
300-
}
301-
if (item.icon || item.iconPath) {
302-
item.icon = addLeadingSlash(item.icon || item.iconPath)
294+
const initializeTabItem = (list) => {
295+
return list.map((item) => {
296+
const resItem = {}
297+
298+
const withLeadingSlashItem = {
299+
activeIcon: getValueByOrder(item, [
300+
'activeIcon',
301+
'selectedIconPath'
302+
]),
303+
icon: getValueByOrder(item, ['icon', 'iconPath']),
304+
pagePath: getValueByOrder(item, ['pagePath'])
303305
}
304-
if (item.pagePath) {
305-
item.pagePath = addLeadingSlash(item.pagePath)
306-
}
307-
if (item.name || item.text) {
308-
item.name = item.name || item.text
306+
const commonItem = {
307+
name: getValueByOrder(item, ['name', 'text'])
309308
}
309+
310+
Object.keys(withLeadingSlashItem).map((key) => {
311+
const value = withLeadingSlashItem[key]
312+
if (value) resItem[key] = addLeadingSlash(value)
313+
})
314+
315+
Object.keys(commonItem).map((key) => {
316+
const value = commonItem[key]
317+
if (value) resItem[key] = value
318+
})
319+
320+
return resItem
310321
})
311322
}
312323

324+
// 默认文字颜色
325+
const textColor = getValueByOrder(appJson.tabBar, [
326+
'textColor',
327+
'color'
328+
])
329+
if (textColor) appJson.tabBar.textColor = textColor
330+
331+
// 修复 items 中的路径问题
332+
if (appJson.tabBar.items) {
333+
appJson.tabBar.items = initializeTabItem(appJson.tabBar.items)
334+
} else if (appJson.tabBar.list) {
335+
// 兼容微信转 web 使用场景
336+
const items = appJson.tabBar.list
337+
delete appJson.tabBar.list
338+
appJson.tabBar.items = initializeTabItem(items)
339+
}
340+
313341
webRuntimeConfig.tabBar = appJson.tabBar
314342
}
315343

packages/plugin-compiler-web/src/utils/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,19 @@ export function getRelativePath(path) {
1313
export const isObject = (param) => {
1414
return Object.prototype.toString.call(param) === '[object Object]'
1515
}
16+
17+
export const getValueByOrder = (obj, props) => {
18+
if (!isObject(obj)) return
19+
const { length } = props
20+
let result
21+
22+
for (let i = 0; i < length; i++) {
23+
const prop = props[i]
24+
if (obj[prop]) {
25+
result = obj[prop]
26+
break
27+
}
28+
}
29+
30+
return result
31+
}

packages/runtime-web/src/components/src/base/tabbar-item.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ export default class TabbarItem extends BaseElement {
126126
<div
127127
style=${styleMap({ display: 'inline-block', position: 'relative' })}
128128
>
129-
<img src="${this.icon}" alt="" class="tiga-tabbar__icon" />
129+
${this.icon
130+
? html`<img src="${this.icon}" alt="" class="tiga-tabbar__icon" />`
131+
: ''}
130132
${this.badgeText
131133
? html` <span class="tiga-badge" style=${styleMap(this.badgeStyle)}>
132134
${this.badgeText}

0 commit comments

Comments
 (0)