Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Checkbox.tsx 2.7KB

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