From 9f0c810d5ce3ad29aee55ea2dcc6c3d2b29cee26 Mon Sep 17 00:00:00 2001 From: Federico Zivolo Date: Fri, 5 Apr 2019 13:54:15 +0200 Subject: [PATCH] fix: make Button accept `ref` property --- .../Button/__snapshots__/index.test.js.snap | 42 ++++++++----- packages/react-core/src/Button/index.js | 62 +++++++++---------- 2 files changed, 55 insertions(+), 49 deletions(-) diff --git a/packages/react-core/src/Button/__snapshots__/index.test.js.snap b/packages/react-core/src/Button/__snapshots__/index.test.js.snap index 20b371fd..22183ca1 100644 --- a/packages/react-core/src/Button/__snapshots__/index.test.js.snap +++ b/packages/react-core/src/Button/__snapshots__/index.test.js.snap @@ -72,16 +72,19 @@ exports[`renders a Button with small size 1`] = ` size="small" type="button" > - + + `; @@ -168,17 +171,22 @@ exports[`renders a disabled and transparent Button 1`] = ` transparent={true} type="button" > - + + `; @@ -255,16 +263,20 @@ exports[`renders the expected markup 1`] = ` size="regular" type="button" > - + + `; diff --git a/packages/react-core/src/Button/index.js b/packages/react-core/src/Button/index.js index e5628294..39a31312 100644 --- a/packages/react-core/src/Button/index.js +++ b/packages/react-core/src/Button/index.js @@ -72,41 +72,35 @@ const reset = css` `; const Button = styled( - ({ - to, - href, - transparent, - size, - importance, - disabled, - type, - children, - ...props - }: Props) => { - let Tag, specificProps; - if (to && !disabled) { - Tag = Link; - specificProps = { to }; - } else if ((href || to) && !disabled) { - Tag = 'a'; - specificProps = { href }; - } else { - Tag = 'button'; - specificProps = { disabled, type }; - } + React.forwardRef( + ( + { + to, + href, + transparent, + size, + importance, + disabled, + type, + ...props + }: Props, + ref: React.ElementRef + ) => { + let Tag, specificProps; + if (to && !disabled) { + Tag = Link; + specificProps = { to }; + } else if ((href || to) && !disabled) { + Tag = 'a'; + specificProps = { href }; + } else { + Tag = 'button'; + specificProps = { disabled, type }; + } - return ( - - {React.Children.map(children, node => - ['string', 'number'].includes(typeof node) ? ( - {node} - ) : ( - node - ) - )} - - ); - } + return ; + } + ) )` ${reset};