File tree Expand file tree Collapse file tree 2 files changed +32
-6
lines changed Expand file tree Collapse file tree 2 files changed +32
-6
lines changed Original file line number Diff line number Diff line change @@ -57,6 +57,27 @@ describe('reactElementToJSXString(ReactElement)', () => {
57
57
delete AnonymousStatelessComponent . displayName ;
58
58
} ) ;
59
59
60
+ it ( 'reactElementToJSXString forwardRef with a displayName' , ( ) => {
61
+ const createReactComponent = tagName => {
62
+ const createForwardRef = ReactComponent => {
63
+ const forwardRef = ( props , ref ) => {
64
+ return < ReactComponent { ...props } forwardedRef = { ref } /> ;
65
+ } ;
66
+ forwardRef . displayName = tagName ;
67
+
68
+ return React . forwardRef ( forwardRef ) ;
69
+ } ;
70
+
71
+ return createForwardRef ( React . createElement ( tagName ) ) ;
72
+ } ;
73
+
74
+ const MyComponent = createReactComponent ( 'my-component' ) ;
75
+
76
+ expect ( reactElementToJSXString ( < MyComponent /> ) ) . toEqual (
77
+ '<my-component />'
78
+ ) ;
79
+ } ) ;
80
+
60
81
it ( "reactElementToJSXString(React.createElement('div'))" , ( ) => {
61
82
expect ( reactElementToJSXString ( React . createElement ( 'div' ) ) ) . toEqual (
62
83
'<div />'
Original file line number Diff line number Diff line change 2
2
3
3
import React , { type Element as ReactElement , Fragment } from 'react' ;
4
4
import type { Options } from './../options' ;
5
+ import * as ReactIs from 'react-is' ;
5
6
import {
6
7
createStringTreeNode ,
7
8
createNumberTreeNode ,
@@ -12,12 +13,16 @@ import type { TreeNode } from './../tree';
12
13
13
14
const supportFragment = Boolean ( Fragment ) ;
14
15
15
- const getReactElementDisplayName = ( element : ReactElement < * > ) : string =>
16
- element . type . displayName ||
17
- ( element . type . name !== '_default' ? element . type . name : null ) || // function name
18
- ( typeof element . type === 'function' // function without a name, you should provide one
19
- ? 'No Display Name'
20
- : element . type ) ;
16
+ const getReactElementDisplayName = ( element : ReactElement < > ) : string => {
17
+ return (
18
+ element . type . displayName ||
19
+ ( element . type . name !== '_default' ? element . type . name : null ) || // function name
20
+ ( ReactIs . isForwardRef ( element ) ? element . type . render . displayName : null ) ||
21
+ ( typeof element . type === 'function' // function without a name, you should provide one
22
+ ? 'No Display Name'
23
+ : element . type )
24
+ ) ;
25
+ } ;
21
26
22
27
const noChildren = ( propsValue , propName ) => propName !== 'children' ;
23
28
You can’t perform that action at this time.
0 commit comments