From 31219b3843e774668962c8ceb897cfbf096e97dc Mon Sep 17 00:00:00 2001 From: CJ Cenizal Date: Tue, 21 Mar 2017 17:10:03 -0700 Subject: [PATCH] Refactor kuiButton to not use createElement, and instead exit early and return the correct element. --- .../button/__snapshots__/button.test.js.snap | 25 ------ ui_framework/components/button/button.js | 82 +++++++++++-------- 2 files changed, 48 insertions(+), 59 deletions(-) diff --git a/ui_framework/components/button/__snapshots__/button.test.js.snap b/ui_framework/components/button/__snapshots__/button.test.js.snap index 3116ce13a5977..e8b18fe774934 100644 --- a/ui_framework/components/button/__snapshots__/button.test.js.snap +++ b/ui_framework/components/button/__snapshots__/button.test.js.snap @@ -12,8 +12,6 @@ exports[`KuiButton Baseline is rendered 1`] = ` @@ -23,8 +21,6 @@ exports[`KuiButton Props children is rendered 1`] = ` @@ -50,8 +44,6 @@ exports[`KuiButton Props href changes the element from a button to an anchor tag className="kuiButton" href="#" onClick={[Function]} - type={null} - value={null} > @@ -61,8 +53,6 @@ exports[`KuiButton Props icon is rendered with children 1`] = ` @@ -102,8 +88,6 @@ exports[`KuiButton Props isIconOnRight moves the icon to the right 1`] = ` @@ -184,8 +161,6 @@ exports[`KuiButton Props testSubject is rendered 1`] = ` className="kuiButton" data-test-subj="test subject string" onClick={[Function]} - type={null} - value={null} > diff --git a/ui_framework/components/button/button.js b/ui_framework/components/button/button.js index d7638dd98aa59..dfda502cf2225 100644 --- a/ui_framework/components/button/button.js +++ b/ui_framework/components/button/button.js @@ -8,20 +8,6 @@ import keyMirror from 'keymirror'; import { KuiLoadingButtonIcon } from './button_icon'; const KuiButton = props => { - if (props.isSubmit) { - // input is a void element tag and can't have children. - if ((props.children && typeof props.children !== 'string') || props.icon) { - throw new Error( - `KuiButton with isSubmit prop set to true can only accept string children, and can't - display icons. This is because input is a void element and can't contain children.` - ); - } - } - - function onClick() { - props.onClick(props.data); - } - const icon = props.isLoading ? @@ -41,17 +27,39 @@ const KuiButton = props => { ); } - const elementType = - props.isSubmit - ? 'input' - : props.href - ? 'a' - : 'button'; + // onClick is optional, so don't even call it if it's not passed in, or if we're disabled. + const onClick = + props.onClick && !props.isDisabled + ? () => props.onClick(props.data) + : () => {}; + + const baseProps = { + 'data-test-subj': props.testSubject, + className, + disabled: props.isDisabled, + onClick, + }; + + if (props.isSubmit) { + // input is a void element tag and can't have children. + if ((props.children && typeof props.children !== 'string') || props.icon) { + throw new Error( + `KuiButton with isSubmit prop set to true can only accept string children, and can't + display icons. This is because input is a void element and can't contain children.` + ); + } + + return ( + + ); + } const children = - props.isSubmit - ? null - : props.isIconOnRight + props.isIconOnRight ? ( {wrappedChildren} @@ -64,17 +72,23 @@ const KuiButton = props => { ); - return React.createElement(elementType, { - 'data-test-subj': props.testSubject, - className, - href: props.href, - target: props.target, - type: props.isSubmit ? 'submit' : null, - disabled: props.isDisabled, - // onClick is optional, so don't even call it if it's not passed in, or if we're disabled. - onClick: props.onClick && !props.isDisabled ? onClick : () => {}, - value: props.isSubmit ? props.children : null, - }, children); + if (props.href) { + return ( + + {children} + + ); + } + + return ( + + ); }; KuiButton.propTypes = {