UNPKG

zmp-react

Version:

Build full featured iOS & Android apps using ZMP & React

100 lines (87 loc) 2.91 kB
import _extends from "@babel/runtime/helpers/extends"; import React, { forwardRef, useRef, useImperativeHandle } from 'react'; import { classNames, getExtraAttrs, extend, emit } from '../shared/utils'; import { colorClasses } from '../shared/mixins'; import { useTooltip } from '../shared/use-tooltip'; import { useTheme } from '../shared/use-theme'; var Icon = /*#__PURE__*/forwardRef(function (props, ref) { var theme = useTheme(); var className = props.className, id = props.id, style = props.style, children = props.children, material = props.material, zmp = props.zmp, icon = props.icon, md = props.md, ios = props.ios, aurora = props.aurora, size = props.size, onClick = props.onClick; var extraAttrs = getExtraAttrs(props); var elRef = useRef(null); useImperativeHandle(ref, function () { return { el: elRef.current }; }); useTooltip(elRef, props); var getClasses = function getClasses() { var classes = { icon: true }; var themeIcon; if (theme && theme.ios) themeIcon = ios;else if (theme && theme.md) themeIcon = md;else if (theme && theme.aurora) themeIcon = aurora; if (themeIcon) { var parts = themeIcon.split(':'); var prop = parts[0]; var value = parts[1]; if (prop === 'material' || prop === 'zmp') { classes['material-icons'] = prop === 'material'; classes['zmp-icons'] = prop === 'zmp'; } if (prop === 'icon') { classes[value] = true; } } else { classes = { icon: true, 'material-icons': material, 'zmp-icons': zmp }; if (icon) classes[icon] = true; } return classNames(className, classes, colorClasses(props)); }; var getIconText = function getIconText() { var text = material || zmp; if (md && theme && theme.md && (md.indexOf('material:') >= 0 || md.indexOf('zmp:') >= 0)) { text = md.split(':')[1]; } else if (ios && theme && theme.ios && (ios.indexOf('material:') >= 0 || ios.indexOf('zmp:') >= 0)) { text = ios.split(':')[1]; } else if (aurora && theme && theme.aurora && (aurora.indexOf('material:') >= 0 || aurora.indexOf('zmp:') >= 0)) { text = aurora.split(':')[1]; } return text; }; var sizeComputed = size; if (typeof size === 'number' || parseFloat(size) === size * 1) { sizeComputed = size + "px"; } var handleOnClick = function handleOnClick(e) { emit(props, 'click', e); }; return /*#__PURE__*/React.createElement("i", _extends({ id: id, style: extend({ fontSize: sizeComputed, width: sizeComputed, height: sizeComputed }, style), className: getClasses(), ref: elRef, onClick: onClick ? handleOnClick : undefined }, extraAttrs), getIconText(), children); }); Icon.displayName = 'zmp-icon'; export default Icon;