-
Notifications
You must be signed in to change notification settings - Fork 7
/
slds-pagination.vue
107 lines (81 loc) · 2.82 KB
/
slds-pagination.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<template>
<div class="slds-button-group">
<slds-button-icon
bordered
:disabled="isInFirstPage || disabled"
icon-name="utility:chevronleft"
@click.stop="handleClickPreviousPage"
/>
<template v-for="page in pages" :key="page">
<slds-button
v-if="page !== 0"
:class="buttonVariant(page)"
:disabled="disabled"
:label="page.toString()"
@click="handleClickPage(page)"
/>
</template>
<slds-button-icon
bordered
:disabled="isInLastPage || disabled"
icon-name="utility:chevronright"
@click="handleClickNextPage"
/>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue"
import SldsButtonIcon from "../slds-button-icon/slds-button-icon.vue"
import SldsButton from "../slds-button/slds-button.vue"
import { EVENTS } from "../../constants"
export default defineComponent ({
name: "SldsPagination",
components: { SldsButton, SldsButtonIcon },
props: {
currentPage: { type: Number, required: true },
disabled: { type: Boolean, default: false },
range: { type: Number, default: 3 },
totalPages: { type: Number, required: true },
},
computed: {
endPage(): number {
return Math.min(this.startPage + this.range - 1, this.totalPages)
},
isInFirstPage(): boolean {
return this.currentPage === 1
},
isInLastPage(): boolean {
return this.currentPage === this.totalPages
},
pages(): number[] {
const pages = []
for (let i = this.startPage; i <= this.endPage; i += 1) pages.push(i)
return pages
},
startPage(): number {
const halfRange = Math.floor(this.range / 2)
if (this.currentPage <= halfRange) return 1
else if (this.currentPage >= this.totalPages - halfRange) return this.totalPages - this.range + 1
else return this.currentPage - halfRange
},
},
methods: {
handleClickPreviousPage(): void {
this.$emit(EVENTS.PAGE_CHANGED, this.currentPage - 1)
},
handleClickPage(page: number): void {
if (page === this.currentPage) return
this.$emit(EVENTS.PAGE_CHANGED, page)
},
handleClickNextPage(): void {
this.$emit(EVENTS.PAGE_CHANGED, this.currentPage + 1)
},
buttonVariant(page: number): string {
let classNames = "slds-p-horizontal_small"
if (page === this.currentPage) classNames += " slds-button_brand"
else classNames += " slds-button_neutral"
return classNames
},
},
})
</script>