From cadd00a428dac5eb4d618ae09412409d018bf2c0 Mon Sep 17 00:00:00 2001 From: Rick Zhou Date: Mon, 16 Nov 2020 00:51:36 -0800 Subject: [PATCH] fix: scroll position should be reset when all tabs are removed (#332) * reset scroll when there is no active tab * add test: scroll to 0 when activeKey is null --- src/TabNavList/index.tsx | 10 +++++++--- tests/overflow.test.tsx | 9 +++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/TabNavList/index.tsx b/src/TabNavList/index.tsx index c7ccbcbb..b384420b 100644 --- a/src/TabNavList/index.tsx +++ b/src/TabNavList/index.tsx @@ -207,9 +207,13 @@ function TabNavList(props: TabNavListProps, ref: React.Ref) { // ========================= Scroll ======================== function scrollToTab(key = activeKey) { - const tabOffset = tabOffsets.get(key); - - if (!tabOffset) return; + const tabOffset = tabOffsets.get(key) || { + width: 0, + height: 0, + left: 0, + right: 0, + top: 0, + }; if (tabPositionTopOrBottom) { // ============ Align with top & bottom ============ diff --git a/tests/overflow.test.tsx b/tests/overflow.test.tsx index 9029b0fe..eeb5a649 100644 --- a/tests/overflow.test.tsx +++ b/tests/overflow.test.tsx @@ -276,6 +276,15 @@ describe('Tabs.Overflow', () => { expect(getTransformX(wrapper)).toEqual(-20); expect(onTabScroll).toHaveBeenCalledWith({ direction: 'left' }); + // scroll to 0 when activeKey is null + onTabScroll.mockReset(); + wrapper.setProps({ activeKey: null }); + act(() => { + jest.runAllTimers(); + wrapper.update(); + }); + expect(getTransformX(wrapper)).toEqual(0); + jest.useRealTimers(); });