Skip to content

Commit

Permalink
Merge pull request #4961 from yjcxy12/addon-info-forward-ref
Browse files Browse the repository at this point in the history
add forwardRef support to addon-info
  • Loading branch information
ndelangen authored Dec 12, 2018
2 parents a070503 + 29fe3b9 commit f0961c3
Show file tree
Hide file tree
Showing 6 changed files with 504 additions and 0 deletions.
1 change: 1 addition & 0 deletions addons/info/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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

0 comments on commit f0961c3

Please sign in to comment.