From 319927e1dc4bb469ac8a34b95bfbc36d24356670 Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Sat, 27 Apr 2024 19:31:28 +0200 Subject: [PATCH] Extract form group label into a separate component --- .../pages/shared/UserProfileFormFields.tsx | 188 ++++++++++-------- 1 file changed, 101 insertions(+), 87 deletions(-) diff --git a/src/login/pages/shared/UserProfileFormFields.tsx b/src/login/pages/shared/UserProfileFormFields.tsx index 459bbb58..b395c31f 100644 --- a/src/login/pages/shared/UserProfileFormFields.tsx +++ b/src/login/pages/shared/UserProfileFormFields.tsx @@ -1,4 +1,4 @@ -import { useEffect, Fragment } from "react"; +import { useEffect, useState, Fragment } from "react"; import type { ClassKey } from "keycloakify/login/TemplateProps"; import { clsx } from "keycloakify/tools/clsx"; import { @@ -51,7 +51,7 @@ export function UserProfileFormFields(props: UserProfileFormFieldsProps) { onIsFormSubmittableValueChange(isFormSubmittable); }, [isFormSubmittable]); - let currentGroupName = ""; + const groupNameRef = { "current": "" }; return ( <> @@ -63,90 +63,14 @@ export function UserProfileFormFields(props: UserProfileFormFieldsProps) { return ( - {(() => { - keycloak_prior_to_24: { - if (attribute.html5DataAnnotations !== undefined) { - break keycloak_prior_to_24; - } - - const { group = "", groupDisplayHeader = "", groupDisplayDescription = "" } = attribute as any as LegacyAttribute; - - return ( - group !== currentGroupName && - (currentGroupName = group) !== "" && ( -
-
- -
- {groupDisplayDescription !== "" && ( -
- -
- )} -
- ) - ); - } - - if (attribute.group?.name !== currentGroupName) { - currentGroupName = attribute.group?.name ?? ""; - - if (currentGroupName !== "") { - assert(attribute.group !== undefined); - - return ( -
[`data-${key}`, value]) - )} - > - {(() => { - const groupDisplayHeader = attribute.group.displayHeader ?? ""; - const groupHeaderText = - groupDisplayHeader !== "" ? advancedMsg(groupDisplayHeader) : attribute.group.name; - - return ( -
- -
- ); - })()} - {(() => { - const groupDisplayDescription = attribute.group.displayDescription ?? ""; - - if (groupDisplayDescription !== "") { - const groupDescriptionText = advancedMsg(groupDisplayDescription); - - return ( -
- -
- ); - } - - return null; - })()} -
- ); - } - } - - return null; - })()} - - {BeforeField && ( + + {BeforeField !== undefined && ( )} - {AfterField && ( + {AfterField !== undefined && ( + {group !== groupNameRef.current && (groupNameRef.current = group) !== "" && ( +
+
+ +
+ {groupDisplayDescription !== "" && ( +
+ +
+ )} +
+ )} + + ); + } + + if (attribute.group?.name !== groupNameRef.current) { + groupNameRef.current = attribute.group?.name ?? ""; + + if (groupNameRef.current !== "") { + assert(attribute.group !== undefined); + + return ( +
[`data-${key}`, value]))} + > + {(() => { + const groupDisplayHeader = attribute.group.displayHeader ?? ""; + const groupHeaderText = groupDisplayHeader !== "" ? advancedMsg(groupDisplayHeader) : attribute.group.name; + + return ( +
+ +
+ ); + })()} + {(() => { + const groupDisplayDescription = attribute.group.displayDescription ?? ""; + + if (groupDisplayDescription !== "") { + const groupDescriptionText = advancedMsg(groupDisplayDescription); + + return ( +
+ +
+ ); + } + + return null; + })()} +
+ ); + } + } + + return null; +} + function AddRemoveButtonsMultiValuedAttribute(props: { formFieldStates: FormFieldState[]; attribute: Attribute;