ny
22 小时以前 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
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
import type { ComputedRef } from 'vue';
 
import type { PaginationProps } from '../types/pagination';
import type { BasicTableProps } from '../types/table';
 
import { computed, ref, unref, watch } from 'vue';
 
import { isBoolean } from '@jnpf/utils';
 
import { $t } from '@vben/locales';
 
import { LeftOutlined, RightOutlined } from '@ant-design/icons-vue';
 
import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '../const';
 
interface ItemRender {
  originalElement: any;
  page: number;
  type: 'next' | 'page' | 'prev';
}
 
function itemRender({ originalElement, page, type }: ItemRender) {
  if (type === 'prev') {
    return page === 0 ? null : <LeftOutlined />;
  } else if (type === 'next') {
    return page === 1 ? null : <RightOutlined />;
  }
  return originalElement;
}
 
export function usePagination(refProps: ComputedRef<BasicTableProps>) {
  const configRef = ref<PaginationProps>({});
  const show = ref(true);
 
  watch(
    () => unref(refProps).pagination,
    (pagination) => {
      if (!isBoolean(pagination) && pagination) {
        configRef.value = {
          ...unref(configRef),
          ...pagination,
        };
      }
    },
  );
 
  const getPaginationInfo = computed((): any => {
    const { pagination } = unref(refProps);
 
    if (!unref(show) || (isBoolean(pagination) && !pagination)) {
      return false;
    }
    return {
      current: 1,
      defaultPageSize: PAGE_SIZE,
      itemRender,
      pageSize: PAGE_SIZE,
      pageSizeOptions: PAGE_SIZE_OPTIONS,
      showQuickJumper: true,
      showSizeChanger: true,
      showTotal: (total) => $t('component.table.total', { total }),
      size: 'small',
      ...(isBoolean(pagination) ? {} : pagination),
      ...unref(configRef),
    };
  });
 
  function setPagination(info: Partial<PaginationProps>) {
    const paginationInfo = unref(getPaginationInfo);
    configRef.value = {
      ...(isBoolean(paginationInfo) ? {} : paginationInfo),
      ...info,
    };
  }
 
  function getPagination() {
    return unref(getPaginationInfo);
  }
 
  function getShowPagination() {
    return unref(show);
  }
 
  async function setShowPagination(flag: boolean) {
    show.value = flag;
  }
 
  return { getPagination, getPaginationInfo, getShowPagination, setPagination, setShowPagination };
}