Commit 0bd98b3c27e41dcd58d12ad19796052cffd1a288

Authored by Cao Duc Anh
Committed by GitHub
1 parent a065de4f

chore: split function for easy maintain, understand and using (#2945)

src/components/Table/src/hooks/useTableScroll.ts
@@ -53,22 +53,7 @@ export function useTableScroll( @@ -53,22 +53,7 @@ export function useTableScroll(
53 let footerEl: HTMLElement | null; 53 let footerEl: HTMLElement | null;
54 let bodyEl: HTMLElement | null; 54 let bodyEl: HTMLElement | null;
55 55
56 - async function calcTableHeight() {  
57 - const { resizeHeightOffset, pagination, maxHeight, isCanResizeParent, useSearchForm } =  
58 - unref(propsRef);  
59 - const tableData = unref(getDataSourceRef);  
60 -  
61 - const table = unref(tableElRef);  
62 - if (!table) return;  
63 -  
64 - const tableEl: Element = table.$el;  
65 - if (!tableEl) return;  
66 -  
67 - if (!bodyEl) {  
68 - bodyEl = tableEl.querySelector('.ant-table-body');  
69 - if (!bodyEl) return;  
70 - }  
71 - 56 + function handleScrollBar(bodyEl: HTMLElement, tableEl: Element) {
72 const hasScrollBarY = bodyEl.scrollHeight > bodyEl.clientHeight; 57 const hasScrollBarY = bodyEl.scrollHeight > bodyEl.clientHeight;
73 const hasScrollBarX = bodyEl.scrollWidth > bodyEl.clientWidth; 58 const hasScrollBarX = bodyEl.scrollWidth > bodyEl.clientWidth;
74 59
@@ -85,20 +70,10 @@ export function useTableScroll( @@ -85,20 +70,10 @@ export function useTableScroll(
85 } else { 70 } else {
86 !tableEl.classList.contains('hide-scrollbar-x') && tableEl.classList.add('hide-scrollbar-x'); 71 !tableEl.classList.contains('hide-scrollbar-x') && tableEl.classList.add('hide-scrollbar-x');
87 } 72 }
  73 + }
88 74
89 - bodyEl!.style.height = 'unset';  
90 -  
91 - if (!unref(getCanResize) || !unref(tableData) || tableData.length === 0) return;  
92 -  
93 - await nextTick();  
94 - // Add a delay to get the correct bottomIncludeBody paginationHeight footerHeight headerHeight  
95 -  
96 - const headEl = tableEl.querySelector('.ant-table-thead ');  
97 -  
98 - if (!headEl) return;  
99 -  
100 - // Table height from bottom height-custom offset  
101 - let paddingHeight = 32; 75 + function caclPaginationHeight(tableEl: Element): number {
  76 + const { pagination } = unref(propsRef);
102 // Pager height 77 // Pager height
103 let paginationHeight = 2; 78 let paginationHeight = 2;
104 if (!isBoolean(pagination)) { 79 if (!isBoolean(pagination)) {
@@ -113,8 +88,12 @@ export function useTableScroll( @@ -113,8 +88,12 @@ export function useTableScroll(
113 } else { 88 } else {
114 paginationHeight = -8; 89 paginationHeight = -8;
115 } 90 }
  91 + return paginationHeight;
  92 + }
116 93
117 - let footerHeight = 0; 94 + function caclFooterHeight(tableEl: Element): number {
  95 + const { pagination } = unref(propsRef);
  96 + let footerHeight = 0;
118 if (!isBoolean(pagination)) { 97 if (!isBoolean(pagination)) {
119 if (!footerEl) { 98 if (!footerEl) {
120 footerEl = tableEl.querySelector('.ant-table-footer') as HTMLElement; 99 footerEl = tableEl.querySelector('.ant-table-footer') as HTMLElement;
@@ -123,12 +102,21 @@ export function useTableScroll( @@ -123,12 +102,21 @@ export function useTableScroll(
123 footerHeight += offsetHeight || 0; 102 footerHeight += offsetHeight || 0;
124 } 103 }
125 } 104 }
  105 + return footerHeight;
  106 + }
126 107
  108 + function calcHeaderHeight(headEl: Element): number {
127 let headerHeight = 0; 109 let headerHeight = 0;
128 if (headEl) { 110 if (headEl) {
129 headerHeight = (headEl as HTMLElement).offsetHeight; 111 headerHeight = (headEl as HTMLElement).offsetHeight;
130 } 112 }
  113 + return headerHeight;
  114 + }
131 115
  116 + function calcBottomAndPaddingHeight(tableEl: Element, headEl: Element) {
  117 + const { pagination, isCanResizeParent, useSearchForm } = unref(propsRef);
  118 + // Table height from bottom height-custom offset
  119 + let paddingHeight = 30;
132 let bottomIncludeBody = 0; 120 let bottomIncludeBody = 0;
133 if (unref(wrapRef) && isCanResizeParent) { 121 if (unref(wrapRef) && isCanResizeParent) {
134 const tablePadding = 12; 122 const tablePadding = 12;
@@ -157,7 +145,46 @@ export function useTableScroll( @@ -157,7 +145,46 @@ export function useTableScroll(
157 // Table height from bottom 145 // Table height from bottom
158 bottomIncludeBody = getViewportOffset(headEl).bottomIncludeBody; 146 bottomIncludeBody = getViewportOffset(headEl).bottomIncludeBody;
159 } 147 }
  148 +
  149 + return {
  150 + paddingHeight,
  151 + bottomIncludeBody,
  152 + };
  153 + }
  154 +
  155 + async function calcTableHeight() {
  156 + const { resizeHeightOffset, maxHeight } = unref(propsRef);
  157 + const tableData = unref(getDataSourceRef);
160 158
  159 + const table = unref(tableElRef);
  160 + if (!table) return;
  161 +
  162 + const tableEl: Element = table.$el;
  163 + if (!tableEl) return;
  164 +
  165 + if (!bodyEl) {
  166 + bodyEl = tableEl.querySelector('.ant-table-body');
  167 + if (!bodyEl) return;
  168 + }
  169 +
  170 + handleScrollBar(bodyEl, tableEl);
  171 +
  172 + bodyEl!.style.height = 'unset';
  173 +
  174 + if (!unref(getCanResize) || !unref(tableData) || tableData.length === 0) return;
  175 +
  176 + await nextTick();
  177 + // Add a delay to get the correct bottomIncludeBody paginationHeight footerHeight headerHeight
  178 +
  179 + const headEl = tableEl.querySelector('.ant-table-thead ');
  180 +
  181 + if (!headEl) return;
  182 +
  183 + const paginationHeight = caclPaginationHeight(tableEl);
  184 + const footerHeight = caclFooterHeight(tableEl);
  185 + const headerHeight = calcHeaderHeight(headEl);
  186 + const { paddingHeight, bottomIncludeBody } = calcBottomAndPaddingHeight(tableEl, headEl);
  187 +
161 let height = 188 let height =
162 bottomIncludeBody - 189 bottomIncludeBody -
163 (resizeHeightOffset || 0) - 190 (resizeHeightOffset || 0) -
@@ -215,4 +242,4 @@ export function useTableScroll( @@ -215,4 +242,4 @@ export function useTableScroll(
215 }); 242 });
216 243
217 return { getScrollRef, redoHeight }; 244 return { getScrollRef, redoHeight };
218 -} 245 -}
  246 +}
219 \ No newline at end of file 247 \ No newline at end of file