All files / src/Link Link.js

56.66% Statements 17/30
33.33% Branches 10/30
66.66% Functions 4/6
56.66% Lines 17/30

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                  32x 32x                                                         32x 32x 32x 32x 32x 2x   32x                               32x 219x   11x 11x       32x                                         35x   35x 35x              
import React from 'react';
import { defaultProps } from './props/defaultProps';
import { propTypes } from './props/propTypes';
import { cancelBubblingEffect } from '@zohodesk/components/es/utils/Common';
import style from './Link.module.css';
import LinkContext from './LinkContext';
 
export default class Link extends React.Component {
  constructor(props, context) {
    super(props, context);
    this.onCallBack = this.onCallBack.bind(this);
  }
 
  onCallBack(urlOutput, href, e) {
    let { onClick, hasReload, target } = this.props;
    let { onCallBack } = this.context.options;
    if (e && (e.metaKey || e.altKey || e.ctrlKey || e.shiftKey)) {
      cancelBubblingEffect(e);
      return;
    }
    onClick && onClick(e);
    if (!hasReload && target != '_blank') {
      e && e.preventDefault();
    }
    cancelBubblingEffect(e);
    !onClick && onCallBack && onCallBack(urlOutput, href, e);
  }
 
  addHttp(url) {
    /* eslint-disable */
    if (!/^(f|ht)tps?:\/\//i.test(url)) {
      url = 'http://' + url;
    }
    /* eslint-enable */
    return url;
  }
 
  render() {
    let { children, href, target, urlData, urlName, className, title, download, rel, dataId, customProps, ariaLabel } =
      this.props;
    let { isLink, constructURL } = this.context.options;
    let urlOutput = href ? href : constructURL(urlName, urlData);
    let option = {};
    if (download) {
      option.download = true;
    }
    let ignoreKeys = [
      'children',
      'href',
      'target',
      'urlData',
      'urlName',
      'className',
      'title',
      'download',
      'rel',
      'dataId',
      'hasReload',
      'customProps',
      'option',
      'ariaLabel'
    ];
    let others = Object.keys(this.props)
      .filter((key) => ignoreKeys.indexOf(key) == -1)
      .reduce((res, key) => {
        res[key] = this.props[key];
        return res;
      }, {});
 
    //urlOutput = this.addHttp(urlOutput);
    return (
      <a
        href={isLink && urlOutput ? urlOutput : 'javascript:void(0);'}
        target={target}
        data-title={title}
        className={`${style.container}  ${className ? className : ''}`}
        {...option}
        rel={rel}
        data-id={dataId}
        data-test-id={dataId}
        {...others}
        onClick={this.onCallBack.bind(this, urlOutput, href)}
        {...customProps}
        aria-label={ariaLabel}
      >
        {children}
      </a>
    );
  }
}
 
Link.contextType = LinkContext;
 
Link.defaultProps = defaultProps;
Link.propTypes = propTypes;
 
// if (__DOCS__) {
//   Link.docs = {
//     componentGroup: 'Atom'
//   };
// }