Skip to content

Commit

Permalink
fix(usePagination): remove ellipsis options
Browse files Browse the repository at this point in the history
  • Loading branch information
nemo-shen committed Mar 2, 2024
1 parent 4c78e29 commit 20d6025
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 53 deletions.
10 changes: 0 additions & 10 deletions packages/core/usePagination/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ describe('usePagination', () => {
expect(pages.value.length).toEqual(3)
})

test('ellipsis', () => {
const { pages } = usePagination({
total: 10,
pageSize: 1,
maxPageCount: 3,
ellipsis: true,
})
expect(pages.value[1].text).toEqual('...')
})

test('currentPage', () => {
const { currentPage, nextPage } = usePagination({
total: 5,
Expand Down
48 changes: 5 additions & 43 deletions packages/core/usePagination/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { ref, computed, type ComputedRef, type Ref } from 'vue'

interface PageItem {
text: string | number;
text: string | number
value: number
isCurrent: boolean
type: 'page' | 'ellipsis'
type: 'page'
}

interface UsePaginationOption {
total: number
pageSize?: number
currentPage?: number
maxPageCount?: number
ellipsis?: boolean
}

interface UsePaginationReturn {
Expand All @@ -32,7 +31,7 @@ interface UsePaginationReturn {
export const usePagination = (
options: UsePaginationOption
): UsePaginationReturn => {
const { total, ellipsis = false } = options
const { total } = options

const currentPage = ref(options.currentPage || 1)
const pageSize = ref(options.pageSize || 10)
Expand All @@ -44,49 +43,12 @@ export const usePagination = (
: options.maxPageCount || 5
const isLastPage = computed(() => currentPage.value === totalPage.value)
const pages = computed(() =>
Array.from({ length: maxPageCount }, (_, index) => {
if (!ellipsis) {
return {
text: index + 1,
value: index + 1,
isCurrent: index + 1 === currentPage.value,
type: 'page',
} as PageItem
}
if (maxPageCount % 2 === 0) {
if (
index + 1 <= maxPageCount / 2 ||
totalPage.value - (index + 1) < maxPageCount / 2
) {
return {
text: index + 1,
value: index + 1,
isCurrent: index + 1 === currentPage.value,
type: 'page',
} as PageItem
}
return {
text: '...',
value: 0,
isCurrent: false,
type: 'ellipsis',
} as PageItem
}
if (Math.floor(maxPageCount / 2) + 1 === index + 1) {
return {
text: '...',
value: 0,
isCurrent: false,
type: 'ellipsis',
} as PageItem
}
return {
Array.from({ length: maxPageCount }, (_, index) => ({
text: index + 1,
value: index + 1,
isCurrent: index + 1 === currentPage.value,
type: 'page',
} as PageItem
})
} as PageItem))
)

const gotoPage = (page: number) => {
Expand Down

0 comments on commit 20d6025

Please sign in to comment.