-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1149 from thunderstore-io/pagination-improvements
Improve pagination support for large page counts
- Loading branch information
Showing
3 changed files
with
121 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<script lang="ts"> | ||
import { Vue, Component, Prop, Watch } from 'vue-property-decorator'; | ||
import { truncatePagination } from "../../utils/Pagination"; | ||
@Component({}) | ||
export default class PaginationButtons extends Vue { | ||
@Prop({ required: true }) | ||
private currentPage!: number; | ||
@Prop({ required: true }) | ||
private pageCount!: number; | ||
@Prop({ required: true }) | ||
private contextSize!: number; | ||
@Prop({ required: true }) | ||
private onClick!: (pageIndex: number) => void; | ||
@Watch("currentPage") | ||
@Watch("pageCount") | ||
@Watch("contextSize") | ||
visibleButtons() { | ||
return truncatePagination({ | ||
currentPage: this.currentPage, | ||
pageCount: this.pageCount, | ||
contextSize: this.contextSize, | ||
}); | ||
} | ||
} | ||
</script> | ||
|
||
<template> | ||
<nav class="pagination"> | ||
<ul class="pagination-list"> | ||
<li | ||
v-for="button in visibleButtons()" | ||
:key="`pagination-${button.index}`" | ||
> | ||
<a | ||
:class="[ | ||
'pagination-link', | ||
'flex-centered', | ||
{'is-current': button.index === currentPage} | ||
]" | ||
@click="onClick(button.index)" | ||
>{{button.title}}</a> | ||
</li> | ||
</ul> | ||
</nav> | ||
</template> | ||
|
||
<style scoped lang="scss"> | ||
.flex-centered { | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
export type PaginationButton = { | ||
index: number; | ||
title: string; | ||
} | ||
|
||
export function truncatePagination(props: { | ||
currentPage: number, | ||
pageCount: number, | ||
contextSize: number, | ||
}): PaginationButton[] { | ||
const result = []; | ||
|
||
if (props.currentPage - props.contextSize > 1) { | ||
result.push({ | ||
index: 1, | ||
title: "1" | ||
}); | ||
} | ||
if (props.currentPage - props.contextSize - 1 > 1) { | ||
result.push({ | ||
index: props.currentPage - props.contextSize - 1, | ||
title: "...", | ||
}); | ||
} | ||
|
||
for ( | ||
let i = props.currentPage - props.contextSize; | ||
i <= props.currentPage + props.contextSize; | ||
i++ | ||
) { | ||
if (i > 0 && i <= props.pageCount) { | ||
result.push({ | ||
index: i, | ||
title: i.toString(), | ||
}); | ||
} | ||
} | ||
|
||
if (props.currentPage + props.contextSize + 1 < props.pageCount) { | ||
result.push({ | ||
index: props.currentPage + props.contextSize + 1, | ||
title: "...", | ||
}); | ||
} | ||
if (props.currentPage + props.contextSize < props.pageCount) { | ||
result.push({ | ||
index: props.pageCount, | ||
title: props.pageCount.toString(), | ||
}); | ||
} | ||
|
||
return result; | ||
} |