All files / elements/Button component.js

100% Statements 23/23
84.62% Branches 11/13
100% Functions 2/2
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144                                          7x                         7x                           7x                   7x                     259x 259x 2x                       257x     7x           259x 259x 259x 259x 1x         258x 1x     1x         257x 1x           256x           258x   7x                       7x            
import React from 'react';
import PropTypes from 'prop-types';
import { removeSomeProps } from 'src/utils/componentHelpers';
import { Box, Flex } from 'src/elements/grid';
import {
  getProgress,
  getProgressDefaultProps,
  getProgressPropTypes,
} from 'src/elements/Progress';
import { emailHrefString } from 'src/elements/typography/Link/utils';
import { Hideable } from 'src/elements/utils';
import { boxShadow } from 'styled-system';
import { Link as ReactRouterLink } from 'react-router-dom';
import {
  flexboxProps,
  layoutProps,
  positionProps,
  spaceProps,
  typography,
} from 'src/utils/styledHelpers';
 
const propsToTrim = {
  ...flexboxProps.propTypes,
  ...layoutProps.propTypes,
  ...positionProps.propTypes,
  ...spaceProps.propTypes,
  ...typography.propTypes,
  ...getProgressPropTypes,
  innerBoxProps: PropTypes.object,
  innerFlexProps: PropTypes.object,
  leadingIcon: PropTypes.any,
  leadingIconProps: PropTypes.object,
  variant: PropTypes.string,
};
const propsToTrimForButton = {
  ...propsToTrim,
  ...boxShadow.propTypes,
  ...getProgressDefaultProps,
  bgActiveColor: PropTypes.string,
  bgHoverColor: PropTypes.string,
  contained: false,
  href: PropTypes.string,
  outline: false,
  raised: false,
  target: PropTypes.string,
  to: PropTypes.string,
};
 
const propsToTrimForLink = {
  ...propsToTrim,
  ...boxShadow.propTypes,
  bgActiveColor: PropTypes.string,
  bgHoverColor: PropTypes.string,
  contained: false,
  outline: false,
  raised: false,
};
 
const getChildren = options => {
  const {
    children,
    innerBoxProps,
    innerFlexProps,
    leadingIcon,
    leadingIconProps,
    p,
    pl,
    px,
    showProgress,
  } = options;
  if (leadingIcon || showProgress) {
    return (
      <Flex {...innerFlexProps}>
        <Box pr={pl || px || p} {...leadingIconProps}>
          <Hideable hide={showProgress}>
            {leadingIcon}
          </Hideable>
          {getProgress({ showProgress, ...options })}
        </Box>
        <Box {...innerBoxProps}>{children}</Box>
      </Flex>
    );
  }
  return <Box {...innerBoxProps}>{children}</Box>;
};
 
export const ButtonComponent = ({
  emailHref,
  href,
  to,
  ...props
}) => {
  let output = null;
  const trimmedPropsLink = removeSomeProps(props, Object.keys(propsToTrimForLink));
  const emailLinkHref = emailHrefString(emailHref);
  if (href) {
    output = (
      <a href={href} {...trimmedPropsLink}>
        {getChildren(props)}
      </a>
    );
  } else if (to) {
    const linkProps = {
      to,
    };
    output = (
      <ReactRouterLink {...trimmedPropsLink} {...linkProps}>
        {getChildren(props)}
      </ReactRouterLink>
    );
  } else if (emailLinkHref) {
    return (
      <a href={emailLinkHref} {...trimmedPropsLink}>
        {getChildren(props)}
      </a>
    );
  } else {
    output = (
      <button type="button" {...removeSomeProps(props, Object.keys(propsToTrimForButton))}>
        {getChildren(props)}
      </button>
    );
  }
  return output;
};
ButtonComponent.propTypes = {
  children: PropTypes.any,
  emailHref: PropTypes.shape({
    bcc: PropTypes.string,
    body: PropTypes.string,
    cc: PropTypes.string,
    subject: PropTypes.string,
    to: PropTypes.string,
  }),
  href: PropTypes.string,
  to: PropTypes.string,
};
ButtonComponent.defaultProps = {
  children: null,
  emailHref: {},
  href: null,
  to: null,
};