Skip to content

Commit 0be588c

Browse files
eamonxgafc163
authored andcommitted
fix: dropdown menu missing first tab in overflow edge case (#866)
1 parent d6b3270 commit 0be588c

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/hooks/useVisibleRange.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ export default function useVisibleRange(
3838
let endIndex = len;
3939
for (let i = 0; i < len; i += 1) {
4040
const offset = tabOffsets.get(tabs[i].key) || DEFAULT_SIZE;
41-
if (Math.floor(offset[position] + offset[charUnit]) > Math.floor(transformSize + visibleTabContentValue)) {
41+
if (
42+
Math.floor(offset[position] + offset[charUnit]) >
43+
Math.floor(transformSize + visibleTabContentValue)
44+
) {
4245
endIndex = i - 1;
4346
break;
4447
}
@@ -53,7 +56,7 @@ export default function useVisibleRange(
5356
}
5457
}
5558

56-
return startIndex >= endIndex ? [0, 0] : [startIndex, endIndex];
59+
return startIndex > endIndex ? [0, -1] : [startIndex, endIndex];
5760
}, [
5861
tabOffsets,
5962
visibleTabContentValue,

tests/overflow.test.tsx

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,4 +574,65 @@ describe('Tabs.Overflow', () => {
574574

575575
jest.useRealTimers();
576576
});
577+
578+
it('should handle no visible tabs when container is too small', () => {
579+
jest.useFakeTimers();
580+
581+
// 设置极小的容器空间,无法显示任何tab
582+
// 可用空间 = container - more - add - extra - tabNode = 40 - 10 - 10 - 10 - 20 = -10
583+
hackOffsetInfo.container = 40;
584+
hackOffsetInfo.more = 10;
585+
hackOffsetInfo.add = 10;
586+
hackOffsetInfo.extra = 10;
587+
hackOffsetInfo.tabNode = 20;
588+
589+
const { container, unmount } = render(
590+
getTabs({
591+
editable: { onEdit: () => {} },
592+
tabBarExtraContent: 'Extra',
593+
}),
594+
);
595+
596+
triggerResize(container);
597+
598+
act(() => {
599+
jest.runAllTimers();
600+
});
601+
602+
// 验证关键行为:当startIndex > endIndex时(返回[0,-1]),容器太小无法显示任何tab
603+
604+
// 1. "更多"按钮存在(在operations区域,用于访问隐藏的tab)
605+
const dropdownTrigger = container.querySelector('.rc-tabs-nav-operations .rc-tabs-nav-more');
606+
expect(dropdownTrigger).toBeTruthy();
607+
608+
// 2. 验证添加按钮(operations区域的添加按钮应该可见)
609+
const operationsAddButton = container.querySelector('.rc-tabs-nav-operations .rc-tabs-nav-add');
610+
expect(operationsAddButton).toBeTruthy();
611+
612+
// 3. Extra内容存在
613+
const extraTrigger = container.querySelector('.rc-tabs-extra-content');
614+
expect(extraTrigger).toBeTruthy();
615+
616+
// 4. transform会是负值,将所有tab移出可视区域
617+
const transformX = getTransformX(container);
618+
expect(transformX).toBe(-10); // 实际测量值,说明组件正确处理了无tab可见的情况
619+
620+
// 5. 获取实际的tab数量(动态计算,不硬编码)
621+
const allTabs = container.querySelectorAll('.rc-tabs-nav-list .rc-tabs-tab');
622+
const expectedTabCount = allTabs.length;
623+
624+
// 6. 触发下拉菜单打开,验证所有tab都在dropdown中
625+
fireEvent.mouseEnter(dropdownTrigger);
626+
627+
act(() => {
628+
jest.runAllTimers();
629+
});
630+
631+
// 验证下拉菜单包含所有tab
632+
const moreDropdownItems = document.querySelectorAll('.rc-tabs-dropdown-menu-item');
633+
expect(moreDropdownItems.length).toBe(expectedTabCount);
634+
635+
unmount();
636+
jest.useRealTimers();
637+
});
577638
});

0 commit comments

Comments
 (0)