zmp-react
Version:
Build full featured iOS & Android apps using ZMP & React
100 lines (88 loc) • 3.06 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import React, { forwardRef, useRef, useImperativeHandle } from 'react';
import { useIsomorphicLayoutEffect } from '../shared/use-isomorphic-layout-effect';
import { classNames, getExtraAttrs, getSlots, emit } from '../shared/utils';
import { colorClasses, routerAttrs, routerClasses, actionsClasses, actionsAttrs } from '../shared/mixins';
import { useRouteProps } from '../shared/use-route-props';
import { useIcon } from '../shared/use-icon';
import { zmpready, zmp } from '../shared/zmp';
import { useTooltip } from '../shared/use-tooltip';
var MenuItem = /*#__PURE__*/forwardRef(function (props, ref) {
var className = props.className,
id = props.id,
style = props.style,
link = props.link,
href = props.href,
target = props.target,
text = props.text,
dropdown = props.dropdown,
iconOnly = props.iconOnly;
var extraAttrs = getExtraAttrs(props);
var elRef = useRef(null);
var onClick = function onClick(e) {
emit(props, 'click', e);
};
var onOpened = function onOpened(el) {
if (elRef.current !== el) return;
emit(props, 'menuOpened', el);
};
var onClosed = function onClosed(el) {
if (elRef.current !== el) return;
emit(props, 'menuClosed', el);
};
useImperativeHandle(ref, function () {
return {
el: elRef.current
};
});
useTooltip(elRef, props);
useRouteProps(elRef, props);
var attachEvents = function attachEvents() {
zmpready(function () {
zmp.on('menuOpened', onOpened);
zmp.on('menuClosed', onClosed);
});
};
var detachEvents = function detachEvents() {
zmp.off('menuOpened', onOpened);
zmp.off('menuClosed', onOpened);
};
useIsomorphicLayoutEffect(function () {
attachEvents();
return detachEvents;
});
var iconEl = useIcon(props);
var slots = getSlots(props);
var iconOnlyComputed;
if (iconOnly || !text && slots.text && slots.text.length === 0 || !text && !slots.text) {
iconOnlyComputed = true;
} else {
iconOnlyComputed = false;
}
var isLink = link || href || href === '';
var Tag = isLink ? 'a' : 'div';
var isDropdown = dropdown || dropdown === '';
var classes = classNames({
'menu-item': true,
'menu-item-dropdown': isDropdown,
'icon-only': iconOnlyComputed
}, className, colorClasses(props), routerClasses(props), actionsClasses(props));
var hrefComputed = href;
if (typeof hrefComputed === 'undefined' && link) hrefComputed = '#';
var attrs = _extends({
href: hrefComputed,
target: target
}, routerAttrs(props), actionsAttrs(props));
return /*#__PURE__*/React.createElement(Tag, _extends({
ref: elRef,
className: classes,
id: id,
style: style
}, attrs, extraAttrs, {
onClick: onClick
}), (text || slots.text && slots.text.length || iconEl) && /*#__PURE__*/React.createElement("div", {
className: "menu-item-content"
}, text, iconEl, slots.text), slots.default);
});
MenuItem.displayName = 'zmp-menu-item';
export default MenuItem;