Design system.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

153 lines
4.0 KiB

  1. import * as React from 'react';
  2. import * as ButtonBase from '@tesseract-design/web-base-button';
  3. import * as CheckControlBase from '@tesseract-design/web-base-checkcontrol';
  4. export type RadioButtonProps = Omit<React.HTMLProps<HTMLInputElement>, 'size' | 'type' | 'style'> & {
  5. /**
  6. * Size of the component.
  7. */
  8. size?: ButtonBase.ButtonSize,
  9. /**
  10. * Variant of the component.
  11. */
  12. variant?: ButtonBase.ButtonVariant,
  13. /**
  14. * Should the component display a border?
  15. */
  16. border?: boolean,
  17. /**
  18. * Should the component occupy the whole width of its parent?
  19. */
  20. block?: boolean,
  21. /**
  22. * Does the component need to conserve space?
  23. */
  24. compact?: boolean,
  25. /**
  26. * Complementary content of the component.
  27. */
  28. subtext?: React.ReactNode,
  29. /**
  30. * Short complementary content displayed at the edge of the component.
  31. */
  32. badge?: React.ReactNode,
  33. }
  34. /**
  35. * Component for performing an action upon activation (e.g. when clicked).
  36. *
  37. * This component functions as a regular button.
  38. */
  39. export const RadioButton = React.forwardRef<HTMLInputElement, RadioButtonProps>(
  40. (
  41. {
  42. size = ButtonBase.ButtonSize.MEDIUM,
  43. variant = ButtonBase.ButtonVariant.OUTLINE,
  44. border = false,
  45. children,
  46. block = false,
  47. disabled = false,
  48. compact = false,
  49. subtext,
  50. badge,
  51. className: _className,
  52. as: _as,
  53. ...etcProps
  54. }: RadioButtonProps,
  55. ref,
  56. ) => {
  57. const styleProps = React.useMemo<ButtonBase.ButtonBaseArgs & CheckControlBase.CheckControlBaseArgs>(() => ({
  58. size,
  59. block,
  60. variant,
  61. border,
  62. compact,
  63. menuItem: false,
  64. disabled,
  65. appearance: CheckControlBase.CheckControlAppearance.BUTTON,
  66. type: CheckControlBase.CheckControlType.RADIO,
  67. uncheckedLabel: false,
  68. }), [size, block, variant, border, compact, disabled]);
  69. return (
  70. <div
  71. className={CheckControlBase.ClickAreaWrapper(styleProps)}
  72. >
  73. <label
  74. className={CheckControlBase.ClickArea()}
  75. >
  76. <input
  77. {...etcProps}
  78. disabled={disabled}
  79. type="radio"
  80. ref={ref}
  81. className={CheckControlBase.CheckStateContainer(styleProps)}
  82. />
  83. <span
  84. className={ButtonBase.Button(styleProps)}
  85. >
  86. <span
  87. className={ButtonBase.Border(styleProps)}
  88. />
  89. <span
  90. className={CheckControlBase.CheckIndicatorArea(styleProps)}
  91. >
  92. <span
  93. className={CheckControlBase.CheckIndicatorWrapper(styleProps)}
  94. />
  95. </span>
  96. <span
  97. className={ButtonBase.Label(styleProps)}
  98. >
  99. <span
  100. className={ButtonBase.MainText()}
  101. data-testid="children"
  102. >
  103. <span
  104. className={ButtonBase.OverflowText()}
  105. >
  106. {children}
  107. </span>
  108. </span>
  109. {
  110. subtext
  111. && (
  112. <>
  113. {' '}
  114. <span
  115. className={ButtonBase.Subtext()}
  116. data-testid="subtext"
  117. >
  118. <span
  119. className={ButtonBase.OverflowText()}
  120. >
  121. {subtext}
  122. </span>
  123. </span>
  124. </>
  125. )
  126. }
  127. </span>
  128. {
  129. badge
  130. && (
  131. <>
  132. {' '}
  133. <span
  134. className={ButtonBase.BadgeContainer(styleProps)}
  135. data-testid="badge"
  136. >
  137. {badge}
  138. </span>
  139. </>
  140. )
  141. }
  142. </span>
  143. </label>
  144. </div>
  145. );
  146. },
  147. );
  148. RadioButton.displayName = 'ActionButton';