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

add forwardRef support to addon-info #4961

Merged
merged 1 commit into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions addons/info/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"nested-object-assign": "^1.0.1",
"prop-types": "^15.6.2",
"react-addons-create-fragment": "^15.5.3",
"react-is": "^16.6.1",
"react-lifecycles-compat": "^3.0.4",
"util-deprecate": "^1.0.2"
},
Expand Down
38 changes: 38 additions & 0 deletions addons/info/src/components/Node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { isForwardRef } from 'react-is';
import PropTypes from 'prop-types';
import Props from './Props';

Expand Down Expand Up @@ -72,6 +73,43 @@ export default function Node(props) {
);
}

if (isForwardRef(node)) {
const childElement = node.type.render(node.props);
return (
<div>
<div style={containerStyleCopy}>
<span style={tagStyle}>
&lt;
{`ForwardRef`}
</span>
<Props
node={node}
maxPropsIntoLine={maxPropsIntoLine}
maxPropObjectKeys={maxPropObjectKeys}
maxPropArrayLength={maxPropArrayLength}
maxPropStringLength={maxPropStringLength}
/>
<span style={tagStyle}>&gt;</span>
</div>
<Node
node={childElement}
depth={depth + 1}
maxPropsIntoLine={maxPropsIntoLine}
maxPropObjectKeys={maxPropObjectKeys}
maxPropArrayLength={maxPropArrayLength}
maxPropStringLength={maxPropStringLength}
/>
<div style={containerStyleCopy}>
<span style={tagStyle}>
&lt;/
{`ForwardRef`}
&gt;
</span>
</div>
</div>
);
}

// Single-line tag
if (!children) {
return (
Expand Down
5 changes: 5 additions & 0 deletions addons/info/src/components/Story.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint no-underscore-dangle: 0 */

import React, { Component, createElement } from 'react';
import { isForwardRef } from 'react-is';
import { polyfill } from 'react-lifecycles-compat';
import PropTypes from 'prop-types';
import global from 'global';
Expand Down Expand Up @@ -337,9 +338,13 @@ class Story extends Component {
if (innerChildren.props && innerChildren.props.children) {
extract(innerChildren.props.children);
}
if (isForwardRef(innerChildren)) {
extract(innerChildren.type.render(innerChildren.props));
}
if (
typeof innerChildren === 'string' ||
typeof innerChildren.type === 'string' ||
isForwardRef(innerChildren) ||
(Array.isArray(propTablesExclude) && // also ignore excluded types
~propTablesExclude.indexOf(innerChildren.type)) // eslint-disable-line no-bitwise
) {
Expand Down
24 changes: 24 additions & 0 deletions examples/official-storybook/components/ForwardedRefButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React, { forwardRef } from 'react';
import PropTypes from 'prop-types';
import BaseButton from './BaseButton';

const ForwardedRefButton = forwardRef((props, ref) => <BaseButton {...props} forwardedRef={ref} />);

ForwardedRefButton.defaultProps = {
disabled: false,
onClick: () => {},
style: {},
};

ForwardedRefButton.propTypes = {
/** Boolean indicating whether the button should render as disabled */
disabled: PropTypes.bool,
/** button label. */
label: PropTypes.string.isRequired,
/** onClick handler */
onClick: PropTypes.func,
/** Custom styles */
style: PropTypes.shape({}),
};

export default ForwardedRefButton;
Loading