UNPKG

@nutui/nutui-react-taro

Version:

京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序

157 lines (156 loc) 7.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "Button", { enumerable: true, get: function() { return Button; } }); var _interop_require_default = require("@swc/helpers/_/_interop_require_default"); var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard"); var _define_property = require("@swc/helpers/_/_define_property"); var _object_spread = require("@swc/helpers/_/_object_spread"); var _object_spread_props = require("@swc/helpers/_/_object_spread_props"); var _object_without_properties = require("@swc/helpers/_/_object_without_properties"); var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react")); var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames")); var _components = require("@tarojs/components"); var _iconsreacttaro = require("@nutui/icons-react-taro"); var _typings = require("../../utils/typings"); var _platform = require("../../utils/taro/platform"); var prefixCls = 'nut-button'; var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), { color: '', type: 'default', size: 'normal', shape: 'round', fill: 'outline', loading: false, disabled: false, block: false, icon: null, rightIcon: null, onClick: function onClick(e) {} }); var Button = /*#__PURE__*/ _react.default.forwardRef(function(props, ref) { var _$_object_spread = (0, _object_spread._)({}, defaultProps, props), color = _$_object_spread.color, shape = _$_object_spread.shape, fill = _$_object_spread.fill, loading = _$_object_spread.loading, disabled = _$_object_spread.disabled, type = _$_object_spread.type, size = _$_object_spread.size, block = _$_object_spread.block, icon = _$_object_spread.icon, rightIcon = _$_object_spread.rightIcon, children = _$_object_spread.children, className = _$_object_spread.className, style = _$_object_spread.style, nativeType = _$_object_spread.nativeType, description = _$_object_spread.description, onClick = _$_object_spread.onClick, rest = (0, _object_without_properties._)(_$_object_spread, [ "color", "shape", "fill", "loading", "disabled", "type", "size", "block", "icon", "rightIcon", "children", "className", "style", "nativeType", "description", "onClick" ]); var mappedSize = (0, _react.useMemo)(function() { if (!size) return 'normal'; var sizeMap = { '48': 'xlarge', '40': 'large', '32': 'normal', '28': 'small', '24': 'mini' }; return sizeMap[size] || size; }, [ size ]); var getStyle = (0, _react.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 = 'var(--nutui-color-primary-text, #fff)'; if ((0, _platform.harmony)()) { style.backgroundColor = color; } style.background = color; style.borderColor = 'transparent'; } } return style; }, [ color, props.fill ]); var getContStyle = (0, _react.useMemo)(function() { var style = {}; if (props.color) { if (props.fill === 'outline' || props.fill === 'dashed') { style.color = color; } else { style.color = 'var(--nutui-color-primary-text, #fff)'; style.background = 'transparent'; style.borderColor = 'transparent'; } } return style; }, [ color, props.fill, props.color ]); var handleClick = (0, _react.useCallback)(function(e) { if (!loading && !disabled && onClick) { onClick(e); } }, [ loading, disabled, onClick ]); var _obj; var buttonClassNames = (0, _classnames.default)(prefixCls, "".concat(prefixCls, "-").concat(type), (_obj = {}, (0, _define_property._)(_obj, "".concat(prefixCls, "-").concat(type, "-solid"), type === 'primary' && !props.fill), (0, _define_property._)(_obj, "".concat(prefixCls, "-").concat(fill), props.fill), (0, _define_property._)(_obj, "".concat(prefixCls, "-").concat(type, "-").concat(fill), props.fill), (0, _define_property._)(_obj, "".concat(prefixCls, "-").concat(mappedSize), mappedSize), (0, _define_property._)(_obj, "".concat(prefixCls, "-has-desc"), !!description), (0, _define_property._)(_obj, "".concat(prefixCls, "-").concat(shape), shape), (0, _define_property._)(_obj, "".concat(prefixCls, "-block"), block), (0, _define_property._)(_obj, "".concat(prefixCls, "-disabled"), disabled || loading), (0, _define_property._)(_obj, "".concat(prefixCls, "-").concat(type).concat(props.fill ? "-".concat(fill) : '', "-disabled"), disabled || loading), (0, _define_property._)(_obj, "".concat(prefixCls, "-loading"), loading), (0, _define_property._)(_obj, "".concat(prefixCls, "-icononly"), !children), _obj), className); // if (getEnv() === 'WEB') { // ;(rest as any).type = formType // } return(// <TaroButton> // @ts-ignore /*#__PURE__*/ /*#__PURE__*/ _react.default.createElement(_components.View, (0, _object_spread_props._)((0, _object_spread._)({}, rest), { ref: ref, // formType={formType || nativeType} className: buttonClassNames, style: (0, _object_spread._)({}, getStyle, style), ariaRole: "button", role: "button", ariaDisabled: disabled || loading, "aria-disabled": disabled || loading, ariaBusy: loading, "aria-busy": loading, onClick: function onClick(e) { return handleClick(e); } }), /*#__PURE__*/ _react.default.createElement(_components.View, { className: "nut-button-wrap" }, loading && /*#__PURE__*/ _react.default.createElement(_iconsreacttaro.Loading, { className: "nut-icon-loading", ariaHidden: true, "aria-hidden": true }), !loading && icon, children && /*#__PURE__*/ _react.default.createElement(_components.View, { className: (0, _classnames.default)('nut-button-children', "nut-button-".concat(mappedSize, "-children"), "nut-button-".concat(type, "-children"), { 'nut-button-text': icon || loading, 'nut-button-text-right': rightIcon }), style: (0, _platform.harmony)() ? getContStyle : {} }, description ? /*#__PURE__*/ _react.default.createElement(_components.View, { className: "nut-button-title" }, children) : children, description && /*#__PURE__*/ _react.default.createElement(_components.View, { className: "nut-button-desc" }, description)), rightIcon))); }); Button.displayName = 'NutButton';