Last active
November 12, 2022 09:24
-
-
Save DonizeteVida/c9080a07e625e484716df6227de0bbfd to your computer and use it in GitHub Desktop.
A not so good Vue3 Tailwind Pagination component
This file contains hidden or 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
| <script setup lang="ts"> | |
| defineProps({ | |
| modelValue: { | |
| required: true, | |
| type: Number | |
| }, | |
| maxPage: { | |
| required: true, | |
| type: Number | |
| }, | |
| minPage: { | |
| required: true, | |
| type: Number | |
| }, | |
| }) | |
| function toRender(currentPage: number = 0, minPage: number = 0, maxPage: number = 0): number[] { | |
| const gap = 2 | |
| function aGoodStart(): number { | |
| const diff = currentPage - gap | |
| if (diff <= minPage) return minPage | |
| return diff | |
| } | |
| function aGoodEnd(): number { | |
| const diff = currentPage + gap | |
| if (diff >= maxPage) return maxPage | |
| return diff | |
| } | |
| const render: number[] = [] | |
| for (let i = aGoodStart(); i < currentPage; i++) render.push(i) | |
| for (let i = currentPage; i <= aGoodEnd(); i++) render.push(i) | |
| return render | |
| } | |
| </script> | |
| <template> | |
| <ul> | |
| <li @click="$emit('update:modelValue', minPage)" | |
| :class="modelValue > minPage ? 'enabled' : 'disabled'"> | |
| << | |
| </li> | |
| <li @click="if (modelValue > minPage) $emit('update:modelValue', modelValue - 1);" | |
| :class="modelValue > minPage ? 'enabled' : 'disabled'"> | |
| < | |
| </li> | |
| <li v-for="num in toRender(modelValue, minPage, maxPage)" | |
| @click="if (num != modelValue) $emit('update:modelValue', num);" | |
| class="cursor-pointer" | |
| :class="num == modelValue ? 'bg-burnt-yellow' : 'bg-blue-violet'"> | |
| {{ num }} | |
| </li> | |
| <li @click="if (modelValue < maxPage) $emit('update:modelValue', modelValue + 1);" | |
| :class="modelValue < maxPage ? 'enabled' : 'disabled'"> | |
| > | |
| </li> | |
| <li @click="if (modelValue < maxPage) $emit('update:modelValue', maxPage);" | |
| :class="modelValue < maxPage ? 'enabled' : 'disabled'"> | |
| >> | |
| </li> | |
| </ul> | |
| </template> | |
| <style scoped lang="postcss"> | |
| ul { | |
| @apply flex gap-2 flex-wrap; | |
| } | |
| li { | |
| @apply rounded p-4 text-white text-sm select-none; | |
| } | |
| .enabled { | |
| @apply bg-blue-violet cursor-pointer; | |
| } | |
| .disabled { | |
| @apply bg-gray cursor-not-allowed; | |
| } | |
| </style> |
Author
DonizeteVida
commented
Nov 12, 2022

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment