# All files / components/Anchor Anchor.js

0% Statements 0/129
0% Branches 0/58
0% Functions 0/14
0% Lines 0/66

    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
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313

     import React from 'react';
    import PropTypes from 'prop-types';
    import { defaultProps } from 'recompose';
    import { Route } from 'react-router-dom';
    import userSpacing from 'extensions/userSpacing';

    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 (children.type && children.type.displayName === 'Icon') {
        return 'icon';
      }

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

      return 'wrap';
    };

    const matchReactRouter = (to, match, exact) => {
      let active;
      if (!exact) {
        // use the default behavior for non-exact matches
        active = !!match;
      } else {
        // when we expect an exact match, dont allow react-router null
        // value, additionally verify special chars are used
        active = match == null ? false : match.url === to;
      }
      return active;
    };

    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,
        /**
         * Bind a handler for the mouseEnter event
         */
        onMouseEnter: PropTypes.func,
        /**
         * Bind a handler for the mouseLeave event
         */
        onMouseLeave: 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,
        /**
         * Adds an icon to the beginning of the link and changes the styling to "icon" mode
         */
        icon: PropTypes.string,
        /**
         * This prop can be used to 'pseudo-focus' an anchor visually. Useful when
         * supporting some advanced keyboard navigation scenarios.
         */
        appearFocused: PropTypes.bool,
        /**
         * If set to true, disables the Anchor.  While disabled, the Anchor will neither react to mouseover events
         * nor will it react to onclick events.
         */
        disabled: PropTypes.bool,
        /**
         * Specify a CSS value or an object { top, right, bottom, left } or { vertical, horizontal } to
         * control the spacing around the heading. Defaults to a large space below the element.
         */
        spacing: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
        /**
         * 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,
        /**
         * Resolves whether a link is active. Specifying custom rules for whether
         * a link is active may be necessary when using custom onClick logic
         */
        active: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]),
      };

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

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

      getComponentType = () => {
        const {
          type,
          external,
          children,
          to,
          icon,
          ExternalTextAnchor,
          ExternalIconAnchor,
          ExternalContentAnchor,
          InternalTextAnchor,
          InternalIconAnchor,
          InternalContentAnchor,
        } = this.props;
        const finalType = type || (!!icon ? 'icon' : 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, disabled } = this.props;

        // Don't handle navigation if disabled
        if (disabled) return event.preventDefault();

        // 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);
        }
      };

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

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

      render() {
        const {
          to,
          exact,
          children,
          active,
          ...rest
        } = this.props;

        const Component = this.getComponentType();

        return (
          <Route
            path={to}
            exact={exact}
            children={({ match }) => (
              <Component
                {...this.extraProps()}
                onClick={this.handleClick}
                spacing={userSpacing.text(this.props)}
                {...rest}
              >
                {this.childrenWithProps({
                  active:
                    typeof active === 'function'
                      ? active(to, match, exact)
                      : !!active,
                })}
              </Component>
            )}
          />
        );
      }
    }

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

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

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

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

    export default Anchor;