-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mp): 新增 useTopBarInfo, useSubmit, usePullDownRefresh
- Loading branch information
Showing
11 changed files
with
64 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
}) | ||
}) | ||
}, []) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, []) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* Taro 3 工具库。 | ||
* Taro 3 工具库。(已废弃,使用小程序工具库代替) | ||
* | ||
* @packageDocumentation | ||
*/ | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |