Skip to content

Commit

Permalink
feat(mp): 新增 useTopBarInfo, useSubmit, usePullDownRefresh
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Aug 28, 2020
1 parent 033a818 commit 19073ec
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 76 deletions.
3 changes: 3 additions & 0 deletions src/mp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ export * from './navigatePageTo'
export * from './patchMiniProgram'
export * from './redirectPageTo'
export * from './submit'
export * from './usePullDownRefresh'
export * from './useSubmit'
export * from './useTopBarInfo'
// @endindex
2 changes: 1 addition & 1 deletion src/mp/navigatePageTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ensureInMiniProgram } from './ensureInMiniProgram'
import { getMiniProgramConfig } from './miniProgramConfig'

/**
* 跳转至某个页面。
* 跳转至某个页面,跳转失败时会尝试切换到 Tab 页
*
* @param url 要跳转去的页面地址
* @param query 查询参数
Expand Down
2 changes: 1 addition & 1 deletion src/mp/redirectPageTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AnyObject } from '../types'
import { navigatePageTo } from './navigatePageTo'

/**
* 关闭当前页面,跳转至某个页面。
* 关闭当前页面,跳转至某个页面,跳转失败时会尝试切换到 Tab 页
*
* @param url 要跳转去的页面地址
* @param query 查询参数
Expand Down
21 changes: 21 additions & 0 deletions src/mp/usePullDownRefresh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ensureInMiniProgram } from './ensureInMiniProgram'
import { isPromiseLike } from '../utils'
import { miniProgramBus } from './miniProgramBus'
import { useEffect } from 'react'
import { useLatest } from '../react'

export function usePullDownRefresh(callback: () => any): void {
const latestCallback = useLatest(callback)
useEffect(() => {
return miniProgramBus.on('currentPagePullDownRefresh', () => {
ensureInMiniProgram(mp => {
const res = latestCallback.current()
if (isPromiseLike(res)) {
res.then(() => mp.stopPullDownRefresh())
} else {
mp.stopPullDownRefresh()
}
})
})
}, [])
}
13 changes: 13 additions & 0 deletions src/mp/useSubmit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React, { useCallback } from 'react'
import { submit } from './submit'
import { SubmitActionPayload } from '../utils'

/**
* 对提交类行为的封装。
*/
export function useSubmit<TResult>(
action: (payload: SubmitActionPayload) => Promise<TResult>,
deps: React.DependencyList,
): () => Promise<TResult> {
return useCallback(() => submit(action), deps)
}
11 changes: 11 additions & 0 deletions src/mp/useTopBarInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { getTopBarInfo, GetTopBarInfoResult } from './getTopBarInfo'
import { useMemo } from 'react'

/**
* 获取顶栏信息。
*
* @returns 返回获取到的顶栏信息
*/
export function useTopBarInfo(): GetTopBarInfoResult {
return useMemo(getTopBarInfo, [])
}
2 changes: 1 addition & 1 deletion src/taro/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Taro 3 工具库。
* Taro 3 工具库。(已废弃,使用小程序工具库代替)
*
* @packageDocumentation
*/
Expand Down
44 changes: 0 additions & 44 deletions src/taro/useAutoStopPullDownRefresh.test.ts

This file was deleted.

18 changes: 3 additions & 15 deletions src/taro/useAutoStopPullDownRefresh.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
import { isPromiseLike } from '../utils'
import { stopPullDownRefresh, usePullDownRefresh } from '@tarojs/taro'
import { usePullDownRefresh as mpUsePullDownRefresh } from '../mp'

/**
* 同 `Taro.usePullDownRefresh`,不过在回调函数完成后会自动调用 `Taro.stopPullDownRefresh()`。
*
* @param callback 回调函数
* @deprecated 使用 `import { usePullDownRefresh } from 'vtils/mp'` 代替
*/
export function useAutoStopPullDownRefresh(callback: () => any) {
usePullDownRefresh(() => {
const res = callback()
if (isPromiseLike(res)) {
res.then(() => stopPullDownRefresh())
} else {
stopPullDownRefresh()
}
})
}
export const useAutoStopPullDownRefresh = mpUsePullDownRefresh
14 changes: 5 additions & 9 deletions src/taro/useSubmit.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import React, { useCallback } from 'react'
import { submit } from '../mp'
import { SubmitActionPayload } from '../utils'
import { useSubmit as mpUseSubmit } from '../mp'

export function useSubmit<TResult>(
action: (payload: SubmitActionPayload) => Promise<TResult>,
deps: React.DependencyList,
): () => Promise<TResult> {
return useCallback(() => submit(action), deps)
}
/**
* @deprecated 使用 `import { useSubmit } from 'vtils/mp'` 代替
*/
export const useSubmit = mpUseSubmit
10 changes: 5 additions & 5 deletions src/taro/useTopBarInfo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getTopBarInfo, GetTopBarInfoResult } from '../mp'
import { useMemo } from 'react'
import { useTopBarInfo as mpUseTopBarInfo } from '../mp'

export function useTopBarInfo(): GetTopBarInfoResult {
return useMemo(getTopBarInfo, [])
}
/**
* @deprecated 使用 `import { useTopBarInfo } from 'vtils/mp'` 代替
*/
export const useTopBarInfo = mpUseTopBarInfo

0 comments on commit 19073ec

Please sign in to comment.