buttonHelpers.js 2.3 KB

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