Website for showcasing all features of Tesseract Web.
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.
 
 

278 rivejä
7.4 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 CheckboxProps = 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. * Does the component have indeterminate check state?
  43. */
  44. indeterminate?: boolean,
  45. }
  46. /**
  47. * Component for performing an action upon activation (e.g. when clicked).
  48. *
  49. * This component functions as a regular button.
  50. */
  51. export const Checkbox = React.forwardRef<HTMLInputElement, CheckboxProps>(
  52. (
  53. {
  54. size = ButtonBase.ButtonSize.MEDIUM,
  55. variant = ButtonBase.ButtonVariant.OUTLINE,
  56. border = false,
  57. children,
  58. block = false,
  59. style = ButtonBase.ButtonStyle.DEFAULT,
  60. disabled = false,
  61. compact = false,
  62. subtext,
  63. badge,
  64. appearance = CheckControlBase.CheckControlAppearance.DEFAULT,
  65. indeterminate = false,
  66. className: _className,
  67. as: _as,
  68. ...etcProps
  69. }: CheckboxProps,
  70. ref,
  71. ) => {
  72. const styleProps = React.useMemo<ButtonBase.ButtonBaseArgs & CheckControlBase.CheckControlBaseArgs>(() => ({
  73. size,
  74. block,
  75. variant,
  76. border,
  77. style,
  78. compact,
  79. menuItem: false,
  80. disabled,
  81. appearance,
  82. type: 'checkbox',
  83. }), [size, block, variant, border, style, compact, disabled, appearance]);
  84. const defaultRef = React.useRef<HTMLInputElement>(null);
  85. const theRef = (ref ?? defaultRef) as React.MutableRefObject<HTMLInputElement>;
  86. React.useEffect(() => {
  87. if (!(indeterminate && theRef.current)) {
  88. return;
  89. }
  90. theRef.current.indeterminate = indeterminate;
  91. }, [theRef, indeterminate]);
  92. const checkIndicatorCommon = (
  93. <span
  94. className={CheckControlBase.CheckIndicatorArea(styleProps)}
  95. >
  96. <span
  97. className={CheckControlBase.CheckIndicatorWrapper(styleProps)}
  98. >
  99. <svg
  100. className={CheckControlBase.CheckIndicator(styleProps)}
  101. viewBox="0 0 24 24"
  102. role="presentation"
  103. >
  104. <polyline
  105. points="20 6 9 17 4 12"
  106. />
  107. </svg>
  108. <svg
  109. className={CheckControlBase.CheckIndicator(styleProps)}
  110. viewBox="0 0 24 24"
  111. role="presentation"
  112. >
  113. <polyline
  114. points="20 12 4 12"
  115. />
  116. </svg>
  117. </span>
  118. </span>
  119. )
  120. return (
  121. <div
  122. className={CheckControlBase.ClickAreaWrapper(styleProps)}
  123. >
  124. <label
  125. className={CheckControlBase.ClickArea()}
  126. >
  127. <input
  128. {...etcProps}
  129. disabled={disabled}
  130. type="checkbox"
  131. ref={theRef}
  132. className={CheckControlBase.CheckStateContainer(styleProps)}
  133. />
  134. {
  135. appearance === CheckControlBase.CheckControlAppearance.DEFAULT
  136. && (
  137. <span>
  138. <span />
  139. {checkIndicatorCommon}
  140. <span>
  141. {children}
  142. {
  143. subtext
  144. && (
  145. <>
  146. <br />
  147. <span
  148. className={CheckControlBase.Subtext()}
  149. >
  150. {subtext}
  151. </span>
  152. </>
  153. )
  154. }
  155. </span>
  156. {
  157. badge
  158. && (
  159. <>
  160. {' '}
  161. <span
  162. className={ButtonBase.BadgeContainer(styleProps)}
  163. >
  164. {badge}
  165. </span>
  166. </>
  167. )
  168. }
  169. </span>
  170. )
  171. }
  172. {
  173. appearance === CheckControlBase.CheckControlAppearance.BUTTON
  174. && (
  175. <span
  176. className={ButtonBase.Button(styleProps)}
  177. >
  178. <span
  179. className={ButtonBase.Border(styleProps)}
  180. />
  181. {checkIndicatorCommon}
  182. <span
  183. className={ButtonBase.Label(styleProps)}
  184. >
  185. <span
  186. className={ButtonBase.MainText()}
  187. >
  188. <span
  189. className={ButtonBase.OverflowText()}
  190. >
  191. {children}
  192. </span>
  193. </span>
  194. {
  195. subtext
  196. && (
  197. <>
  198. {' '}
  199. <span
  200. className={ButtonBase.Subtext()}
  201. >
  202. <span
  203. className={ButtonBase.OverflowText()}
  204. >
  205. {subtext}
  206. </span>
  207. </span>
  208. </>
  209. )
  210. }
  211. </span>
  212. {
  213. badge
  214. && (
  215. <>
  216. {' '}
  217. <span
  218. className={ButtonBase.BadgeContainer(styleProps)}
  219. >
  220. {badge}
  221. </span>
  222. </>
  223. )
  224. }
  225. </span>
  226. )
  227. }
  228. {
  229. appearance === CheckControlBase.CheckControlAppearance.SWITCH
  230. && (
  231. <span>
  232. <span />
  233. {checkIndicatorCommon}
  234. <span>
  235. {children}
  236. {
  237. subtext
  238. && (
  239. <>
  240. <br />
  241. <span
  242. className={CheckControlBase.Subtext()}
  243. >
  244. {subtext}
  245. </span>
  246. </>
  247. )
  248. }
  249. </span>
  250. {
  251. badge
  252. && (
  253. <>
  254. {' '}
  255. <span
  256. className={ButtonBase.BadgeContainer(styleProps)}
  257. >
  258. {badge}
  259. </span>
  260. </>
  261. )
  262. }
  263. </span>
  264. )
  265. }
  266. </label>
  267. </div>
  268. );
  269. },
  270. );
  271. Checkbox.displayName = 'ActionButton';