From 27a9427c3264897af641178d58357b619e9a69ee Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Wed, 27 Mar 2019 23:15:46 +0100 Subject: [PATCH] Merge pull request #6222 from Krustal/addon-info-forward-ref-opt-in forwardRef elements use displayName if available --- addons/info/src/components/Node.js | 2 +- .../ForwardedRefButtonWDisplayName.js | 28 +++ .../addon-info.stories.storyshot | 236 ++++++++++++++++++ .../stories/addon-info.stories.js | 4 + 4 files changed, 269 insertions(+), 1 deletion(-) create mode 100644 examples/official-storybook/components/ForwardedRefButtonWDisplayName.js diff --git a/addons/info/src/components/Node.js b/addons/info/src/components/Node.js index 87e9a787253e..926b664c3588 100644 --- a/addons/info/src/components/Node.js +++ b/addons/info/src/components/Node.js @@ -73,7 +73,7 @@ export default function Node(props) { ); } - if (isForwardRef(node)) { + if (isForwardRef(node) && !node.type.displayName) { const childElement = node.type.render(node.props); return (
diff --git a/examples/official-storybook/components/ForwardedRefButtonWDisplayName.js b/examples/official-storybook/components/ForwardedRefButtonWDisplayName.js new file mode 100644 index 000000000000..cbd36e0d3407 --- /dev/null +++ b/examples/official-storybook/components/ForwardedRefButtonWDisplayName.js @@ -0,0 +1,28 @@ +import React, { forwardRef } from 'react'; +import PropTypes from 'prop-types'; +import BaseButton from './BaseButton'; + +const ForwardedRefButtonWDisplayName = forwardRef((props, ref) => ( + +)); + +ForwardedRefButtonWDisplayName.defaultProps = { + disabled: false, + onClick: () => {}, + style: {}, +}; + +ForwardedRefButtonWDisplayName.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({}), +}; + +ForwardedRefButtonWDisplayName.displayName = 'ButtonDisplayName'; + +export default ForwardedRefButtonWDisplayName; diff --git a/examples/official-storybook/stories/__snapshots__/addon-info.stories.storyshot b/examples/official-storybook/stories/__snapshots__/addon-info.stories.storyshot index 9df5eabc138f..6ba5cff70069 100644 --- a/examples/official-storybook/stories/__snapshots__/addon-info.stories.storyshot +++ b/examples/official-storybook/stories/__snapshots__/addon-info.stories.storyshot @@ -626,6 +626,242 @@ exports[`Storyshots Addons|Info/ForwardRef Displays forwarded ref components cor
`; +exports[`Storyshots Addons|Info/ForwardRef Uses forwardRef displayName if available 1`] = ` +
+
+ +
+ + +
+`; + exports[`Storyshots Addons|Info/GitHub issues #1814 1`] = `
( + )) + .add('Uses forwardRef displayName if available', () => ( + )); storiesOf('Addons|Info/deprecated', module).add(