Skip to content

Commit 63ed557

Browse files
authored
fix(kpagination): ts improvements (#2698)
1 parent ebf2936 commit 63ed557

File tree

2 files changed

+119
-78
lines changed

2 files changed

+119
-78
lines changed

src/components/KPagination/KPagination.vue

+35-76
Original file line numberDiff line numberDiff line change
@@ -149,82 +149,41 @@
149149
</nav>
150150
</template>
151151

152-
<script setup lang="ts">
153-
import type { Ref, PropType } from 'vue'
152+
<script setup lang="ts" generic="T">
154153
import { ref, computed, watch, onMounted, onUnmounted, nextTick } from 'vue'
155154
import KDropdown from '@/components/KDropdown/KDropdown.vue'
156155
import KButton from '@/components/KButton/KButton.vue'
157156
import PaginationOffset from './PaginationOffset.vue'
158-
import type { PageSizeChangeData, PageChangeData, DropdownItem, PopoverAttributes } from '@/types'
157+
import type { DropdownItem, PopoverAttributes, PaginationProps, PaginationEmits } from '@/types'
159158
import { BackIcon, ForwardIcon, ChevronDownIcon } from '@kong/icons'
160159
import { ResizeObserverHelper } from '@/utilities/resizeObserverHelper'
161160
162161
const kpopAttrs = {
163162
placement: 'top',
164163
} satisfies PopoverAttributes
165164
166-
const props = defineProps({
167-
items: {
168-
type: Array,
169-
default: () => [],
170-
},
171-
totalCount: {
172-
type: Number,
173-
default: 0,
174-
},
175-
pageSizes: {
176-
type: Array as PropType<number[]>,
177-
default: () => [15, 30, 50, 75, 100],
178-
validator: (pageSizes: number[]): boolean => !!pageSizes.length && pageSizes.every(i => typeof i === 'number'),
179-
},
180-
initialPageSize: {
181-
type: Number,
182-
default: null,
183-
},
184-
neighbors: {
185-
type: Number,
186-
default: 1,
187-
},
188-
searchTriggered: {
189-
type: Boolean,
190-
default: false,
191-
},
192-
currentPage: {
193-
type: Number,
194-
default: null,
195-
},
196-
disablePageJump: {
197-
type: Boolean,
198-
default: false,
199-
},
200-
offset: {
201-
type: Boolean,
202-
default: false,
203-
},
204-
offsetPreviousButtonDisabled: {
205-
type: Boolean,
206-
default: false,
207-
},
208-
offsetNextButtonDisabled: {
209-
type: Boolean,
210-
default: false,
211-
},
212-
})
213-
214-
const emit = defineEmits<{
215-
(e: 'pageChange', val: PageChangeData): void
216-
(e: 'pageSizeChange', val: PageSizeChangeData): void
217-
(e: 'getNextOffset'): void
218-
(e: 'getPreviousOffset'): void
219-
}>()
165+
const {
166+
items = [],
167+
totalCount = 0,
168+
pageSizes = [15, 30, 50, 75, 100],
169+
initialPageSize = null,
170+
neighbors = 1,
171+
currentPage = null,
172+
disablePageJump,
173+
offset,
174+
offsetPreviousButtonDisabled,
175+
offsetNextButtonDisabled,
176+
} = defineProps<PaginationProps<T>>()
177+
178+
const emit = defineEmits<PaginationEmits<T>>()
220179
221180
const kPaginationElement = ref<HTMLElement | null>(null)
222181
const resizeObserver = ref<ResizeObserverHelper>()
223182
224-
const currPage: Ref<number> = ref(props.currentPage ? props.currentPage : 1)
225-
const currentPageSize: Ref<number> = ref(props.initialPageSize ? props.initialPageSize : props.pageSizes[0])
226-
const pageCount = computed((): number => Math.ceil(props.totalCount / currentPageSize.value))
227-
const pageSizeOptions = props.pageSizes.map((size, i) => ({
183+
const currPage = ref<number>(currentPage ? currentPage : 1)
184+
const currentPageSize = ref<number>(initialPageSize ? initialPageSize : pageSizes[0])
185+
const pageCount = computed((): number => Math.ceil(totalCount / currentPageSize.value))
186+
const pageSizeOptions = pageSizes.map((size, i) => ({
228187
label: `${size}`,
229188
key: `size-${i}`,
230189
value: size,
@@ -235,7 +194,7 @@ const pageSizeText = computed((): string => `${currentPageSize.value} ${currentP
235194
* KPagination will try to display specified number of neighbors
236195
* However, if it will detect overflow, it will try to reduce the number of neighbors to a minimum of 1
237196
*/
238-
const fittingNeighbors = ref<number>(props.neighbors)
197+
const fittingNeighbors = ref<number>(neighbors)
239198
/**
240199
* By default KPagination tries to display 3 items sequentially
241200
* However, if it will detect overflow, it will try to reduce the number of items to a minimum of 1
@@ -273,7 +232,7 @@ const fixOverflow = async (): Promise<void> => {
273232
}
274233
275234
const getVisiblePages = (currPage: number, pageCount: number, firstDetached: boolean, lastDetached: boolean): number[] => {
276-
if (props.disablePageJump) {
235+
if (disablePageJump) {
277236
return []
278237
}
279238
@@ -312,13 +271,13 @@ const forwardDisabled = ref<boolean>(currPage.value === pageCount.value)
312271
const startCount = computed((): number => (currPage.value - 1) * currentPageSize.value + 1)
313272
const endCount = computed((): number => {
314273
const calculatedEndCount = startCount.value - 1 + currentPageSize.value
315-
return calculatedEndCount > props.totalCount
316-
? props.totalCount
274+
return calculatedEndCount > totalCount
275+
? totalCount
317276
: calculatedEndCount
318277
})
319278
const pagesString = computed((): string => `${startCount.value} to ${endCount.value}`)
320-
const pageCountString = computed((): string => ` of ${props.totalCount}`)
321-
const currentlySelectedPage = computed((): number => props.currentPage ? props.currentPage : currPage.value)
279+
const pageCountString = computed((): string => ` of ${totalCount}`)
280+
const currentlySelectedPage = computed((): number => currentPage ? currentPage : currPage.value)
322281
323282
// Selected page, first page, last page, 2 placeholders and 2 * neighbors
324283
const visiblePages = computed((): number => 5 + 2 * fittingNeighbors.value)
@@ -341,14 +300,14 @@ const pageBack = ():void => {
341300
updatePage()
342301
}
343302
344-
const changePage = (page: number):void => {
303+
const changePage = (page: number): void => {
345304
currPage.value = page
346305
updatePage()
347306
}
348307
349308
const updatePage = (): void => {
350309
const lastEntry = (currPage.value - 1) * currentPageSize.value + currentPageSize.value
351-
forwardDisabled.value = lastEntry >= props.totalCount
310+
forwardDisabled.value = lastEntry >= totalCount
352311
backDisabled.value = currPage.value === 1
353312
// The view will hold
354313
if (pageCount.value <= visiblePages.value) {
@@ -367,7 +326,7 @@ const updatePage = (): void => {
367326
pageCount: pageCount.value,
368327
firstItem: startCount.value,
369328
lastItem: endCount.value,
370-
visibleItems: props.items.slice(startCount.value - 1, endCount.value),
329+
visibleItems: items.slice(startCount.value - 1, endCount.value),
371330
})
372331
}
373332
@@ -380,7 +339,7 @@ const updatePageSize = (item: DropdownItem): void => {
380339
pageCount: pageCount.value,
381340
})
382341
383-
if (props.currentPage !== 1) {
342+
if (currentPage !== 1) {
384343
changePage(1)
385344
}
386345
}
@@ -394,8 +353,8 @@ const getPreviousOffset = (): void => {
394353
emit('getPreviousOffset')
395354
}
396355
397-
watch(() => props.currentPage, (newVal, oldVal) => {
398-
if (newVal !== oldVal) {
356+
watch(() => currentPage, (newVal, oldVal) => {
357+
if (newVal !== oldVal && newVal) {
399358
changePage(newVal)
400359
}
401360
})
@@ -416,21 +375,21 @@ watch(pageCount, (newVal, oldVal) => {
416375
})
417376
418377
watch(pagesVisible, () => {
419-
if (!props.disablePageJump && !props.offset) {
378+
if (!disablePageJump && !offset) {
420379
fixOverflow()
421380
}
422381
})
423382
424383
onMounted(() => {
425-
if (!props.disablePageJump && !props.offset) {
384+
if (!disablePageJump && !offset) {
426385
resizeObserver.value = ResizeObserverHelper.create(fixOverflow)
427386
428387
resizeObserver.value.observe(kPaginationElement.value as HTMLDivElement)
429388
}
430389
})
431390
432391
onUnmounted(() => {
433-
if (!props.disablePageJump && !props.offset) {
392+
if (!disablePageJump && !offset) {
434393
resizeObserver.value?.unobserve(kPaginationElement.value as HTMLDivElement)
435394
}
436395
})

src/types/pagination.ts

+84-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,94 @@
1-
export interface PageChangeData {
1+
export interface PageChangeData<T = any> {
22
page: number
33
pageCount: number
44
firstItem: number
55
lastItem: number
6-
visibleItems: any[]
6+
visibleItems: T[]
77
}
88

99
export interface PageSizeChangeData {
1010
pageSize: number
1111
pageCount: number
1212
}
13+
14+
export interface PaginationProps<T> {
15+
/**
16+
* Optional array of items that can be provided for easy pagination.
17+
* Slice of this array with visible items is returned as visibleItems inside the pageChange event.
18+
* @default []
19+
*/
20+
items?: T[]
21+
22+
/**
23+
* The total number of items.
24+
* @default 0
25+
*/
26+
totalCount?: number
27+
28+
/**
29+
* The available page size options.
30+
* @default [15, 30, 50, 75, 100]
31+
*/
32+
pageSizes?: number[]
33+
34+
/**
35+
* The initial page size.
36+
* @default null
37+
*/
38+
initialPageSize?: number | null
39+
40+
/**
41+
* The number of neighboring pages to display on each side of the current page.
42+
* @default 1
43+
*/
44+
neighbors?: number
45+
46+
/**
47+
* The current page number.
48+
* @default null
49+
*/
50+
currentPage?: number | null
51+
52+
/**
53+
* Disables the page jump functionality.
54+
* @default false
55+
*/
56+
disablePageJump?: boolean
57+
58+
/**
59+
* Enables or disables offset mode(offset mode omits the concept of pages).
60+
* @default false
61+
*/
62+
offset?: boolean
63+
64+
/**
65+
* Disables the "previous" button in offset mode.
66+
* @default false
67+
*/
68+
offsetPreviousButtonDisabled?: boolean
69+
70+
/**
71+
* Disables the "next" button in offset mode.
72+
* @default false
73+
*/
74+
offsetNextButtonDisabled?: boolean
75+
}
76+
77+
export interface PaginationEmits<T> {
78+
/**
79+
* Emitted when the page changes, pass the current page information along with it.
80+
*/
81+
pageChange: [data: PageChangeData<T>]
82+
/**
83+
* Emitted when the page size changes, pass the current page size and page count.
84+
*/
85+
pageSizeChange: [data: PageSizeChangeData]
86+
/**
87+
* Emitted when going to the next page in offset mode.
88+
*/
89+
getNextOffset: []
90+
/**
91+
* Emitted when going to the previous page in offset mode.
92+
*/
93+
getPreviousOffset: []
94+
}

0 commit comments

Comments
 (0)