@zohodesk/dot
Version:
In this Library, we Provide Some Basic Components to Build Your Application
104 lines (91 loc) • 2.59 kB
JavaScript
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 /*#__PURE__*/React.createElement("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);
}
}
Link.contextType = LinkContext;
Link.defaultProps = defaultProps;
Link.propTypes = propTypes; // if (__DOCS__) {
// Link.docs = {
// componentGroup: 'Atom'
// };
// }