From ddf2c2ffd6f58743f25253289297bfe19fa17c75 Mon Sep 17 00:00:00 2001 From: Spencer Ahrens Date: Mon, 11 Jun 2018 19:10:27 -0700 Subject: [PATCH] fix forwardRef displayName on Text and View Reviewed By: TheSavior Differential Revision: D8342852 fbshipit-source-id: 5af80edfd5de5b6d6ea6fdc24abf8931f767c812 --- Libraries/Components/View/View.js | 31 ++++++++++++++++++------------- Libraries/Text/Text.js | 16 ++++++++++------ jest/mockComponent.js | 7 ++++++- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/Libraries/Components/View/View.js b/Libraries/Components/View/View.js index c494643c474..ed420338c24 100644 --- a/Libraries/Components/View/View.js +++ b/Libraries/Components/View/View.js @@ -32,19 +32,24 @@ const RCTView = requireNativeComponent('RCTView'); let ViewToExport = RCTView; if (__DEV__) { + const View = (props: Props, forwardedRef: ?React.Ref<'RCTView'>) => { + return ( + + {hasTextAncestor => { + invariant( + !hasTextAncestor, + 'Nesting of within is not currently supported.', + ); + return ; + }} + + ); + }; + View.displayName = 'View'; // TODO(T30332650) remove bug workaround // $FlowFixMe - TODO T29156721 `React.forwardRef` is not defined in Flow, yet. - ViewToExport = React.forwardRef((props, ref) => ( - - {hasTextAncestor => { - invariant( - !hasTextAncestor, - 'Nesting of within is not currently supported.', - ); - return ; - }} - - )); - ViewToExport.displayName = 'View'; + ViewToExport = React.forwardRef(View); } -module.exports = ((ViewToExport: any): Class>); +module.exports = ((ViewToExport: $FlowFixMe): Class< + NativeComponent, +>); diff --git a/Libraries/Text/Text.js b/Libraries/Text/Text.js index 5da883aa630..aaa283a6daf 100644 --- a/Libraries/Text/Text.js +++ b/Libraries/Text/Text.js @@ -261,13 +261,17 @@ const RCTVirtualText = uiViewClassName: 'RCTVirtualText', })); +const Text = ( + props: TextProps, + forwardedRef: ?React.Ref<'RCTText' | 'RCTVirtualText'>, +) => { + return ; +}; +Text.displayName = 'Text'; // TODO(T30332650) remove bug workaround // $FlowFixMe - TODO T29156721 `React.forwardRef` is not defined in Flow, yet. -const Text = React.forwardRef((props, ref) => ( - -)); -Text.displayName = 'Text'; +const TextToExport = React.forwardRef(Text); // TODO: Deprecate this. -Text.propTypes = TextPropTypes; +TextToExport.propTypes = TextPropTypes; -module.exports = ((Text: any): Class>); +module.exports = (TextToExport: Class>); diff --git a/jest/mockComponent.js b/jest/mockComponent.js index 6d5a80b47e0..3f98a160d5c 100644 --- a/jest/mockComponent.js +++ b/jest/mockComponent.js @@ -18,7 +18,12 @@ module.exports = (moduleName, instanceMethods) => { const Component = class extends SuperClass { render() { - const name = RealComponent.displayName || RealComponent.name; + const name = + RealComponent.displayName || + RealComponent.name || + (RealComponent.render // handle React.forwardRef + ? RealComponent.render.displayName || RealComponent.render.name + : 'Unknown'); const props = Object.assign({}, RealComponent.defaultProps);