Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/taro-h5/__tests__/ui/navigation.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { eventCenter } from '@tarojs/runtime'
import * as Taro from '@tarojs/taro-h5'

import { buildApp } from '../utils'
Expand Down Expand Up @@ -96,4 +97,26 @@ describe('navigation', () => {
})
})
})
describe('hideHomeButton', () => {
test('should ask the navigation bar to hide the home button', async () => {
const listener = jest.fn()
const success = jest.fn()
const fail = jest.fn()
const complete = jest.fn()
const expectRes = { errMsg: 'hideHomeButton:ok' }
eventCenter.on('__taroH5HideHomeButton', listener)

try {
await expect(Taro.hideHomeButton({ success, fail, complete })).resolves.toEqual(expectRes)
await expect(Taro.hideHomeButton()).resolves.toEqual(expectRes)
} finally {
eventCenter.off('__taroH5HideHomeButton', listener)
}

expect(listener).toHaveBeenCalledTimes(2)
expect(success.mock.calls).toEqual([[expectRes]])
expect(complete.mock.calls).toEqual([[expectRes]])
expect(fail).not.toHaveBeenCalled()
})
})
})
14 changes: 11 additions & 3 deletions packages/taro-h5/src/api/ui/navigation-bar/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Taro from '@tarojs/api'
import { setNavigationBarLoading, setNavigationBarStyle, setTitle } from '@tarojs/router'
import { hideNavigationBarHomeButton, setNavigationBarLoading, setNavigationBarStyle, setTitle } from '@tarojs/router'

import { getParameterError, shouldBeObject, temporarilyNotSupport } from '../../../utils'
import { getParameterError, shouldBeObject } from '../../../utils'
import { MethodHandler } from '../../../utils/handler'

// 导航栏
Expand Down Expand Up @@ -66,4 +66,12 @@ export function hideNavigationBarLoading (options: Taro.hideNavigationBarLoading
return handle.success()
}

export const hideHomeButton = /* @__PURE__ */ temporarilyNotSupport('hideHomeButton')
/**
* 隐藏当前页面导航栏中的返回首页按钮
*/
export const hideHomeButton: typeof Taro.hideHomeButton = (options = {}) => {
const { success, fail, complete } = options
const handle = new MethodHandler({ name: 'hideHomeButton', success, fail, complete })
hideNavigationBarHomeButton()
return handle.success()
}
82 changes: 76 additions & 6 deletions packages/taro-router/__tests__/navigation-bar-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import NavigationBarHandler from '../src/router/navigation-bar'
import stacks from '../src/router/stack'
import { hideNavigationBarHomeButton } from '../src/utils/navigate'

import type { PageInstance } from '@tarojs/runtime'
import type PageHandler from '../src/router/page'
Expand Down Expand Up @@ -42,13 +43,13 @@ describe('NavigationBarHandler', () => {
setStackSize(0)
})

