zmp-react
Version:
Build full featured iOS & Android apps using ZMP & React
148 lines (137 loc) • 4.83 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import React, { forwardRef, useRef, useImperativeHandle } from 'react';
import { classNames, getExtraAttrs, extend, isStringProp, emit } from '../shared/utils';
import { colorClasses, actionsAttrs, actionsClasses, routerAttrs, routerClasses } from '../shared/mixins';
import { useTooltip } from '../shared/use-tooltip';
import { useIcon } from '../shared/use-icon';
import { useRouteProps } from '../shared/use-route-props';
import Preloader from './preloader';
var Button = /*#__PURE__*/forwardRef(function (props, ref) {
var className = props.className,
id = props.id,
style = props.style,
children = props.children,
text = props.text,
type = props.type,
_props$href = props.href,
href = _props$href === void 0 ? '#' : _props$href,
target = props.target,
tabLink = props.tabLink,
tabLinkActive = props.tabLinkActive,
round = props.round,
roundIos = props.roundIos,
roundAurora = props.roundAurora,
roundMd = props.roundMd,
fill = props.fill,
fillIos = props.fillIos,
fillAurora = props.fillAurora,
fillMd = props.fillMd,
large = props.large,
largeIos = props.largeIos,
largeAurora = props.largeAurora,
largeMd = props.largeMd,
small = props.small,
smallIos = props.smallIos,
smallAurora = props.smallAurora,
smallMd = props.smallMd,
raised = props.raised,
raisedIos = props.raisedIos,
raisedAurora = props.raisedAurora,
raisedMd = props.raisedMd,
active = props.active,
outline = props.outline,
outlineIos = props.outlineIos,
outlineAurora = props.outlineAurora,
outlineMd = props.outlineMd,
disabled = props.disabled,
preloader = props.preloader,
preloaderSize = props.preloaderSize,
preloaderColor = props.preloaderColor,
loading = props.loading;
var extraAttrs = getExtraAttrs(props);
var elRef = useRef(null);
var onClick = function onClick(e) {
emit(props, 'click', e);
};
useImperativeHandle(ref, function () {
return {
el: elRef.current
};
});
useTooltip(elRef, props);
useRouteProps(elRef, props);
var getClasses = function getClasses() {
return classNames(className, 'button', {
'tab-link': tabLink || tabLink === '',
'tab-link-active': tabLinkActive,
'button-round': round,
'button-round-ios': roundIos,
'button-round-aurora': roundAurora,
'button-round-md': roundMd,
'button-fill': fill,
'button-fill-ios': fillIos,
'button-fill-aurora': fillAurora,
'button-fill-md': fillMd,
'button-large': large,
'button-large-ios': largeIos,
'button-large-aurora': largeAurora,
'button-large-md': largeMd,
'button-small': small,
'button-small-ios': smallIos,
'button-small-aurora': smallAurora,
'button-small-md': smallMd,
'button-raised': raised,
'button-raised-ios': raisedIos,
'button-raised-aurora': raisedAurora,
'button-raised-md': raisedMd,
'button-active': active,
'button-outline': outline,
'button-outline-ios': outlineIos,
'button-outline-aurora': outlineAurora,
'button-outline-md': outlineMd,
'button-preloader': preloader,
'button-loading': loading,
disabled: disabled
}, colorClasses(props), routerClasses(props), actionsClasses(props));
};
var ButtonTag = type === 'submit' || type === 'reset' || type === 'button' ? 'button' : 'a';
var getAttrs = function getAttrs() {
var hrefComputed = href;
if (href === true) hrefComputed = '#';
if (href === false || ButtonTag === 'button') hrefComputed = undefined; // no href attribute
return extend({
href: hrefComputed,
target: target,
type: type,
'data-tab': isStringProp(tabLink) && tabLink || undefined
}, routerAttrs(props), actionsAttrs(props));
};
var iconEl = useIcon(props);
var textEl;
if (text) {
textEl = /*#__PURE__*/React.createElement("span", null, text);
}
if (preloader) {
return /*#__PURE__*/React.createElement(ButtonTag, _extends({
ref: elRef,
id: id,
style: style,
className: getClasses()
}, getAttrs(), extraAttrs, {
onClick: onClick
}), /*#__PURE__*/React.createElement(Preloader, {
size: preloaderSize,
color: preloaderColor
}), /*#__PURE__*/React.createElement("span", null, iconEl, textEl, children));
}
return /*#__PURE__*/React.createElement(ButtonTag, _extends({
ref: elRef,
id: id,
style: style,
className: getClasses()
}, getAttrs(), extraAttrs, {
onClick: onClick
}), iconEl, textEl, children);
});
Button.displayName = 'zmp-button';
export default Button;