Design system.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

124 linhas
3.3 KiB

  1. import * as React from 'react';
  2. import * as CheckControlBase from '@tesseract-design/web-base-checkcontrol';
  3. export type ToggleSwitchProps = Omit<React.HTMLProps<HTMLInputElement>, 'size' | 'type' | 'style'> & {
  4. /**
  5. * Label of the component when in the unchecked state.
  6. */
  7. uncheckedLabel?: React.ReactNode,
  8. /**
  9. * Label of the component when in the checked state.
  10. */
  11. checkedLabel?: React.ReactNode,
  12. /**
  13. * Should the component occupy the whole width of its parent?
  14. */
  15. block?: boolean,
  16. /**
  17. * Does the component need to conserve space?
  18. */
  19. compact?: boolean,
  20. /**
  21. * Complementary content of the component.
  22. */
  23. subtext?: React.ReactNode,
  24. }
  25. /**
  26. * Component for performing an action upon activation (e.g. when clicked).
  27. *
  28. * This component functions as a regular button.
  29. */
  30. export const ToggleSwitch = React.forwardRef<HTMLInputElement, ToggleSwitchProps>(
  31. (
  32. {
  33. checkedLabel,
  34. uncheckedLabel,
  35. block = false,
  36. compact = false,
  37. subtext,
  38. className: _className,
  39. as: _as,
  40. children: _children,
  41. ...etcProps
  42. }: ToggleSwitchProps,
  43. ref,
  44. ) => {
  45. const styleProps = React.useMemo<CheckControlBase.CheckControlBaseArgs>(() => ({
  46. block,
  47. compact,
  48. menuItem: false,
  49. appearance: CheckControlBase.CheckControlAppearance.SWITCH,
  50. type: CheckControlBase.CheckControlType.CHECKBOX,
  51. uncheckedLabel: Boolean(uncheckedLabel),
  52. }), [block, compact, uncheckedLabel]);
  53. return (
  54. <div
  55. className={CheckControlBase.ClickAreaWrapper(styleProps)}
  56. >
  57. <label
  58. className={CheckControlBase.ClickArea()}
  59. >
  60. <input
  61. {...etcProps}
  62. type="checkbox"
  63. ref={ref}
  64. className={CheckControlBase.CheckStateContainer(styleProps)}
  65. />
  66. <span>
  67. <span>
  68. {uncheckedLabel}
  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. {checkedLabel}
  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. ToggleSwitch.displayName = 'ActionButton';