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.

FormField.tsx 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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 styled from '@emotion/styled';
  21. import { ReactNode } from 'react';
  22. import tw from 'twin.macro';
  23. import { Highlight, Note } from '../Text';
  24. import { RequiredIcon } from '../icons';
  25. interface Props {
  26. ariaLabel?: string;
  27. children: ReactNode;
  28. className?: string;
  29. description?: string | ReactNode;
  30. help?: ReactNode;
  31. htmlFor?: string;
  32. id?: string;
  33. label: string | ReactNode;
  34. required?: boolean;
  35. requiredAriaLabel?: string;
  36. title?: string;
  37. }
  38. export function FormField({
  39. children,
  40. className,
  41. description,
  42. help,
  43. id,
  44. required,
  45. label,
  46. htmlFor,
  47. title,
  48. ariaLabel,
  49. requiredAriaLabel,
  50. }: Props) {
  51. return (
  52. <FieldWrapper className={className} id={id}>
  53. <Highlight className="sw-mb-2 sw-flex sw-items-center sw-gap-2">
  54. <StyledLabel aria-label={ariaLabel} htmlFor={htmlFor} title={title}>
  55. {label}
  56. {required && (
  57. <RequiredIcon aria-label={requiredAriaLabel ?? 'required'} className="sw-ml-1" />
  58. )}
  59. </StyledLabel>
  60. {help}
  61. </Highlight>
  62. {children}
  63. {description && <Note className="sw-mt-2">{description}</Note>}
  64. </FieldWrapper>
  65. );
  66. }
  67. // This is needed to prevent the target input/button from being focused
  68. // when clicking/hovering on the label. More info https://stackoverflow.com/questions/9098581/why-is-hover-for-input-triggered-on-corresponding-label-in-css
  69. const StyledLabel = styled.label`
  70. pointer-events: none;
  71. `;
  72. const FieldWrapper = styled.div`
  73. ${tw`sw-flex sw-flex-col sw-w-full`}
  74. &:not(:last-of-type) {
  75. ${tw`sw-mb-6`}
  76. }
  77. `;