Skip to content

Commit

Permalink
Fix: forward ref
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Thornton committed Mar 22, 2020
1 parent 9f4270d commit 29ed88b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
12 changes: 4 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
"smoke": "node tests/smoke/run"
},
"lint-staged": {
"*.js": [
"prettier --write \"**/*.{js,json}\"",
"git add"
]
"*.js": ["prettier --write \"**/*.{js,json}\"", "git add"]
},
"author": {
"name": "Algolia, Inc.",
Expand Down Expand Up @@ -84,11 +81,10 @@
},
"dependencies": {
"@base2/pretty-print-object": "1.0.0",
"is-plain-object": "3.0.0"
"is-plain-object": "3.0.0",
"react-is": "^16.13.1"
},
"jest": {
"setupFilesAfterEnv": [
"<rootDir>tests/setupTests.js"
]
"setupFilesAfterEnv": ["<rootDir>tests/setupTests.js"]
}
}
21 changes: 21 additions & 0 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ describe('reactElementToJSXString(ReactElement)', () => {
delete AnonymousStatelessComponent.displayName;
});

it('reactElementToJSXString forwardRef with a displayName', () => {
const createReactComponent = tagName => {
const createForwardRef = ReactComponent => {
const forwardRef = (props, ref) => {
return <ReactComponent {...props} forwardedRef={ref} />;
};
forwardRef.displayName = tagName;

return React.forwardRef(forwardRef);
};

return createForwardRef(React.createElement(tagName));
};

const MyComponent = createReactComponent('my-component');

expect(reactElementToJSXString(<MyComponent />)).toEqual(
'<my-component />'
);
});

it("reactElementToJSXString(React.createElement('div'))", () => {
expect(reactElementToJSXString(React.createElement('div'))).toEqual(
'<div />'
Expand Down
17 changes: 11 additions & 6 deletions src/parser/parseReactElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ import {
createReactFragmentTreeNode,
} from './../tree';
import type { TreeNode } from './../tree';
import * as ReactIs from 'react-is';

const supportFragment = Boolean(Fragment);

const getReactElementDisplayName = (element: ReactElement<*>): string =>
element.type.displayName ||
(element.type.name !== '_default' ? element.type.name : null) || // function name
(typeof element.type === 'function' // function without a name, you should provide one
? 'No Display Name'
: element.type);
const getReactElementDisplayName = (element: ReactElement<>): string => {
return (
element.type.displayName ||
(element.type.name !== '_default' ? element.type.name : null) || // function name
(ReactIs.isForwardRef(element) ? element.type.render.displayName : null) ||
(typeof element.type === 'function' // function without a name, you should provide one
? 'No Display Name'
: element.type)
);
};

const noChildren = (propsValue, propName) => propName !== 'children';

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7298,6 +7298,11 @@ react-is@^16.12.0:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.12.0.tgz#2cc0fe0fba742d97fd527c42a13bec4eeb06241c"
integrity sha512-rPCkf/mWBtKc97aLL9/txD8DZdemK0vkA3JMLShjlJB3Pj3s+lpf1KaBzMfQrAmhMQB0n1cU/SUGgKKBCe837Q==

react-is@^16.13.1:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==

react-is@^16.5.2:
version "16.5.2"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.5.2.tgz#e2a7b7c3f5d48062eb769fcb123505eb928722e3"
Expand Down

0 comments on commit 29ed88b

Please sign in to comment.