Website for showcasing all features of Tesseract Web.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

251 行
6.7 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. * Style of the component.
  23. */
  24. style?: ButtonBase.ButtonStyle,
  25. /**
  26. * Does the component need to conserve space?
  27. */
  28. compact?: boolean,
  29. /**
  30. * Complementary content of the component.
  31. */
  32. subtext?: React.ReactNode,
  33. /**
  34. * Short complementary content displayed at the edge of the component.
  35. */
  36. badge?: React.ReactNode,
  37. /**
  38. * Appearance of the component.
  39. */
  40. appearance?: CheckControlBase.CheckControlAppearance,
  41. }
  42. /**
  43. * Component for performing an action upon activation (e.g. when clicked).
  44. *
  45. * This component functions as a regular button.
  46. */
  47. export const RadioButton = React.forwardRef<HTMLInputElement, RadioButtonProps>(
  48. (
  49. {
  50. size = ButtonBase.ButtonSize.MEDIUM,
  51. variant = ButtonBase.ButtonVariant.OUTLINE,
  52. border = false,
  53. children,
  54. block = false,
  55. style = ButtonBase.ButtonStyle.DEFAULT,
  56. disabled = false,
  57. compact = false,
  58. subtext,
  59. badge,
  60. appearance = CheckControlBase.CheckControlAppearance.DEFAULT,
  61. className: _className,
  62. as: _as,
  63. ...etcProps
  64. }: RadioButtonProps,
  65. ref,
  66. ) => {
  67. const styleProps = React.useMemo<ButtonBase.ButtonBaseArgs & CheckControlBase.CheckControlBaseArgs>(() => ({
  68. size,
  69. block,
  70. variant,
  71. border,
  72. style,
  73. compact,
  74. menuItem: false,
  75. disabled,
  76. appearance,
  77. type: 'radio',
  78. }), [size, block, variant, border, style, compact, disabled, appearance]);
  79. const checkIndicatorCommon = (
  80. <span
  81. className={CheckControlBase.CheckIndicatorArea(styleProps)}
  82. >
  83. <span
  84. className={CheckControlBase.CheckIndicatorWrapper(styleProps)}
  85. />
  86. </span>
  87. )
  88. return (
  89. <div
  90. className={CheckControlBase.ClickAreaWrapper(styleProps)}
  91. >
  92. <label
  93. className={CheckControlBase.ClickArea()}
  94. >
  95. <input
  96. {...etcProps}
  97. disabled={disabled}
  98. type="radio"
  99. ref={ref}
  100. className={CheckControlBase.CheckStateContainer(styleProps)}
  101. />
  102. {
  103. appearance === CheckControlBase.CheckControlAppearance.DEFAULT
  104. && (
  105. <span>
  106. <span />
  107. {checkIndicatorCommon}
  108. <span>
  109. {children}
  110. {
  111. subtext
  112. && (
  113. <>
  114. <br />
  115. <span
  116. className={CheckControlBase.Subtext()}
  117. >
  118. {subtext}
  119. </span>
  120. </>
  121. )
  122. }
  123. </span>
  124. {
  125. badge
  126. && (
  127. <>
  128. {' '}
  129. <span
  130. className={ButtonBase.BadgeContainer(styleProps)}
  131. >
  132. {badge}
  133. </span>
  134. </>
  135. )
  136. }
  137. </span>
  138. )
  139. }
  140. {
  141. appearance === CheckControlBase.CheckControlAppearance.BUTTON
  142. && (
  143. <span
  144. className={ButtonBase.Button(styleProps)}
  145. >
  146. <span
  147. className={ButtonBase.Border(styleProps)}
  148. />
  149. {checkIndicatorCommon}
  150. <span
  151. className={ButtonBase.Label(styleProps)}
  152. >
  153. <span
  154. className={ButtonBase.MainText()}
  155. >
  156. <span
  157. className={ButtonBase.OverflowText()}
  158. >
  159. {children}
  160. </span>
  161. </span>
  162. {
  163. subtext
  164. && (
  165. <>
  166. {' '}
  167. <span
  168. className={ButtonBase.Subtext()}
  169. >
  170. <span
  171. className={ButtonBase.OverflowText()}
  172. >
  173. {subtext}
  174. </span>
  175. </span>
  176. </>
  177. )
  178. }
  179. </span>
  180. {
  181. badge
  182. && (
  183. <>
  184. {' '}
  185. <span
  186. className={ButtonBase.BadgeContainer(styleProps)}
  187. >
  188. {badge}
  189. </span>
  190. </>
  191. )
  192. }
  193. </span>
  194. )
  195. }
  196. {
  197. appearance === CheckControlBase.CheckControlAppearance.SWITCH
  198. && (
  199. <span>
  200. <span />
  201. <span
  202. className={CheckControlBase.CheckIndicatorArea(styleProps)}
  203. >
  204. <span
  205. className={CheckControlBase.CheckIndicatorWrapper(styleProps)}
  206. />
  207. </span>
  208. <span>
  209. {children}
  210. {
  211. subtext
  212. && (
  213. <>
  214. <br />
  215. <span
  216. className={CheckControlBase.Subtext()}
  217. >
  218. {subtext}
  219. </span>
  220. </>
  221. )
  222. }
  223. </span>
  224. {
  225. badge
  226. && (
  227. <>
  228. {' '}
  229. <span
  230. className={ButtonBase.BadgeContainer(styleProps)}
  231. >
  232. {badge}
  233. </span>
  234. </>
  235. )
  236. }
  237. </span>
  238. )
  239. }
  240. </label>
  241. </div>
  242. );
  243. },
  244. );
  245. RadioButton.displayName = 'ActionButton';