import type { JSX } from "@keycloakify/keycloak-login-ui/tools/JSX"; import { useEffect, Fragment } from "react"; import { useUserProfileForm, type FormAction, type FormFieldError } from "@keycloakify/keycloak-login-ui/useUserProfileForm"; import { GroupLabel } from "./GroupLabel"; import { FieldErrors } from "./FieldErrors"; import { InputFieldByType } from "./InputFieldByType"; import type { Attribute } from "@keycloakify/keycloak-login-ui/KcContext"; import { useKcContext } from "../../KcContext"; import { useI18n } from "../../i18n"; import { useKcClsx } from "@keycloakify/keycloak-login-ui/useKcClsx"; import { DO_MAKE_USER_CONFIRM_PASSWORD } from "./DO_MAKE_USER_CONFIRM_PASSWORD"; import { assert } from "tsafe/assert"; export type UserProfileFormFieldsProps = { onIsFormSubmittableValueChange: (isFormSubmittable: boolean) => void; BeforeField?: (props: BeforeAfterFieldProps) => JSX.Element | null; AfterField?: (props: BeforeAfterFieldProps) => JSX.Element | null; }; type BeforeAfterFieldProps = { attribute: Attribute; dispatchFormAction: React.Dispatch; displayableErrors: FormFieldError[]; valueOrValues: string | string[]; }; export function UserProfileFormFields(props: UserProfileFormFieldsProps) { const { onIsFormSubmittableValueChange, BeforeField, AfterField } = props; const { kcContext } = useKcContext(); assert("profile" in kcContext); const i18n = useI18n(); const { advancedMsg } = i18n; const { formState: { formFieldStates, isFormSubmittable }, dispatchFormAction } = useUserProfileForm({ kcContext, i18n, doMakeUserConfirmPassword: DO_MAKE_USER_CONFIRM_PASSWORD }); useEffect(() => { onIsFormSubmittableValueChange(isFormSubmittable); }, [isFormSubmittable]); const { kcClsx } = useKcClsx(); const groupNameRef = { current: "" }; return ( <> {formFieldStates.map(({ attribute, displayableErrors, valueOrValues }) => { return ( {BeforeField !== undefined && ( )}
{attribute.required && <> *}
{attribute.annotations.inputHelperTextBefore !== undefined && (
{advancedMsg(attribute.annotations.inputHelperTextBefore)}
)} {attribute.annotations.inputHelperTextAfter !== undefined && (
{advancedMsg(attribute.annotations.inputHelperTextAfter)}
)} {AfterField !== undefined && ( )} {/* NOTE: Downloading of html5DataAnnotations scripts is done in the useUserProfileForm hook */}
); })} ); }