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.

buttons.tsx 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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 classNames from 'classnames';
  21. import * as React from 'react';
  22. import { colors } from '../../app/theme';
  23. import ChevronRightIcon from '../icons/ChevronRightIcon';
  24. import ClearIcon, { ClearIconProps } from '../icons/ClearIcon';
  25. import DeleteIcon from '../icons/DeleteIcon';
  26. import EditIcon from '../icons/EditIcon';
  27. import { IconProps } from '../icons/Icon';
  28. import './buttons.css';
  29. import Tooltip, { TooltipProps } from './Tooltip';
  30. type AllowedButtonAttributes = Pick<
  31. React.ButtonHTMLAttributes<HTMLButtonElement>,
  32. 'aria-label' | 'className' | 'disabled' | 'id' | 'style' | 'title'
  33. >;
  34. interface ButtonProps extends AllowedButtonAttributes {
  35. autoFocus?: boolean;
  36. children?: React.ReactNode;
  37. innerRef?: React.Ref<HTMLButtonElement>;
  38. name?: string;
  39. onClick?: () => void;
  40. preventDefault?: boolean;
  41. stopPropagation?: boolean;
  42. type?: 'button' | 'submit' | 'reset';
  43. }
  44. export class Button extends React.PureComponent<ButtonProps> {
  45. handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
  46. const { disabled, onClick, preventDefault = true, stopPropagation = false } = this.props;
  47. if (preventDefault || disabled) {
  48. event.preventDefault();
  49. }
  50. if (stopPropagation) {
  51. event.stopPropagation();
  52. }
  53. if (onClick && !disabled) {
  54. onClick();
  55. }
  56. };
  57. render() {
  58. const {
  59. className,
  60. disabled,
  61. innerRef,
  62. onClick,
  63. preventDefault,
  64. stopPropagation,
  65. type = 'button',
  66. ...props
  67. } = this.props;
  68. // Instead of undoing button style we simply not apply the class.
  69. const isPlain = className && className.indexOf('button-plain') !== -1;
  70. return (
  71. <button
  72. {...props}
  73. aria-disabled={disabled}
  74. disabled={disabled}
  75. className={classNames(isPlain ? '' : 'button', className, { disabled })}
  76. id={this.props.id}
  77. onClick={this.handleClick}
  78. ref={this.props.innerRef}
  79. // eslint-disable-next-line react/button-has-type
  80. type={type}
  81. />
  82. );
  83. }
  84. }
  85. export function ButtonLink({ className, ...props }: ButtonProps) {
  86. return <Button {...props} className={classNames('button-link', className)} />;
  87. }
  88. export function ButtonPlain({ className, ...props }: ButtonProps) {
  89. return <Button {...props} className={classNames('button-plain', className)} />;
  90. }
  91. export function SubmitButton(props: Omit<ButtonProps, 'type'>) {
  92. // do not prevent default to actually submit a form
  93. return <Button {...props} preventDefault={false} type="submit" />;
  94. }
  95. export function ResetButtonLink(props: Omit<ButtonProps, 'type'>) {
  96. return <ButtonLink {...props} type="reset" />;
  97. }
  98. export interface ButtonIconProps extends ButtonProps {
  99. 'aria-label'?: string;
  100. 'aria-labelledby'?: string;
  101. className?: string;
  102. color?: string;
  103. onClick?: () => void;
  104. tooltip?: React.ReactNode;
  105. tooltipProps?: Partial<TooltipProps>;
  106. }
  107. export function ButtonIcon(props: ButtonIconProps) {
  108. const { className, color, tooltip, tooltipProps, ...other } = props;
  109. return (
  110. <Tooltip mouseEnterDelay={0.4} overlay={tooltip} {...tooltipProps}>
  111. <Button
  112. className={classNames(className, 'button-icon')}
  113. stopPropagation={true}
  114. style={{ color: color || colors.darkBlue }}
  115. {...other}
  116. />
  117. </Tooltip>
  118. );
  119. }
  120. interface ClearButtonProps extends ButtonIconProps {
  121. className?: string;
  122. iconProps?: ClearIconProps;
  123. onClick?: () => void;
  124. }
  125. export function ClearButton({ color, iconProps = {}, ...props }: ClearButtonProps) {
  126. return (
  127. <ButtonIcon color={color || colors.gray60} {...props}>
  128. <ClearIcon {...iconProps} />
  129. </ButtonIcon>
  130. );
  131. }
  132. interface ActionButtonProps extends ButtonIconProps {
  133. className?: string;
  134. iconProps?: IconProps;
  135. onClick?: () => void;
  136. }
  137. export function DeleteButton({ iconProps = {}, ...props }: ActionButtonProps) {
  138. return (
  139. <ButtonIcon color={colors.red} {...props}>
  140. <DeleteIcon {...iconProps} />
  141. </ButtonIcon>
  142. );
  143. }
  144. export function EditButton({ iconProps = {}, ...props }: ActionButtonProps) {
  145. return (
  146. <ButtonIcon {...props}>
  147. <EditIcon {...iconProps} />
  148. </ButtonIcon>
  149. );
  150. }
  151. export function ListButton({ className, children, ...props }: ButtonProps) {
  152. return (
  153. <Button className={classNames('button-list', className)} {...props}>
  154. {children}
  155. <ChevronRightIcon />
  156. </Button>
  157. );
  158. }