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.

InputForSecured.tsx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 React from 'react';
  21. import { colors } from '../../../../app/theme';
  22. import { Button } from '../../../../components/controls/buttons';
  23. import LockIcon from '../../../../components/icons/LockIcon';
  24. import { translate } from '../../../../helpers/l10n';
  25. import {
  26. DefaultInputProps,
  27. DefaultSpecializedInputProps,
  28. getUniqueName,
  29. isDefaultOrInherited
  30. } from '../../utils';
  31. interface State {
  32. changing: boolean;
  33. }
  34. interface Props extends DefaultInputProps {
  35. input: React.ComponentType<DefaultSpecializedInputProps>;
  36. }
  37. export default class InputForSecured extends React.PureComponent<Props, State> {
  38. state: State = {
  39. changing: !this.props.setting.hasValue
  40. };
  41. componentWillReceiveProps(nextProps: Props) {
  42. /*
  43. * Reset `changing` if:
  44. * - the value is reset (valueChanged -> !valueChanged)
  45. * or
  46. * - the value changes from outside the input (i.e. store update/reset/cancel)
  47. */
  48. if (
  49. (this.props.hasValueChanged || this.props.setting !== nextProps.setting) &&
  50. !nextProps.hasValueChanged
  51. ) {
  52. this.setState({ changing: !nextProps.setting.hasValue });
  53. }
  54. }
  55. handleInputChange = (value: string) => {
  56. this.props.onChange(value);
  57. };
  58. handleChangeClick = () => {
  59. this.setState({ changing: true });
  60. };
  61. renderInput() {
  62. const { input: Input, setting, value } = this.props;
  63. const name = getUniqueName(setting.definition);
  64. return (
  65. // The input hidden will prevent browser asking for saving login information
  66. <>
  67. <input className="hidden" type="password" />
  68. <Input
  69. autoComplete="off"
  70. className="js-setting-input settings-large-input"
  71. isDefault={isDefaultOrInherited(setting)}
  72. name={name}
  73. onChange={this.handleInputChange}
  74. setting={setting}
  75. type="password"
  76. value={value}
  77. />
  78. </>
  79. );
  80. }
  81. render() {
  82. if (this.state.changing) {
  83. return this.renderInput();
  84. }
  85. return (
  86. <>
  87. <LockIcon className="text-middle big-spacer-right" fill={colors.gray60} />
  88. <Button className="text-middle" onClick={this.handleChangeClick}>
  89. {translate('change_verb')}
  90. </Button>
  91. </>
  92. );
  93. }
  94. }