54 lines
1.7 KiB
Vue
54 lines
1.7 KiB
Vue
<script setup lang="ts">
|
|
const props = defineProps<{ page: number; total: number; perPage: number }>();
|
|
const emit = defineEmits<{ change: [page: number] }>();
|
|
|
|
const pages = computed(() => Math.max(1, Math.ceil(props.total / props.perPage)));
|
|
const window_ = computed(() => {
|
|
const p = props.page;
|
|
const n = pages.value;
|
|
const start = Math.max(1, Math.min(p - 2, n - 4));
|
|
const out: number[] = [];
|
|
for (let i = start; i <= Math.min(n, start + 4); i++) out.push(i);
|
|
return out;
|
|
});
|
|
|
|
function go(p: number): void {
|
|
if (p >= 1 && p <= pages.value && p !== props.page) emit("change", p);
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<nav v-if="pages > 1" class="my-[30px] flex justify-center gap-1.5">
|
|
<button
|
|
type="button"
|
|
class="border-border bg-surface h-[34px] min-w-[34px] rounded-sm border px-2 text-[13px] transition-colors disabled:cursor-not-allowed disabled:opacity-40"
|
|
:disabled="page <= 1"
|
|
@click="go(page - 1)"
|
|
>
|
|
{{ $t("common.prev") }}
|
|
</button>
|
|
<button
|
|
v-for="p in window_"
|
|
:key="p"
|
|
type="button"
|
|
class="border-border h-[34px] min-w-[34px] rounded-sm border px-2 text-[13px] transition-colors"
|
|
:class="
|
|
p === page
|
|
? 'border-primary bg-primary text-white'
|
|
: 'bg-surface hover:border-primary hover:text-primary'
|
|
"
|
|
@click="go(p)"
|
|
>
|
|
{{ p }}
|
|
</button>
|
|
<button
|
|
type="button"
|
|
class="border-border bg-surface h-[34px] min-w-[34px] rounded-sm border px-2 text-[13px] transition-colors disabled:cursor-not-allowed disabled:opacity-40"
|
|
:disabled="page >= pages"
|
|
@click="go(page + 1)"
|
|
>
|
|
{{ $t("common.next") }}
|
|
</button>
|
|
</nav>
|
|
</template>
|