describe('setFnBtnState', () => {
const getFnBtn = () => {
if (navigationBar.classList.contains('taro-navigation-bar-back-icon')) return 'back'
if (navigationBar.classList.contains('taro-navigation-bar-home-icon')) return 'home'
return 'none'
}
const getFnBtn = () => {
if (navigationBar.classList.contains('taro-navigation-bar-back-icon')) return 'back'
if (navigationBar.classList.contains('taro-navigation-bar-home-icon')) return 'home'
return 'none'
}

describe('setFnBtnState', () => {
it.each([
['the home page opened as the only page', HOME_PAGE, 1, 'none'],
['the home page navigated to from another page', HOME_PAGE, 2, 'back'],
Expand Down Expand Up @@ -77,6 +78,75 @@ describe('NavigationBarHandler', () => {
})
})

describe('hideHomeButton', () => {
it('hides the home button of the current page', () => {
setStackSize(1)
const handler = createHandler(OTHER_PAGE)
handler.load()
expect(getFnBtn()).toBe('home')

hideNavigationBarHomeButton()

expect(getFnBtn()).toBe('none')
})

it('keeps the home button hidden when called before the page is loaded', () => {
setStackSize(1)
const handler = createHandler(OTHER_PAGE)

// 页面在 onShow 中调用时,导航栏还未根据当前页面刷新
hideNavigationBarHomeButton()
handler.load()

expect(getFnBtn()).toBe('none')
})

it('does not hide the back button', () => {
setStackSize(2)
const handler = createHandler(OTHER_PAGE)
handler.load()

hideNavigationBarHomeButton()

expect(getFnBtn()).toBe('back')
})

it('only applies to the page that hid it', () => {
setStackSize(1)
const handler = createHandler(OTHER_PAGE)
handler.load()
hideNavigationBarHomeButton()
expect(getFnBtn()).toBe('none')

const anotherPage = '/pages/another/index'
Object.assign(handler.pageContext, { currentPage: anotherPage, originPathname: anotherPage })
handler.load()
expect(getFnBtn()).toBe('home')

Object.assign(handler.pageContext, { currentPage: OTHER_PAGE, originPathname: OTHER_PAGE })
handler.load()
expect(getFnBtn()).toBe('none')
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

现有用例都是在栈深为 1 的情况下切换页面,没覆盖最常见的 navigateTo 再 navigateBack 回来的流程。补一个用例确认中间出现返回按钮后,回到该页面时首页按钮仍保持隐藏,这个行为不管缓存最终按路由还是按页面实例都应该成立。

Suggested change
})
})
it('stays hidden after navigating to another page and back', () => {
setStackSize(1)
const handler = createHandler(OTHER_PAGE)
handler.load()
hideNavigationBarHomeButton()
const anotherPage = '/pages/another/index'
setStackSize(2)
Object.assign(handler.pageContext, { currentPage: anotherPage, originPathname: anotherPage })
handler.load()
expect(getFnBtn()).toBe('back')
setStackSize(1)
Object.assign(handler.pageContext, { currentPage: OTHER_PAGE, originPathname: OTHER_PAGE })
handler.load()
expect(getFnBtn()).toBe('none')
})

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the navigate-away-and-back case in 213c091. It checks the back button shows on the intermediate page and the home button is still hidden after returning.


it('stays hidden after navigating to another page and back', () => {
setStackSize(1)
const handler = createHandler(OTHER_PAGE)
handler.load()
hideNavigationBarHomeButton()

const anotherPage = '/pages/another/index'
setStackSize(2)
Object.assign(handler.pageContext, { currentPage: anotherPage, originPathname: anotherPage })
handler.load()
expect(getFnBtn()).toBe('back')

setStackSize(1)
Object.assign(handler.pageContext, { currentPage: OTHER_PAGE, originPathname: OTHER_PAGE })
handler.load()
expect(getFnBtn()).toBe('none')
})
})

describe('setNavigationBarVisible', () => {
const isVisible = () => {
const show = navigationBar.classList.contains('taro-navigation-bar-show')
Expand Down
15 changes: 14 additions & 1 deletion packages/taro-router/src/router/navigation-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface NavigationBarCache {
title?: string
show?: boolean
loading?: boolean
homeButtonHidden?: boolean
}

export default class NavigationBarHandler {
Expand All @@ -38,6 +39,10 @@ export default class NavigationBarHandler {

if (typeof frontColor === 'string') this.setNavigationBarTextStyle(frontColor)
})

eventCenter.on('__taroH5HideHomeButton', () => {
this.hideHomeButton()
})
}

private toHomeFn () {
Expand Down Expand Up @@ -102,13 +107,21 @@ export default class NavigationBarHandler {
this.fnBtnToggleToNone()
} else if (stacks.length > 1) {
this.fnBtnToggleToBack()
} else if (this.pageContext.homePage === currentRouter) {
} else if (this.pageContext.homePage === currentRouter || this.cache[this.pageContext.originPathname]?.homeButtonHidden) {
this.fnBtnToggleToNone()
} else {
this.fnBtnToggleToHome()
}
}

hideHomeButton () {
// Note: 页面通常在 onShow 中调用,此时 load 尚未执行,需要先初始化缓存,确保 load 时不会重新展示首页按钮
// 与标题、颜色等导航栏状态一致,按路由路径缓存,同一路由的后续页面实例也会保持隐藏
this.setCacheValue()
this.cache[this.pageContext.originPathname].homeButtonHidden = true
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
this.setFnBtnState()
}

shiftLoadingState (show: boolean) {
if (!this.loadingElement) return
if (show) {
Expand Down
4 changes: 4 additions & 0 deletions packages/taro-router/src/utils/navigate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ export function setNavigationBarStyle (option: { backgroundColor: string, frontC
export function setNavigationBarLoading (loading: boolean): void {
eventCenter.trigger('__taroH5setNavigationBarLoading', loading)
}

export function hideNavigationBarHomeButton (): void {
eventCenter.trigger('__taroH5HideHomeButton')
}
3 changes: 2 additions & 1 deletion packages/taro/types/api/ui/navigation-bar.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ declare module '../../index' {
hideNavigationBarLoading(option?: hideNavigationBarLoading.Option): void

/** 隐藏返回首页按钮。微信7.0.7版本起,当用户打开的小程序最底层页面是非首页时,默认展示“返回首页”按钮,开发者可在页面 onShow 中调用 hideHomeButton 进行隐藏。
* @supported weapp, tt, harmony
* @supported weapp, tt, h5, harmony
* @h5 隐藏当前页面 H5 导航栏中的返回首页按钮,不影响返回按钮
* @see https://developers.weixin.qq.com/miniprogram/dev/api/ui/navigation-bar/wx.hideHomeButton.html
*/
hideHomeButton(option?: hideHomeButton.Option): Promise<TaroGeneral.CallbackResult>
Expand Down
Loading