usePagination.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import type { PaginationProps } from '../types/pagination';
  2. import type { BasicTableProps } from '../types/table';
  3. import { computed, unref, ref, ComputedRef, watch } from 'vue';
  4. import { LeftOutlined, RightOutlined } from '@ant-design/icons-vue';
  5. import { isBoolean } from '/@/utils/is';
  6. import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '../const';
  7. import { useI18n } from '/@/hooks/web/useI18n';
  8. import { Select } from 'ant-design-vue';
  9. interface ItemRender {
  10. page: number;
  11. type: 'page' | 'prev' | 'next';
  12. originalElement: any;
  13. }
  14. function itemRender({ page, type, originalElement }: ItemRender) {
  15. if (type === 'prev') {
  16. return page === 0 ? null : <LeftOutlined />;
  17. } else if (type === 'next') {
  18. return page === 1 ? null : <RightOutlined />;
  19. }
  20. return originalElement;
  21. }
  22. export function usePagination(refProps: ComputedRef<BasicTableProps>) {
  23. const { t } = useI18n();
  24. const configRef = ref<PaginationProps>({});
  25. const show = ref(true);
  26. watch(
  27. () => unref(refProps).pagination,
  28. (pagination) => {
  29. if (!isBoolean(pagination) && pagination) {
  30. configRef.value = {
  31. ...unref(configRef),
  32. ...(pagination ?? {}),
  33. selectComponentClass: () => (
  34. <Select
  35. defaultValue={configRef.value.pageSize}
  36. options={[
  37. { label: '5条每页', value: 5 },
  38. { label: '10条每页', value: 10 },
  39. { label: '30条每页', value: 30 },
  40. { label: '50条每页', value: 50 },
  41. { label: '100条每页', value: 100 },
  42. ]}
  43. style="width: 100px; margin-right: 10px;"
  44. onChange={updataPageSize}
  45. ></Select>
  46. ),
  47. };
  48. }
  49. }
  50. );
  51. const getPaginationInfo = computed((): PaginationProps | boolean => {
  52. const { pagination } = unref(refProps);
  53. if (!unref(show) || (isBoolean(pagination) && !pagination)) {
  54. return false;
  55. }
  56. return {
  57. current: 1,
  58. pageSize: PAGE_SIZE,
  59. size: 'small',
  60. defaultPageSize: PAGE_SIZE,
  61. showTotal: (total) => t('component.table.total', { total }),
  62. showSizeChanger: true,
  63. pageSizeOptions: PAGE_SIZE_OPTIONS,
  64. itemRender: itemRender,
  65. showQuickJumper: true,
  66. // ...(isBoolean(pagination) ? {} : pagination),
  67. ...unref(configRef),
  68. };
  69. });
  70. function setPagination(info: Partial<PaginationProps>) {
  71. const paginationInfo = unref(getPaginationInfo);
  72. configRef.value = {
  73. ...(!isBoolean(paginationInfo) ? paginationInfo : {}),
  74. ...info,
  75. selectComponentClass: () => (
  76. <Select
  77. defaultValue={configRef.value.pageSize}
  78. options={[
  79. { label: '5条每页', value: 5 },
  80. { label: '10条每页', value: 10 },
  81. { label: '30条每页', value: 30 },
  82. { label: '50条每页', value: 50 },
  83. { label: '100条每页', value: 100 },
  84. ]}
  85. style="width: 100px; margin-right: 10px;"
  86. onChange={updataPageSize}
  87. ></Select>
  88. ),
  89. };
  90. }
  91. function updataPageSize(size) {
  92. configRef.value.pageSize = size;
  93. }
  94. function getPagination() {
  95. return unref(getPaginationInfo);
  96. }
  97. function getShowPagination() {
  98. return unref(show);
  99. }
  100. async function setShowPagination(flag: boolean) {
  101. show.value = flag;
  102. }
  103. return { getPagination, getPaginationInfo, setShowPagination, getShowPagination, setPagination };
  104. }