zmp-react
Version:
Build full featured iOS & Android apps using ZMP & React
103 lines (92 loc) • 3.34 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import React, { forwardRef, useRef, useImperativeHandle, useContext } from 'react';
import { classNames, getExtraAttrs, isStringProp, emit } from '../shared/utils';
import { colorClasses, routerAttrs, routerClasses, actionsAttrs, actionsClasses } from '../shared/mixins';
import { useIcon } from '../shared/use-icon';
import { useRouteProps } from '../shared/use-route-props';
import { useTooltip } from '../shared/use-tooltip';
import { TabbarContext } from '../shared/tabbar-context';
import Badge from './badge';
import { useSmartSelect } from '../shared/use-smart-select';
var Link = /*#__PURE__*/forwardRef(function (props, ref) {
var _zmpSmartSelect = useRef(null);
var className = props.className,
id = props.id,
style = props.style,
children = props.children,
noLinkClass = props.noLinkClass,
text = props.text,
tabLink = props.tabLink,
tabLinkActive = props.tabLinkActive,
tabbarLabel = props.tabbarLabel,
iconOnly = props.iconOnly,
badge = props.badge,
badgeColor = props.badgeColor,
_props$href = props.href,
href = _props$href === void 0 ? '#' : _props$href,
target = props.target,
smartSelect = props.smartSelect,
smartSelectParams = props.smartSelectParams;
var tabbarContext = useContext(TabbarContext);
var isTabbarLabel = tabbarLabel || tabbarContext.tabbarHasLabels;
var extraAttrs = getExtraAttrs(props);
var elRef = useRef(null);
var onClick = function onClick(e) {
emit(props, 'click', e);
};
useImperativeHandle(ref, function () {
return {
el: elRef.current,
zmpSmartSelect: function zmpSmartSelect() {
return _zmpSmartSelect.current;
}
};
});
useTooltip(elRef, props);
useRouteProps(elRef, props);
useSmartSelect(smartSelect, smartSelectParams, _zmpSmartSelect, function () {
return elRef.current;
});
var textEl;
var badgeEl;
if (text) {
if (badge) badgeEl = /*#__PURE__*/React.createElement(Badge, {
color: badgeColor
}, badge);
textEl = /*#__PURE__*/React.createElement("span", {
className: isTabbarLabel ? 'tabbar-label' : ''
}, text, badgeEl);
}
var iconEl = useIcon(props);
var iconOnlyComputed;
if (iconOnly || !text && children && children.length === 0 || !text && !children) {
iconOnlyComputed = true;
} else {
iconOnlyComputed = false;
}
var classes = classNames(className, {
link: !(noLinkClass || isTabbarLabel),
'icon-only': iconOnlyComputed,
'tab-link': tabLink || tabLink === '',
'tab-link-active': tabLinkActive,
'smart-select': smartSelect
}, colorClasses(props), routerClasses(props), actionsClasses(props));
var hrefComputed = href;
if (href === true) hrefComputed = '#';
if (href === false) hrefComputed = undefined; // no href attribute
var attrs = _extends({
href: hrefComputed,
target: target,
'data-tab': isStringProp(tabLink) && tabLink || undefined
}, routerAttrs(props), actionsAttrs(props));
return /*#__PURE__*/React.createElement("a", _extends({
ref: elRef,
id: id,
style: style,
className: classes
}, attrs, extraAttrs, {
onClick: onClick
}), iconEl, textEl, children);
});
Link.displayName = 'zmp-link';
export default Link;