1- import React , { useState , useContext , useRef , useEffect } from 'react'
1+ import React , {
2+ useState ,
3+ useContext ,
4+ useRef ,
5+ useEffect
6+ } from 'react'
27import PropTypes from 'prop-types'
38import classNames from 'classnames'
49import CFade from '../fade/CFade'
@@ -9,7 +14,7 @@ import { Context } from './CTabs.js'
914import { Context as FadeContext } from './CTabContent.js'
1015
1116//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 )
1318
1419const getState = r => r . current . dataset . tab || getIndex ( r . current )
1520
@@ -22,17 +27,22 @@ const CTabPane = props => {
2227 ...attributes
2328 } = props
2429
25- const context = useContext ( Context )
30+ const { active : activeTab , setActiveTab } = useContext ( Context ) || { }
2631 const fade = useContext ( FadeContext )
27- const act = ( context || { } ) . active
2832 const ref = typeof innerRef === 'object' ? innerRef : useRef ( )
2933 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 ] )
3240
3341 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 ] )
3646
3747 //render
3848 const classes = classNames (
@@ -56,9 +66,8 @@ const CTabPane = props => {
5666
5767CTabPane . propTypes = {
5868 className : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . array , PropTypes . object ] ) ,
59- //
6069 innerRef : PropTypes . oneOfType ( [ PropTypes . func , PropTypes . object ] ) ,
61- active : PropTypes . bool ,
70+ active : PropTypes . bool
6271}
6372
6473export default CTabPane
0 commit comments