@alicloudfe/components
Version:
A react component library for aliyun
114 lines (113 loc) • 5.32 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);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var next_1 = require("@alifd/next");
var react_1 = __importStar(require("react"));
var hoist_non_react_statics_1 = __importDefault(require("hoist-non-react-statics"));
var classnames_1 = __importDefault(require("classnames"));
var withThemeClass_1 = require("../utils/withThemeClass");
var useCssVar_1 = require("../utils/useCssVar");
var isReactFragment_1 = __importDefault(require("../utils/isReactFragment"));
var rxTwoToThreeCNChar = /^[\u4e00-\u9fa5]{2,3}$/;
var rxFourCNChar = /^[\u4e00-\u9fa5]{4}$/;
var isTwoToThreeCNChar = rxTwoToThreeCNChar.test.bind(rxTwoToThreeCNChar);
var isFourCNChar = rxFourCNChar.test.bind(rxFourCNChar);
var mapTeamixIconSize = function (size) {
return {
large: 'medium',
medium: 'small',
small: 'xs'
}[size];
};
var Button = withThemeClass_1.withThemeClass(react_1.default.forwardRef(function (props, ref) {
var _a, _b, _c, _d;
var className = props.className, iconSize = props.iconSize, _e = props.size, size = _e === void 0 ? 'medium' : _e;
var children = props.children;
var count = react_1.Children.count(children);
var theme = useCssVar_1.useCssVar('--alicloudfe-components-theme').trim();
if (isReactFragment_1.default(children) &&
theme !== 'wind' &&
!theme.startsWith('xconsole')) {
children = react_1.default.createElement("span", { className: 'next-btn-helper' }, children);
}
// 判断是否是2-3个汉字
if (
// xconsole相关主题不需要该功能
theme !== 'wind' &&
!theme.startsWith('xconsole') &&
typeof children === 'string' &&
isTwoToThreeCNChar(children)) {
return (react_1.default.createElement(next_1.Button, __assign({}, props, { className: classnames_1.default('isTwoToThreeCNCharBtn', className), ref: ref }), children));
}
// 判断是否是4个汉字
if (
// xconsole相关主题不需要该功能
theme !== 'wind' &&
!theme.startsWith('xconsole') &&
typeof children === 'string' &&
isFourCNChar(children)) {
return (react_1.default.createElement(next_1.Button, __assign({}, props, { className: classnames_1.default('isFourCNCharBtn', className), ref: ref }), children));
}
if ((count === 1 &&
((_b = (_a = children) === null || _a === void 0 ? void 0 : _a.type) === null || _b === void 0 ? void 0 : _b.displayName) === 'Config(Icon)') ||
((_d = (_c = children) === null || _c === void 0 ? void 0 : _c.type) === null || _d === void 0 ? void 0 : _d.displayName) === 'TeamixIcon') {
return (react_1.default.createElement(next_1.Button, __assign({}, props, { className: classnames_1.default('isOnlyIcon', className), ref: ref }), children));
}
var clonedChildren = react_1.Children.map(children, function (child, index) {
var _a;
var _b;
// 针对 teamix-icon 进行处理
if (child &&
['function', 'object'].indexOf(typeof child.type) > -1 &&
((_b = child.type) === null || _b === void 0 ? void 0 : _b.displayName) === 'TeamixIcon') {
var iconCls = classnames_1.default((_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 react_1.default.cloneElement(child, {
className: iconCls,
size: iconSize || mapTeamixIconSize(size)
});
}
return child;
});
return (react_1.default.createElement(next_1.Button, __assign({}, props, { className: className, ref: ref }), clonedChildren));
}));
hoist_non_react_statics_1.default(Button, next_1.Button);
// @ts-ignore
Button.displayName = next_1.Button.displayName;
exports.default = Button;
;