vben
2020-12-01 962f90de445d7935ad76ea7b74a98f12ce9a7498
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
import type { PaginationProps } from '../types/pagination';
import type { BasicTableProps } from '../types/table';
 
import { computed, unref, ref, ComputedRef } from 'vue';
import { LeftOutlined, RightOutlined } from '@ant-design/icons-vue';
 
import { isBoolean } from '/@/utils/is';
 
import { PAGE_SIZE, PAGE_SIZE_OPTIONS } from '../const';
import { useProps } from './useProps';
import { useI18n } from '/@/hooks/web/useI18n';
 
const { t } = useI18n();
export function usePagination(refProps: ComputedRef<BasicTableProps>) {
  const configRef = ref<PaginationProps>({});
  const { propsRef } = useProps(refProps);
 
  const getPaginationRef = computed((): PaginationProps | false => {
    const { pagination } = unref(propsRef);
    if (isBoolean(pagination) && !pagination) {
      return false;
    }
    return {
      current: 1,
      pageSize: PAGE_SIZE,
      size: 'small',
      defaultPageSize: PAGE_SIZE,
      showTotal: (total) => t('component.table.total', { total }),
      showSizeChanger: true,
      pageSizeOptions: PAGE_SIZE_OPTIONS,
      itemRender: ({ page, type, originalElement }) => {
        if (type === 'prev') {
          if (page === 0) {
            return null;
          }
          return <LeftOutlined />;
        } else if (type === 'next') {
          if (page === 1) {
            return null;
          }
          return <RightOutlined />;
        }
        return originalElement;
      },
      showQuickJumper: true,
      ...(isBoolean(pagination) ? {} : pagination),
      ...unref(configRef),
    };
  });
 
  function setPagination(info: Partial<PaginationProps>) {
    configRef.value = {
      ...unref(getPaginationRef),
      ...info,
    };
  }
  return { getPaginationRef, setPagination };
}