All files / elements/typography/Link component.js

100% Statements 11/11
100% Branches 4/4
100% Functions 1/1
100% Lines 11/11

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              10x           21x 21x 21x 1x       1x           20x 1x           19x             10x                       10x        
import React from 'react';
import PropTypes from 'prop-types';
import { removeSomeProps } from 'src/utils/componentHelpers';
import { Link as ReactRouterLink } from 'react-router-dom';
import { spaceProps, typography } from 'src/utils/styledHelpers';
import { emailHrefString } from './utils';
 
export const LinkComponent = ({
  children,
  emailHref,
  to,
  ...props
}) => {
  const trimmedProps = removeSomeProps(props, Object.keys({ ...spaceProps.propTypes, ...typography.propTypes }));
  const emailLinkHref = emailHrefString(emailHref);
  if (to) {
    const linkProps = {
      to,
      ...trimmedProps,
    };
    return (
      <ReactRouterLink {...linkProps}>
        {children}
      </ReactRouterLink>
    );
  }
  if (emailLinkHref) {
    return (
      <a href={emailLinkHref} {...trimmedProps}>
        {children}
      </a>
    );
  }
  return (
    <a {...trimmedProps}>
      {children}
    </a>
  );
};
 
LinkComponent.propTypes = {
  children: PropTypes.any.isRequired,
  emailHref: PropTypes.shape({
    bcc: PropTypes.string,
    body: PropTypes.string,
    cc: PropTypes.string,
    subject: PropTypes.string,
    to: PropTypes.string,
  }),
  to: PropTypes.string,
};
 
LinkComponent.defaultProps = {
  emailHref: {},
  to: '',
};