2024-04-28 19:35:16 +02:00
|
|
|
import { useEffect, Fragment } from "react";
|
2023-03-21 05:27:31 +01:00
|
|
|
import type { ClassKey } from "keycloakify/login/TemplateProps";
|
2023-03-18 06:14:05 +01:00
|
|
|
import { clsx } from "keycloakify/tools/clsx";
|
2024-04-27 19:09:22 +02:00
|
|
|
import {
|
|
|
|
useUserProfileForm,
|
|
|
|
type KcContextLike,
|
|
|
|
type FormAction,
|
|
|
|
type FormFieldError,
|
2024-05-05 18:51:33 +02:00
|
|
|
type FormFieldState
|
2024-04-27 19:09:22 +02:00
|
|
|
} from "keycloakify/login/lib/useUserProfileForm";
|
2024-04-22 06:36:41 +02:00
|
|
|
import type { Attribute, LegacyAttribute } from "keycloakify/login/kcContext/KcContext";
|
2023-03-18 18:54:33 +01:00
|
|
|
import type { I18n } from "../../i18n";
|
2024-04-22 06:36:41 +02:00
|
|
|
import { assert } from "tsafe/assert";
|
2022-09-09 02:07:29 +02:00
|
|
|
|
|
|
|
export type UserProfileFormFieldsProps = {
|
2024-04-22 06:36:41 +02:00
|
|
|
kcContext: KcContextLike;
|
2023-03-18 06:14:05 +01:00
|
|
|
i18n: I18n;
|
|
|
|
getClassName: (classKey: ClassKey) => string;
|
|
|
|
onIsFormSubmittableValueChange: (isFormSubmittable: boolean) => void;
|
2024-04-27 19:09:22 +02:00
|
|
|
BeforeField?: (props: BeforeAfterFieldProps) => JSX.Element | null;
|
|
|
|
AfterField?: (props: BeforeAfterFieldProps) => JSX.Element | null;
|
2023-03-18 06:14:05 +01:00
|
|
|
};
|
|
|
|
|
2024-04-27 19:09:22 +02:00
|
|
|
type BeforeAfterFieldProps = {
|
|
|
|
attribute: Attribute;
|
|
|
|
dispatchFormAction: React.Dispatch<FormAction>;
|
2024-05-05 18:51:33 +02:00
|
|
|
displayableErrors: FormFieldError[];
|
2024-04-27 19:09:22 +02:00
|
|
|
i18n: I18n;
|
2024-05-05 18:51:33 +02:00
|
|
|
valueOrValues: string | string[];
|
2024-04-27 19:09:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// NOTE: Enabled by default but it's a UX best practice to set it to false.
|
|
|
|
const doMakeUserConfirmPassword = true;
|
|
|
|
|
2023-03-18 06:14:05 +01:00
|
|
|
export function UserProfileFormFields(props: UserProfileFormFieldsProps) {
|
|
|
|
const { kcContext, onIsFormSubmittableValueChange, i18n, getClassName, BeforeField, AfterField } = props;
|
2022-09-09 02:07:29 +02:00
|
|
|
|
2024-04-28 19:35:16 +02:00
|
|
|
const { advancedMsg } = i18n;
|
2023-02-25 18:11:23 +01:00
|
|
|
|
|
|
|
const {
|
2024-04-27 19:09:22 +02:00
|
|
|
formState: { formFieldStates, isFormSubmittable },
|
|
|
|
dispatchFormAction
|
|
|
|
} = useUserProfileForm({
|
2023-02-25 18:11:23 +01:00
|
|
|
kcContext,
|
2024-04-27 19:09:22 +02:00
|
|
|
i18n,
|
|
|
|
doMakeUserConfirmPassword
|
2023-02-25 18:11:23 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
onIsFormSubmittableValueChange(isFormSubmittable);
|
|
|
|
}, [isFormSubmittable]);
|
|
|
|
|
2024-04-27 19:31:28 +02:00
|
|
|
const groupNameRef = { "current": "" };
|
2022-09-09 02:07:29 +02:00
|
|
|
|
2023-02-25 18:11:23 +01:00
|
|
|
return (
|
|
|
|
<>
|
2024-05-05 18:51:33 +02:00
|
|
|
{formFieldStates.map(({ attribute, displayableErrors, valueOrValues }) => {
|
2023-03-18 06:14:05 +01:00
|
|
|
const formGroupClassName = clsx(
|
|
|
|
getClassName("kcFormGroupClass"),
|
|
|
|
displayableErrors.length !== 0 && getClassName("kcFormGroupErrorClass")
|
|
|
|
);
|
2022-09-09 02:07:29 +02:00
|
|
|
|
2023-02-25 18:11:23 +01:00
|
|
|
return (
|
2024-05-05 18:51:33 +02:00
|
|
|
<Fragment key={attribute.name}>
|
2024-04-27 19:31:28 +02:00
|
|
|
<GroupLabel
|
|
|
|
attribute={attribute}
|
|
|
|
getClassName={getClassName}
|
|
|
|
i18n={i18n}
|
|
|
|
groupNameRef={groupNameRef}
|
|
|
|
formGroupClassName={formGroupClassName}
|
|
|
|
/>
|
|
|
|
{BeforeField !== undefined && (
|
2024-04-27 19:09:22 +02:00
|
|
|
<BeforeField
|
|
|
|
attribute={attribute}
|
|
|
|
dispatchFormAction={dispatchFormAction}
|
2024-05-05 18:51:33 +02:00
|
|
|
displayableErrors={displayableErrors}
|
2024-04-27 19:09:22 +02:00
|
|
|
i18n={i18n}
|
2024-05-05 18:51:33 +02:00
|
|
|
valueOrValues={valueOrValues}
|
2024-04-27 19:09:22 +02:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<div
|
|
|
|
className={formGroupClassName}
|
2024-04-28 19:35:16 +02:00
|
|
|
style={{ "display": attribute.name === "password-confirm" && !doMakeUserConfirmPassword ? "none" : undefined }}
|
2024-04-27 19:09:22 +02:00
|
|
|
>
|
2023-03-18 06:14:05 +01:00
|
|
|
<div className={getClassName("kcLabelWrapperClass")}>
|
|
|
|
<label htmlFor={attribute.name} className={getClassName("kcLabelClass")}>
|
2023-02-25 18:11:23 +01:00
|
|
|
{advancedMsg(attribute.displayName ?? "")}
|
|
|
|
</label>
|
|
|
|
{attribute.required && <>*</>}
|
|
|
|
</div>
|
2023-03-18 06:14:05 +01:00
|
|
|
<div className={getClassName("kcInputWrapperClass")}>
|
2024-05-05 18:51:33 +02:00
|
|
|
{attribute.annotations.inputHelperTextBefore !== undefined && (
|
2024-04-27 19:09:22 +02:00
|
|
|
<div
|
|
|
|
className={getClassName("kcInputHelperTextBeforeClass")}
|
|
|
|
id={`form-help-text-before-${attribute.name}`}
|
|
|
|
aria-live="polite"
|
|
|
|
>
|
|
|
|
{advancedMsg(attribute.annotations.inputHelperTextBefore)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<InputFiledByType
|
|
|
|
attribute={attribute}
|
|
|
|
index={index}
|
|
|
|
value={value}
|
|
|
|
formValidationDispatch={dispatchFormAction}
|
|
|
|
getClassName={getClassName}
|
|
|
|
i18n={i18n}
|
|
|
|
/>
|
2024-05-05 18:51:33 +02:00
|
|
|
|
|
|
|
{/*attribute.multivalued && (
|
2024-04-27 19:09:22 +02:00
|
|
|
<AddRemoveButtonsMultiValuedAttribute
|
|
|
|
formFieldStates={formFieldStates}
|
|
|
|
attribute={attribute}
|
|
|
|
index={index}
|
|
|
|
dispatchFormAction={dispatchFormAction}
|
|
|
|
i18n={i18n}
|
|
|
|
/>
|
2024-05-05 18:51:33 +02:00
|
|
|
)*/}
|
2024-04-27 19:09:22 +02:00
|
|
|
{displayableErrors.length !== 0 && (
|
2024-04-27 19:35:42 +02:00
|
|
|
<FieldErrors
|
|
|
|
attribute={attribute}
|
|
|
|
getClassName={getClassName}
|
|
|
|
displayableErrors={displayableErrors}
|
2024-05-05 18:51:33 +02:00
|
|
|
fieldIndex={undefined}
|
2024-04-27 19:35:42 +02:00
|
|
|
/>
|
2024-04-27 19:09:22 +02:00
|
|
|
)}
|
2024-05-05 18:51:33 +02:00
|
|
|
{attribute.annotations.inputHelperTextAfter !== undefined && (
|
2024-04-27 19:09:22 +02:00
|
|
|
<div
|
|
|
|
className={getClassName("kcInputHelperTextAfterClass")}
|
2024-05-05 18:51:33 +02:00
|
|
|
id={`form-help-text-after-${attribute.name}`}
|
2024-04-27 19:09:22 +02:00
|
|
|
aria-live="polite"
|
|
|
|
>
|
|
|
|
{advancedMsg(attribute.annotations.inputHelperTextAfter)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
2024-04-27 19:31:28 +02:00
|
|
|
{AfterField !== undefined && (
|
2024-04-27 19:09:22 +02:00
|
|
|
<AfterField
|
|
|
|
attribute={attribute}
|
|
|
|
dispatchFormAction={dispatchFormAction}
|
2024-05-05 18:51:33 +02:00
|
|
|
displayableErrors={displayableErrors}
|
2024-04-27 19:09:22 +02:00
|
|
|
i18n={i18n}
|
2024-05-05 18:51:33 +02:00
|
|
|
valueOrValues={valueOrValues}
|
2024-04-27 19:09:22 +02:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{/*
|
|
|
|
TODO:
|
|
|
|
|
|
|
|
<#list profile.html5DataAnnotations?keys as key>
|
|
|
|
<script type="module" src="${url.resourcesPath}/js/${key}.js"></script>
|
|
|
|
</#list>
|
|
|
|
|
|
|
|
*/}
|
2022-09-09 02:07:29 +02:00
|
|
|
</div>
|
2023-02-25 18:11:23 +01:00
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2024-04-27 19:09:22 +02:00
|
|
|
|
2024-04-27 19:31:28 +02:00
|
|
|
function GroupLabel(props: {
|
|
|
|
attribute: Attribute;
|
|
|
|
getClassName: UserProfileFormFieldsProps["getClassName"];
|
|
|
|
i18n: I18n;
|
|
|
|
groupNameRef: {
|
|
|
|
current: string;
|
|
|
|
};
|
|
|
|
formGroupClassName: string;
|
|
|
|
}) {
|
|
|
|
const { attribute, getClassName, i18n, groupNameRef, formGroupClassName } = props;
|
|
|
|
|
|
|
|
const { advancedMsg } = i18n;
|
|
|
|
|
|
|
|
keycloak_prior_to_24: {
|
|
|
|
if (attribute.html5DataAnnotations !== undefined) {
|
|
|
|
break keycloak_prior_to_24;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { group = "", groupDisplayHeader = "", groupDisplayDescription = "" } = attribute as any as LegacyAttribute;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{group !== groupNameRef.current && (groupNameRef.current = group) !== "" && (
|
|
|
|
<div className={formGroupClassName}>
|
|
|
|
<div className={getClassName("kcContentWrapperClass")}>
|
|
|
|
<label id={`header-${group}`} className={getClassName("kcFormGroupHeader")}>
|
|
|
|
{advancedMsg(groupDisplayHeader) || groupNameRef.current}
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
{groupDisplayDescription !== "" && (
|
|
|
|
<div className={getClassName("kcLabelWrapperClass")}>
|
|
|
|
<label id={`description-${group}`} className={getClassName("kcLabelClass")}>
|
|
|
|
{advancedMsg(groupDisplayDescription)}
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attribute.group?.name !== groupNameRef.current) {
|
|
|
|
groupNameRef.current = attribute.group?.name ?? "";
|
|
|
|
|
|
|
|
if (groupNameRef.current !== "") {
|
|
|
|
assert(attribute.group !== undefined);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={getClassName("kcFormGroupClass")}
|
|
|
|
{...Object.fromEntries(Object.entries(attribute.group.html5DataAnnotations).map(([key, value]) => [`data-${key}`, value]))}
|
|
|
|
>
|
|
|
|
{(() => {
|
|
|
|
const groupDisplayHeader = attribute.group.displayHeader ?? "";
|
|
|
|
const groupHeaderText = groupDisplayHeader !== "" ? advancedMsg(groupDisplayHeader) : attribute.group.name;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={getClassName("kcContentWrapperClass")}>
|
|
|
|
<label id={`header-${attribute.group.name}`} className={getClassName("kcFormGroupHeader")}>
|
|
|
|
{groupHeaderText}
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})()}
|
|
|
|
{(() => {
|
|
|
|
const groupDisplayDescription = attribute.group.displayDescription ?? "";
|
|
|
|
|
|
|
|
if (groupDisplayDescription !== "") {
|
|
|
|
const groupDescriptionText = advancedMsg(groupDisplayDescription);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={getClassName("kcLabelWrapperClass")}>
|
|
|
|
<label id={`description-${attribute.group.name}`} className={getClassName("kcLabelClass")}>
|
|
|
|
{groupDescriptionText}
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
})()}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-04-27 19:35:42 +02:00
|
|
|
function FieldErrors(props: {
|
|
|
|
attribute: Attribute;
|
|
|
|
getClassName: UserProfileFormFieldsProps["getClassName"];
|
|
|
|
displayableErrors: FormFieldError[];
|
2024-05-05 18:51:33 +02:00
|
|
|
fieldIndex: number | undefined;
|
2024-04-27 19:35:42 +02:00
|
|
|
}) {
|
2024-05-05 18:51:33 +02:00
|
|
|
const { attribute, getClassName, displayableErrors, fieldIndex } = props;
|
2024-04-27 19:35:42 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<span
|
2024-05-05 18:51:33 +02:00
|
|
|
id={`input-error-${attribute.name}${fieldIndex === undefined ? "" : `-${fieldIndex}`}`}
|
2024-04-27 19:35:42 +02:00
|
|
|
className={getClassName("kcInputErrorMessageClass")}
|
|
|
|
style={{
|
|
|
|
"position": displayableErrors.length === 1 ? "absolute" : undefined
|
|
|
|
}}
|
|
|
|
aria-live="polite"
|
|
|
|
>
|
2024-05-05 18:51:33 +02:00
|
|
|
{displayableErrors
|
|
|
|
.filter(error => error.fieldIndex === fieldIndex)
|
|
|
|
.map(({ errorMessage }, i, arr) => (
|
|
|
|
<>
|
|
|
|
<span key={i}>{errorMessage}</span>
|
|
|
|
{arr.length - 1 !== i && <br />}
|
|
|
|
</>
|
|
|
|
))}
|
2024-04-27 19:35:42 +02:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-04-27 19:09:22 +02:00
|
|
|
function AddRemoveButtonsMultiValuedAttribute(props: {
|
|
|
|
formFieldStates: FormFieldState[];
|
|
|
|
attribute: Attribute;
|
|
|
|
index: number;
|
|
|
|
dispatchFormAction: React.Dispatch<
|
|
|
|
Extract<FormAction, { action: "add value to multi-valued attribute" | "remove value from multi-valued attribute" }>
|
|
|
|
>;
|
|
|
|
i18n: I18n;
|
|
|
|
}) {
|
|
|
|
const { formFieldStates, attribute, index, dispatchFormAction, i18n } = props;
|
|
|
|
|
|
|
|
const { msg } = i18n;
|
|
|
|
|
|
|
|
const currentCount = formFieldStates.filter(({ attribute: attribute_i }) => attribute_i.name === attribute.name).length;
|
|
|
|
|
|
|
|
const hasRemove = (() => {
|
|
|
|
if (currentCount === 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const minCount = (() => {
|
|
|
|
const { multivalued } = attribute.validators;
|
|
|
|
|
|
|
|
if (multivalued === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const minStr = multivalued.min;
|
|
|
|
|
|
|
|
if (minStr === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseInt(minStr);
|
|
|
|
})();
|
|
|
|
|
|
|
|
if (minCount === undefined) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentCount === minCount) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
})();
|
|
|
|
|
|
|
|
const hasAdd = (() => {
|
|
|
|
if (index + 1 !== currentCount) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const maxCount = (() => {
|
|
|
|
const { multivalued } = attribute.validators;
|
|
|
|
|
|
|
|
if (multivalued === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const maxStr = multivalued.max;
|
|
|
|
|
|
|
|
if (maxStr === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseInt(maxStr);
|
|
|
|
})();
|
|
|
|
|
|
|
|
if (maxCount === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentCount === maxCount) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
})();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{hasRemove && (
|
|
|
|
<button
|
|
|
|
id={`kc-remove-${attribute.name}-${index + 1}`}
|
|
|
|
type="button"
|
|
|
|
className="pf-c-button pf-m-inline pf-m-link"
|
|
|
|
onClick={() =>
|
|
|
|
dispatchFormAction({
|
|
|
|
"action": "remove value from multi-valued attribute",
|
|
|
|
"name": attribute.name,
|
|
|
|
index
|
|
|
|
})
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{msg("remove")}
|
|
|
|
{hasRemove ? <> | </> : null}
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
{hasAdd && (
|
|
|
|
<button
|
|
|
|
id="kc-add-titles-1"
|
|
|
|
type="button"
|
|
|
|
className="pf-c-button pf-m-inline pf-m-link"
|
|
|
|
onClick={() =>
|
|
|
|
dispatchFormAction({
|
|
|
|
"action": "add value to multi-valued attribute",
|
|
|
|
"name": attribute.name
|
|
|
|
})
|
|
|
|
}
|
|
|
|
>
|
2024-04-28 19:35:16 +02:00
|
|
|
{msg("addValue")}
|
2024-04-27 19:09:22 +02:00
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-04-28 19:35:16 +02:00
|
|
|
type PropsOfInputFiledByType = {
|
2024-04-27 19:09:22 +02:00
|
|
|
attribute: Attribute;
|
2024-05-05 18:51:33 +02:00
|
|
|
valueOrValues: string | string[];
|
2024-04-28 19:35:16 +02:00
|
|
|
displayableErrors: FormFieldError[];
|
2024-04-27 19:09:22 +02:00
|
|
|
formValidationDispatch: React.Dispatch<FormAction>;
|
|
|
|
getClassName: UserProfileFormFieldsProps["getClassName"];
|
|
|
|
i18n: I18n;
|
2024-04-28 19:35:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function InputFiledByType(props: PropsOfInputFiledByType) {
|
2024-05-05 18:51:33 +02:00
|
|
|
const { attribute, valueOrValues } = props;
|
2024-04-27 19:09:22 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
<#macro inputFieldByType attribute>
|
|
|
|
<#switch attribute.annotations.inputType!''>
|
|
|
|
<#case 'textarea'>
|
|
|
|
<@textareaTag attribute=attribute/>
|
|
|
|
<#break>
|
|
|
|
<#case 'select'>
|
|
|
|
<#case 'multiselect'>
|
|
|
|
<@selectTag attribute=attribute/>
|
|
|
|
<#break>
|
|
|
|
<#case 'select-radiobuttons'>
|
|
|
|
<#case 'multiselect-checkboxes'>
|
|
|
|
<@inputTagSelects attribute=attribute/>
|
|
|
|
<#break>
|
|
|
|
<#default>
|
|
|
|
<#if attribute.multivalued && attribute.values?has_content>
|
|
|
|
<#list attribute.values as value>
|
|
|
|
<@inputTag attribute=attribute value=value!''/>
|
|
|
|
</#list>
|
|
|
|
<#else>
|
|
|
|
<@inputTag attribute=attribute value=attribute.value!''/>
|
|
|
|
</#if>
|
|
|
|
</#switch>
|
|
|
|
</#macro>
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (attribute.annotations.inputType) {
|
|
|
|
case "textarea":
|
2024-04-28 19:35:16 +02:00
|
|
|
return <TextareaTag {...props} />;
|
2024-04-27 19:09:22 +02:00
|
|
|
case "select":
|
|
|
|
case "multiselect":
|
2024-04-28 19:35:16 +02:00
|
|
|
return <SelectTag {...props} />;
|
2024-04-27 19:09:22 +02:00
|
|
|
case "select-radiobuttons":
|
|
|
|
case "multiselect-checkboxes":
|
2024-04-28 19:35:16 +02:00
|
|
|
return <InputTagSelects {...props} />;
|
2024-04-27 19:09:22 +02:00
|
|
|
default:
|
2024-05-05 18:51:33 +02:00
|
|
|
if (valueOrValues instanceof Array) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{valueOrValues.map((...[, i]) => (
|
|
|
|
<InputTag key={i} {...props} fieldIndex={i} />
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return <InputTag {...props} fieldIndex={undefined} />;
|
2024-04-27 19:09:22 +02:00
|
|
|
}
|
|
|
|
}
|
2024-04-28 19:35:16 +02:00
|
|
|
|
2024-05-05 18:51:33 +02:00
|
|
|
function InputTag(props: PropsOfInputFiledByType & { fieldIndex: number | undefined }) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-04-30 12:07:35 +02:00
|
|
|
function InputTagSelects(props: PropsOfInputFiledByType) {
|
|
|
|
/*
|
|
|
|
<#macro inputTagSelects attribute>
|
|
|
|
<#if attribute.annotations.inputType=='select-radiobuttons'>
|
|
|
|
<#assign inputType='radio'>
|
|
|
|
<#assign classDiv=properties.kcInputClassRadio!>
|
|
|
|
<#assign classInput=properties.kcInputClassRadioInput!>
|
|
|
|
<#assign classLabel=properties.kcInputClassRadioLabel!>
|
|
|
|
<#else>
|
|
|
|
<#assign inputType='checkbox'>
|
|
|
|
<#assign classDiv=properties.kcInputClassCheckbox!>
|
|
|
|
<#assign classInput=properties.kcInputClassCheckboxInput!>
|
|
|
|
<#assign classLabel=properties.kcInputClassCheckboxLabel!>
|
|
|
|
</#if>
|
|
|
|
|
|
|
|
<#if attribute.annotations.inputOptionsFromValidation?? && attribute.validators[attribute.annotations.inputOptionsFromValidation]?? && attribute.validators[attribute.annotations.inputOptionsFromValidation].options??>
|
|
|
|
<#assign options=attribute.validators[attribute.annotations.inputOptionsFromValidation].options>
|
|
|
|
<#elseif attribute.validators.options?? && attribute.validators.options.options??>
|
|
|
|
<#assign options=attribute.validators.options.options>
|
|
|
|
<#else>
|
|
|
|
<#assign options=[]>
|
|
|
|
</#if>
|
|
|
|
|
|
|
|
<#list options as option>
|
|
|
|
<div class="${classDiv}">
|
|
|
|
<input type="${inputType}" id="${attribute.name}-${option}" name="${attribute.name}" value="${option}" class="${classInput}"
|
|
|
|
aria-invalid="<#if messagesPerField.existsError('${attribute.name}')>true</#if>"
|
|
|
|
<#if attribute.readOnly>disabled</#if>
|
|
|
|
<#if attribute.values?seq_contains(option)>checked</#if>
|
|
|
|
/>
|
|
|
|
<label for="${attribute.name}-${option}" class="${classLabel}<#if attribute.readOnly> ${properties.kcInputClassRadioCheckboxLabelDisabled!}</#if>"><@selectOptionLabelText attribute=attribute option=option/></label>
|
|
|
|
</div>
|
|
|
|
</#list>
|
|
|
|
</#macro>
|
|
|
|
*/
|
|
|
|
|
2024-05-05 18:51:33 +02:00
|
|
|
const { attribute, formValidationDispatch, getClassName, valueOrValues } = props;
|
2024-04-30 12:07:35 +02:00
|
|
|
|
|
|
|
const { advancedMsg } = props.i18n;
|
|
|
|
|
2024-05-05 18:51:33 +02:00
|
|
|
const { classDiv, classInput, classLabel, inputType } = (() => {
|
|
|
|
const { inputType } = attribute.annotations;
|
|
|
|
|
|
|
|
assert(inputType === "select-radiobuttons" || inputType === "multiselect-checkboxes");
|
|
|
|
|
|
|
|
switch (inputType) {
|
|
|
|
case "select-radiobuttons":
|
|
|
|
return {
|
|
|
|
"inputType": "radio",
|
|
|
|
"classDiv": getClassName("kcInputClassRadio"),
|
|
|
|
"classInput": getClassName("kcInputClassRadioInput"),
|
|
|
|
"classLabel": getClassName("kcInputClassRadioLabel")
|
|
|
|
};
|
|
|
|
case "multiselect-checkboxes":
|
|
|
|
return {
|
|
|
|
"inputType": "checkbox",
|
|
|
|
"classDiv": getClassName("kcInputClassCheckbox"),
|
|
|
|
"classInput": getClassName("kcInputClassCheckboxInput"),
|
|
|
|
"classLabel": getClassName("kcInputClassCheckboxLabel")
|
|
|
|
};
|
|
|
|
}
|
|
|
|
})();
|
2024-04-30 12:07:35 +02:00
|
|
|
|
|
|
|
const options = (() => {
|
|
|
|
walk: {
|
|
|
|
const { inputOptionsFromValidation } = attribute.annotations;
|
|
|
|
|
|
|
|
if (inputOptionsFromValidation === undefined) {
|
|
|
|
break walk;
|
|
|
|
}
|
|
|
|
|
|
|
|
const validator = (attribute.validators as Record<string, { options?: string[] }>)[inputOptionsFromValidation];
|
|
|
|
|
|
|
|
if (validator === undefined) {
|
|
|
|
break walk;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (validator.options === undefined) {
|
|
|
|
break walk;
|
|
|
|
}
|
|
|
|
|
|
|
|
return validator.options;
|
|
|
|
}
|
|
|
|
|
|
|
|
return attribute.validators.options?.options ?? [];
|
|
|
|
})();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{options.map(option => (
|
|
|
|
<div key={option} className={classDiv}>
|
|
|
|
<input
|
|
|
|
type={inputType}
|
2024-05-05 18:51:33 +02:00
|
|
|
id={`${attribute.name}-${option}`}
|
2024-04-30 12:07:35 +02:00
|
|
|
name={attribute.name}
|
|
|
|
value={option}
|
|
|
|
className={classInput}
|
|
|
|
aria-invalid={props.displayableErrors.length !== 0}
|
|
|
|
disabled={attribute.readOnly}
|
2024-05-05 18:51:33 +02:00
|
|
|
checked={valueOrValues.includes(option)}
|
|
|
|
onChange={event =>
|
2024-04-30 12:07:35 +02:00
|
|
|
formValidationDispatch({
|
2024-05-05 18:51:33 +02:00
|
|
|
"action": "update",
|
2024-04-30 12:07:35 +02:00
|
|
|
"name": attribute.name,
|
2024-05-05 18:51:33 +02:00
|
|
|
"valueOrValues": (() => {
|
|
|
|
const isChecked = event.target.checked;
|
|
|
|
|
|
|
|
if (valueOrValues instanceof Array) {
|
|
|
|
const newValues = [...valueOrValues];
|
|
|
|
|
|
|
|
if (isChecked) {
|
|
|
|
newValues.push(option);
|
|
|
|
} else {
|
|
|
|
newValues.splice(newValues.indexOf(option), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newValues;
|
|
|
|
}
|
|
|
|
|
|
|
|
return event.target.checked ? option : "";
|
|
|
|
})()
|
2024-04-30 12:07:35 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
onBlur={() =>
|
|
|
|
formValidationDispatch({
|
|
|
|
"action": "focus lost",
|
|
|
|
"name": attribute.name,
|
2024-05-05 18:51:33 +02:00
|
|
|
"fieldIndex": undefined
|
2024-04-30 12:07:35 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<label
|
|
|
|
htmlFor={`${attribute.name}-${option}`}
|
|
|
|
className={`${classLabel}${attribute.readOnly ? ` ${getClassName("kcInputClassRadioCheckboxLabelDisabled")}` : ""}`}
|
|
|
|
>
|
|
|
|
{advancedMsg(option)}
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-04-28 19:35:16 +02:00
|
|
|
function TextareaTag(props: PropsOfInputFiledByType) {
|
2024-05-05 18:51:33 +02:00
|
|
|
const { attribute, formValidationDispatch, getClassName, displayableErrors, valueOrValues } = props;
|
|
|
|
|
|
|
|
assert(typeof valueOrValues === "string");
|
|
|
|
|
|
|
|
const value = valueOrValues;
|
2024-04-28 19:35:16 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<textarea
|
2024-05-05 18:51:33 +02:00
|
|
|
id={attribute.name}
|
2024-04-28 19:35:16 +02:00
|
|
|
name={attribute.name}
|
|
|
|
className={getClassName("kcInputClass")}
|
|
|
|
aria-invalid={displayableErrors.length !== 0}
|
|
|
|
disabled={attribute.readOnly}
|
|
|
|
cols={attribute.annotations.inputTypeCols === undefined ? undefined : parseInt(attribute.annotations.inputTypeCols)}
|
|
|
|
rows={attribute.annotations.inputTypeRows === undefined ? undefined : parseInt(attribute.annotations.inputTypeRows)}
|
|
|
|
maxLength={attribute.annotations.inputTypeMaxlength === undefined ? undefined : parseInt(attribute.annotations.inputTypeMaxlength)}
|
|
|
|
value={value}
|
|
|
|
onChange={event =>
|
|
|
|
formValidationDispatch({
|
2024-05-05 18:51:33 +02:00
|
|
|
"action": "update",
|
2024-04-28 19:35:16 +02:00
|
|
|
"name": attribute.name,
|
2024-05-05 18:51:33 +02:00
|
|
|
"valueOrValues": event.target.value
|
2024-04-28 19:35:16 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
onBlur={() =>
|
|
|
|
formValidationDispatch({
|
|
|
|
"action": "focus lost",
|
|
|
|
"name": attribute.name,
|
2024-05-05 18:51:33 +02:00
|
|
|
"fieldIndex": undefined
|
2024-04-28 19:35:16 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2024-04-30 12:07:35 +02:00
|
|
|
|
|
|
|
function SelectTag(props: PropsOfInputFiledByType) {
|
2024-05-05 18:51:33 +02:00
|
|
|
const { attribute, formValidationDispatch, getClassName, displayableErrors, i18n, valueOrValues } = props;
|
2024-04-30 12:07:35 +02:00
|
|
|
|
|
|
|
const { advancedMsg } = i18n;
|
|
|
|
|
|
|
|
const isMultiple = attribute.annotations.inputType === "multiselect";
|
|
|
|
|
|
|
|
return (
|
|
|
|
<select
|
2024-05-05 18:51:33 +02:00
|
|
|
id={attribute.name}
|
2024-04-30 12:07:35 +02:00
|
|
|
name={attribute.name}
|
|
|
|
className={getClassName("kcInputClass")}
|
|
|
|
aria-invalid={displayableErrors.length !== 0}
|
|
|
|
disabled={attribute.readOnly}
|
|
|
|
multiple={isMultiple}
|
|
|
|
size={attribute.annotations.inputTypeSize === undefined ? undefined : parseInt(attribute.annotations.inputTypeSize)}
|
2024-05-05 18:51:33 +02:00
|
|
|
value={valueOrValues}
|
2024-04-30 12:07:35 +02:00
|
|
|
onChange={event =>
|
|
|
|
formValidationDispatch({
|
2024-05-05 18:51:33 +02:00
|
|
|
"action": "update",
|
2024-04-30 12:07:35 +02:00
|
|
|
"name": attribute.name,
|
2024-05-05 18:51:33 +02:00
|
|
|
"valueOrValues": (() => {
|
|
|
|
if (isMultiple) {
|
|
|
|
return Array.from(event.target.selectedOptions).map(option => option.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return event.target.value;
|
|
|
|
})()
|
2024-04-30 12:07:35 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
onBlur={() =>
|
|
|
|
formValidationDispatch({
|
|
|
|
"action": "focus lost",
|
|
|
|
"name": attribute.name,
|
2024-05-05 18:51:33 +02:00
|
|
|
"fieldIndex": undefined
|
2024-04-30 12:07:35 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
>
|
2024-05-05 18:51:33 +02:00
|
|
|
{!isMultiple && <option value=""></option>}
|
2024-04-30 12:07:35 +02:00
|
|
|
{(() => {
|
|
|
|
const options = (() => {
|
|
|
|
walk: {
|
|
|
|
const { inputOptionsFromValidation } = attribute.annotations;
|
|
|
|
|
|
|
|
assert(typeof inputOptionsFromValidation === "string");
|
|
|
|
|
|
|
|
if (inputOptionsFromValidation === undefined) {
|
|
|
|
break walk;
|
|
|
|
}
|
|
|
|
|
|
|
|
const validator = (attribute.validators as Record<string, { options?: string[] }>)[inputOptionsFromValidation];
|
|
|
|
|
|
|
|
if (validator === undefined) {
|
|
|
|
break walk;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (validator.options === undefined) {
|
|
|
|
break walk;
|
|
|
|
}
|
|
|
|
|
|
|
|
return validator.options;
|
|
|
|
}
|
|
|
|
|
|
|
|
return attribute.validators.options?.options ?? [];
|
|
|
|
})();
|
|
|
|
|
|
|
|
return options.map(option => (
|
2024-05-05 18:51:33 +02:00
|
|
|
<option key={option} value={option}>
|
2024-04-30 12:07:35 +02:00
|
|
|
{(() => {
|
|
|
|
if (attribute.annotations.inputOptionLabels !== undefined) {
|
|
|
|
const { inputOptionLabels } = attribute.annotations;
|
|
|
|
|
|
|
|
return advancedMsg(inputOptionLabels[option] ?? option);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attribute.annotations.inputOptionLabelsI18nPrefix !== undefined) {
|
|
|
|
return advancedMsg(`${attribute.annotations.inputOptionLabelsI18nPrefix}.${option}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return option;
|
|
|
|
})()}
|
|
|
|
</option>
|
|
|
|
));
|
|
|
|
})()}
|
|
|
|
</select>
|
|
|
|
);
|
|
|
|
}
|