Skip to content

Commit

Permalink
chore: add refresh method in usePagingList
Browse files Browse the repository at this point in the history
  • Loading branch information
vikiboss committed Sep 5, 2024
1 parent d3d81ef commit aea132d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
14 changes: 8 additions & 6 deletions packages/react-use/src/use-paging-list/demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface Item {
}

export function App() {
const { list, loading, form, query, pagination, selection } = usePagingList<Item, FormState>(
const { list, loading, form, refresh, query, pagination, selection } = usePagingList<Item, FormState>(
async (params) => {
const { page, pageSize, form, setTotal } = params
const { data, total } = await fetchPagination({ page, pageSize })
Expand All @@ -27,16 +27,16 @@ export function App() {
form: {
initialValue: {
name: '',
gender: 'Girl',
color: ['Cyan'],
gender: 'Boy',
color: ['Red'],
},
},
query: {
refreshInterval: 3_000,
refreshInterval: 6_000,
},
pagination: {
page: 2,
pageSize: 10,
page: 1,
pageSize: 50,
},
immediateQueryKeys: ['color', 'gender'],
},
Expand All @@ -56,6 +56,8 @@ export function App() {
<span className="text-right inline-block w-80px">Name:</span>
<Input name="name" placeholder="Name" />
<Button type="submit">Search</Button>
<Button type="reset">Reset</Button>
<Button onClick={() => refresh()}>Refresh</Button>
</Zone>
<Zone>
<span className="text-right inline-block w-80px">Gender:</span>
Expand Down
5 changes: 5 additions & 0 deletions packages/react-use/src/use-paging-list/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const {
pagination,
selection,
list,
refresh,
setTotal,
loading
} = usePagingList(fetcher, options)
Expand Down Expand Up @@ -197,6 +198,10 @@ export interface UsePagingListReturns<
* query instance, refer to useQuery documentation
*/
query: UseQueryOptions<Fetcher>
/**
* refresh query
*/
refresh: () => void
/**
* Set total data amount
*/
Expand Down
22 changes: 19 additions & 3 deletions packages/react-use/src/use-paging-list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export interface UsePagingListReturns<
* query instance
*/
query: UseQueryOptions<Fetcher>
/**
* refresh query
*/
refresh: () => void
/**
* set total count
*/
Expand Down Expand Up @@ -125,10 +129,19 @@ export function usePagingList<
...options.form,
onReset(...args) {
startNewQuery()
latest.current.options.form?.onReset?.(...args)
return latest.current.options.form?.onReset?.(...args)
},
onSubmit(...args) {
startNewQuery()
const form = args[0]

query.run({
previousData: previousDataRef.current,
page: paginationState.page,
pageSize: paginationState.pageSize,
form,
setTotal,
})

latest.current.options.form?.onSubmit?.(...args)
},
onChange(...args) {
Expand All @@ -146,7 +159,7 @@ export function usePagingList<

previousFormRef.current = nextForm

latest.current.options.form?.onChange?.(...args)
return latest.current.options.form?.onChange?.(...args)
},
})

Expand Down Expand Up @@ -230,6 +243,8 @@ export function usePagingList<

const latest = useLatest({ options, paginationState })

const refresh = useStableFn(() => query.refresh())

return {
get loading() {
return query.loading
Expand All @@ -238,6 +253,7 @@ export function usePagingList<
form,
query,
setTotal,
refresh,
selection: {
...select,
...selectActions,
Expand Down
5 changes: 5 additions & 0 deletions packages/react-use/src/use-paging-list/index.zh-cn.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const {
pagination,
selection,
list,
refresh,
setTotal,
loading
} = usePagingList(fetcher, options)
Expand Down Expand Up @@ -197,6 +198,10 @@ export interface UsePagingListReturns<
* query 实例,参考 useQuery 文档
*/
query: UseQueryOptions<Fetcher>
/**
* 使用上次请求参数(表单状态等),刷新数据
*/
refresh: () => void
/**
* 设置数据总数
*/
Expand Down

0 comments on commit aea132d

Please sign in to comment.