Skip to content

Commit eba59c2

Browse files
committed
fix: make components using Transition component use new API #127
1 parent c1022a8 commit eba59c2

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

src/carousel/CCarouselItem.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useContext, createRef, useEffect } from 'react'
1+
import React, { useState, useContext, useEffect, useRef } from 'react'
22
import PropTypes from 'prop-types'
33
import classNames from 'classnames'
44
import { Transition } from 'react-transition-group'
@@ -20,10 +20,9 @@ const CCarouselItem = props => {
2020
const {
2121
children,
2222
className,
23-
//
2423
innerRef,
2524
...attributes
26-
} = props;
25+
} = props
2726

2827
const {
2928
animate,
@@ -34,8 +33,9 @@ const CCarouselItem = props => {
3433
setAnimating
3534
} = useContext(Context)
3635

37-
const ref = createRef()
38-
innerRef && innerRef(ref)
36+
const ref = typeof innerRef === 'object' ? innerRef : useRef()
37+
typeof innerRef === 'function' && innerRef(ref)
38+
3939
const [isIn, setIsIn] = useState()
4040

4141
useEffect(() => {
@@ -49,9 +49,9 @@ const CCarouselItem = props => {
4949
setAnimating(false)
5050
}
5151

52-
const onEntering = (node) => {
52+
const onEntering = () => {
5353
/* eslint-disable no-unused-vars */
54-
const offsetHeight = node.offsetHeight
54+
const offsetHeight = ref.current.offsetHeight
5555
setAnimating(true)
5656
/* eslint-enable no-unused-vars */
5757
}
@@ -68,6 +68,7 @@ const CCarouselItem = props => {
6868
setAnimating(false)
6969
}
7070

71+
// const nodeRef = React.useRef()
7172

7273
//render
7374
if (!animate || state[0] === null) {
@@ -96,6 +97,7 @@ const CCarouselItem = props => {
9697
onExit={onExit}
9798
onExiting={onExiting}
9899
onExited={onExited}
100+
nodeRef={ref}
99101
>
100102
{(status) => {
101103
const direction = getDirection(state)
@@ -133,8 +135,7 @@ const CCarouselItem = props => {
133135
CCarouselItem.propTypes = {
134136
children: PropTypes.node,
135137
className: PropTypes.string,
136-
//
137138
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
138-
};
139+
}
139140

140141
export default CCarouselItem

src/collapse/CCollapse.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react'
1+
import React, { useState, useRef } from 'react'
22
import PropTypes from 'prop-types'
33
import classNames from 'classnames'
44
import { Transition } from 'react-transition-group'
@@ -24,20 +24,23 @@ const CCollapse = props => {
2424

2525
const [height, setHeight] = useState()
2626

27-
const onEntering = node => {
28-
setHeight(node.scrollHeight)
27+
const ref = typeof innerRef === 'object' ? innerRef : useRef()
28+
typeof innerRef === 'function' && innerRef(ref)
29+
30+
const onEntering = () => {
31+
setHeight(ref.current.scrollHeight)
2932
}
3033

3134
const onEntered = () => {
3235
setHeight(null)
3336
}
3437

35-
const onExit = node => {
36-
setHeight(node.scrollHeight)
38+
const onExit = () => {
39+
setHeight(ref.current.scrollHeight)
3740
}
3841

39-
const onExiting = node => {
40-
const _unused = node.offsetHeight // eslint-disable-line no-unused-vars
42+
const onExiting = () => {
43+
const _unused = ref.current.offsetHeight // eslint-disable-line no-unused-vars
4144
setHeight(0)
4245
}
4346

@@ -58,6 +61,7 @@ const CCollapse = props => {
5861
onExit={onExit}
5962
onExiting={onExiting}
6063
onExited={onExited}
64+
nodeRef={ref}
6165
>
6266
{(status) => {
6367
let collapseClass = getTransitionClass(status)
@@ -72,7 +76,7 @@ const CCollapse = props => {
7276
{...attributes}
7377
style={{ ...attributes.style, ...style }}
7478
className={classes}
75-
ref={innerRef}
79+
ref={ref}
7680
>
7781
{children}
7882
</div>
@@ -94,8 +98,4 @@ CCollapse.propTypes = {
9498
navbar: PropTypes.bool
9599
}
96100

97-
CCollapse.defaultProps = {
98-
show: false
99-
}
100-
101101
export default CCollapse

src/fade/CFade.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ const CFade = props => {
2323
const transitionProps = pickByKeys(rest, TransitionPropTypeKeys)
2424
const childProps = omitByKeys(rest, TransitionPropTypeKeys)
2525

26-
const childRef = useRef()
26+
const ref = typeof innerRef === 'object' ? innerRef : useRef()
27+
typeof innerRef === 'function' && innerRef(ref)
28+
29+
2730

2831
return (
29-
<Transition {...transitionProps} nodeRef={childRef}>
32+
<Transition {...transitionProps} nodeRef={ref}>
3033
{(status) => {
3134
const isActive = status === 'entered'
3235
const classes = classNames(
@@ -35,7 +38,7 @@ const CFade = props => {
3538
isActive && baseClassActive
3639
)
3740
return (
38-
<Tag className={classes} {...childProps} ref={innerRef}>
41+
<Tag className={classes} {...childProps} ref={ref}>
3942
{children}
4043
</Tag>
4144
)

0 commit comments

Comments
 (0)