UNPKG

zmp-react

Version:

Build full featured iOS & Android apps using ZMP & React

186 lines (166 loc) 5.44 kB
import _extends from "@babel/runtime/helpers/extends"; import React, { useRef, useImperativeHandle, forwardRef, useContext, useMemo } from 'react'; import { validateUrl } from '../../common/utils'; import { zmp } from '../shared/zmp'; import { classNames, getExtraAttrs, isStringProp } 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 '../components/badge'; import { useSmartSelect } from '../shared/use-smart-select'; /* dts-imports import { SmartSelect } from 'zmp-core/types'; import { Icon } from 'zmp-core/types'; */ /* dts-props id?: string | number; className?: string; style?: React.CSSProperties; noLinkClass? : boolean text? : string tabLink? : boolean | string tabLinkActive? : boolean tabbarLabel? : boolean iconOnly? : boolean badge? : string | number badgeColor? : string iconBadge? : string | number href? : string | boolean tooltip? : string tooltipTrigger? : string COLOR_PROPS ICON_PROPS ACTIONS_PROPS ROUTER_PROPS onClick? : (event?: any) => void ref?: React.MutableRefObject<{el: HTMLElement | null}>; iconZMP: Icon.IconZMP CHILDREN_PROP */ var ZMPLink = /*#__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, onClick = props.onClick; var tabbarContext = useContext(TabbarContext); var isTabbarLabel = tabbarLabel || tabbarContext.tabbarHasLabels; var extraAttrs = getExtraAttrs(props); var elRef = useRef(null); 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 linkTarget = target; var url = href; var disabledLink = false; var _zmp$routes = zmp.routes, routes = _zmp$routes === void 0 ? [] : _zmp$routes; var paths = useMemo(function () { return routes.map(function (route) { return route.path; }); }, []); if (url && url !== '#' && !validateUrl(url, paths)) { url = '#'; disabledLink = true; } 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, disabled: disabledLink }, colorClasses(props), routerClasses(props), actionsClasses(props)); var attrs = _extends({ href: '#', target: linkTarget, 'data-tab': isStringProp(tabLink) && tabLink || undefined }, routerAttrs(props), actionsAttrs(props)); var handleClick = function handleClick(e) { e.preventDefault(); var options = {}; var routeProps = props.routeProps, reloadCurrent = props.reloadCurrent, reloadPrevious = props.reloadPrevious, reloadAll = props.reloadAll, animate = props.animate, ignoreCache = props.ignoreCache, transition = props.transition; if (routeProps !== undefined) { options.props = routeProps; } if (reloadCurrent !== undefined) { options.reloadCurrent = reloadCurrent; } if (reloadPrevious !== undefined) { options.reloadPrevious = reloadPrevious; } if (reloadAll !== undefined) { options.reloadAll = reloadAll; } if (animate !== undefined) { options.animate = animate; } if (ignoreCache !== undefined) { options.ignoreCache = ignoreCache; } if (transition !== undefined) { options.transition = transition; } zmp.views.current.router.navigate(href, options); }; return /*#__PURE__*/React.createElement("a", _extends({ ref: elRef, id: id, style: style, className: classes }, attrs, extraAttrs, { onClick: url && url !== '#' || href && href !== '#' ? handleClick : onClick }), iconEl, textEl, children); }); ZMPLink.displayName = 'zmp-link'; export default ZMPLink;