Skip to content

Commit 0bd98b3

Browse files
authored
chore: split function for easy maintain, understand and using (#2945)
1 parent a065de4 commit 0bd98b3

File tree

1 file changed

+58
-31
lines changed

1 file changed

+58
-31
lines changed

src/components/Table/src/hooks/useTableScroll.ts

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,7 @@ export function useTableScroll(
5353
let footerEl: HTMLElement | null;
5454
let bodyEl: HTMLElement | null;
5555

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) {
7257
const hasScrollBarY = bodyEl.scrollHeight > bodyEl.clientHeight;
7358
const hasScrollBarX = bodyEl.scrollWidth > bodyEl.clientWidth;
7459

@@ -85,20 +70,10 @@ export function useTableScroll(
8570
} else {
8671
!tableEl.classList.contains('hide-scrollbar-x') && tableEl.classList.add('hide-scrollbar-x');
8772
}
73+
}
8874

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);
10277
// Pager height
10378
let paginationHeight = 2;
10479
if (!isBoolean(pagination)) {
@@ -113,8 +88,12 @@ export function useTableScroll(
11388
} else {
11489
paginationHeight = -8;
11590
}
91+
return paginationHeight;
92+
}
11693

117-
let footerHeight = 0;
94+
function caclFooterHeight(tableEl: Element): number {
95+
const { pagination } = unref(propsRef);
96+
let footerHeight = 0;
11897
if (!isBoolean(pagination)) {
11998
if (!footerEl) {
12099
footerEl = tableEl.querySelector('.ant-table-footer') as HTMLElement;
@@ -123,12 +102,21 @@ export function useTableScroll(
123102
footerHeight += offsetHeight || 0;
124103
}
125104
}
105+
return footerHeight;
106+
}
126107

108+
function calcHeaderHeight(headEl: Element): number {
127109
let headerHeight = 0;
128110
if (headEl) {
129111
headerHeight = (headEl as HTMLElement).offsetHeight;
130112
}
113+
return headerHeight;
114+
}
131115

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;
132120
let bottomIncludeBody = 0;
133121
if (unref(wrapRef) && isCanResizeParent) {
134122
const tablePadding = 12;
@@ -157,7 +145,46 @@ export function useTableScroll(
157145
// Table height from bottom
158146
bottomIncludeBody = getViewportOffset(headEl).bottomIncludeBody;
159147
}
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);
160158

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+
161188
let height =
162189
bottomIncludeBody -
163190
(resizeHeightOffset || 0) -
@@ -215,4 +242,4 @@ export function useTableScroll(
215242
});
216243

217244
return { getScrollRef, redoHeight };
218-
}
245+
}

0 commit comments

Comments
 (0)