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 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /* eslint-disable jsx-a11y/anchor-has-content */
  21. /* eslint-disable jsx-a11y/control-has-associated-label */
  22. import classNames from 'classnames';
  23. import * as React from 'react';
  24. import DeferredSpinner from '../ui/DeferredSpinner';
  25. import './Checkbox.css';
  26. interface Props {
  27. checked: boolean;
  28. disabled?: boolean;
  29. children?: React.ReactNode;
  30. className?: string;
  31. id?: string;
  32. label?: string;
  33. loading?: boolean;
  34. onCheck: (checked: boolean, id?: string) => void;
  35. right?: boolean;
  36. thirdState?: boolean;
  37. title?: string;
  38. }
  39. export default class Checkbox extends React.PureComponent<Props> {
  40. static defaultProps = {
  41. thirdState: false
  42. };
  43. handleClick = (event: React.SyntheticEvent<HTMLElement>) => {
  44. event.preventDefault();
  45. event.currentTarget.blur();
  46. if (!this.props.disabled) {
  47. this.props.onCheck(!this.props.checked, this.props.id);
  48. }
  49. };
  50. render() {
  51. const {
  52. checked,
  53. children,
  54. disabled,
  55. id,
  56. label,
  57. loading,
  58. right,
  59. thirdState,
  60. title
  61. } = this.props;
  62. const className = classNames('icon-checkbox', {
  63. 'icon-checkbox-checked': checked,
  64. 'icon-checkbox-single': thirdState,
  65. 'icon-checkbox-disabled': disabled
  66. });
  67. if (children) {
  68. return (
  69. <a
  70. aria-checked={thirdState ? 'mixed' : checked}
  71. aria-label={label}
  72. className={classNames('link-checkbox', this.props.className, {
  73. disabled
  74. })}
  75. href="#"
  76. id={id}
  77. onClick={this.handleClick}
  78. role="checkbox"
  79. title={title}>
  80. {right && children}
  81. <DeferredSpinner loading={Boolean(loading)}>
  82. <i className={className} />
  83. </DeferredSpinner>
  84. {!right && children}
  85. </a>
  86. );
  87. }
  88. if (loading) {
  89. return <DeferredSpinner ariaLabel={label} />;
  90. }
  91. return (
  92. <a
  93. aria-checked={thirdState ? 'mixed' : checked}
  94. aria-label={label}
  95. className={classNames(className, this.props.className)}
  96. href="#"
  97. id={id}
  98. onClick={this.handleClick}
  99. role="checkbox"
  100. title={title}
  101. />
  102. );
  103. }
  104. }