useTableScroll.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import type { BasicTableProps, TableRowSelection } from '../types/table';
  2. import type { Ref, ComputedRef } from 'vue';
  3. import { computed, unref, ref, nextTick, watch } from 'vue';
  4. import { getViewportOffset } from '/@/utils/domUtils';
  5. import { isBoolean } from '/@/utils/is';
  6. import { useWindowSizeFn } from '/@/hooks/event/useWindowSizeFn';
  7. import { useModalContext } from '/@/components/Modal';
  8. import { useDebounce } from '/@/hooks/core/useDebounce';
  9. import type { BasicColumn } from '/@/components/Table';
  10. export function useTableScroll(
  11. propsRef: ComputedRef<BasicTableProps>,
  12. tableElRef: Ref<ComponentRef>,
  13. columnsRef: ComputedRef<BasicColumn[]>,
  14. rowSelectionRef: ComputedRef<TableRowSelection<any> | null>
  15. ) {
  16. const tableHeightRef: Ref<Nullable<number>> = ref(null);
  17. const modalFn = useModalContext();
  18. // const [debounceCalcTableHeight] = useDebounce(calcTableHeight, 80);
  19. const [debounceRedoHeight] = useDebounce(redoHeight, 250);
  20. const getCanResize = computed(() => {
  21. const { canResize, scroll } = unref(propsRef);
  22. return canResize && !(scroll || {}).y;
  23. });
  24. watch(
  25. () => unref(getCanResize),
  26. () => {
  27. debounceRedoHeight();
  28. },
  29. {
  30. immediate: true,
  31. }
  32. );
  33. function redoHeight() {
  34. if (unref(getCanResize)) {
  35. nextTick(() => {
  36. calcTableHeight();
  37. });
  38. }
  39. }
  40. function setHeight(heigh: number) {
  41. tableHeightRef.value = heigh;
  42. // Solve the problem of modal adaptive height calculation when the form is placed in the modal
  43. modalFn?.redoModalHeight?.();
  44. }
  45. // No need to repeat queries
  46. let paginationEl: HTMLElement | null;
  47. let footerEl: HTMLElement | null;
  48. let bodyEl: HTMLElement | null;
  49. async function calcTableHeight() {
  50. const { resizeHeightOffset, pagination, maxHeight } = unref(propsRef);
  51. if (!unref(getCanResize)) return;
  52. await nextTick();
  53. //Add a delay to get the correct bottomIncludeBody paginationHeight footerHeight headerHeight
  54. setTimeout(() => {
  55. const table = unref(tableElRef);
  56. if (!table) return;
  57. const tableEl: Element = table.$el;
  58. if (!tableEl) return;
  59. const headEl = tableEl.querySelector('.ant-table-thead ');
  60. if (!headEl) return;
  61. // Table height from bottom
  62. const { bottomIncludeBody } = getViewportOffset(headEl);
  63. // Table height from bottom height-custom offset
  64. const paddingHeight = 32;
  65. const borderHeight = 0;
  66. // Pager height
  67. let paginationHeight = 2;
  68. if (!isBoolean(pagination)) {
  69. if (!paginationEl) {
  70. paginationEl = tableEl.querySelector('.ant-pagination') as HTMLElement;
  71. }
  72. if (paginationEl) {
  73. const offsetHeight = paginationEl.offsetHeight;
  74. paginationHeight += offsetHeight || 0;
  75. } else {
  76. // TODO First fix 24
  77. paginationHeight += 24;
  78. }
  79. }
  80. let footerHeight = 0;
  81. if (!isBoolean(pagination)) {
  82. if (!footerEl) {
  83. footerEl = tableEl.querySelector('.ant-table-footer') as HTMLElement;
  84. } else {
  85. const offsetHeight = footerEl.offsetHeight;
  86. footerHeight += offsetHeight || 0;
  87. }
  88. }
  89. let headerHeight = 0;
  90. if (headEl) {
  91. headerHeight = (headEl as HTMLElement).offsetHeight;
  92. }
  93. let height =
  94. bottomIncludeBody -
  95. (resizeHeightOffset || 0) -
  96. paddingHeight -
  97. borderHeight -
  98. paginationHeight -
  99. footerHeight -
  100. headerHeight;
  101. height = (height > maxHeight! ? (maxHeight as number) : height) ?? height;
  102. setHeight(height);
  103. if (!bodyEl) {
  104. bodyEl = tableEl.querySelector('.ant-table-body');
  105. }
  106. bodyEl!.style.height = `${height}px`;
  107. }, 200);
  108. }
  109. useWindowSizeFn(calcTableHeight, 200);
  110. const getScrollX = computed(() => {
  111. let width = 0;
  112. if (unref(rowSelectionRef)) {
  113. width += 60;
  114. }
  115. // TODO props ?? 0;
  116. const NORMAL_WIDTH = 150;
  117. const columns = unref(columnsRef).filter((item) => !item.defaultHidden);
  118. columns.forEach((item) => {
  119. width += Number.parseInt(item.width as string) || 0;
  120. });
  121. const unsetWidthColumns = columns.filter((item) => !Reflect.has(item, 'width'));
  122. const len = unsetWidthColumns.length;
  123. if (len !== 0) {
  124. width += len * NORMAL_WIDTH;
  125. }
  126. const table = unref(tableElRef);
  127. const tableWidth = table?.$el?.offsetWidth ?? 0;
  128. return tableWidth > width ? '100%' : width;
  129. });
  130. const getScrollRef = computed(() => {
  131. const tableHeight = unref(tableHeightRef);
  132. const { canResize, scroll } = unref(propsRef);
  133. return {
  134. x: unref(getScrollX),
  135. y: canResize ? tableHeight : null,
  136. scrollToFirstRowOnChange: false,
  137. ...scroll,
  138. };
  139. });
  140. return { getScrollRef, redoHeight };
  141. }