Skip to content

Commit

Permalink
feat(usePagination): add countStart and countEnd to returns
Browse files Browse the repository at this point in the history
  • Loading branch information
vikiboss committed Sep 4, 2024
1 parent 26142ff commit dcbcad9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
26 changes: 21 additions & 5 deletions packages/react-use/src/use-pagination/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export type UsePaginationOptions = {
### Returns
```tsx
export interface UsePaginationReturnsState {
export interface UsePaginationReturnsState<T = any> {
/**
* Total number of items.
*/
Expand All @@ -91,20 +91,32 @@ export interface UsePaginationReturnsState {
currentPageSize: number
/**
* The current page number.
*
* @since 1.7.0
*/
page: number
/**
* The number of items to display per page.
*
* @since 1.7.0
*/
pageSize: number
/**
* The total number of pages.
*/
pageCount: number
/**
* The start index of the displayed list (helpful to slice the list).
*/
indexStart: number
/**
* The end index of the displayed list (helpful to slice the list).
*/
indexEnd: number
/**
* The start count of the displayed list.
*/
countStart: number
/**
* The end count of the displayed list.
*/
countEnd: number
/**
* Whether the current page is the first page.
*/
Expand All @@ -113,6 +125,10 @@ export interface UsePaginationReturnsState {
* Whether the current page is the last page.
*/
isLastPage: boolean
/**
* The sliced list to display.
*/
list: T[]
}

export interface UsePaginationReturnsActions {
Expand Down
13 changes: 12 additions & 1 deletion packages/react-use/src/use-pagination/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ export interface UsePaginationReturnsState<T = any> {
* The end index of the displayed list (helpful to slice the list).
*/
indexEnd: number
/**
* The start count of the displayed list.
*/
countStart: number
/**
* The end count of the displayed list.
*/
countEnd: number
/**
* Whether the current page is the first page.
*/
Expand Down Expand Up @@ -137,7 +145,7 @@ export type UsePaginationReturns<T> = readonly [UsePaginationReturnsState<T>, Us
* A React Hook that manage pagination state.
*/
export function usePagination<T = any>(options: UsePaginationOptions<T> = {}): UsePaginationReturns<T> {
const total = options.list ? options.list.length : Number.POSITIVE_INFINITY
const total = options.list ? options.list.length : options.total ?? Number.POSITIVE_INFINITY
const { list = [] as T[], page = 1, pageSize = 10, onPageChange, onPageSizeChange, onPageCountChange } = options

const isInfinity = total === Number.POSITIVE_INFINITY
Expand All @@ -151,6 +159,7 @@ export function usePagination<T = any>(options: UsePaginationOptions<T> = {}): U
const setPageSize = useStableFn((size: number) => pageSizeActions.set(size))

const [indexStart, indexEnd] = [(currentPage - 1) * currentPageSize, Math.min(currentPage * currentPageSize, total)]
const [countStart, countEnd] = [indexStart + 1, indexEnd]

const slicedList = useMemo(() => list.slice(indexStart, indexEnd), [list, indexStart, indexEnd])
const isFirstPage = currentPage === 1
Expand All @@ -165,6 +174,8 @@ export function usePagination<T = any>(options: UsePaginationOptions<T> = {}): U
pageCount,
indexStart,
indexEnd,
countStart,
countEnd,
isFirstPage,
isLastPage,
list: slicedList,
Expand Down
34 changes: 25 additions & 9 deletions packages/react-use/src/use-pagination/index.zh-cn.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -72,39 +72,51 @@ export type UsePaginationOptions = {
### 返回值 \{#returns}
```tsx
export interface UsePaginationReturnsState {
export interface UsePaginationReturnsState<T = any> {
/**
* 总项目数
* 列表总项目数
*/
total: number
/**
* 当前页码。
*
*
* @deprecated 请使用 `page` 代替。
*/
currentPage: number
/**
* 每页展示的项目数
* 当前每页展示的项目数
*
* @deprecated 请使用 `pageSize` 代替。
*/
currentPageSize: number
/**
* 当前页码。
*
* @since 1.7.0
*/
page: number
/**
* 每页展示的项目数。
*
* @since 1.7.0
* 当前每页展示的项目数。
*/
pageSize: number
/**
* 总页数。
*/
pageCount: number
/**
* 当前列表数据的起始索引。
*/
indexStart: number
/**
* 当前列表数据的结束索引。
*/
indexEnd: number
/**
* 当前展示的列表的起始计数。
*/
countStart: number
/**
* 当前展示的列表的结束计数。
*/
countEnd: number
/**
* 是否为第一页。
*/
Expand All @@ -113,6 +125,10 @@ export interface UsePaginationReturnsState {
* 是否为最后一页。
*/
isLastPage: boolean
/**
* 分页后的列表数据。
*/
list: T[]
}

export interface UsePaginationReturnsActions {
Expand Down

0 comments on commit dcbcad9

Please sign in to comment.