Skip to content

Commit 4338578

Browse files
update complete-example
1 parent b76517c commit 4338578

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import React from 'react';
2+
export default function LazyPanel(props) {
3+
return <p>tab content {props.id} is loaded lazily.</p>;
4+
}

example/complete-example/src/components/tabs/tabs.js

+29-28
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
1-
import React, {useState, useRef} from 'react';
1+
import React, {useState, useRef, Suspense} from 'react';
22
import 'react-dyn-tabs/style/react-dyn-tabs.css';
33
import 'react-dyn-tabs/themes/react-dyn-tabs-card.css';
44
import useDynTabs from 'react-dyn-tabs';
55
import useLog from '../useLog.js';
66
import './tabs.css';
7-
export default function () {
7+
export default function (props) {
88
const ref = useRef({});
99
const [ConsoleComponent, log] = useLog();
1010
// assign and update ref.current.log with new value of log function
1111
ref.current.log = log;
12-
const allComponents = {
13-
Panel1: (props) => <p>tab content 1</p>,
14-
Panel2: (props) => <p>tab content 2</p>,
15-
Panel3: (props) => <p>tab content 3</p>,
16-
Panel4: (props) => <p>tab content 4</p>,
17-
};
12+
// define panel components
13+
function Panel1() {
14+
return <p>tab content 1</p>;
15+
}
16+
function Panel2() {
17+
return <p>tab content 2</p>;
18+
}
19+
const Panel3 = React.lazy(() => import('./lazyPanel.js'));
20+
function LazyPanel3(props) {
21+
return (
22+
<Suspense fallback={<div>Loading...</div>}>
23+
<Panel3 {...props}></Panel3>
24+
</Suspense>
25+
);
26+
}
27+
// options
1828
const options = {
1929
tabs: [
20-
{
21-
id: '1',
22-
title: 'tab 1',
23-
panelComponent: allComponents.Panel1,
24-
iconClass: 'fa fa-home',
25-
},
26-
{
27-
id: '2',
28-
title: 'tab 2',
29-
panelComponent: allComponents.Panel2,
30-
iconClass: 'fa fa-book',
31-
},
32-
{id: '3', title: 'tab 3', panelComponent: allComponents.Panel3},
33-
{id: '4', title: 'tab 4', panelComponent: allComponents.Panel4},
30+
{id: '1', title: 'tab 1', panelComponent: Panel1, iconClass: 'fa fa-home'},
31+
{id: '2', title: 'tab 2', lazy: true, panelComponent: Panel2, iconClass: 'fa fa-book'},
32+
{id: '3', title: 'lazy tab 3', lazy: true, panelComponent: LazyPanel3},
3433
],
3534
selectedTabID: '1',
3635
onLoad: function () {
@@ -93,17 +92,19 @@ export default function () {
9392
_instance.setOption('isVertical', !_isVertical).refresh();
9493
setIsVertical(!_isVertical);
9594
},
96-
selectTab4: () => {
97-
_instance.isOpen('4') &&
98-
_instance.select('4').then(() => {
99-
ref.current.log('(tab 4 is selected)');
95+
selectTab3: () => {
96+
if (_instance.isOpen('3')) {
97+
_instance.select('3').then(() => {
98+
ref.current.log('(tab 3 is selected)');
10099
});
100+
}
101101
},
102102
disableSelectedTab: () => {
103103
_instance.setTab(_instance.getData().selectedTabID, {disable: true}).refresh();
104104
},
105105
closeSelectedTab: () => {
106-
_instance.close(_instance.getData().selectedTabID);
106+
const {selectedTabID} = _instance.getData();
107+
_instance.close(selectedTabID);
107108
},
108109
disableAccessibility: () => {
109110
_instance.setOption('accessibility', false).refresh();
@@ -149,7 +150,7 @@ export default function () {
149150
<button onClick={actions.openNewTab}>open new tab</button>
150151
<button onClick={actions.toggleDirection}>toggle direction</button>
151152
<button onClick={actions.toggleVertical}>vertical | horizontal</button>
152-
<button onClick={actions.selectTab4}>select tab 4</button>
153+
<button onClick={actions.selectTab3}>select tab 3</button>
153154
<button onClick={actions.disableSelectedTab}>disable selected tab</button>
154155
<button onClick={actions.closeSelectedTab}>close selected tab</button>
155156
<button onClick={actions.disableAccessibility}>disable accessibility</button>

0 commit comments

Comments
 (0)