cl-pagination.uvue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <view class="cl-pagination">
  3. <view
  4. class="cl-pagination__prev"
  5. :class="[
  6. {
  7. 'is-disabled': value == 1,
  8. 'is-dark': isDark
  9. },
  10. pt.item?.className,
  11. pt.prev?.className
  12. ]"
  13. @tap="prev"
  14. >
  15. <slot name="prev" :disabled="value == 1">
  16. <cl-icon
  17. :name="pt.prevIcon?.name ?? 'arrow-left-s-line'"
  18. :size="pt.prevIcon?.size ?? 16"
  19. :color="pt.prevIcon?.color"
  20. :pt="{
  21. className: pt.prevIcon?.className
  22. }"
  23. ></cl-icon>
  24. </slot>
  25. </view>
  26. <view
  27. v-for="(item, index) in list"
  28. :key="index"
  29. class="cl-pagination__item"
  30. :class="[
  31. {
  32. 'is-active': item == value,
  33. 'is-dark': isDark
  34. },
  35. pt.item?.className
  36. ]"
  37. @tap="toPage(item)"
  38. >
  39. <cl-text
  40. :pt="{
  41. className: parseClass([
  42. 'cl-pagination__item-text',
  43. {
  44. 'text-white': item == value
  45. },
  46. pt.itemText?.className
  47. ])
  48. }"
  49. >{{ item }}</cl-text
  50. >
  51. </view>
  52. <view
  53. class="cl-pagination__next"
  54. :class="[
  55. {
  56. 'is-disabled': value == totalPage,
  57. 'is-dark': isDark
  58. },
  59. pt.item?.className,
  60. pt.next?.className
  61. ]"
  62. @tap="next"
  63. >
  64. <slot name="next" :disabled="value == totalPage">
  65. <cl-icon
  66. :name="pt.nextIcon?.name ?? 'arrow-right-s-line'"
  67. :size="pt.nextIcon?.size ?? 16"
  68. :color="pt.nextIcon?.color"
  69. :pt="{
  70. className: pt.nextIcon?.className
  71. }"
  72. ></cl-icon>
  73. </slot>
  74. </view>
  75. </view>
  76. </template>
  77. <script setup lang="ts">
  78. import type { PassThroughProps } from "../../types";
  79. import { isDark, parseClass, parsePt } from "@/.cool";
  80. import { computed, ref, watch } from "vue";
  81. import type { ClIconProps } from "../cl-icon/props";
  82. defineOptions({
  83. name: "cl-pagination"
  84. });
  85. const props = defineProps({
  86. pt: {
  87. type: Object,
  88. default: () => ({})
  89. },
  90. modelValue: {
  91. type: Number,
  92. default: 1
  93. },
  94. total: {
  95. type: Number,
  96. default: 0
  97. },
  98. size: {
  99. type: Number,
  100. default: 10
  101. }
  102. });
  103. const emit = defineEmits(["update:modelValue", "change"]);
  104. // 透传样式类型定义
  105. type PassThrough = {
  106. className?: string;
  107. item?: PassThroughProps;
  108. itemText?: PassThroughProps;
  109. prev?: PassThroughProps;
  110. prevIcon?: ClIconProps;
  111. next?: PassThroughProps;
  112. nextIcon?: ClIconProps;
  113. };
  114. // 解析透传样式配置
  115. const pt = computed(() => parsePt<PassThrough>(props.pt));
  116. // 计算总页数,根据总数和每页大小向上取整
  117. const totalPage = computed(() => {
  118. if (props.total == 0) return 1;
  119. return Math.ceil(props.total / props.size);
  120. });
  121. // 绑定值
  122. const value = ref(props.modelValue);
  123. // 计算分页列表,根据当前页码和总页数生成显示的分页按钮
  124. const list = computed(() => {
  125. const total = totalPage.value;
  126. const list: (number | string)[] = [];
  127. if (total <= 7) {
  128. // 总页数小于等于7,显示所有页码
  129. for (let i = 1; i <= total; i++) {
  130. list.push(i);
  131. }
  132. } else {
  133. // 总是显示第一页
  134. list.push(1);
  135. if (value.value <= 4) {
  136. // 当前页在前面: 1 2 3 4 5 ... 100
  137. for (let i = 2; i <= 5; i++) {
  138. list.push(i);
  139. }
  140. list.push("...");
  141. list.push(total);
  142. } else if (value.value >= total - 3) {
  143. // 当前页在后面: 1 ... 96 97 98 99 100
  144. list.push("...");
  145. for (let i = total - 4; i <= total; i++) {
  146. list.push(i);
  147. }
  148. } else {
  149. // 当前页在中间: 1 ... 4 5 6 ... 100
  150. list.push("...");
  151. for (let i = value.value - 1; i <= value.value + 1; i++) {
  152. list.push(i);
  153. }
  154. list.push("...");
  155. list.push(total);
  156. }
  157. }
  158. return list;
  159. });
  160. // 跳转到指定页面,处理页码点击事件
  161. function toPage(item: number | string) {
  162. // 忽略省略号点击
  163. if (item == "..." || typeof item !== "number") return;
  164. // 边界检查,确保页码在有效范围内
  165. if (typeof item == "number") {
  166. if (item > totalPage.value) return;
  167. if ((item as number) < 1) return;
  168. }
  169. value.value = item;
  170. // 触发双向绑定更新和变化事件
  171. emit("update:modelValue", item);
  172. emit("change", item);
  173. }
  174. // 跳转到上一页
  175. function prev() {
  176. toPage(value.value - 1);
  177. }
  178. // 跳转到下一页
  179. function next() {
  180. toPage(value.value + 1);
  181. }
  182. watch(
  183. computed(() => props.modelValue),
  184. (val: number) => {
  185. value.value = val;
  186. },
  187. {
  188. immediate: true
  189. }
  190. );
  191. defineExpose({
  192. prev,
  193. next
  194. });
  195. </script>
  196. <style lang="scss" scoped>
  197. .cl-pagination {
  198. @apply flex flex-row justify-center w-full;
  199. &__prev,
  200. &__next,
  201. &__item {
  202. @apply flex flex-row items-center justify-center bg-surface-100 rounded-lg;
  203. height: 30px;
  204. width: 30px;
  205. &.is-disabled {
  206. @apply opacity-50;
  207. }
  208. &.is-dark {
  209. @apply bg-surface-700;
  210. }
  211. }
  212. &__item {
  213. margin: 0 3px;
  214. &.is-active {
  215. @apply bg-primary-500;
  216. }
  217. }
  218. &__prev {
  219. margin-right: 3px;
  220. }
  221. &__next {
  222. margin-left: 3px;
  223. }
  224. }
  225. </style>