All files / components/organisms/PaginationBar PaginationBar.vue

78.84% Statements 41/52
83.33% Branches 5/6
0% Functions 0/5
78.84% Lines 41/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 107 108 109 110 111 112 113 114 1151x 1x 1x   1x         1x 1x 1x 1x 1x   1x         1x         1x           1x   1x 1x 1x 1x       1x 1x 1x 1x 1x   1x 1x 1x 1x   1x 3x 2x 2x 2x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x                                                                                                
<script setup lang="ts">
import { type Ref, ref, onMounted, watch } from "vue";
import { usePaginationBar } from "./usePaginationBar.ts";
 
const props = defineProps<{
  maxPage: number;
  paddingSize?: number;
}>();
 
const paddingSize = props.paddingSize ?? 1;
const pageNum: Ref<number> = ref(1);
const pageItem: Ref<string[]> = ref([]);
const emits = defineEmits(["change"]);
const { setPaginationItems } = usePaginationBar();
 
const showByPageNum = (index: number) => {
  pageNum.value = index;
  emits("change", pageNum.value);
};
 
const showPreviousPage = () => {
  pageNum.value = pageNum.value > 1 ? pageNum.value - 1 : 1;
  emits("change", pageNum.value);
};
 
const showNextPage = () => {
  pageNum.value =
    pageNum.value < props.maxPage ? pageNum.value + 1 : props.maxPage;
  emits("change", pageNum.value);
};
 
watch(pageNum, () =>
  setPaginationItems(pageItem.value, pageNum.value, props.maxPage, paddingSize),
);
onMounted(() =>
  setPaginationItems(pageItem.value, pageNum.value, props.maxPage, paddingSize),
);
</script>
 
<template>
  <div :class="$style.pagination_bar_container">
    <nav :class="$style.bar">
      <ul :class="$style.pagination_buttons">
        <li :class="$style.pagination_button">
          <a @click="showPreviousPage" :class="$style.pagination_button_content">
            <
          </a>
        </li>
        <li v-for="num in pageItem" :key="num" :class="$style.pagination_button">
          <a v-if="num==='...'" :class="$style.pagination_button_content">
            ...
          </a>
          <a v-else-if="num === String(pageNum)" @click="showByPageNum(Number(num))" :class="$style.selected_page_button">
            {{ num }}
          </a>
          <a v-else @click="showByPageNum(Number(num))" :class="$style.pagination_button_content">
            {{ num }}
          </a>
        </li>
        <li :class="$style.pagination_button">
          <a @click="showNextPage" :class="$style.pagination_button_content">
            >
          </a>
        </li>
      </ul>
    </nav>
  </div>
</template>
 
<style module>
.pagination_bar_container {
  padding: 0 32px;
}
 
.bar {
  display: flex;
  flex: 1 1 0%;
  justify-content: center;
}
 
.pagination_buttons {
  display: inline-flex;
  cursor: pointer;
}
 
.pagination_button {
  width: 32px;
  height: 32px;
  font-size: 16px;
  list-style: none;
  border: 1px solid #333;
  display: table;
  vertical-align: middle;
}
 
.pagination_button_content {
  padding: 8px;
  line-height: 1.25;
  display: table-cell;
  text-align: center;
}
 
.selected_page_button {
  padding: 8px;
  line-height: 1.25;
  background-color: #4a78ff;
  display: table-cell;
  text-align: center;
}
 
.selected_page_button:hover {
  background-color: #1550ff;
}
</style>