Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: forward ref not working #543

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -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.",
@@ -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
@@ -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 />'
17 changes: 11 additions & 6 deletions src/parser/parseReactElement.js
Original file line number Diff line number Diff line change
@@ -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';

5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -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"