Skip to content

Commit 4138d3c

Browse files
committed
fix: tabs size error, close #7491 #7482
1 parent dec67a6 commit 4138d3c

File tree

1 file changed

+51
-16
lines changed

1 file changed

+51
-16
lines changed

components/tabs/src/TabNavList/index.tsx

+51-16
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,34 @@ interface ExtraContentProps {
6464
extra?: (info?: { position: 'left' | 'right' }) => TabBarExtraContent;
6565
}
6666

67+
const getTabSize = (tab: HTMLElement, containerRect: { x: number; y: number }) => {
68+
// tabListRef
69+
const { offsetWidth, offsetHeight, offsetTop, offsetLeft } = tab;
70+
const { width, height, x, y } = tab.getBoundingClientRect();
71+
72+
// Use getBoundingClientRect to avoid decimal inaccuracy
73+
if (Math.abs(width - offsetWidth) < 1) {
74+
return [width, height, x - containerRect.x, y - containerRect.y];
75+
}
76+
77+
return [offsetWidth, offsetHeight, offsetLeft, offsetTop];
78+
};
79+
80+
// const getSize = (refObj: ShallowRef<HTMLElement>) => {
81+
// const { offsetWidth = 0, offsetHeight = 0 } = refObj.value || {};
82+
83+
// // Use getBoundingClientRect to avoid decimal inaccuracy
84+
// if (refObj.value) {
85+
// const { width, height } = refObj.value.getBoundingClientRect();
86+
87+
// if (Math.abs(width - offsetWidth) < 1) {
88+
// return [width, height];
89+
// }
90+
// }
91+
92+
// return [offsetWidth, offsetHeight];
93+
// };
94+
6795
export default defineComponent({
6896
compatConfig: { MODE: 3 },
6997
name: 'TabNavList',
@@ -288,7 +316,29 @@ export default defineComponent({
288316

289317
return ([visibleStart.value, visibleEnd.value] = [startIndex, endIndex]);
290318
});
319+
const updateTabSizes = () => {
320+
setTabSizes(() => {
321+
const newSizes: TabSizeMap = new Map();
322+
const listRect = tabListRef.value?.getBoundingClientRect();
323+
tabs.value.forEach(({ key }) => {
324+
const btnRef = btnRefs.value.get(key);
325+
const btnNode = (btnRef as any)?.$el || btnRef;
326+
if (btnNode) {
327+
const [width, height, left, top] = getTabSize(btnNode, listRect);
328+
newSizes.set(key, { width, height, left, top });
329+
}
330+
});
331+
return newSizes;
332+
});
333+
};
291334

335+
watch(
336+
() => tabs.value.map(tab => tab.key).join('%%'),
337+
() => {
338+
updateTabSizes();
339+
},
340+
{ flush: 'post' },
341+
);
292342
const onListHolderResize = () => {
293343
// Update wrapper records
294344
const offsetWidth = tabsWrapperRef.value?.offsetWidth || 0;
@@ -308,22 +358,7 @@ export default defineComponent({
308358
setWrapperScrollHeight(newWrapperScrollHeight);
309359

310360
// Update buttons records
311-
setTabSizes(() => {
312-
const newSizes: TabSizeMap = new Map();
313-
tabs.value.forEach(({ key }) => {
314-
const btnRef = btnRefs.value.get(key);
315-
const btnNode = (btnRef as any)?.$el || btnRef;
316-
if (btnNode) {
317-
newSizes.set(key, {
318-
width: btnNode.offsetWidth,
319-
height: btnNode.offsetHeight,
320-
left: btnNode.offsetLeft,
321-
top: btnNode.offsetTop,
322-
});
323-
}
324-
});
325-
return newSizes;
326-
});
361+
updateTabSizes();
327362
};
328363

329364
// ======================== Dropdown =======================

0 commit comments

Comments
 (0)