keycloak-scim/js/libs/ui-shared/src/controls/FormLabel.tsx
Jon Koops d3c2475041
Upgrade admin and account console to PatternFly 5 (#28196)
Closes #21345
Closes #21344

Signed-off-by: Jon Koops <jonkoops@gmail.com>
Co-authored-by: Erik Jan de Wit <erikjan.dewit@gmail.com>
Co-authored-by: Mark Franceschelli <mfrances@redhat.com>
Co-authored-by: Hynek Mlnařík <hmlnarik@redhat.com>
Co-authored-by: Agnieszka Gancarczyk <agancarc@redhat.com>
2024-04-05 16:37:05 +02:00

40 lines
1 KiB
TypeScript

import { FormGroup, FormGroupProps } from "@patternfly/react-core";
import { PropsWithChildren, ReactNode } from "react";
import { FieldError, FieldValues, Merge } from "react-hook-form";
import { FormErrorText } from "./FormErrorText";
import { HelpItem } from "./HelpItem";
export type FieldProps<T extends FieldValues = FieldValues> = {
label?: string;
name: string;
labelIcon?: string | ReactNode;
error?: FieldError | Merge<FieldError, T>;
isRequired: boolean;
};
type FormLabelProps = FieldProps & Omit<FormGroupProps, "label" | "labelIcon">;
export const FormLabel = ({
name,
label,
labelIcon,
error,
children,
...rest
}: PropsWithChildren<FormLabelProps>) => (
<FormGroup
label={label || name}
fieldId={name}
labelIcon={
labelIcon ? (
<HelpItem helpText={labelIcon} fieldLabelId={name} />
) : undefined
}
{...rest}
>
{children}
{error && (
<FormErrorText data-testid={`${name}-helper`} message={error.message} />
)}
</FormGroup>
);