Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Convert Pagination component to Composition API #127

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 54 additions & 56 deletions lib/components/base/Pagination.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
:key="'page-' + item + '-' + index"
:class="{
'page-number': page !== item,
shrink: item > 99,
shrink: (item as number) > 99,
Mysterious-Dev marked this conversation as resolved.
Show resolved Hide resolved
}"
class="page-number-container"
>
Expand All @@ -26,7 +26,7 @@
v-else
:class="{
'page-number current': page === item,
shrink: item > 99,
shrink: (item as number) > 99,
Mysterious-Dev marked this conversation as resolved.
Show resolved Hide resolved
}"
:href="linkFunction(item)"
@click.prevent="page !== item ? switchPage(item) : null"
Expand All @@ -49,66 +49,64 @@
</a>
</div>
</template>
<script setup>
<script setup lang="ts">
import { computed } from 'vue'
import { GapIcon, LeftArrowIcon, RightArrowIcon } from '@'
</script>
<script>
import { defineComponent } from 'vue'

export default defineComponent({
props: {
page: {
type: Number,
default: 1,
},
count: {
type: Number,
default: 1,
},
linkFunction: {
type: Function,
default() {
return null
},
},

const emit = defineEmits<{
'switch-page': [page: number]
}>()

const props = defineProps({
page: {
type: Number,
default: 1,
},
emits: ['switch-page'],
computed: {
pages() {
let pages = []

if (this.count > 7) {
if (this.page + 3 >= this.count) {
pages = [
1,
'-',
this.count - 4,
this.count - 3,
this.count - 2,
this.count - 1,
this.count,
]
} else if (this.page > 5) {
pages = [1, '-', this.page - 1, this.page, this.page + 1, '-', this.count]
} else {
pages = [1, 2, 3, 4, 5, '-', this.count]
}
} else {
pages = Array.from({ length: this.count }, (_, i) => i + 1)
}

return pages
},
count: {
type: Number,
default: 1,
},
methods: {
switchPage(newPage) {
this.$emit('switch-page', newPage)
if (newPage !== null && newPage !== '' && !isNaN(newPage)) {
this.$emit('switch-page', Math.min(Math.max(newPage, 1), this.count))
}
linkFunction: {
type: Function,
default() {
return null
},
},
})
Mysterious-Dev marked this conversation as resolved.
Show resolved Hide resolved

const pages = computed(() => {
let pages = []
Mysterious-Dev marked this conversation as resolved.
Show resolved Hide resolved

if (props.count > 7) {
if (props.page + 3 >= props.count) {
pages = [
1,
'-',
props.count - 4,
props.count - 3,
props.count - 2,
props.count - 1,
props.count,
]
} else if (props.page > 5) {
pages = [1, '-', props.page - 1, props.page, props.page + 1, '-', props.count]
} else {
pages = [1, 2, 3, 4, 5, '-', props.count]
}
} else {
pages = Array.from({ length: props.count }, (_, i) => i + 1)
}

return pages
})

function switchPage(newPage: number | string) {
let newPageNumber: number = newPage as number
emit('switch-page', newPageNumber)
if (newPage !== null && newPage !== '' && !isNaN(newPageNumber)) {
emit('switch-page', Math.min(Math.max(newPageNumber, 1), props.count))
}
}
Mysterious-Dev marked this conversation as resolved.
Show resolved Hide resolved
</script>

<style scoped lang="scss">
Expand Down