# All files / components/Anchor Anchor.js

4.65% Statements 2/43
0% Branches 0/36
0% Functions 0/10
4.65% Lines 2/43

    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
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221

    5x

    5x

     import React from 'react';
    import PropTypes from 'prop-types';
    import { withProps } from 'recompose';
    import { Route } from 'react-router-dom';
    import DefaultExternalContentAnchor from './styles/ExternalContentAnchor';
    import DefaultExternalIconAnchor from './styles/ExternalIconAnchor';
    import DefaultExternalTextAnchor from './styles/ExternalTextAnchor';
    // 'internal' anchors link to in-app pages, not external URLs
    import DefaultInternalContentAnchor from './styles/InternalContentAnchor';
    import DefaultInternalTextAnchor from './styles/InternalTextAnchor';
    import DefaultInternalIconAnchor from './styles/InternalIconAnchor';

    const inferType = children => {
      if (children === null) {
        return 'wrap';
      }

      if (typeof children === 'string') {
        return 'text';
      }

      if (children.type && children.type.displayName === 'Icon') {
        return 'icon';
      }

      return 'wrap';
    };

    class Anchor extends React.Component {
      static propTypes = {
        /**
         * A location to link to with this anchor.
         */
        to: PropTypes.string,
        /**
         * Rendered contents inside the anchor.
         */
        children: PropTypes.node,
        /**
         * Anchors pass an 'active' prop to their children when the route matches. This controls whether that is exact matching.
         */
        exact: PropTypes.bool,
        /**
         * A handler for the anchor's onclick event.
         */
        onClick: PropTypes.func,
        /**
         * whether the contents should be treated as text (underlined),
         * icon (has specific styling), or 'content' (no styling).
         */
        type: PropTypes.oneOf(['text', 'icon', 'wrap', 'content']),
        /**
         * Use to override the automatic inference of whether the URL is external or internal
         */
        external: PropTypes.bool,
        /**
         * A class name to pass to the <a> element.
         */
        className: PropTypes.string,
        /**
         * An id to pass to the <a> element.
         */
        id: PropTypes.string,
        /**
         * Opens the link in a new tab.
         */
        newTab: PropTypes.bool,
        /**
         * Renders a text anchor linking to an external site
         */
        ExternalTextAnchor: PropTypes.func,
        /**
         * Renders an icon anchor linking to an external site
         */
        ExternalIconAnchor: PropTypes.func,
        /**
         * Renders a content (image, element) anchor linking to an external site
         */
        ExternalContentAnchor: PropTypes.func,
        /**
         * Renders a text anchor linking to a page in-app
         */
        InternalTextAnchor: PropTypes.func,
        /**
         * Renders an icon anchor linking to a page in-app
         */
        InternalIconAnchor: PropTypes.func,
        /**
         * Renders a content (image, element) anchor linking to a page in-app
         */
        InternalContentAnchor: PropTypes.func,
      };

      static defaultProps = {
        to: '#',
        children: null,
        exact: false,
        onClick: () => null,
        type: null,
        className: null,
        id: null,
        newTab: false,
        external: undefined,
        ExternalTextAnchor: DefaultExternalTextAnchor,
        ExternalIconAnchor: DefaultExternalIconAnchor,
        ExternalContentAnchor: DefaultExternalContentAnchor,
        InternalTextAnchor: DefaultInternalTextAnchor,
        InternalIconAnchor: DefaultInternalIconAnchor,
        InternalContentAnchor: DefaultInternalContentAnchor,
      };

      isExternal = () => {
        const { external, to } = this.props;
        if (external !== undefined) {
          return external;
        }
        return /^(https?:)*\/\//.test(to);
      };

      getComponentType = () => {
        const {
          type,
          external,
          children,
          to,
          ExternalTextAnchor,
          ExternalIconAnchor,
          ExternalContentAnchor,
          InternalTextAnchor,
          InternalIconAnchor,
          InternalContentAnchor,
        } = this.props;
        const finalType = type || inferType(children);
        const finalExternal = this.isExternal();
        switch (finalType) {
          case 'wrap':
          case 'content':
            return finalExternal ? ExternalContentAnchor : InternalContentAnchor;
          case 'icon':
            return finalExternal ? ExternalIconAnchor : InternalIconAnchor;
          default:
            return finalExternal ? ExternalTextAnchor : InternalTextAnchor;
        }
      };

      handleClick = event => {
        const { onClick, to } = this.props;
        // if the user isn't using this link to navigate,
        // prevent default navigation
        if (onClick && to === '#') {
          event.preventDefault();
          onClick(event);
        } else if (onClick) {
          // still do onClick event if navigation occurs
          onClick(event);
        }
      };

      // adds all non-children props to children
      childrenWithProps = extraProps => {
        const { children } = this.props;
        if (!children) {
          return null;
        }
        if (typeof children === 'string') {
          return children;
        }
        return React.Children.map(children, child =>
          React.cloneElement(child, extraProps),
        );
      };

      // provides extra properties based on certain factors to the underlying element
      extraProps = () => {
        const { to, newTab } = this.props;
        const newTabProps = newTab ? { rel: 'noopener', target: '_blank' } : {};
        if (this.isExternal()) {
          return {
            href: to,
            ...newTabProps,
          };
        } else {
          return {
            to,
            ...newTabProps,
          };
        }
      };

      render() {
        const { to, exact, children, className, id } = this.props;
        const Component = this.getComponentType();

        return (
          <Route
            path={to}
            exact={exact}
            children={({ match }) => (
              <Component
                {...this.extraProps()}
                onClick={this.handleClick}
                className={className}
                id={id}
              >
                {this.childrenWithProps({ active: !!match })}
              </Component>
            )}
          />
        );
      }
    }

    Anchor.Danger = withProps({
      ExternalTextAnchor: DefaultExternalTextAnchor.Danger,
      ExternalIconAnchor: DefaultExternalIconAnchor.Danger,
      InternalTextAnchor: DefaultInternalTextAnchor.Danger,
      InternalIconAnchor: DefaultInternalIconAnchor.Danger,
    })(Anchor);

    export default Anchor;