1
1
import React , { PropTypes , cloneElement } from 'react' ;
2
- import { findDOMNode } from 'react-dom' ;
3
2
import cx from 'classnames' ;
4
3
import jss from 'js-stylesheet' ;
5
4
import uuid from '../helpers/uuid' ;
@@ -22,9 +21,9 @@ module.exports = React.createClass({
22
21
23
22
propTypes : {
24
23
className : PropTypes . string ,
25
- selectedIndex : PropTypes . number ,
24
+ selectedIndex : PropTypes . number , // eslint-disable-line react/no-unused-prop-types
26
25
onSelect : PropTypes . func ,
27
- focus : PropTypes . bool ,
26
+ focus : PropTypes . bool , // eslint-disable-line react/no-unused-prop-types
28
27
children : childrenPropType ,
29
28
forceRenderTabPanel : PropTypes . bool ,
30
29
} ,
@@ -59,7 +58,7 @@ module.exports = React.createClass({
59
58
60
59
componentDidMount ( ) {
61
60
if ( useDefaultStyles ) {
62
- jss ( require ( '../helpers/styles.js ' ) ) ; // eslint-disable-line global-require
61
+ jss ( require ( '../helpers/styles' ) ) ; // eslint-disable-line global-require
63
62
}
64
63
} ,
65
64
@@ -98,16 +97,14 @@ module.exports = React.createClass({
98
97
99
98
// Look for non-disabled tab from index to the last tab on the right
100
99
for ( let i = index + 1 ; i < count ; i ++ ) {
101
- const tab = this . getTab ( i ) ;
102
- if ( ! isTabDisabled ( findDOMNode ( tab ) ) ) {
100
+ if ( ! isTabDisabled ( this . getTab ( i ) ) ) {
103
101
return i ;
104
102
}
105
103
}
106
104
107
105
// If no tab found, continue searching from first on left to index
108
106
for ( let i = 0 ; i < index ; i ++ ) {
109
- const tab = this . getTab ( i ) ;
110
- if ( ! isTabDisabled ( findDOMNode ( tab ) ) ) {
107
+ if ( ! isTabDisabled ( this . getTab ( i ) ) ) {
111
108
return i ;
112
109
}
113
110
}
@@ -121,17 +118,15 @@ module.exports = React.createClass({
121
118
122
119
// Look for non-disabled tab from index to first tab on the left
123
120
while ( i -- ) {
124
- const tab = this . getTab ( i ) ;
125
- if ( ! isTabDisabled ( findDOMNode ( tab ) ) ) {
121
+ if ( ! isTabDisabled ( this . getTab ( i ) ) ) {
126
122
return i ;
127
123
}
128
124
}
129
125
130
126
// If no tab found, continue searching from last tab on right to index
131
127
i = this . getTabsCount ( ) ;
132
128
while ( i -- > index ) {
133
- const tab = this . getTab ( i ) ;
134
- if ( ! isTabDisabled ( findDOMNode ( tab ) ) ) {
129
+ if ( ! isTabDisabled ( this . getTab ( i ) ) ) {
135
130
return i ;
136
131
}
137
132
}
@@ -150,16 +145,8 @@ module.exports = React.createClass({
150
145
return React . Children . count ( this . props . children . slice ( 1 ) ) ;
151
146
} ,
152
147
153
- getTabList ( ) {
154
- return this . refs . tablist ;
155
- } ,
156
-
157
148
getTab ( index ) {
158
- return this . refs [ `tabs-${ index } ` ] ;
159
- } ,
160
-
161
- getPanel ( index ) {
162
- return this . refs [ `panels-${ index } ` ] ;
149
+ return this . tabNodes [ `tabs-${ index } ` ] ;
163
150
} ,
164
151
165
152
getChildren ( ) {
@@ -193,15 +180,14 @@ module.exports = React.createClass({
193
180
if ( count ++ === 0 ) {
194
181
// TODO try setting the uuid in the "constructor" for `Tab`/`TabPanel`
195
182
result = cloneElement ( child , {
196
- ref : 'tablist' ,
197
183
children : React . Children . map ( child . props . children , ( tab ) => {
198
184
// null happens when conditionally rendering TabPanel/Tab
199
185
// see https://github.com/rackt/react-tabs/issues/37
200
186
if ( tab === null ) {
201
187
return null ;
202
188
}
203
189
204
- const ref = `tabs-${ index } ` ;
190
+ const tabRef = ( node ) => { this . tabNodes [ `tabs-${ index } ` ] = node ; } ;
205
191
const id = tabIds [ index ] ;
206
192
const panelId = panelIds [ index ] ;
207
193
const selected = state . selectedIndex === index ;
@@ -210,7 +196,7 @@ module.exports = React.createClass({
210
196
index ++ ;
211
197
212
198
return cloneElement ( tab , {
213
- ref ,
199
+ tabRef ,
214
200
id,
215
201
panelId,
216
202
selected,
@@ -224,15 +210,13 @@ module.exports = React.createClass({
224
210
}
225
211
// Clone TabPanel components to have refs
226
212
else {
227
- const ref = `panels-${ index } ` ;
228
213
const id = panelIds [ index ] ;
229
214
const tabId = tabIds [ index ] ;
230
215
const selected = state . selectedIndex === index ;
231
216
232
217
index ++ ;
233
218
234
219
result = cloneElement ( child , {
235
- ref,
236
220
id,
237
221
tabId,
238
222
selected,
@@ -243,6 +227,8 @@ module.exports = React.createClass({
243
227
} ) ;
244
228
} ,
245
229
230
+ tabNodes : [ ] ,
231
+
246
232
handleKeyDown ( e ) {
247
233
if ( this . isTabFromContainer ( e . target ) ) {
248
234
let index = this . state . selectedIndex ;
@@ -323,9 +309,8 @@ module.exports = React.createClass({
323
309
324
310
// Check if the first occurrence of a Tabs container is `this` one.
325
311
let nodeAncestor = node . parentElement ;
326
- const tabsNode = findDOMNode ( this ) ;
327
312
do {
328
- if ( nodeAncestor === tabsNode ) return true ;
313
+ if ( nodeAncestor === this . node ) return true ;
329
314
else if ( nodeAncestor . getAttribute ( 'data-tabs' ) ) break ;
330
315
331
316
nodeAncestor = nodeAncestor . parentElement ;
@@ -375,6 +360,7 @@ module.exports = React.createClass({
375
360
) }
376
361
onClick = { this . handleClick }
377
362
onKeyDown = { this . handleKeyDown }
363
+ ref = { ( node ) => { this . node = node ; } }
378
364
data-tabs
379
365
>
380
366
{ this . getChildren ( ) }
0 commit comments