Keycloak
1import { UserProfileAttributeMetadata } from "@keycloak/keycloak-admin-client/lib/defs/userProfileMetadata";
2import { FormGroup, InputGroup } from "@patternfly/react-core";
3import { TFunction } from "i18next";
4import { get } from "lodash-es";
5import { PropsWithChildren, ReactNode } from "react";
6import { UseFormReturn } from "react-hook-form";
7
8import { HelpItem } from "../controls/HelpItem";
9import {
10UserFormFields,
11fieldName,
12isRequiredAttribute,
13labelAttribute,
14} from "./utils";
15
16export type UserProfileGroupProps = {
17t: TFunction;
18form: UseFormReturn<UserFormFields>;
19attribute: UserProfileAttributeMetadata;
20renderer?: (attribute: UserProfileAttributeMetadata) => ReactNode;
21};
22
23export const UserProfileGroup = ({
24t,
25form,
26attribute,
27renderer,
28children,
29}: PropsWithChildren<UserProfileGroupProps>) => {
30const helpText = attribute.annotations?.["inputHelperTextBefore"] as string;
31const {
32formState: { errors },
33} = form;
34
35const component = renderer?.(attribute);
36return (
37<FormGroup
38key={attribute.name}
39label={labelAttribute(t, attribute) || ""}
40fieldId={attribute.name}
41isRequired={isRequiredAttribute(attribute)}
42validated={get(errors, fieldName(attribute.name)) ? "error" : "default"}
43helperTextInvalid={t(
44get(errors, fieldName(attribute.name))?.message as string,
45)}
46labelIcon={
47helpText ? (
48<HelpItem helpText={helpText} fieldLabelId={attribute.name!} />
49) : undefined
50}
51>
52{component ? (
53<InputGroup>
54{children}
55{component}
56</InputGroup>
57) : (
58children
59)}
60</FormGroup>
61);
62};
63