@alicloudfe/components
Version:
A react component library for aliyun
55 lines (54 loc) • 2.56 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);
};
// 判断button类组件是否是2-4汉字
import React from 'react';
import hoistNonReactStatics from 'hoist-non-react-statics';
import cls from 'classnames';
import { useCssVar } from './useCssVar';
var rxTwoToThreeCNChar = /^[\u4e00-\u9fa5]{2,3}$/;
var rxFourCNChar = /^[\u4e00-\u9fa5]{4}$/;
var isTwoToThreeCNChar = rxTwoToThreeCNChar.test.bind(rxTwoToThreeCNChar);
var isFourCNChar = rxFourCNChar.test.bind(rxFourCNChar);
var CNCHARHOC = function (WrappedComponents) {
var Wrapper = React.forwardRef(function (props, ref) {
var _a, _b;
var children = props.children, className = props.className;
var theme = useCssVar('--alicloudfe-components-theme').trim();
// 判断是否是2-3个汉字
if (
// xconsole相关主题不需要该功能
theme !== 'wind' &&
!theme.startsWith('xconsole') &&
typeof children === 'string' &&
isTwoToThreeCNChar(children)) {
return (React.createElement(WrappedComponents, __assign({}, props, { className: cls('isTwoToThreeCNCharBtn', className), ref: ref }), children));
}
// 判断是否是4个汉字
if (
// xconsole相关主题不需要该功能
theme !== 'wind' &&
!theme.startsWith('xconsole') &&
typeof children === 'string' &&
isFourCNChar(children)) {
return (React.createElement(WrappedComponents, __assign({}, props, { className: cls('isFourCNCharBtn', className), ref: ref }), children));
}
// 判断是否只有图标
if (React.Children.count(children) === 1 &&
((_b = (_a = children) === null || _a === void 0 ? void 0 : _a.type) === null || _b === void 0 ? void 0 : _b.displayName) === 'Config(Icon)') {
return (React.createElement(WrappedComponents, __assign({}, props, { className: cls('isOnlyIcon', className), ref: ref }), children));
}
return (React.createElement(WrappedComponents, __assign({}, props, { className: className, ref: ref }), children));
});
hoistNonReactStatics(Wrapper, WrappedComponents);
return Wrapper;
};
export default CNCHARHOC;