Skip to content

Commit 0814c1a

Browse files
authored
feat: fix resize (#620)
* feat: fix resize * feat: update test case * feat: update tabsSize * fate: update * feat: update tabsSize * feat: update tabsSize for reviewer
1 parent b2ed47a commit 0814c1a

File tree

3 files changed

+113
-18
lines changed

3 files changed

+113
-18
lines changed

src/TabNavList/index.tsx

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,28 @@ function TabNavList(props: TabNavListProps, ref: React.Ref<HTMLDivElement>) {
310310
);
311311
});
312312

313+
// Update buttons records
314+
const updateTabSizes = () =>
315+
setTabSizes(() => {
316+
const newSizes: TabSizeMap = new Map();
317+
tabs.forEach(({ key }) => {
318+
const btnNode = getBtnRef(key).current;
319+
if (btnNode) {
320+
newSizes.set(key, {
321+
width: btnNode.offsetWidth,
322+
height: btnNode.offsetHeight,
323+
left: btnNode.offsetLeft,
324+
top: btnNode.offsetTop,
325+
});
326+
}
327+
});
328+
return newSizes;
329+
});
330+
331+
useEffect(() => {
332+
updateTabSizes();
333+
}, [tabs.map(tab => tab.key).join('_')]);
334+
313335
const onListHolderResize = useRaf(() => {
314336
// Update wrapper records
315337
const containerSize = getSize(containerRef);
@@ -332,23 +354,6 @@ function TabNavList(props: TabNavListProps, ref: React.Ref<HTMLDivElement>) {
332354
tabContentFullSize[0] - newAddSize[0],
333355
tabContentFullSize[1] - newAddSize[1],
334356
]);
335-
336-
// Update buttons records
337-
setTabSizes(() => {
338-
const newSizes: TabSizeMap = new Map();
339-
tabs.forEach(({ key }) => {
340-
const btnNode = getBtnRef(key).current;
341-
if (btnNode) {
342-
newSizes.set(key, {
343-
width: btnNode.offsetWidth,
344-
height: btnNode.offsetHeight,
345-
left: btnNode.offsetLeft,
346-
top: btnNode.offsetTop,
347-
});
348-
}
349-
});
350-
return newSizes;
351-
});
352357
});
353358

354359
// ======================== Dropdown =======================

tests/common/util.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ export function getOffsetSizeFunc(info: HackInfo = {}) {
4747
return more;
4848
}
4949

50+
if (this.classList.contains('rc-tabs-ink-bar')) {
51+
return container;
52+
}
53+
5054
if (this.classList.contains('rc-tabs-nav-operations')) {
5155
return this.querySelector('.rc-tabs-nav-add') ? more + add : more;
5256
}

tests/index.test.tsx

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,44 @@
11
import React from 'react';
22
import type { ReactWrapper } from 'enzyme';
3-
import { render } from '@testing-library/react';
3+
import { render, screen, fireEvent } from '@testing-library/react';
4+
import '@testing-library/dom';
45
import { mount } from 'enzyme';
6+
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
57
import KeyCode from 'rc-util/lib/KeyCode';
68
import Tabs from '../src';
79
import type { TabsProps } from '../src/Tabs';
10+
import type { HackInfo } from './common/util';
11+
import { getOffsetSizeFunc } from './common/util';
812

913
describe('Tabs.Basic', () => {
14+
let domSpy: ReturnType<typeof spyElementPrototypes>;
15+
let holder: HTMLDivElement;
16+
17+
const hackOffsetInfo: HackInfo = {};
18+
19+
beforeEach(() => {
20+
Object.keys(hackOffsetInfo).forEach(key => {
21+
delete hackOffsetInfo[key];
22+
});
23+
});
24+
25+
beforeAll(() => {
26+
holder = document.createElement('div');
27+
document.body.appendChild(holder);
28+
29+
domSpy = spyElementPrototypes(HTMLElement, {
30+
scrollIntoView: () => {},
31+
offsetWidth: {
32+
get: getOffsetSizeFunc(hackOffsetInfo),
33+
},
34+
});
35+
});
36+
37+
afterAll(() => {
38+
domSpy.mockRestore();
39+
document.body.removeChild(holder);
40+
});
41+
1042
function getTabs(props: TabsProps = null) {
1143
const mergedProps = {
1244
items: [
@@ -86,6 +118,60 @@ describe('Tabs.Basic', () => {
86118
mount(getTabs({ items: null }));
87119
});
88120

121+
it('same width in windows call resize', async () => {
122+
const App = () => {
123+
const [list, setList] = React.useState([
124+
{
125+
label: `Tab 1`,
126+
key: '1',
127+
children: `Content of Tab Pane 1`,
128+
},
129+
{
130+
label: `Tab 2`,
131+
key: '2',
132+
children: `Content of Tab Pane 2`,
133+
},
134+
{
135+
label: `Tab 3`,
136+
key: '3',
137+
children: `Content of Tab Pane 3`,
138+
},
139+
]);
140+
const changeItems = () => {
141+
const listCp = [
142+
{
143+
label: `Tab 4`,
144+
key: '4',
145+
children: `Content of Tab Pane 4`,
146+
},
147+
{
148+
label: `Tab 5`,
149+
key: '5',
150+
children: `Content of Tab Pane 5`,
151+
},
152+
{
153+
label: `Tab 6`,
154+
key: '6',
155+
children: `Content of Tab Pane 6`,
156+
},
157+
];
158+
setList(listCp);
159+
};
160+
return (
161+
<div className="App">
162+
<button id="changeItems" onClick={changeItems}>
163+
change
164+
</button>
165+
<Tabs items={list} />
166+
</div>
167+
);
168+
};
169+
170+
const { container } = render(<App />);
171+
fireEvent.click(screen.getByRole('button', { name: 'change' }));
172+
expect(container.querySelector<HTMLElement>('.rc-tabs-ink-bar').offsetWidth).toBe(50);
173+
});
174+
89175
describe('onChange and onTabClick should work', () => {
90176
const list: { name: string; trigger: (wrapper: ReactWrapper) => void }[] = [
91177
{

0 commit comments

Comments
 (0)