buttonHelpers.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. "use client";
  3. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  4. Object.defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports._ButtonVariantTypes = exports._ButtonColorTypes = void 0;
  8. exports.convertLegacyProps = convertLegacyProps;
  9. exports.isString = isString;
  10. exports.isTwoCNChar = void 0;
  11. exports.isUnBorderedButtonVariant = isUnBorderedButtonVariant;
  12. exports.spaceChildren = spaceChildren;
  13. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  14. var _react = _interopRequireDefault(require("react"));
  15. var _reactNode = require("../_util/reactNode");
  16. var _interface = require("../theme/interface");
  17. const rxTwoCNChar = /^[\u4E00-\u9FA5]{2}$/;
  18. const isTwoCNChar = exports.isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar);
  19. function convertLegacyProps(type) {
  20. if (type === 'danger') {
  21. return {
  22. danger: true
  23. };
  24. }
  25. return {
  26. type
  27. };
  28. }
  29. function isString(str) {
  30. return typeof str === 'string';
  31. }
  32. function isUnBorderedButtonVariant(type) {
  33. return type === 'text' || type === 'link';
  34. }
  35. function splitCNCharsBySpace(child, needInserted) {
  36. if (child === null || child === undefined) {
  37. return;
  38. }
  39. const SPACE = needInserted ? ' ' : '';
  40. if (typeof child !== 'string' && typeof child !== 'number' && isString(child.type) && isTwoCNChar(child.props.children)) {
  41. return (0, _reactNode.cloneElement)(child, {
  42. children: child.props.children.split('').join(SPACE)
  43. });
  44. }
  45. if (isString(child)) {
  46. return isTwoCNChar(child) ? /*#__PURE__*/_react.default.createElement("span", null, child.split('').join(SPACE)) : /*#__PURE__*/_react.default.createElement("span", null, child);
  47. }
  48. if ((0, _reactNode.isFragment)(child)) {
  49. return /*#__PURE__*/_react.default.createElement("span", null, child);
  50. }
  51. return child;
  52. }
  53. function spaceChildren(children, needInserted) {
  54. let isPrevChildPure = false;
  55. const childList = [];
  56. _react.default.Children.forEach(children, child => {
  57. const type = typeof child;
  58. const isCurrentChildPure = type === 'string' || type === 'number';
  59. if (isPrevChildPure && isCurrentChildPure) {
  60. const lastIndex = childList.length - 1;
  61. const lastChild = childList[lastIndex];
  62. childList[lastIndex] = `${lastChild}${child}`;
  63. } else {
  64. childList.push(child);
  65. }
  66. isPrevChildPure = isCurrentChildPure;
  67. });
  68. return _react.default.Children.map(childList, child => splitCNCharsBySpace(child, needInserted));
  69. }
  70. const _ButtonTypes = ['default', 'primary', 'dashed', 'link', 'text'];
  71. const _ButtonShapes = ['default', 'circle', 'round'];
  72. const _ButtonHTMLTypes = ['submit', 'button', 'reset'];
  73. const _ButtonVariantTypes = exports._ButtonVariantTypes = ['outlined', 'dashed', 'solid', 'filled', 'text', 'link'];
  74. const _ButtonColorTypes = exports._ButtonColorTypes = ['default', 'primary', 'danger'].concat((0, _toConsumableArray2.default)(_interface.PresetColors));