diff --git a/src/lib/forwardRefFactory/forwardFunctionFactory.js b/src/lib/forwardRefFactory/forwardFunctionFactory.js new file mode 100644 index 0000000000..207ceb5a71 --- /dev/null +++ b/src/lib/forwardRefFactory/forwardFunctionFactory.js @@ -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 ( + + + + ) + } + + if (supportsRef(props.as)) { + return createElement(Component, { + ...props, + [forwardRefSymbol]: ref, + }) + } + + return Component +} + +export default forwardFunctionFactory diff --git a/src/lib/forwardRefFactory/forwardRefFactory.js b/src/lib/forwardRefFactory/forwardRefFactory.js index 29416b5c1e..c3c902d2fc 100644 --- a/src/lib/forwardRefFactory/forwardRefFactory.js +++ b/src/lib/forwardRefFactory/forwardRefFactory.js @@ -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 ( - - - - ) - } - - if (supportsRef(props.as)) { - return - } - - return Component -} +import forwardFunctionFactory from './forwardFunctionFactory' /** * Wraps passed component with react 'forwardRef' function, which produce new component with type 'object' and structure @@ -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 diff --git a/src/lib/forwardRefFactory/index.js b/src/lib/forwardRefFactory/index.js new file mode 100644 index 0000000000..6e8c0a49b5 --- /dev/null +++ b/src/lib/forwardRefFactory/index.js @@ -0,0 +1,2 @@ +export { forwardRefSymbol } from './forwardFunctionFactory' +export forwardRefFactory from './forwardRefFactory'