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.

SelectionCard.tsx 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 classNames from 'classnames';
  22. import { useIntl } from 'react-intl';
  23. import tw from 'twin.macro';
  24. import { themeBorder, themeColor, themeContrast, themeShadow } from '../helpers/theme';
  25. import { LightLabel } from './Text';
  26. import { RecommendedIcon } from './icons/RecommendedIcon';
  27. import { RadioButtonStyled } from './input/RadioButton';
  28. export interface SelectionCardProps {
  29. children?: React.ReactNode;
  30. className?: string;
  31. disabled?: boolean;
  32. onClick?: VoidFunction;
  33. recommended?: boolean;
  34. recommendedReason?: string;
  35. selected?: boolean;
  36. title: string;
  37. titleInfo?: React.ReactNode;
  38. vertical?: boolean;
  39. }
  40. export function SelectionCard(props: SelectionCardProps) {
  41. const {
  42. children,
  43. className,
  44. disabled,
  45. onClick,
  46. recommended,
  47. recommendedReason,
  48. selected = false,
  49. title,
  50. titleInfo,
  51. vertical = false,
  52. } = props;
  53. const isActionable = Boolean(onClick);
  54. const intl = useIntl();
  55. return (
  56. <StyledButton
  57. aria-checked={selected}
  58. aria-disabled={disabled}
  59. className={classNames(
  60. 'js-radio-card',
  61. {
  62. 'card-actionable': isActionable && !disabled,
  63. 'card-vertical': vertical,
  64. disabled,
  65. selected,
  66. },
  67. className,
  68. )}
  69. onClick={isActionable && !disabled && !selected ? onClick : undefined}
  70. role={isActionable ? 'radio' : 'presentation'}
  71. tabIndex={disabled ? -1 : 0}
  72. type="button"
  73. >
  74. <StyledContent>
  75. {isActionable && (
  76. <div className="sw-items-start sw-mt-1/2 sw-mr-2">
  77. <RadioButtonStyled
  78. as="i"
  79. className={classNames({ 'is-checked': selected, 'is-disabled': disabled })}
  80. />
  81. </div>
  82. )}
  83. <div>
  84. <StyledLabel>
  85. {title}
  86. <LightLabel>{titleInfo}</LightLabel>
  87. </StyledLabel>
  88. <StyledBody>{children}</StyledBody>
  89. </div>
  90. </StyledContent>
  91. {recommended && (
  92. <StyledRecommended>
  93. <StyledRecommendedIcon className="sw-mr-1" />
  94. <span className="sw-align-middle">
  95. <strong>{intl.formatMessage({ id: 'recommended' })}</strong> {recommendedReason}
  96. </span>
  97. </StyledRecommended>
  98. )}
  99. </StyledButton>
  100. );
  101. }
  102. const StyledButton = styled.button`
  103. ${tw`sw-relative sw-flex sw-flex-col`}
  104. ${tw`sw-rounded-2`}
  105. ${tw`sw-box-border`}
  106. background-color: ${themeColor('backgroundSecondary')};
  107. border: ${themeBorder('default', 'selectionCardBorder')};
  108. color: inherit;
  109. &:focus {
  110. outline: none;
  111. border: ${themeBorder('default', 'selectionCardBorderHover')};
  112. box-shadow: ${themeShadow('sm')};
  113. }
  114. &.card-vertical {
  115. ${tw`sw-w-full`}
  116. min-height: auto;
  117. }
  118. &.card-actionable {
  119. ${tw`sw-cursor-pointer`}
  120. &:hover {
  121. border: ${themeBorder('default', 'selectionCardBorderHover')};
  122. box-shadow: ${themeShadow('sm')};
  123. }
  124. &.selected {
  125. border: ${themeBorder('default', 'selectionCardBorderSelected')};
  126. }
  127. }
  128. &.disabled {
  129. ${tw`sw-cursor-not-allowed`}
  130. background-color: ${themeColor('selectionCardDisabled')};
  131. color: ${themeColor('selectionCardDisabledText')};
  132. border: ${themeBorder('default', 'selectionCardBorderDisabled')};
  133. }
  134. `;
  135. const StyledContent = styled.div`
  136. ${tw`sw-my-4 sw-mx-3`}
  137. ${tw`sw-flex sw-grow`}
  138. ${tw`sw-text-left`}
  139. `;
  140. const StyledRecommended = styled.div`
  141. ${tw`sw-body-sm`}
  142. ${tw`sw-py-2 sw-px-4`}
  143. ${tw`sw-box-border`}
  144. ${tw`sw-rounded-b-2`}
  145. color: ${themeContrast('infoBackground')};
  146. background-color: ${themeColor('infoBackground')};
  147. `;
  148. const StyledRecommendedIcon = styled(RecommendedIcon)`
  149. color: ${themeColor('iconInfo')};
  150. ${tw`sw-align-middle`}
  151. `;
  152. const StyledLabel = styled.label`
  153. ${tw`sw-flex`}
  154. ${tw`sw-mb-3 sw-gap-2`}
  155. ${tw`sw-body-sm-highlight`}
  156. color: ${themeColor('selectionCardHeader')};
  157. cursor: inherit;
  158. .disabled & {
  159. color: ${themeContrast('selectionCardDisabled')};
  160. }
  161. `;
  162. const StyledBody = styled.div`
  163. ${tw`sw-flex sw-grow`}
  164. ${tw`sw-flex-col sw-justify-between`}
  165. `;