All files / src/components/ui/form/layout FormLayout.tsx

70% Statements 28/40
50% Branches 3/6
25% Functions 3/12
62.06% Lines 18/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 1745x 5x 5x                                     5x 26x               5x               5x 26x     5x           5x                             5x                   52x                                             5x                               5x               5x               5x                 5x   5x                                                                    
import React, { FC, ReactNode, useState } from "react";
import { Label } from "ui/form/Label";
import {
  StyledFormColumn,
  StyledFormColumns,
  StyledFormContainer,
  StyledFormDivider,
  StyledFormField,
  StyledFormFieldInfo,
  StyledFormFieldInput,
  StyledFormHeader,
  StyledFormLink,
  StyledFormRow,
  StyledFormSection,
  StyledFormSectionTitle,
} from "ui/form/layout/style";
 
interface FormContainerProps {
  children: ReactNode;
}
 
export const FormContainer = ({ children }: FormContainerProps) => (
  <StyledFormContainer children={children} />
);
 
interface FormHeaderProps {
  variant?: "normal" | "prefab";
  children: ReactNode;
}
 
export const FormHeader = ({ children, variant }: FormHeaderProps) => (
  <StyledFormHeader $variant={variant} children={children} />
);
 
interface FormRowProps {
  children: ReactNode;
}
 
export const FormRow = ({ children }: FormRowProps) => (
  <StyledFormRow children={children} />
);
 
export const FormDivider = () => <StyledFormDivider />;
 
interface FormFieldInfoProps {
  children: ReactNode;
}
 
export const FormFieldInfo = ({ children }: FormFieldInfoProps) => (
  <StyledFormFieldInfo children={children} />
);
 
export interface FormFieldProps {
  readonly name: string;
  readonly label?: string | React.ReactNode;
  readonly title?: string;
  readonly info?: string;
  readonly alignCheckbox?: boolean;
  readonly variant?: "normal" | "error" | "warning";
  readonly hasOverride?: boolean;
  readonly children?: ReactNode;
}
 
export const FormField: FC<FormFieldProps> = ({
  name,
  label,
  title,
  info,
  variant,
  alignCheckbox,
  hasOverride,
  children,
}: FormFieldProps) => (
  <StyledFormField
    $variant={variant}
    $alignCheckbox={alignCheckbox}
    $hasOverride={hasOverride}
  >
    {label && (
      <Label htmlFor={name} title={title}>
        {label}
      </Label>
    )}
    <StyledFormFieldInput $hasOverride={hasOverride}>
      {children}
    </StyledFormFieldInput>
    {info && <StyledFormFieldInfo>{info}</StyledFormFieldInfo>}
  </StyledFormField>
);
 
interface FormSectionTitleProps {
  readonly noTopBorder?: boolean;
  readonly noMarginBottom?: boolean;
  children: ReactNode;
}
 
export const FormSectionTitle = ({
  noTopBorder,
  noMarginBottom,
  children,
}: FormSectionTitleProps) => (
  <StyledFormSectionTitle
    $noTopBorder={noTopBorder}
    $noMarginBottom={noMarginBottom}
    children={children}
  />
);
 
interface FormSectionProps {
  children: ReactNode;
}
 
export const FormSection = ({ children }: FormSectionProps) => (
  <StyledFormSection children={children} />
);
 
interface FormColumnsProps {
  children: ReactNode;
}
 
export const FormColumns = ({ children }: FormColumnsProps) => (
  <StyledFormColumns children={children} />
);
 
interface FormColumnProps {
  children: ReactNode;
}
 
export const FormColumn = ({ children }: FormColumnProps) => (
  <StyledFormColumn children={children} />
);
 
interface ToggleableFormFieldProps {
  readonly enabled: boolean;
  readonly disabledLabel: string | React.ReactNode;
}
 
export const ToggleableFormField: FC<
  ToggleableFormFieldProps & FormFieldProps
> = ({
  enabled,
  disabledLabel,
  name,
  label,
  info,
  variant,
  hasOverride,
  children,
}) => {
  const [isEnabled, setIsEnabled] = useState(enabled);
 
  if (!isEnabled) {
    return (
      <div>
        <StyledFormLink onClick={() => setIsEnabled(true)}>
          {disabledLabel}
        </StyledFormLink>
      </div>
    );
  }
 
  return (
    <FormField
      name={name}
      label={label}
      info={info}
      variant={variant}
      hasOverride={hasOverride}
    >
      {children}
    </FormField>
  );
};