Skip to content

Commit

Permalink
refactor forwarders
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksandr Fediashov <ofediashov@exadel.com>
  • Loading branch information
Oleksandr Fediashov committed Jul 20, 2018
1 parent 976c54d commit 5cc56b6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 34 deletions.
39 changes: 39 additions & 0 deletions src/lib/forwardRefFactory/forwardFunctionFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React, { createElement } from 'react'

import Ref from '../../addons/Ref'
import { isStatelessComponent, supportsRef } from '../componentUtils'

/**
* Use just a string for now (react 16.3), since react doesn't support Symbols in props yet.
* https://github.com/facebook/react/issues/7552
* @type {String}
*/
export const forwardRefSymbol = '__forwardRef__'

/**
* Creates a function that will choose how to pass a ref.
*
* @param {Function|Component} Component A Component to wrap
* @return {Function}
*/
const forwardFunctionFactory = Component => (props, ref) => {
// eslint-disable-next-line react/prop-types
if (isStatelessComponent(props.as)) {
return (
<Ref innerRef={ref}>
<Component {...props} />
</Ref>
)
}

if (supportsRef(props.as)) {
return createElement(Component, {
...props,
[forwardRefSymbol]: ref,
})
}

return Component
}

export default forwardFunctionFactory
39 changes: 5 additions & 34 deletions src/lib/forwardRefFactory/forwardRefFactory.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,7 @@
import hoistStatics from 'hoist-non-react-statics'
import React, { forwardRef } from 'react'
import { forwardRef } from 'react'

import Ref from '../../addons/Ref'
import { isStatelessComponent, supportsRef } from '../componentUtils'

/**
* Use just a string for now (react 16.3), since react doesn't support Symbols in props yet
* https://github.com/facebook/react/issues/7552
* @type {String}
*/
export const forwardRefSymbol = '__forwardRef__'

/**
* Creates a function that will choose how to pass a ref.
*
* @param {Function|Component} Component A Component to wrap
* @return {Function}
*/
export const forwardFunctionFactory = Component => (props, ref) => {
// eslint-disable-next-line react/prop-types
if (isStatelessComponent(props.as)) {
return (
<Ref innerRef={ref}>
<Component {...props} />
</Ref>
)
}

if (supportsRef(props.as)) {
return <Component {...{ [forwardRefSymbol]: ref, ...props }} />
}

return Component
}
import forwardFunctionFactory from './forwardFunctionFactory'

/**
* Wraps passed component with react 'forwardRef' function, which produce new component with type 'object' and structure
Expand All @@ -42,9 +11,11 @@ export const forwardFunctionFactory = Component => (props, ref) => {
* @param {Function|Component} Component A Component to wrap with forwardRef()
* @return {Object}
*/
export const forwardRefFactory = (Component) => {
const forwardRefFactory = (Component) => {
const forwarder = forwardRef(forwardFunctionFactory(Component))

hoistStatics(forwarder, Component, { $$typeof: true, render: true })
return forwarder
}

export default forwardRefFactory
2 changes: 2 additions & 0 deletions src/lib/forwardRefFactory/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { forwardRefSymbol } from './forwardFunctionFactory'
export forwardRefFactory from './forwardRefFactory'

0 comments on commit 5cc56b6

Please sign in to comment.