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.

Checkbox.tsx 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import styled from '@emotion/styled';
  21. import React from 'react';
  22. import tw from 'twin.macro';
  23. import { themeBorder, themeColor, themeContrast } from '../helpers/theme';
  24. import DeferredSpinner from './DeferredSpinner';
  25. import CheckIcon from './icons/CheckIcon';
  26. import { CustomIcon } from './icons/Icon';
  27. interface Props {
  28. checked: boolean;
  29. children?: React.ReactNode;
  30. className?: string;
  31. disabled?: boolean;
  32. id?: string;
  33. loading?: boolean;
  34. onCheck: (checked: boolean, id?: string) => void;
  35. onClick?: (event: React.MouseEvent<HTMLInputElement>) => void;
  36. onFocus?: VoidFunction;
  37. right?: boolean;
  38. thirdState?: boolean;
  39. title?: string;
  40. }
  41. export default function Checkbox({
  42. checked,
  43. disabled,
  44. children,
  45. className,
  46. id,
  47. loading = false,
  48. onCheck,
  49. onFocus,
  50. onClick,
  51. right,
  52. thirdState = false,
  53. title,
  54. }: Props) {
  55. const handleChange = () => {
  56. if (!disabled) {
  57. onCheck(!checked, id);
  58. }
  59. };
  60. return (
  61. <CheckboxContainer className={className} disabled={disabled}>
  62. {right && children}
  63. <AccessibleCheckbox
  64. aria-label={title}
  65. checked={checked}
  66. disabled={disabled || loading}
  67. id={id}
  68. onChange={handleChange}
  69. onClick={onClick}
  70. onFocus={onFocus}
  71. type="checkbox"
  72. />
  73. <DeferredSpinner loading={loading}>
  74. <StyledCheckbox aria-hidden={true} data-clickable="true" title={title}>
  75. <CheckboxIcon checked={checked} thirdState={thirdState} />
  76. </StyledCheckbox>
  77. </DeferredSpinner>
  78. {!right && children}
  79. </CheckboxContainer>
  80. );
  81. }
  82. interface CheckIconProps {
  83. checked?: boolean;
  84. thirdState?: boolean;
  85. }
  86. function CheckboxIcon({ checked, thirdState }: CheckIconProps) {
  87. if (checked && thirdState) {
  88. return (
  89. <CustomIcon>
  90. <rect fill="currentColor" height="2" rx="1" width="50%" x="4" y="7" />
  91. </CustomIcon>
  92. );
  93. } else if (checked) {
  94. return <CheckIcon fill="currentColor" />;
  95. }
  96. return null;
  97. }
  98. const CheckboxContainer = styled.label<{ disabled?: boolean }>`
  99. color: ${themeContrast('backgroundSecondary')};
  100. user-select: none;
  101. ${tw`sw-inline-flex sw-items-center`};
  102. &:hover {
  103. ${tw`sw-cursor-pointer`}
  104. }
  105. &:disabled {
  106. color: ${themeContrast('checkboxDisabled')};
  107. ${tw`sw-cursor-not-allowed`}
  108. }
  109. `;
  110. export const StyledCheckbox = styled.span`
  111. border: ${themeBorder('default', 'primary')};
  112. color: ${themeContrast('primary')};
  113. ${tw`sw-w-4 sw-h-4`};
  114. ${tw`sw-rounded-1/2`};
  115. ${tw`sw-box-border`}
  116. ${tw`sw-inline-flex sw-items-center sw-justify-center`};
  117. `;
  118. export const AccessibleCheckbox = styled.input`
  119. // Following css makes the checkbox accessible and invisible
  120. border: 0;
  121. clip: rect(0 0 0 0);
  122. clip-path: inset(50%);
  123. height: 1px;
  124. overflow: hidden;
  125. padding: 0;
  126. white-space: nowrap;
  127. width: 1px;
  128. &:focus,
  129. &:active {
  130. &:not(:disabled) + ${StyledCheckbox} {
  131. outline: ${themeBorder('focus', 'primary')};
  132. }
  133. }
  134. &:checked {
  135. & + ${StyledCheckbox} {
  136. background: ${themeColor('primary')};
  137. }
  138. &:disabled + ${StyledCheckbox} {
  139. background: ${themeColor('checkboxDisabledChecked')};
  140. }
  141. }
  142. &:hover {
  143. &:not(:disabled) + ${StyledCheckbox} {
  144. background: ${themeColor('checkboxHover')};
  145. border: ${themeBorder('default', 'primary')};
  146. }
  147. &:checked:not(:disabled) + ${StyledCheckbox} {
  148. background: ${themeColor('checkboxCheckedHover')};
  149. border: ${themeBorder('default', 'checkboxCheckedHover')};
  150. }
  151. }
  152. &:disabled + ${StyledCheckbox} {
  153. background: ${themeColor('checkboxDisabled')};
  154. color: ${themeColor('checkboxDisabled')};
  155. border: ${themeBorder('default', 'checkboxDisabledChecked')};
  156. }
  157. `;