1
- import React , { useState , useContext , useRef , useEffect } from 'react'
1
+ import React , {
2
+ useState ,
3
+ useContext ,
4
+ useRef ,
5
+ useEffect
6
+ } from 'react'
2
7
import PropTypes from 'prop-types'
3
8
import classNames from 'classnames'
4
9
import CFade from '../fade/CFade'
@@ -9,7 +14,7 @@ import { Context } from './CTabs.js'
9
14
import { Context as FadeContext } from './CTabContent.js'
10
15
11
16
//component - CoreUI / CTabPane
12
- const getIndex = ( el ) => Array . from ( el . parentNode . children ) . indexOf ( el )
17
+ const getIndex = el => Array . from ( el . parentNode . children ) . indexOf ( el )
13
18
14
19
const getState = r => r . current . dataset . tab || getIndex ( r . current )
15
20
@@ -22,17 +27,22 @@ const CTabPane = props => {
22
27
...attributes
23
28
} = props
24
29
25
- const context = useContext ( Context )
30
+ const { active : activeTab , setActiveTab } = useContext ( Context ) || { }
26
31
const fade = useContext ( FadeContext )
27
- const act = ( context || { } ) . active
28
32
const ref = typeof innerRef === 'object' ? innerRef : useRef ( )
29
33
typeof innerRef === 'function' && innerRef ( ref )
30
-
31
- const [ isActive , setIsActive ] = useState ( active )
34
+
35
+ const [ isActive , setIsActive ] = useState ( )
36
+
37
+ useEffect ( ( ) => {
38
+ setIsActive ( activeTab === getState ( ref ) )
39
+ } , [ activeTab ] )
32
40
33
41
useEffect ( ( ) => {
34
- setIsActive ( active !== undefined ? active : act === getState ( ref ) )
35
- } , [ act , active ] )
42
+ if ( active !== undefined ) {
43
+ setActiveTab ? setActiveTab ( active && getState ( ref ) ) : setIsActive ( active )
44
+ }
45
+ } , [ active ] )
36
46
37
47
//render
38
48
const classes = classNames (
@@ -56,9 +66,8 @@ const CTabPane = props => {
56
66
57
67
CTabPane . propTypes = {
58
68
className : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . array , PropTypes . object ] ) ,
59
- //
60
69
innerRef : PropTypes . oneOfType ( [ PropTypes . func , PropTypes . object ] ) ,
61
- active : PropTypes . bool ,
70
+ active : PropTypes . bool
62
71
}
63
72
64
73
export default CTabPane
0 commit comments