useTableScroll.ts 4.3 KB
Newer Older
V
vben 已提交
1
import type { BasicTableProps, TableRowSelection } from '../types/table';
V
vben 已提交
2 3
import type { Ref, ComputedRef } from 'vue';
import { computed, unref, ref, nextTick, watchEffect } from 'vue';
V
vben 已提交
4

陈文彬 已提交
5 6
import { getViewportOffset } from '/@/utils/domUtils';
import { isBoolean } from '/@/utils/is';
V
vben 已提交
7

8
import { useWindowSizeFn } from '/@/hooks/event/useWindowSizeFn';
V
vben 已提交
9
import { useModalContext } from '/@/components/Modal';
V
vben 已提交
10 11
import { useDebounce } from '/@/hooks/core/useDebounce';
import type { BasicColumn } from '/@/components/Table';
陈文彬 已提交
12

V
vben 已提交
13 14
export function useTableScroll(
  propsRef: ComputedRef<BasicTableProps>,
V
vben 已提交
15 16 17
  tableElRef: Ref<ComponentRef>,
  columnsRef: ComputedRef<BasicColumn[]>,
  rowSelectionRef: ComputedRef<TableRowSelection<any> | null>
V
vben 已提交
18 19
) {
  const tableHeightRef: Ref<Nullable<number>> = ref(null);
陈文彬 已提交
20

V
vben 已提交
21
  const modalFn = useModalContext();
22

V
vben 已提交
23 24 25
  // const [debounceCalcTableHeight] = useDebounce(calcTableHeight, 80);
  const [debounceRedoHeight] = useDebounce(redoHeight, 250);

V
vben 已提交
26 27 28 29
  const getCanResize = computed(() => {
    const { canResize, scroll } = unref(propsRef);
    return canResize && !(scroll || {}).y;
  });
30

V
vben 已提交
31
  watchEffect(() => {
V
vben 已提交
32
    unref(getCanResize) && debounceRedoHeight();
V
vben 已提交
33
  });
陈文彬 已提交
34

V
vben 已提交
35 36 37 38 39 40
  function redoHeight() {
    if (unref(getCanResize)) {
      nextTick(() => {
        calcTableHeight();
      });
    }
陈文彬 已提交
41 42
  }

V
vben 已提交
43 44 45 46 47 48
  function setHeight(heigh: number) {
    tableHeightRef.value = heigh;
    //  Solve the problem of modal adaptive height calculation when the form is placed in the modal
    modalFn?.redoModalHeight?.();
  }

V
vben 已提交
49
  // No need to repeat queries
V
vben 已提交
50 51
  let paginationEl: HTMLElement | null;
  let footerEl: HTMLElement | null;
V
vben 已提交
52

V
vben 已提交
53
  async function calcTableHeight() {
V
vben 已提交
54 55
    const { resizeHeightOffset, pagination, maxHeight } = unref(propsRef);
    if (!unref(getCanResize)) return;
56

陈文彬 已提交
57
    await nextTick();
V
vben 已提交
58
    const table = unref(tableElRef);
59
    if (!table) return;
陈文彬 已提交
60 61

    const tableEl: Element = table.$el;
62
    if (!tableEl) return;
V
vben 已提交
63

V
vben 已提交
64 65
    const headEl = tableEl.querySelector('.ant-table-thead ');
    if (!headEl) return;
66

V
vben 已提交
67
    // Table height from bottom
V
vben 已提交
68
    const { bottomIncludeBody } = getViewportOffset(headEl);
V
vben 已提交
69
    // Table height from bottom height-custom offset
陈文彬 已提交
70 71 72

    const paddingHeight = 32;
    const borderHeight = 2 * 2;
V
vben 已提交
73
    // Pager height
V
vben 已提交
74 75 76 77 78 79 80 81 82
    let paginationHeight = 2;
    if (!isBoolean(pagination)) {
      if (!paginationEl) {
        paginationEl = tableEl.querySelector('.ant-pagination') as HTMLElement;
      }
      if (paginationEl) {
        const offsetHeight = paginationEl.offsetHeight;
        paginationHeight += offsetHeight || 0;
      } else {
V
vben 已提交
83
        // TODO First fix 24
V
vben 已提交
84 85 86
        paginationHeight += 24;
      }
    }
陈文彬 已提交
87 88 89

    let footerHeight = 0;
    if (!isBoolean(pagination)) {
V
vben 已提交
90 91 92
      if (!footerEl) {
        footerEl = tableEl.querySelector('.ant-table-footer') as HTMLElement;
      } else {
陈文彬 已提交
93 94 95 96
        const offsetHeight = footerEl.offsetHeight;
        footerHeight += offsetHeight || 0;
      }
    }
V
vben 已提交
97

陈文彬 已提交
98
    let headerHeight = 0;
V
vben 已提交
99 100
    if (headEl) {
      headerHeight = (headEl as HTMLElement).offsetHeight;
陈文彬 已提交
101
    }
V
vben 已提交
102

V
vben 已提交
103
    let height =
陈文彬 已提交
104 105 106 107 108 109 110
      bottomIncludeBody -
      (resizeHeightOffset || 0) -
      paddingHeight -
      borderHeight -
      paginationHeight -
      footerHeight -
      headerHeight;
V
vben 已提交
111

V
vben 已提交
112 113
    height = (height > maxHeight! ? (maxHeight as number) : height) ?? height;
    setHeight(height);
陈文彬 已提交
114 115
  }

V
vben 已提交
116 117 118 119 120 121 122 123
  useWindowSizeFn(calcTableHeight, 200);

  const getScrollX = computed(() => {
    let width = 0;
    if (unref(rowSelectionRef)) {
      width += 60;
    }

124
    // TODO propsdth ?? 0;
V
vben 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137
    const NORMAL_WIDTH = 150;

    const columns = unref(columnsRef);

    columns.forEach((item) => {
      width += Number.parseInt(item.width as string) || 0;
    });
    const unsetWidthColumns = columns.filter((item) => !Reflect.has(item, 'width'));

    const len = unsetWidthColumns.length;
    if (len !== 0) {
      width += len * NORMAL_WIDTH;
    }
138 139 140 141

    const table = unref(tableElRef);
    const tableWidth = table?.$el?.offsetWidth ?? 0;
    return tableWidth > width ? tableWidth - 24 : width;
V
vben 已提交
142
  });
陈文彬 已提交
143 144 145 146 147 148

  const getScrollRef = computed(() => {
    const tableHeight = unref(tableHeightRef);
    const { canResize, scroll } = unref(propsRef);

    return {
V
vben 已提交
149
      x: unref(getScrollX),
陈文彬 已提交
150 151 152 153 154
      y: canResize ? tableHeight : null,
      scrollToFirstRowOnChange: false,
      ...scroll,
    };
  });
V
vben 已提交
155

陈文彬 已提交
156 157
  return { getScrollRef, redoHeight };
}