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.

Toggle.tsx 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 { translate } from '../../helpers/l10n';
  23. import CheckIcon from '../icons/CheckIcon';
  24. import { Button } from './buttons';
  25. import './Toggle.css';
  26. interface Props {
  27. ariaLabel?: string;
  28. disabled?: boolean;
  29. name?: string;
  30. onChange?: (value: boolean) => void;
  31. value: boolean | string;
  32. }
  33. export default class Toggle extends React.PureComponent<Props> {
  34. getValue = () => {
  35. const { value } = this.props;
  36. return typeof value === 'string' ? value === 'true' : value;
  37. };
  38. handleClick = () => {
  39. if (this.props.onChange) {
  40. const value = this.getValue();
  41. this.props.onChange(!value);
  42. }
  43. };
  44. render() {
  45. const { ariaLabel, disabled, name } = this.props;
  46. const value = this.getValue();
  47. const className = classNames('boolean-toggle', { 'boolean-toggle-on': value });
  48. return (
  49. <Button className={className} disabled={disabled} name={name} onClick={this.handleClick}>
  50. <div
  51. aria-label={ariaLabel ?? translate(value ? 'on' : 'off')}
  52. className="boolean-toggle-handle">
  53. <CheckIcon size={12} />
  54. </div>
  55. </Button>
  56. );
  57. }
  58. }