Website for showcasing all features of Tesseract Web.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 

125 lignes
3.3 KiB

  1. import * as React from 'react';
  2. import * as CheckControlBase from '@tesseract-design/web-base-checkcontrol';
  3. export type ToggleTickBoxProps = Omit<React.HTMLProps<HTMLInputElement>, 'type' | 'style'> & {
  4. /**
  5. * Should the component occupy the whole width of its parent?
  6. */
  7. block?: boolean,
  8. /**
  9. * Does the component need to conserve space?
  10. */
  11. compact?: boolean,
  12. /**
  13. * Complementary content of the component.
  14. */
  15. subtext?: React.ReactNode,
  16. /**
  17. * Does the component have indeterminate check state?
  18. */
  19. indeterminate?: boolean,
  20. }
  21. /**
  22. * Component for performing an action upon activation (e.g. when clicked).
  23. *
  24. * This component functions as a regular button.
  25. */
  26. export const ToggleTickBox = React.forwardRef<HTMLInputElement, ToggleTickBoxProps>(
  27. (
  28. {
  29. children,
  30. block = false,
  31. compact = false,
  32. subtext,
  33. indeterminate = false,
  34. className: _className,
  35. as: _as,
  36. ...etcProps
  37. }: ToggleTickBoxProps,
  38. ref,
  39. ) => {
  40. const styleProps = React.useMemo<CheckControlBase.CheckControlBaseArgs>(() => ({
  41. block,
  42. compact,
  43. appearance: CheckControlBase.CheckControlAppearance.TICK_BOX,
  44. type: 'checkbox',
  45. uncheckedLabel: false,
  46. }), [block, compact]);
  47. const defaultRef = React.useRef<HTMLInputElement>(null);
  48. const theRef = (ref ?? defaultRef) as React.MutableRefObject<HTMLInputElement>;
  49. React.useEffect(() => {
  50. if (!(indeterminate && theRef.current)) {
  51. return;
  52. }
  53. theRef.current.indeterminate = indeterminate;
  54. }, [theRef, indeterminate]);
  55. return (
  56. <div
  57. className={CheckControlBase.ClickAreaWrapper(styleProps)}
  58. >
  59. <label
  60. className={CheckControlBase.ClickArea()}
  61. >
  62. <input
  63. {...etcProps}
  64. type="checkbox"
  65. ref={theRef}
  66. className={CheckControlBase.CheckStateContainer(styleProps)}
  67. />
  68. <span>
  69. <span />
  70. <span
  71. className={CheckControlBase.CheckIndicatorArea(styleProps)}
  72. >
  73. <span
  74. className={CheckControlBase.CheckIndicatorWrapper(styleProps)}
  75. >
  76. <svg
  77. className={CheckControlBase.CheckIndicator(styleProps)}
  78. viewBox="0 0 24 24"
  79. role="presentation"
  80. >
  81. <polyline
  82. points="20 6 9 17 4 12"
  83. />
  84. </svg>
  85. <svg
  86. className={CheckControlBase.CheckIndicator(styleProps)}
  87. viewBox="0 0 24 24"
  88. role="presentation"
  89. >
  90. <polyline
  91. points="20 12 4 12"
  92. />
  93. </svg>
  94. </span>
  95. </span>
  96. <span>
  97. {children}
  98. {
  99. subtext
  100. && (
  101. <>
  102. <br />
  103. <span
  104. className={CheckControlBase.Subtext()}
  105. data-testid="subtext"
  106. >
  107. {subtext}
  108. </span>
  109. </>
  110. )
  111. }
  112. </span>
  113. </span>
  114. </label>
  115. </div>
  116. );
  117. },
  118. );
  119. ToggleTickBox.displayName = 'ActionButton';