Skip to content

Commit deb723e

Browse files
create lazy panels
1 parent 4329a4c commit deb723e

File tree

3 files changed

+86
-5
lines changed

3 files changed

+86
-5
lines changed

src/index.test.js

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,16 @@ beforeEach(() => {
3535
beforeSelect: jest.fn(function () {}),
3636
onDestroy: jest.fn(function () {}),
3737
};
38-
App = function App() {
39-
const [Tablist, Panellist, readyFunction] = useDynTabs(op);
38+
App = function App({options}) {
39+
const _options = Object.assign({}, op, options);
40+
if (_options.tabs) {
41+
const _tabs = [];
42+
_options.tabs.map((tab) => {
43+
_tabs.push({...tab});
44+
});
45+
_options.tabs = _tabs;
46+
}
47+
const [Tablist, Panellist, readyFunction] = useDynTabs(_options);
4048
ready = readyFunction;
4149
readyFunction((instanceParam) => {
4250
instance = instanceParam;
@@ -48,9 +56,9 @@ beforeEach(() => {
4856
</div>
4957
);
5058
};
51-
renderApp = () => {
59+
renderApp = (options = {}) => {
5260
act(() => {
53-
render(<App></App>, container);
61+
render(<App options={options}></App>, container);
5462
});
5563
};
5664
});
@@ -168,6 +176,74 @@ describe('apply multiple actions : ', () => {
168176
expect(op.beforeSelect.mock.calls.length).toBe(0);
169177
});
170178
});
179+
describe('lazy tabs : ', () => {
180+
const Panel = React.lazy(() => import('./mock/mock-lazy-panel-1.js'));
181+
const LazyPanel = () => {
182+
return (
183+
<React.Suspense fallback={<p>loading...</p>}>
184+
<Panel></Panel>
185+
</React.Suspense>
186+
);
187+
};
188+
const renderLazyApp = () =>
189+
renderApp({
190+
tabs: [
191+
{
192+
id: '1',
193+
title: 'mock tab 1',
194+
lazy: true,
195+
panelComponent: <p>tab1 content</p>,
196+
},
197+
{
198+
id: '2',
199+
title: 'mock tab 2',
200+
panelComponent: <p>tab2 content</p>,
201+
},
202+
{
203+
id: '3',
204+
title: 'mock tab 3',
205+
lazy: true,
206+
panelComponent: () => <p>tab3 content</p>,
207+
},
208+
{
209+
id: '4',
210+
title: 'mock tab 4',
211+
lazy: true,
212+
panelComponent: LazyPanel,
213+
},
214+
],
215+
});
216+
test('lazy panels should be null initially expect selected panel', () => {
217+
expect.assertions(16);
218+
renderLazyApp();
219+
expect(document.querySelector('.rc-dyn-tabs-panel[tab-id="1"] p') !== null).toBe(true);
220+
expect(document.querySelector('.rc-dyn-tabs-panel[tab-id="2"] p') !== null).toBe(true);
221+
expect(document.querySelector('.rc-dyn-tabs-panel[tab-id="3"] p') === null).toBe(true);
222+
expect(document.querySelector('.rc-dyn-tabs-panel[tab-id="4"] p') === null).toBe(true);
223+
act(() => {
224+
instance.select('4');
225+
instance.select('3');
226+
});
227+
expect(document.querySelector('.rc-dyn-tabs-panel[tab-id="1"] p') !== null).toBe(true);
228+
expect(document.querySelector('.rc-dyn-tabs-panel[tab-id="2"] p') !== null).toBe(true);
229+
expect(document.querySelector('.rc-dyn-tabs-panel[tab-id="3"] p') !== null).toBe(true);
230+
expect(document.querySelector('.rc-dyn-tabs-panel[tab-id="4"] p') === null).toBe(true);
231+
act(() => {
232+
instance.select('4');
233+
});
234+
expect(document.querySelector('.rc-dyn-tabs-panel[tab-id="1"] p') !== null).toBe(true);
235+
expect(document.querySelector('.rc-dyn-tabs-panel[tab-id="2"] p') !== null).toBe(true);
236+
expect(document.querySelector('.rc-dyn-tabs-panel[tab-id="3"] p') !== null).toBe(true);
237+
expect(document.querySelector('.rc-dyn-tabs-panel[tab-id="4"] p') !== null).toBe(true);
238+
act(() => {
239+
instance.select('3');
240+
});
241+
expect(document.querySelector('.rc-dyn-tabs-panel[tab-id="1"] p') !== null).toBe(true);
242+
expect(document.querySelector('.rc-dyn-tabs-panel[tab-id="2"] p') !== null).toBe(true);
243+
expect(document.querySelector('.rc-dyn-tabs-panel[tab-id="3"] p') !== null).toBe(true);
244+
expect(document.querySelector('.rc-dyn-tabs-panel[tab-id="4"] p') !== null).toBe(true);
245+
});
246+
});
171247
describe('calling some action inside the events options', () => {
172248
test('select method can be called inside the onLoad option', () => {
173249
op.onLoad = jest.fn(function () {

src/mock/mock-lazy-panel-1.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import React from 'react';
2+
export default (props) => {
3+
return <p> mock lazy panel 1</p>;
4+
};

src/panel/panel.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ const PanelComponent = function PanelComponent(props) {
88
api = React.useContext(ApiContext),
99
isSelected = id === selectedTabID,
1010
panelProps = panelPropsManager({isSelected, api, id}),
11+
previousSelectedTabID = api.state.selectedTabID,
1112
{panelComponent: PanelComponent, lazy} = api.getTab(id);
1213
let hasBeenSelected = false;
13-
if (!lazy || isSelected || api.activedTabsHistory.tabsId.indexOf(id) >= 0) {
14+
if (!lazy || isSelected || previousSelectedTabID === id || api.activedTabsHistory.tabsId.indexOf(id) >= 0) {
1415
hasBeenSelected = true;
1516
}
1617
return (

0 commit comments

Comments
 (0)