diff --git a/src/components/button/button.tsx b/src/components/button/button.tsx index 3f744ff4787f..0652e58da6a5 100644 --- a/src/components/button/button.tsx +++ b/src/components/button/button.tsx @@ -102,17 +102,16 @@ export interface EuiButtonProps extends CommonProps { } export interface EuiButtonDisplayProps extends EuiButtonProps { - element: 'a' | 'button' | 'label'; + element: keyof JSX.IntrinsicElements; } /** * * *INTERNAL ONLY* Component for displaying any element as a button + * EuiButton is largely responsible for providing relevant props + * and the logic for element-specific attributes */ -const EuiButtonDisplay = React.forwardRef< - HTMLAnchorElement | HTMLButtonElement | HTMLLabelElement, - EuiButtonDisplayProps ->( +const EuiButtonDisplay = React.forwardRef( ( { children, @@ -127,14 +126,11 @@ const EuiButtonDisplay = React.forwardRef< contentProps, textProps, fullWidth, - element: Element = 'button', + element = 'button', ...rest }, ref ) => { - // If in the loading state, force disabled to true - isDisabled = isLoading ? true : isDisabled; - const classes = classNames( 'euiButton', color ? colorToClassNameMap[color] : null, @@ -170,10 +166,14 @@ const EuiButtonDisplay = React.forwardRef< ); - return ( - - {innerNode} - + return React.createElement( + element, + { + className: classes, + ref, + ...rest, + }, + innerNode ); } ); @@ -210,11 +210,17 @@ export const EuiButton: FunctionComponent = ({ buttonRef, ...rest }) => { - const buttonIsDisabled = isDisabled || disabled; - // elements don't respect the `disabled` attribute. So if we're disabled, we'll just pretend - // this is a button and piggyback off its disabled styles. + const buttonIsDisabled = rest.isLoading || isDisabled || disabled; const element = href && !isDisabled ? 'a' : 'button'; + let elementProps = {}; + // Props for all elements + elementProps = { ...elementProps, isDisabled: buttonIsDisabled }; + // Element-specific attributes + if (element === 'button') { + elementProps = { ...elementProps, disabled: buttonIsDisabled }; + } + const relObj: { rel?: string; href?: string; @@ -233,10 +239,10 @@ export const EuiButton: FunctionComponent = ({ return ( } - {...rest as HTMLAttributes} + ref={buttonRef} + {...rest} /> ); };