@alicloud/console-components
Version:
Alibaba Cloud React Components
68 lines (67 loc) • 3.75 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import React, { Children, forwardRef, cloneElement } from 'react';
import hoistNonReactStatics from 'hoist-non-react-statics';
import cls from 'classnames';
import { Button as FusionButton } from '@alifd/next';
import { ETheme } from '../../../enum';
import { isReactFragment, isCnChars2Or3, isCnChars4 } from '../../../utils';
import { useCssTheme } from '../../../hook';
import { hocWithThemeClass } from '../../../hoc';
import { mapTeamixIconSize } from './util';
var Button = hocWithThemeClass(forwardRef(function (props, ref) {
var _a, _b;
var className = props.className, iconSize = props.iconSize, _c = props.size, size = _c === void 0 ? 'medium' : _c;
var children = props.children;
var count = Children.count(children);
var theme = useCssTheme();
if (isReactFragment(children) && theme !== ETheme.WIND && !theme.startsWith(ETheme.XCONSOLE)) {
children = React.createElement("span", { className: "next-btn-helper" }, children);
}
// 判断是否是 2-3 个汉字
// xconsole 相关主题不需要该功能
if (theme !== ETheme.WIND && !theme.startsWith(ETheme.XCONSOLE) && typeof children === 'string' && isCnChars2Or3(children)) {
return React.createElement(FusionButton, __assign({}, __assign(__assign({}, props), { className: cls('isTwoToThreeCNCharBtn', className), ref: ref })), children);
}
// 判断是否是 4 个汉字
// xconsole 相关主题不需要该功能
if (theme !== ETheme.WIND && !theme.startsWith(ETheme.XCONSOLE) && typeof children === 'string' && isCnChars4(children)) {
return React.createElement(FusionButton, __assign({}, __assign(__assign({}, props), { className: cls('isFourCNCharBtn', className), ref: ref })), children);
}
if ((count === 1 && ((_a = children === null || children === void 0 ? void 0 : children.type) === null || _a === void 0 ? void 0 : _a.displayName) === 'Config(Icon)') || ((_b = children === null || children === void 0 ? void 0 : children.type) === null || _b === void 0 ? void 0 : _b.displayName) === 'TeamixIcon') {
return React.createElement(FusionButton, __assign({}, __assign(__assign({}, props), { className: cls('isOnlyIcon', className), ref: ref })), children);
}
var clonedChildren = Children.map(children, function (child, index) {
var _a;
var _b;
// 针对 teamix-icon 进行处理
if (child && ['function', 'object'].includes(typeof child.type) && ((_b = child.type) === null || _b === void 0 ? void 0 : _b.displayName) === 'TeamixIcon') {
var iconCls = cls((_a = {
'teamix-icon-first': count > 1 && index === 0,
'teamix-icon-last': count > 1 && index === count - 1
},
_a[child.props.className] = !!child.props.className,
_a));
return cloneElement(child, {
className: iconCls,
size: iconSize || mapTeamixIconSize(size)
});
}
return child;
});
return React.createElement(FusionButton, __assign({}, __assign(__assign({}, props), { className: className, ref: ref })), clonedChildren);
}));
hoistNonReactStatics(Button, FusionButton);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Button.displayName = FusionButton.displayName;
export default Button;