Skip to content

Commit 72ca369

Browse files
authored
Fix: apply destroyInactiveTabPane from the TabPane (#675)
* apply destroyInactiveTabPane from the TabPane * added TabPane `destroyInactiveTabPane` to readme * fixing syntax error * added unit test
1 parent 415ff34 commit 72ca369

File tree

3 files changed

+60
-14
lines changed

3 files changed

+60
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ React.render(
9090

9191
| name | type | default | description |
9292
| --- | --- | --- | --- |
93+
| destroyInactiveTabPane | boolean | false | whether destroy inactive TabPane when change tab |
9394
| key | string | - | corresponding to activeKey, should be unique |
9495
| forceRender | boolean | false | forced render of content in tabs, not lazy render after clicking on tabs |
9596
| tab | ReactNode | - | current tab's title corresponding to current tabPane |

src/TabPanelList/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ export default function TabPanelList({
3333
})}
3434
>
3535
{tabs.map(
36-
({ key, forceRender, style: paneStyle, className: paneClassName, ...restTabProps }) => {
36+
({ key, forceRender, style: paneStyle, className: paneClassName, destroyInactiveTabPane: itemDestroyInactiveTabPane, ...restTabProps }) => {
3737
const active = key === activeKey;
3838

3939
return (
4040
<CSSMotion
4141
key={key}
4242
visible={active}
4343
forceRender={forceRender}
44-
removeOnLeave={!!destroyInactiveTabPane}
44+
removeOnLeave={!!(destroyInactiveTabPane || itemDestroyInactiveTabPane)}
4545
leavedClassName={`${tabPanePrefixCls}-hidden`}
4646
{...animated.tabPaneMotion}
4747
>

tests/index.test.tsx

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,42 @@ describe('Tabs.Basic', () => {
372372
matchText('Bamboo');
373373
});
374374

375+
it('destroyInactiveTabPane from TabPane', () => {
376+
const props = {
377+
activeKey: 'light',
378+
379+
items: [
380+
{
381+
key: 'light',
382+
children: 'Light',
383+
destroyInactiveTabPane: true,
384+
},
385+
{
386+
key: 'bamboo',
387+
children: 'Bamboo',
388+
destroyInactiveTabPane: true,
389+
},
390+
] as any,
391+
};
392+
393+
const { container, rerender } = render(getTabs(props));
394+
395+
function matchText(text: string) {
396+
expect(container.querySelectorAll('.rc-tabs-tabpane')).toHaveLength(1);
397+
expect(container.querySelector('.rc-tabs-tabpane-active').textContent).toEqual(text);
398+
}
399+
400+
matchText('Light');
401+
402+
rerender(
403+
getTabs({
404+
...props,
405+
activeKey: 'bamboo',
406+
}),
407+
);
408+
matchText('Bamboo');
409+
});
410+
375411
describe('editable', () => {
376412
it('no and', () => {
377413
const onEdit = jest.fn();
@@ -493,12 +529,21 @@ describe('Tabs.Basic', () => {
493529

494530
const removes = container.querySelectorAll('.rc-tabs-tab-remove');
495531
expect(removes.length).toBe(2);
496-
expect(container.querySelector('[data-node-key="light1"]').querySelector('.rc-tabs-tab-remove')).toBeFalsy();
497-
expect(container.querySelector('[data-node-key="light2"]').querySelector('.rc-tabs-tab-remove')).toBeFalsy();
498-
expect(container.querySelector('[data-node-key="light3"]').querySelector('.rc-tabs-tab-remove')).toBeTruthy();
499-
expect(container.querySelector('[data-node-key="light4"]').querySelector('.rc-tabs-tab-remove')).toBeTruthy();
500-
expect(container.querySelector('[data-node-key="light5"]').querySelector('.rc-tabs-tab-remove')).toBeFalsy();
501-
532+
expect(
533+
container.querySelector('[data-node-key="light1"]').querySelector('.rc-tabs-tab-remove'),
534+
).toBeFalsy();
535+
expect(
536+
container.querySelector('[data-node-key="light2"]').querySelector('.rc-tabs-tab-remove'),
537+
).toBeFalsy();
538+
expect(
539+
container.querySelector('[data-node-key="light3"]').querySelector('.rc-tabs-tab-remove'),
540+
).toBeTruthy();
541+
expect(
542+
container.querySelector('[data-node-key="light4"]').querySelector('.rc-tabs-tab-remove'),
543+
).toBeTruthy();
544+
expect(
545+
container.querySelector('[data-node-key="light5"]').querySelector('.rc-tabs-tab-remove'),
546+
).toBeFalsy();
502547
});
503548
});
504549

@@ -577,20 +622,20 @@ describe('Tabs.Basic', () => {
577622
});
578623

579624
it('key contains double quote should not crash', () => {
580-
render(<Tabs items={[{key: '"key"', label: 'test'}]} />)
625+
render(<Tabs items={[{ key: '"key"', label: 'test' }]} />);
581626
});
582627

583628
it('key could be number', () => {
584-
render(<Tabs items={[{key: 1 as any, label: 'test'}]} />)
585-
})
629+
render(<Tabs items={[{ key: 1 as any, label: 'test' }]} />);
630+
});
586631

587-
it('support indicatorSize', async () => {
632+
it('support indicatorSize', async () => {
588633
const { container, rerender } = render(getTabs({ indicatorSize: 10 }));
589634
await waitFakeTimer();
590635
expect(container.querySelector('.rc-tabs-ink-bar')).toHaveStyle({ width: '10px' });
591636

592-
rerender(getTabs({ indicatorSize: (origin) => origin - 2 }));
637+
rerender(getTabs({ indicatorSize: origin => origin - 2 }));
593638
await waitFakeTimer();
594639
expect(container.querySelector('.rc-tabs-ink-bar')).toHaveStyle({ width: '18px' });
595-
})
640+
});
596641
});

0 commit comments

Comments
 (0)