149
149
</nav >
150
150
</template >
151
151
152
- <script setup lang="ts">
153
- import type { Ref , PropType } from ' vue'
152
+ <script setup lang="ts" generic =" T " >
154
153
import { ref , computed , watch , onMounted , onUnmounted , nextTick } from ' vue'
155
154
import KDropdown from ' @/components/KDropdown/KDropdown.vue'
156
155
import KButton from ' @/components/KButton/KButton.vue'
157
156
import PaginationOffset from ' ./PaginationOffset.vue'
158
- import type { PageSizeChangeData , PageChangeData , DropdownItem , PopoverAttributes } from ' @/types'
157
+ import type { DropdownItem , PopoverAttributes , PaginationProps , PaginationEmits } from ' @/types'
159
158
import { BackIcon , ForwardIcon , ChevronDownIcon } from ' @kong/icons'
160
159
import { ResizeObserverHelper } from ' @/utilities/resizeObserverHelper'
161
160
162
161
const kpopAttrs = {
163
162
placement: ' top' ,
164
163
} satisfies PopoverAttributes
165
164
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 >>()
220
179
221
180
const kPaginationElement = ref <HTMLElement | null >(null )
222
181
const resizeObserver = ref <ResizeObserverHelper >()
223
182
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 ) => ({
228
187
label: ` ${size } ` ,
229
188
key: ` size-${i } ` ,
230
189
value: size ,
@@ -235,7 +194,7 @@ const pageSizeText = computed((): string => `${currentPageSize.value} ${currentP
235
194
* KPagination will try to display specified number of neighbors
236
195
* However, if it will detect overflow, it will try to reduce the number of neighbors to a minimum of 1
237
196
*/
238
- const fittingNeighbors = ref <number >(props . neighbors )
197
+ const fittingNeighbors = ref <number >(neighbors )
239
198
/**
240
199
* By default KPagination tries to display 3 items sequentially
241
200
* 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> => {
273
232
}
274
233
275
234
const getVisiblePages = (currPage : number , pageCount : number , firstDetached : boolean , lastDetached : boolean ): number [] => {
276
- if (props . disablePageJump ) {
235
+ if (disablePageJump ) {
277
236
return []
278
237
}
279
238
@@ -312,13 +271,13 @@ const forwardDisabled = ref<boolean>(currPage.value === pageCount.value)
312
271
const startCount = computed ((): number => (currPage .value - 1 ) * currentPageSize .value + 1 )
313
272
const endCount = computed ((): number => {
314
273
const calculatedEndCount = startCount .value - 1 + currentPageSize .value
315
- return calculatedEndCount > props . totalCount
316
- ? props . totalCount
274
+ return calculatedEndCount > totalCount
275
+ ? totalCount
317
276
: calculatedEndCount
318
277
})
319
278
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 )
322
281
323
282
// Selected page, first page, last page, 2 placeholders and 2 * neighbors
324
283
const visiblePages = computed ((): number => 5 + 2 * fittingNeighbors .value )
@@ -341,14 +300,14 @@ const pageBack = ():void => {
341
300
updatePage ()
342
301
}
343
302
344
- const changePage = (page : number ): void => {
303
+ const changePage = (page : number ): void => {
345
304
currPage .value = page
346
305
updatePage ()
347
306
}
348
307
349
308
const updatePage = (): void => {
350
309
const lastEntry = (currPage .value - 1 ) * currentPageSize .value + currentPageSize .value
351
- forwardDisabled .value = lastEntry >= props . totalCount
310
+ forwardDisabled .value = lastEntry >= totalCount
352
311
backDisabled .value = currPage .value === 1
353
312
// The view will hold
354
313
if (pageCount .value <= visiblePages .value ) {
@@ -367,7 +326,7 @@ const updatePage = (): void => {
367
326
pageCount: pageCount .value ,
368
327
firstItem: startCount .value ,
369
328
lastItem: endCount .value ,
370
- visibleItems: props . items .slice (startCount .value - 1 , endCount .value ),
329
+ visibleItems: items .slice (startCount .value - 1 , endCount .value ),
371
330
})
372
331
}
373
332
@@ -380,7 +339,7 @@ const updatePageSize = (item: DropdownItem): void => {
380
339
pageCount: pageCount .value ,
381
340
})
382
341
383
- if (props . currentPage !== 1 ) {
342
+ if (currentPage !== 1 ) {
384
343
changePage (1 )
385
344
}
386
345
}
@@ -394,8 +353,8 @@ const getPreviousOffset = (): void => {
394
353
emit (' getPreviousOffset' )
395
354
}
396
355
397
- watch (() => props . currentPage , (newVal , oldVal ) => {
398
- if (newVal !== oldVal ) {
356
+ watch (() => currentPage , (newVal , oldVal ) => {
357
+ if (newVal !== oldVal && newVal ) {
399
358
changePage (newVal )
400
359
}
401
360
})
@@ -416,21 +375,21 @@ watch(pageCount, (newVal, oldVal) => {
416
375
})
417
376
418
377
watch (pagesVisible , () => {
419
- if (! props . disablePageJump && ! props . offset ) {
378
+ if (! disablePageJump && ! offset ) {
420
379
fixOverflow ()
421
380
}
422
381
})
423
382
424
383
onMounted (() => {
425
- if (! props . disablePageJump && ! props . offset ) {
384
+ if (! disablePageJump && ! offset ) {
426
385
resizeObserver .value = ResizeObserverHelper .create (fixOverflow )
427
386
428
387
resizeObserver .value .observe (kPaginationElement .value as HTMLDivElement )
429
388
}
430
389
})
431
390
432
391
onUnmounted (() => {
433
- if (! props . disablePageJump && ! props . offset ) {
392
+ if (! disablePageJump && ! offset ) {
434
393
resizeObserver .value ?.unobserve (kPaginationElement .value as HTMLDivElement )
435
394
}
436
395
})
0 commit comments