Convert Pagination component to Composition API (#127)

* Convert Pagination component to Composition API

* Apply suggestions by brawaru

* Fix lint error

* Apply suggestions from brawaru
This commit is contained in:
Mysterious_Dev 2023-11-13 21:06:06 +01:00 committed by GitHub
parent 67f77d027a
commit 6169ff99a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,7 +15,7 @@
:key="'page-' + item + '-' + index" :key="'page-' + item + '-' + index"
:class="{ :class="{
'page-number': page !== item, 'page-number': page !== item,
shrink: item > 99, shrink: item !== '-' && item > 99,
}" }"
class="page-number-container" class="page-number-container"
> >
@ -49,66 +49,56 @@
</a> </a>
</div> </div>
</template> </template>
<script setup> <script setup lang="ts">
import { computed } from 'vue'
import { GapIcon, LeftArrowIcon, RightArrowIcon } from '@' import { GapIcon, LeftArrowIcon, RightArrowIcon } from '@'
</script>
<script>
import { defineComponent } from 'vue'
export default defineComponent({ const emit = defineEmits<{
props: { 'switch-page': [page: number]
page: { }>()
type: Number,
default: 1,
},
count: {
type: Number,
default: 1,
},
linkFunction: {
type: Function,
default() {
return null
},
},
},
emits: ['switch-page'],
computed: {
pages() {
let pages = []
if (this.count > 7) { const props = withDefaults(
if (this.page + 3 >= this.count) { defineProps<{
pages = [ page: number
1, count: number
'-', linkFunction: (page: number) => string | undefined
this.count - 4, }>(),
this.count - 3, {
this.count - 2, page: 1,
this.count - 1, count: 1,
this.count, linkFunction: (page: number) => void page,
] }
} 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 const pages = computed(() => {
}, let pages: ('-' | number)[] = []
},
methods: { if (props.count > 7) {
switchPage(newPage) { if (props.page + 3 >= props.count) {
this.$emit('switch-page', newPage) pages = [
if (newPage !== null && newPage !== '' && !isNaN(newPage)) { 1,
this.$emit('switch-page', Math.min(Math.max(newPage, 1), this.count)) '-',
} 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) {
emit('switch-page', Math.min(Math.max(newPage, 1), props.count))
}
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">