Skip to content

Commit

Permalink
Merge pull request pmndrs#185 from psachs21/forward-refs
Browse files Browse the repository at this point in the history
Feature: Add ref forwarding to animated components
  • Loading branch information
drcmda committed Aug 19, 2018
2 parents 1ca95d4 + 62def49 commit 756b49a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
6 changes: 3 additions & 3 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"gzipped": 2150
},
"dist/web.umd.js": {
"bundled": 89751,
"minified": 36155,
"gzipped": 12303
"bundled": 88718,
"minified": 35816,
"gzipped": 12184
}
}
19 changes: 16 additions & 3 deletions src/animated/createAnimatedComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import AnimatedProps from './AnimatedProps'
import * as Globals from './Globals'

export default function createAnimatedComponent(Component) {
return class AnimatedComponent extends React.Component {
class AnimatedComponent extends React.Component {
static propTypes = {
style(props, propName, componentName) {
if (!Component.propTypes) return
Expand Down Expand Up @@ -61,8 +61,21 @@ export default function createAnimatedComponent(Component) {
}

render() {
const props = this._propsAnimated.__getValue()
return <Component {...props} ref={node => (this.node = node)} />
const { forwardedRef, ...rest } = this._propsAnimated.__getValue()
return (
<Component
{...rest}
ref={node => {
this.node = node
if (forwardedRef) {
forwardedRef(node)
}
}}
/>
)
}
}
return React.forwardRef((props, ref) => {
return <AnimatedComponent {...props} forwardedRef={ref} />
})
}
9 changes: 7 additions & 2 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ let tree
class Test extends React.Component {
count = 0
state = { resolve: undefined, props: {}, result: undefined }
ref = null

render() {
let { resolve, props, result } = this.state
return resolve ? (
Expand Down Expand Up @@ -36,12 +38,15 @@ class Test extends React.Component {
tree.update()
result = result || props.to
expect(tree.find('div').get(0).props.style).toMatchObject(result)
console.log(this.ref)
resolve()
})
}>
{props.native
? styles => <animated.div style={styles} />
: styles => <div style={styles} />}
? styles => (
<animated.div style={styles} ref={node => (this.ref = node)} />
)
: styles => <div style={styles} ref={node => (this.ref = node)} />}
</Spring>
) : null
}
Expand Down

0 comments on commit 756b49a

Please sign in to comment.