Skip to content

Commit

Permalink
feat: fix resize (react-component#620)
Browse files Browse the repository at this point in the history
* feat: fix resize

* feat: update test case

* feat: update tabsSize

* fate: update

* feat: update tabsSize

* feat: update tabsSize for reviewer
  • Loading branch information
heiyu4585 authored Dec 7, 2022
1 parent b2ed47a commit 0814c1a
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 18 deletions.
39 changes: 22 additions & 17 deletions src/TabNavList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,28 @@ function TabNavList(props: TabNavListProps, ref: React.Ref<HTMLDivElement>) {
);
});

// Update buttons records
const updateTabSizes = () =>
setTabSizes(() => {
const newSizes: TabSizeMap = new Map();
tabs.forEach(({ key }) => {
const btnNode = getBtnRef(key).current;
if (btnNode) {
newSizes.set(key, {
width: btnNode.offsetWidth,
height: btnNode.offsetHeight,
left: btnNode.offsetLeft,
top: btnNode.offsetTop,
});
}
});
return newSizes;
});

useEffect(() => {
updateTabSizes();
}, [tabs.map(tab => tab.key).join('_')]);

const onListHolderResize = useRaf(() => {
// Update wrapper records
const containerSize = getSize(containerRef);
Expand All @@ -332,23 +354,6 @@ function TabNavList(props: TabNavListProps, ref: React.Ref<HTMLDivElement>) {
tabContentFullSize[0] - newAddSize[0],
tabContentFullSize[1] - newAddSize[1],
]);

// Update buttons records
setTabSizes(() => {
const newSizes: TabSizeMap = new Map();
tabs.forEach(({ key }) => {
const btnNode = getBtnRef(key).current;
if (btnNode) {
newSizes.set(key, {
width: btnNode.offsetWidth,
height: btnNode.offsetHeight,
left: btnNode.offsetLeft,
top: btnNode.offsetTop,
});
}
});
return newSizes;
});
});

// ======================== Dropdown =======================
Expand Down
4 changes: 4 additions & 0 deletions tests/common/util.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export function getOffsetSizeFunc(info: HackInfo = {}) {
return more;
}

if (this.classList.contains('rc-tabs-ink-bar')) {
return container;
}

if (this.classList.contains('rc-tabs-nav-operations')) {
return this.querySelector('.rc-tabs-nav-add') ? more + add : more;
}
Expand Down
88 changes: 87 additions & 1 deletion tests/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,44 @@
import React from 'react';
import type { ReactWrapper } from 'enzyme';
import { render } from '@testing-library/react';
import { render, screen, fireEvent } from '@testing-library/react';
import '@testing-library/dom';
import { mount } from 'enzyme';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import KeyCode from 'rc-util/lib/KeyCode';
import Tabs from '../src';
import type { TabsProps } from '../src/Tabs';
import type { HackInfo } from './common/util';
import { getOffsetSizeFunc } from './common/util';

describe('Tabs.Basic', () => {
let domSpy: ReturnType<typeof spyElementPrototypes>;
let holder: HTMLDivElement;

const hackOffsetInfo: HackInfo = {};

beforeEach(() => {
Object.keys(hackOffsetInfo).forEach(key => {
delete hackOffsetInfo[key];
});
});

beforeAll(() => {
holder = document.createElement('div');
document.body.appendChild(holder);

domSpy = spyElementPrototypes(HTMLElement, {
scrollIntoView: () => {},
offsetWidth: {
get: getOffsetSizeFunc(hackOffsetInfo),
},
});
});

afterAll(() => {
domSpy.mockRestore();
document.body.removeChild(holder);
});

function getTabs(props: TabsProps = null) {
const mergedProps = {
items: [
Expand Down Expand Up @@ -86,6 +118,60 @@ describe('Tabs.Basic', () => {
mount(getTabs({ items: null }));
});

it('same width in windows call resize', async () => {
const App = () => {
const [list, setList] = React.useState([
{
label: `Tab 1`,
key: '1',
children: `Content of Tab Pane 1`,
},
{
label: `Tab 2`,
key: '2',
children: `Content of Tab Pane 2`,
},
{
label: `Tab 3`,
key: '3',
children: `Content of Tab Pane 3`,
},
]);
const changeItems = () => {
const listCp = [
{
label: `Tab 4`,
key: '4',
children: `Content of Tab Pane 4`,
},
{
label: `Tab 5`,
key: '5',
children: `Content of Tab Pane 5`,
},
{
label: `Tab 6`,
key: '6',
children: `Content of Tab Pane 6`,
},
];
setList(listCp);
};
return (
<div className="App">
<button id="changeItems" onClick={changeItems}>
change
</button>
<Tabs items={list} />
</div>
);
};

const { container } = render(<App />);
fireEvent.click(screen.getByRole('button', { name: 'change' }));
expect(container.querySelector<HTMLElement>('.rc-tabs-ink-bar').offsetWidth).toBe(50);
});

describe('onChange and onTabClick should work', () => {
const list: { name: string; trigger: (wrapper: ReactWrapper) => void }[] = [
{
Expand Down

0 comments on commit 0814c1a

Please sign in to comment.