@@ -23,7 +23,7 @@ React Dynamic Tabs with full API
23
23
24
24
- [ Installation] ( #installation )
25
25
- [ Basic Example] ( #basic-example )
26
- - [ ready function ] ( #ready-function )
26
+ - [ Simple manipulation ] ( #simple-manipulation )
27
27
- [ Options] ( #options )
28
28
- [ tabs] ( #tabs )
29
29
- [ selectedTabID] ( #selectedTabID )
@@ -82,63 +82,88 @@ $ yarn add react-dyn-tabs
82
82
## Basic Example
83
83
84
84
``` js
85
- import React , {useEffect } from ' react' ;
86
- import useDynTabs from ' react-dyn-tabs' ;
85
+ import React from ' react' ;
87
86
import ' react-dyn-tabs/style/react-dyn-tabs.css' ;
88
87
import ' react-dyn-tabs/themes/react-dyn-tabs-card.css' ;
89
- import ContactComponent from ' ./contact-component ' ;
88
+ import useDynTabs from ' react-dyn-tabs ' ;
90
89
91
90
export default () => {
92
91
const options = {
93
92
tabs: [
94
93
{
95
94
id: ' 1' ,
96
- title: ' home' ,
97
- closable: false ,
98
- panelComponent : (porps ) => < p> home content < / p> ,
95
+ title: ' tab 1' ,
96
+ panelComponent : (porps ) => < p> panel 1 < / p> ,
99
97
},
100
98
{
101
99
id: ' 2' ,
102
- title: ' contact ' ,
103
- panelComponent: ContactComponent, // or can be <ContactComponent />
100
+ title: ' tab 2 ' ,
101
+ panelComponent : ( porps ) => < p > panel 2 < / p > ,
104
102
},
105
103
],
106
104
selectedTabID: ' 1' ,
107
- onLoad : function () {},
108
105
};
109
- const [TabList , PanelList , ready ] = useDynTabs (options);
110
- useEffect (() => {
111
- ready ((instance ) => {
112
- // open more tabs
113
- instance .open ({id: ' 3' , title: ' Tab 3' , panelComponent : (porps ) => < p> Tab 3 content < / p> });
114
- instance .open ({id: ' 4' , title: ' Tab 4' , panelComponent : (porps ) => < p> Tab 4 content < / p> });
115
- // switch to new tab
116
- instance .select (' 4' );
117
- });
118
- }, []);
106
+ const [TabList , PanelList ] = useDynTabs (options);
119
107
return (
120
- < div >
108
+ <>
121
109
< TabList>< / TabList>
122
110
< PanelList>< / PanelList>
123
- < / div >
111
+ < / >
124
112
);
125
113
};
126
114
```
127
115
128
- ## ready function
116
+ ## Simple manipulation
129
117
130
- - ready function is returned by useDynTabs hook.
118
+ ``` js
119
+ import React from ' react' ;
120
+ import ' react-dyn-tabs/style/scss/react-dyn-tabs.scss' ;
121
+ import ' react-dyn-tabs/themes/scss/react-dyn-tabs-card.scss' ;
122
+ import useDynTabs from ' react-dyn-tabs' ;
123
+
124
+ export default () => {
125
+ const options = {
126
+ tabs: [
127
+ {
128
+ id: ' 1' ,
129
+ title: ' tab1' ,
130
+ panelComponent : (porps ) => < p> panel 1 < / p> ,
131
+ },
132
+ {
133
+ id: ' 2' ,
134
+ title: ' tab2' ,
135
+ panelComponent : (porps ) => < p> panel 2 < / p> ,
136
+ },
137
+ ],
138
+ selectedTabID: ' 1' ,
139
+ };
140
+ let _instance;
141
+ const [TabList , PanelList , ready ] = useDynTabs (options);
142
+ ready ((instance ) => {
143
+ _instance = instance;
144
+ });
145
+ const addTab3 = function () {
146
+ // open tab 3
147
+ _instance .open ({id: ' 3' , title: ' Tab 3' , panelComponent : (porps ) => < p> panel 3 < / p> });
148
+ // switch to tab 3
149
+ _instance .select (' 3' );
150
+ };
151
+ return (
152
+ <>
153
+ < button onClick= {addTab3}> Add tab 3 < / button>
154
+ < TabList>< / TabList>
155
+ < PanelList>< / PanelList>
156
+ < / >
157
+ );
158
+ };
159
+ ```
131
160
132
- - ready function accepts a function as a parameter and calls it with instance object after the first render, when the component is mounted.
161
+ ** NOTE : **
133
162
134
163
- Tabs can't be manipulated safely before the first render, use ready() to make a function available after the component is mounted.
135
164
136
- - ready function can be called multiple times
137
-
138
165
- ready function and instance Object will not be changed after re-rendering multiple times.
139
166
140
- - When ready function is called after the first render, it calls its function parameter with instance object immediately.
141
-
142
167
## Options
143
168
144
169
### tabs
@@ -426,6 +451,10 @@ const [TabList, PanelList, ready] = useDynTabs({
426
451
});
427
452
```
428
453
454
+ ** NOTE :**
455
+
456
+ Do not use setState inside the onInit callback because it leads to an infinite loop.
457
+
429
458
### onChange
430
459
431
460
<table >
@@ -636,7 +665,7 @@ Parameters:
636
665
** Example**
637
666
638
667
``` js
639
- const result = instance .isOpen (' tab ID' );
668
+ const result = instance .isOpen (' Your tab ID' );
640
669
```
641
670
642
671
### open
@@ -680,7 +709,7 @@ Parameters:
680
709
** Example**
681
710
682
711
``` js
683
- const result = instance .isSelected (' tab ID' );
712
+ const result = instance .isSelected (' Your tab ID' );
684
713
```
685
714
686
715
### select
@@ -715,8 +744,6 @@ Closing an already closed tab only triggers 'onInit' event.
715
744
716
745
It switches to the previously selected tab before closing if switching parameter was true and tab was already opened and selected.
717
746
718
- When the user clicks on the default close icon, close function is called with true value as a switching parameter.
719
-
720
747
Return value : Promise
721
748
722
749
Parameters:
@@ -745,9 +772,7 @@ Return value : Promise
745
772
** Example**
746
773
747
774
``` js
748
- instance .refresh ().then (({currentData, instance}) => {
749
- // do sth here
750
- });
775
+ instance .refresh ().then (({currentData, instance}) => {});
751
776
```
752
777
753
778
### getOption
@@ -945,7 +970,7 @@ const {selectedTabID, openTabIDs} = instance.getPreviousData();
945
970
<tr>
946
971
<td>panelComponent</td>
947
972
<td>React Element | React Component | null</td>
948
- <td>function component which returns empty div</td>
973
+ <td>A function component which returns empty div</td>
949
974
<td>false</td>
950
975
</tr>
951
976
<tr>
0 commit comments