Load number unformat for pre form submission

This commit is contained in:
Joseph Garrone 2024-05-06 15:25:43 +02:00
parent b4d924adfa
commit d04950cbc9
2 changed files with 44 additions and 1 deletions

View File

@ -535,7 +535,9 @@ export type Attribute = {
name: string; name: string;
displayDescription?: string; displayDescription?: string;
}; };
html5DataAnnotations: Record<string, string>; html5DataAnnotations: {
kcNumberFormat?: string;
};
readOnly: boolean; readOnly: boolean;
validators: Validators; validators: Validators;
annotations: { annotations: {

View File

@ -8,6 +8,8 @@ import { emailRegexp } from "keycloakify/tools/emailRegExp";
import type { KcContext, PasswordPolicies } from "keycloakify/login/kcContext/KcContext"; import type { KcContext, PasswordPolicies } from "keycloakify/login/kcContext/KcContext";
import { assert, type Equals } from "tsafe/assert"; import { assert, type Equals } from "tsafe/assert";
import type { I18n } from "../i18n"; import type { I18n } from "../i18n";
import { formatNumber } from "keycloakify/tools/formatNumber";
import { usePrepareTemplate } from "keycloakify/lib/usePrepareTemplate";
export type FormFieldError = { export type FormFieldError = {
errorMessage: JSX.Element; errorMessage: JSX.Element;
@ -66,10 +68,14 @@ export type KcContextLike = {
messagesPerField: Pick<KcContext.Common["messagesPerField"], "existsError" | "get">; messagesPerField: Pick<KcContext.Common["messagesPerField"], "existsError" | "get">;
profile: { profile: {
attributes: Attribute[]; attributes: Attribute[];
html5DataAnnotations: Record<string, string>;
}; };
passwordRequired?: boolean; passwordRequired?: boolean;
realm: { registrationEmailAsUsername: boolean }; realm: { registrationEmailAsUsername: boolean };
passwordPolicies?: PasswordPolicies; passwordPolicies?: PasswordPolicies;
url: {
resourcesPath: string;
};
}; };
export type ParamsOfUseUserProfileForm = { export type ParamsOfUseUserProfileForm = {
@ -103,6 +109,24 @@ namespace internal {
export function useUserProfileForm(params: ParamsOfUseUserProfileForm): ReturnTypeOfUseUserProfileForm { export function useUserProfileForm(params: ParamsOfUseUserProfileForm): ReturnTypeOfUseUserProfileForm {
const { kcContext, i18n, doMakeUserConfirmPassword } = params; const { kcContext, i18n, doMakeUserConfirmPassword } = params;
usePrepareTemplate({
"styles": [],
// NOTE: The ?? {} is for compat with Keycloak version prior to 24
"scripts": Object.keys(kcContext.profile.html5DataAnnotations ?? {})
.filter(key => key !== "kcMultivalued" && key !== "kcNumberFormat") // NOTE: Keycloakify handles it.
.map(key => ({
"isModule": true,
"source": {
"type": "url",
"src": `${kcContext.url.resourcesPath}/js/${key}.js`
}
})),
"htmlClassName": undefined,
"bodyClassName": undefined,
"htmlLangProperty": undefined,
"documentTitle": undefined
});
const attributesWithPassword = useMemo(() => { const attributesWithPassword = useMemo(() => {
const attributesWithPassword: Attribute[] = []; const attributesWithPassword: Attribute[] = [];
@ -164,6 +188,23 @@ export function useUserProfileForm(params: ParamsOfUseUserProfileForm): ReturnTy
case "update": case "update":
formFieldState.valueOrValues = params.valueOrValues; formFieldState.valueOrValues = params.valueOrValues;
apply_formatters: {
const { attribute } = formFieldState;
// NOTE: The `?? {}` is for compat with Keycloak version prior to 24
const { kcNumberFormat } = attribute.html5DataAnnotations ?? {};
if (kcNumberFormat === undefined) {
break apply_formatters;
}
if (formFieldState.valueOrValues instanceof Array) {
formFieldState.valueOrValues = formFieldState.valueOrValues.map(value => formatNumber(value, kcNumberFormat));
} else {
formFieldState.valueOrValues = formatNumber(formFieldState.valueOrValues, kcNumberFormat);
}
}
formFieldState.errors = getErrors({ formFieldState.errors = getErrors({
"attributeName": params.name, "attributeName": params.name,
"formFieldStates": state.formFieldStates "formFieldStates": state.formFieldStates