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.
 
 

123 regels
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. }), [block, compact]);
  46. const defaultRef = React.useRef<HTMLInputElement>(null);
  47. const theRef = (ref ?? defaultRef) as React.MutableRefObject<HTMLInputElement>;
  48. React.useEffect(() => {
  49. if (!(indeterminate && theRef.current)) {
  50. return;
  51. }
  52. theRef.current.indeterminate = indeterminate;
  53. }, [theRef, indeterminate]);
  54. return (
  55. <div
  56. className={CheckControlBase.ClickAreaWrapper(styleProps)}
  57. >
  58. <label
  59. className={CheckControlBase.ClickArea()}
  60. >
  61. <input
  62. {...etcProps}
  63. type="checkbox"
  64. ref={theRef}
  65. className={CheckControlBase.CheckStateContainer(styleProps)}
  66. />
  67. <span>
  68. <span />
  69. <span
  70. className={CheckControlBase.CheckIndicatorArea(styleProps)}
  71. >
  72. <span
  73. className={CheckControlBase.CheckIndicatorWrapper(styleProps)}
  74. >
  75. <svg
  76. className={CheckControlBase.CheckIndicator(styleProps)}
  77. viewBox="0 0 24 24"
  78. role="presentation"
  79. >
  80. <polyline
  81. points="20 6 9 17 4 12"
  82. />
  83. </svg>
  84. <svg
  85. className={CheckControlBase.CheckIndicator(styleProps)}
  86. viewBox="0 0 24 24"
  87. role="presentation"
  88. >
  89. <polyline
  90. points="20 12 4 12"
  91. />
  92. </svg>
  93. </span>
  94. </span>
  95. <span>
  96. {children}
  97. {
  98. subtext
  99. && (
  100. <>
  101. <br />
  102. <span
  103. className={CheckControlBase.Subtext()}
  104. >
  105. {subtext}
  106. </span>
  107. </>
  108. )
  109. }
  110. </span>
  111. </span>
  112. </label>
  113. </div>
  114. );
  115. },
  116. );
  117. ToggleTickBox.displayName = 'ActionButton';