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.

OrganizationUrlInput.tsx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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 * as classNames from 'classnames';
  22. import { isWebUri } from 'valid-url';
  23. import { translate } from 'sonar-ui-common/helpers/l10n';
  24. import ValidationInput from 'sonar-ui-common/components/controls/ValidationInput';
  25. interface Props {
  26. initialValue?: string;
  27. onChange: (value: string | undefined) => void;
  28. }
  29. interface State {
  30. editing: boolean;
  31. error?: string;
  32. touched: boolean;
  33. value: string;
  34. }
  35. export default class OrganizationUrlInput extends React.PureComponent<Props, State> {
  36. state: State = { error: undefined, editing: false, touched: false, value: '' };
  37. componentDidMount() {
  38. if (this.props.initialValue) {
  39. const value = this.props.initialValue;
  40. const error = this.validateUrl(value);
  41. this.setState({ error, touched: Boolean(error), value });
  42. }
  43. }
  44. handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
  45. const value = event.currentTarget.value.trim();
  46. const error = this.validateUrl(value);
  47. this.setState({ error, touched: true, value });
  48. this.props.onChange(error === undefined ? value : undefined);
  49. };
  50. handleBlur = () => {
  51. this.setState({ editing: false });
  52. };
  53. handleFocus = () => {
  54. this.setState({ editing: true });
  55. };
  56. validateUrl(url: string) {
  57. if (url.length > 0 && !isWebUri(url)) {
  58. return translate('onboarding.create_organization.url.error');
  59. }
  60. return undefined;
  61. }
  62. render() {
  63. const isInvalid = this.state.touched && !this.state.editing && this.state.error !== undefined;
  64. const isValid = this.state.touched && this.state.error === undefined && this.state.value !== '';
  65. return (
  66. <ValidationInput
  67. error={this.state.error}
  68. id="organization-url"
  69. isInvalid={isInvalid}
  70. isValid={isValid}
  71. label={translate('onboarding.create_organization.url')}>
  72. <input
  73. className={classNames('input-super-large', 'text-middle', {
  74. 'is-invalid': isInvalid,
  75. 'is-valid': isValid
  76. })}
  77. id="organization-url"
  78. onBlur={this.handleBlur}
  79. onChange={this.handleChange}
  80. onFocus={this.handleFocus}
  81. type="text"
  82. value={this.state.value}
  83. />
  84. </ValidationInput>
  85. );
  86. }
  87. }