@zohodesk/dot
Version:
In this Library, we Provide Some Basic Components to Build Your Application
93 lines (82 loc) • 2.28 kB
JavaScript
import React, { useContext, useRef } from 'react';
import { defaultProps } from "./props/defaultProps";
import { propTypes } from "./props/propTypes";
import { cancelBubblingEffect } from '@zohodesk/components/es/utils/Common';
import LinkContext from "../../Link/LinkContext";
import style from "../../Link/Link.module.css";
export default function Link(props) {
let {
children,
href,
target,
urlData,
urlName,
className,
title,
download,
rel,
dataId,
customProps,
ariaLabel,
hasReload,
onClick
} = props;
const {
options
} = useContext(LinkContext);
const {
isLink,
constructURL,
onCallBack
} = options;
let urlOutput = href ? href : constructURL(urlName, urlData);
let option = useRef({});
if (download) {
option.current.download = true;
}
let ignoreKeys = ['children', 'href', 'target', 'urlData', 'urlName', 'className', 'title', 'download', 'rel', 'dataId', 'hasReload', 'customProps', 'option', 'ariaLabel'];
let others = Object.keys(props).filter(key => ignoreKeys.indexOf(key) == -1).reduce((res, key) => {
res[key] = props[key];
return res;
}, {});
function callBack(e) {
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(e);
}
function addHttp(url) {
/* eslint-disable */
if (!/^(f|ht)tps?:\/\//i.test(url)) {
url = 'http://' + url;
}
/* eslint-enable */
return url;
} //urlOutput = 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.current,
rel: rel,
"data-id": dataId,
"data-test-id": dataId,
...others,
onClick: callBack,
...customProps,
"aria-label": ariaLabel
}, children);
}
Link.defaultProps = defaultProps;
Link.propTypes = propTypes; // if (__DOCS__) {
// Link.docs = {
// componentGroup: 'Atom'
// };
// }