UNPKG

@alicloudfe/components

Version:

A react component library for aliyun

90 lines (89 loc) 3.96 kB
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 { Button as NextButton } from '@alifd/next'; import React, { Children } from 'react'; import hoistNonReactStatics from 'hoist-non-react-statics'; import cls from 'classnames'; import { withThemeClass } from '../utils/withThemeClass'; import { useCssVar } from '../utils/useCssVar'; import isReactFragment from '../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(React.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 = Children.count(children); var theme = useCssVar('--alicloudfe-components-theme').trim(); if (isReactFragment(children) && theme !== 'wind' && !theme.startsWith('xconsole')) { children = React.createElement("span", { className: 'next-btn-helper' }, children); } // 判断是否是2-3个汉字 if ( // xconsole相关主题不需要该功能 theme !== 'wind' && !theme.startsWith('xconsole') && typeof children === 'string' && isTwoToThreeCNChar(children)) { return (React.createElement(NextButton, __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(NextButton, __assign({}, props, { className: cls('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.createElement(NextButton, __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'].indexOf(typeof child.type) > -1 && ((_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 React.cloneElement(child, { className: iconCls, size: iconSize || mapTeamixIconSize(size) }); } return child; }); return (React.createElement(NextButton, __assign({}, props, { className: className, ref: ref }), clonedChildren)); })); hoistNonReactStatics(Button, NextButton); // @ts-ignore Button.displayName = NextButton.displayName; export default Button;