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.

ValidationInput.tsx 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 * as React from 'react';
  21. import { translate } from '../../helpers/l10n';
  22. import AlertErrorIcon from '../icons/AlertErrorIcon';
  23. import AlertSuccessIcon from '../icons/AlertSuccessIcon';
  24. import MandatoryFieldMarker from '../ui/MandatoryFieldMarker';
  25. import HelpTooltip from './HelpTooltip';
  26. export interface ValidationInputProps {
  27. description?: React.ReactNode;
  28. children: React.ReactNode;
  29. className?: string;
  30. error?: string;
  31. errorPlacement?: ValidationInputErrorPlacement;
  32. help?: string;
  33. id?: string;
  34. isInvalid: boolean;
  35. isValid: boolean;
  36. label?: React.ReactNode;
  37. required?: boolean;
  38. }
  39. export enum ValidationInputErrorPlacement {
  40. Right,
  41. Bottom
  42. }
  43. export default function ValidationInput(props: ValidationInputProps) {
  44. const {
  45. children,
  46. className,
  47. description,
  48. error,
  49. errorPlacement = ValidationInputErrorPlacement.Right,
  50. help,
  51. id,
  52. isInvalid,
  53. isValid,
  54. label,
  55. required
  56. } = props;
  57. const hasError = isInvalid && error !== undefined;
  58. let childrenWithStatus: React.ReactNode;
  59. if (errorPlacement === ValidationInputErrorPlacement.Right) {
  60. childrenWithStatus = (
  61. <>
  62. {children}
  63. {isValid && (
  64. <AlertSuccessIcon
  65. ariaLabel={translate('valid_input')}
  66. className="spacer-left text-middle"
  67. />
  68. )}
  69. {isInvalid && <AlertErrorIcon className="spacer-left text-middle" />}
  70. {hasError && <span className="little-spacer-left text-danger text-middle">{error}</span>}
  71. </>
  72. );
  73. } else {
  74. childrenWithStatus = (
  75. <>
  76. {children}
  77. {isValid && (
  78. <AlertSuccessIcon
  79. ariaLabel={translate('valid_input')}
  80. className="spacer-left text-middle"
  81. />
  82. )}
  83. <div className="spacer-top">
  84. {isInvalid && <AlertErrorIcon className="text-middle" />}
  85. {hasError && <span className="little-spacer-left text-danger text-middle">{error}</span>}
  86. </div>
  87. </>
  88. );
  89. }
  90. return (
  91. <div className={className}>
  92. {id && label && (
  93. <label htmlFor={id}>
  94. <span className="text-middle">
  95. <strong>{label}</strong>
  96. {required && <MandatoryFieldMarker />}
  97. </span>
  98. {help && <HelpTooltip className="spacer-left" overlay={help} />}
  99. </label>
  100. )}
  101. <div className="little-spacer-top spacer-bottom">{childrenWithStatus}</div>
  102. {description && <div className="note abs-width-400">{description}</div>}
  103. </div>
  104. );
  105. }