|
1 | | -import React, {useState, useRef} from 'react'; |
| 1 | +import React, {useState, useRef, Suspense} from 'react'; |
2 | 2 | import 'react-dyn-tabs/style/react-dyn-tabs.css'; |
3 | 3 | import 'react-dyn-tabs/themes/react-dyn-tabs-card.css'; |
4 | 4 | import useDynTabs from 'react-dyn-tabs'; |
5 | 5 | import useLog from '../useLog.js'; |
6 | 6 | import './tabs.css'; |
7 | | -export default function () { |
| 7 | +export default function (props) { |
8 | 8 | const ref = useRef({}); |
9 | 9 | const [ConsoleComponent, log] = useLog(); |
10 | 10 | // assign and update ref.current.log with new value of log function |
11 | 11 | 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 |
18 | 28 | const options = { |
19 | 29 | 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}, |
34 | 33 | ], |
35 | 34 | selectedTabID: '1', |
36 | 35 | onLoad: function () { |
@@ -93,17 +92,19 @@ export default function () { |
93 | 92 | _instance.setOption('isVertical', !_isVertical).refresh(); |
94 | 93 | setIsVertical(!_isVertical); |
95 | 94 | }, |
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)'); |
100 | 99 | }); |
| 100 | + } |
101 | 101 | }, |
102 | 102 | disableSelectedTab: () => { |
103 | 103 | _instance.setTab(_instance.getData().selectedTabID, {disable: true}).refresh(); |
104 | 104 | }, |
105 | 105 | closeSelectedTab: () => { |
106 | | - _instance.close(_instance.getData().selectedTabID); |
| 106 | + const {selectedTabID} = _instance.getData(); |
| 107 | + _instance.close(selectedTabID); |
107 | 108 | }, |
108 | 109 | disableAccessibility: () => { |
109 | 110 | _instance.setOption('accessibility', false).refresh(); |
@@ -149,7 +150,7 @@ export default function () { |
149 | 150 | <button onClick={actions.openNewTab}>open new tab</button> |
150 | 151 | <button onClick={actions.toggleDirection}>toggle direction</button> |
151 | 152 | <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> |
153 | 154 | <button onClick={actions.disableSelectedTab}>disable selected tab</button> |
154 | 155 | <button onClick={actions.closeSelectedTab}>close selected tab</button> |
155 | 156 | <button onClick={actions.disableAccessibility}>disable accessibility</button> |
|
0 commit comments