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 2424 "smoke" : " node tests/smoke/run"
2525 },
2626 "lint-staged" : {
27- "*.js" : [
28- " prettier --write \" **/*.{js,json}\" " ,
29- " git add"
30- ]
27+ "*.js" : [" prettier --write \" **/*.{js,json}\" " , " git add" ]
3128 },
3229 "author" : {
3330 "name" : " Algolia, Inc." ,
8481 },
8582 "dependencies" : {
8683 "@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"
8886 },
8987 "jest" : {
90- "setupFilesAfterEnv" : [
91- " <rootDir>tests/setupTests.js"
92- ]
88+ "setupFilesAfterEnv" : [" <rootDir>tests/setupTests.js" ]
9389 }
9490}
Original file line number Diff line number Diff line change @@ -57,6 +57,27 @@ describe('reactElementToJSXString(ReactElement)', () => {
5757 delete AnonymousStatelessComponent . displayName ;
5858 } ) ;
5959
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+
6081 it ( "reactElementToJSXString(React.createElement('div'))" , ( ) => {
6182 expect ( reactElementToJSXString ( React . createElement ( 'div' ) ) ) . toEqual (
6283 '<div />'
Original file line number Diff line number Diff line change 22
33import React , { type Element as ReactElement , Fragment } from 'react' ;
44import type { Options } from './../options' ;
5+ import * as ReactIs from 'react-is' ;
56import {
67 createStringTreeNode ,
78 createNumberTreeNode ,
@@ -12,12 +13,16 @@ import type { TreeNode } from './../tree';
1213
1314const supportFragment = Boolean ( Fragment ) ;
1415
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+ } ;
2126
2227const noChildren = ( propsValue , propName ) => propName !== 'children' ;
2328
Original file line number Diff line number Diff line change @@ -7298,6 +7298,11 @@ react-is@^16.12.0:
72987298 resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.12.0.tgz#2cc0fe0fba742d97fd527c42a13bec4eeb06241c"
72997299 integrity sha512-rPCkf/mWBtKc97aLL9/txD8DZdemK0vkA3JMLShjlJB3Pj3s+lpf1KaBzMfQrAmhMQB0n1cU/SUGgKKBCe837Q==
73007300
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+
73017306react-is@^16.5.2 :
73027307 version "16.5.2"
73037308 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