@nutui/nutui-react
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
87 lines (86 loc) • 4.33 kB
JavaScript
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import React, { useCallback, useMemo } from "react";
import classNames from "classnames";
import { Loading } from "@nutui/icons-react";
import { ComponentDefaults } from "../../utils/typings";
var prefixCls = 'nut-button';
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
color: '',
type: 'default',
size: 'normal',
shape: 'round',
fill: 'outline',
loading: false,
disabled: false,
block: false,
icon: null,
rightIcon: null,
nativeType: 'button',
onClick: function() {}
});
export var Button = /*#__PURE__*/ React.forwardRef(function(props, ref) {
var _ref = _object_spread({}, defaultProps, props), color = _ref.color, shape = _ref.shape, fill = _ref.fill, loading = _ref.loading, disabled = _ref.disabled, type = _ref.type, size = _ref.size, block = _ref.block, icon = _ref.icon, rightIcon = _ref.rightIcon, children = _ref.children, className = _ref.className, style = _ref.style, nativeType = _ref.nativeType, onClick = _ref.onClick, rest = _object_without_properties(_ref, [
"color",
"shape",
"fill",
"loading",
"disabled",
"type",
"size",
"block",
"icon",
"rightIcon",
"children",
"className",
"style",
"nativeType",
"onClick"
]);
var getStyle = useMemo(function() {
var style = {};
if (color) {
if (props.fill === 'outline' || props.fill === 'dashed') {
style.color = color;
if (!(color === null || color === void 0 ? void 0 : color.includes('gradient'))) {
style.borderColor = color;
}
} else {
style.color = '#fff';
style.background = color;
style.borderColor = 'transparent';
}
}
return style;
}, [
color,
props.fill
]);
var handleClick = useCallback(function(e) {
if (!loading && !disabled && onClick) {
onClick(e);
}
}, [
loading,
disabled,
onClick
]);
var _obj;
var buttonClassNames = classNames(prefixCls, "".concat(prefixCls, "-").concat(type), (_obj = {}, _define_property(_obj, "".concat(prefixCls, "-").concat(type, "-solid"), type === 'primary' && !props.fill), _define_property(_obj, "".concat(prefixCls, "-").concat(fill), props.fill), _define_property(_obj, "".concat(prefixCls, "-").concat(type, "-").concat(fill), props.fill), _define_property(_obj, "".concat(prefixCls, "-").concat(size), size), _define_property(_obj, "".concat(prefixCls, "-").concat(shape), shape), _define_property(_obj, "".concat(prefixCls, "-block"), block), _define_property(_obj, "".concat(prefixCls, "-disabled"), disabled || loading), _define_property(_obj, "".concat(prefixCls, "-").concat(type).concat(props.fill ? "-".concat(fill) : '', "-disabled"), disabled || loading), _define_property(_obj, "".concat(prefixCls, "-loading"), loading), _define_property(_obj, "".concat(prefixCls, "-icononly"), !children), _obj), className);
return /*#__PURE__*/ React.createElement("button", _object_spread_props(_object_spread({}, rest), {
ref: ref,
type: nativeType,
className: buttonClassNames,
style: _object_spread({}, getStyle, style),
onClick: handleClick
}), /*#__PURE__*/ React.createElement("div", {
className: "nut-button-wrap"
}, loading && /*#__PURE__*/ React.createElement(Loading, {
className: "nut-icon-loading"
}), !loading && icon, children && /*#__PURE__*/ React.createElement("div", {
className: "".concat(props.fill || disabled || loading ? "nut-button-".concat(type).concat(props.fill ? "-".concat(fill) : '').concat(disabled || loading ? '-disabled' : '') : '').concat(icon || loading ? ' nut-button-text' : '').concat(rightIcon ? ' nut-button-text-right' : '')
}, children), rightIcon));
});
Button.displayName = 'NutButton';