File tree Expand file tree Collapse file tree 4 files changed +41
-14
lines changed Expand file tree Collapse file tree 4 files changed +41
-14
lines changed Original file line number Diff line number Diff line change 24
24
"smoke" : " node tests/smoke/run"
25
25
},
26
26
"lint-staged" : {
27
- "*.js" : [
28
- " prettier --write \" **/*.{js,json}\" " ,
29
- " git add"
30
- ]
27
+ "*.js" : [" prettier --write \" **/*.{js,json}\" " , " git add" ]
31
28
},
32
29
"author" : {
33
30
"name" : " Algolia, Inc." ,
84
81
},
85
82
"dependencies" : {
86
83
"@base2/pretty-print-object" : " 1.0.0" ,
87
- "is-plain-object" : " 3.0.0"
84
+ "is-plain-object" : " 3.0.0" ,
85
+ "react-is" : " ^16.13.1"
88
86
},
89
87
"jest" : {
90
- "setupFilesAfterEnv" : [
91
- " <rootDir>tests/setupTests.js"
92
- ]
88
+ "setupFilesAfterEnv" : [" <rootDir>tests/setupTests.js" ]
93
89
}
94
90
}
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
Original file line number Diff line number Diff line change @@ -7298,6 +7298,11 @@ react-is@^16.12.0:
7298
7298
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.12.0.tgz#2cc0fe0fba742d97fd527c42a13bec4eeb06241c"
7299
7299
integrity sha512-rPCkf/mWBtKc97aLL9/txD8DZdemK0vkA3JMLShjlJB3Pj3s+lpf1KaBzMfQrAmhMQB0n1cU/SUGgKKBCe837Q==
7300
7300
7301
+ react-is@^16.13.1 :
7302
+ version "16.13.1"
7303
+ resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
7304
+ integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
7305
+
7301
7306
react-is@^16.5.2 :
7302
7307
version "16.5.2"
7303
7308
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.5.2.tgz#e2a7b7c3f5d48062eb769fcb123505eb928722e3"
You can’t perform that action at this time.
0 commit comments