Design system.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

173 行
3.9 KiB

  1. import * as React from 'react';
  2. import * as ButtonBase from '@tesseract-design/web-base-button';
  3. /**
  4. * Available ActionButton type values.
  5. */
  6. export enum ActionButtonType {
  7. SUBMIT = 'submit',
  8. RESET = 'reset',
  9. BUTTON = 'button',
  10. }
  11. /**
  12. * Props for the component.
  13. */
  14. export interface ActionButtonProps extends Omit<React.HTMLProps<HTMLButtonElement>, 'size' | 'type' | 'style'> {
  15. /**
  16. * Size of the component.
  17. */
  18. size?: ButtonBase.ButtonSize,
  19. /**
  20. * Variant of the component.
  21. */
  22. variant?: ButtonBase.ButtonVariant,
  23. /**
  24. * Should the component display a border?
  25. */
  26. border?: boolean,
  27. /**
  28. * Should the component occupy the whole width of its parent?
  29. */
  30. block?: boolean,
  31. /**
  32. * Type of the component.
  33. */
  34. type?: ActionButtonType,
  35. /**
  36. * Does the component need to conserve space?
  37. */
  38. compact?: boolean,
  39. /**
  40. * Complementary content of the component.
  41. */
  42. subtext?: React.ReactNode,
  43. /**
  44. * Short complementary content displayed at the edge of the component.
  45. */
  46. badge?: React.ReactNode,
  47. /**
  48. * Is this component part of a menu?
  49. */
  50. menuItem?: boolean,
  51. }
  52. /**
  53. * Component for performing an action upon activation (e.g. when clicked).
  54. *
  55. * This component functions as a regular button.
  56. */
  57. export const ActionButton = React.forwardRef<HTMLButtonElement, ActionButtonProps>(
  58. (
  59. {
  60. size = ButtonBase.ButtonSize.MEDIUM,
  61. variant = ButtonBase.ButtonVariant.OUTLINE,
  62. border = false,
  63. children,
  64. type = ActionButtonType.BUTTON,
  65. block = false,
  66. disabled = false,
  67. compact = false,
  68. subtext,
  69. badge,
  70. menuItem = false,
  71. className: _className,
  72. as: _as,
  73. ...etcProps
  74. }: ActionButtonProps,
  75. ref,
  76. ) => {
  77. const styleProps = React.useMemo<ButtonBase.ButtonBaseArgs>(() => ({
  78. size,
  79. block,
  80. variant,
  81. border,
  82. compact,
  83. menuItem,
  84. disabled,
  85. }), [size, block, variant, border, compact, menuItem, disabled]);
  86. return (
  87. <button
  88. {...etcProps}
  89. disabled={disabled}
  90. className={ButtonBase.Button(styleProps)}
  91. ref={ref}
  92. type={type}
  93. >
  94. <span
  95. className={ButtonBase.Border(styleProps)}
  96. />
  97. <span
  98. className={ButtonBase.Label(styleProps)}
  99. >
  100. <span
  101. className={ButtonBase.MainText()}
  102. data-testid="children"
  103. >
  104. <span
  105. className={ButtonBase.OverflowText()}
  106. >
  107. {children}
  108. </span>
  109. </span>
  110. {
  111. subtext
  112. && (
  113. <>
  114. {' '}
  115. <span
  116. className={ButtonBase.Subtext()}
  117. data-testid="subtext"
  118. >
  119. <span
  120. className={ButtonBase.OverflowText()}
  121. >
  122. {subtext}
  123. </span>
  124. </span>
  125. </>
  126. )
  127. }
  128. </span>
  129. {
  130. badge
  131. && (
  132. <>
  133. {' '}
  134. <span
  135. className={ButtonBase.BadgeContainer(styleProps)}
  136. data-testid="badge"
  137. >
  138. {badge}
  139. </span>
  140. </>
  141. )
  142. }
  143. {
  144. menuItem
  145. && (
  146. <>
  147. {' '}
  148. <span
  149. className={ButtonBase.IndicatorWrapper(styleProps)}
  150. data-testid="menuItemIndicator"
  151. >
  152. <svg
  153. className={ButtonBase.Indicator()}
  154. viewBox="0 0 24 24"
  155. role="presentation"
  156. >
  157. <polyline points="9 18 15 12 9 6"/>
  158. </svg>
  159. </span>
  160. </>
  161. )
  162. }
  163. </button>
  164. );
  165. },
  166. );
  167. ActionButton.displayName = 'ActionButton';