keycloakify 11.3.8 → 11.3.9-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/login/lib/getUserProfileApi/getUserProfileApi.d.ts +88 -0
- package/login/lib/getUserProfileApi/getUserProfileApi.js +1076 -0
- package/login/lib/getUserProfileApi/getUserProfileApi.js.map +1 -0
- package/login/lib/getUserProfileApi/index.d.ts +1 -0
- package/login/lib/getUserProfileApi/index.js +2 -0
- package/login/lib/getUserProfileApi/index.js.map +1 -0
- package/login/lib/getUserProfileApi/kcNumberUnFormat.d.ts +1 -0
- package/login/lib/getUserProfileApi/kcNumberUnFormat.js +84 -0
- package/login/lib/getUserProfileApi/kcNumberUnFormat.js.map +1 -0
- package/login/lib/useUserProfileForm.d.ts +9 -34
- package/login/lib/useUserProfileForm.js +44 -998
- package/login/lib/useUserProfileForm.js.map +1 -1
- package/package.json +13 -1
- package/src/login/lib/getUserProfileApi/getUserProfileApi.ts +1561 -0
- package/src/login/lib/getUserProfileApi/index.ts +1 -0
- package/src/login/lib/getUserProfileApi/kcNumberUnFormat.ts +109 -0
- package/src/login/lib/useUserProfileForm.tsx +82 -1322
@@ -0,0 +1 @@
|
|
1
|
+
export * from "./getUserProfileApi";
|
@@ -0,0 +1,109 @@
|
|
1
|
+
import { assert } from "keycloakify/tools/assert";
|
2
|
+
let cleanup: (() => void) | undefined;
|
3
|
+
const handledElements = new WeakSet<HTMLElement>();
|
4
|
+
const KC_NUMBER_UNFORMAT = "kcNumberUnFormat";
|
5
|
+
const SELECTOR = `input[data-${KC_NUMBER_UNFORMAT}]`;
|
6
|
+
|
7
|
+
export function unFormatNumberOnSubmit() {
|
8
|
+
cleanup?.();
|
9
|
+
|
10
|
+
const handleElement = (element: HTMLInputElement) => {
|
11
|
+
if (handledElements.has(element)) {
|
12
|
+
return;
|
13
|
+
}
|
14
|
+
|
15
|
+
const form = element.closest("form");
|
16
|
+
|
17
|
+
if (form === null) {
|
18
|
+
return;
|
19
|
+
}
|
20
|
+
|
21
|
+
form.addEventListener("submit", () => {
|
22
|
+
const rawFormat = element.getAttribute(`data-${KC_NUMBER_UNFORMAT}`);
|
23
|
+
if (rawFormat) {
|
24
|
+
element.value = formatNumber(element.value, rawFormat);
|
25
|
+
}
|
26
|
+
});
|
27
|
+
|
28
|
+
handledElements.add(element);
|
29
|
+
};
|
30
|
+
|
31
|
+
document.querySelectorAll(SELECTOR).forEach(element => {
|
32
|
+
assert(element instanceof HTMLInputElement);
|
33
|
+
handleElement(element);
|
34
|
+
});
|
35
|
+
|
36
|
+
const observer = new MutationObserver(mutationsList => {
|
37
|
+
for (const mutation of mutationsList) {
|
38
|
+
if (mutation.type === "childList" && mutation.addedNodes.length > 0) {
|
39
|
+
mutation.addedNodes.forEach(node => {
|
40
|
+
if (node.nodeType === Node.ELEMENT_NODE) {
|
41
|
+
const element = (node as HTMLElement).querySelector(SELECTOR);
|
42
|
+
if (element !== null) {
|
43
|
+
assert(element instanceof HTMLInputElement);
|
44
|
+
handleElement(element);
|
45
|
+
}
|
46
|
+
}
|
47
|
+
});
|
48
|
+
}
|
49
|
+
}
|
50
|
+
});
|
51
|
+
|
52
|
+
observer.observe(document.body, { childList: true, subtree: true });
|
53
|
+
|
54
|
+
cleanup = () => observer.disconnect();
|
55
|
+
}
|
56
|
+
|
57
|
+
// NOTE: Keycloak code
|
58
|
+
const formatNumber = (input: string, format: string) => {
|
59
|
+
if (!input) {
|
60
|
+
return "";
|
61
|
+
}
|
62
|
+
|
63
|
+
// array holding the patterns for the number of expected digits in each part
|
64
|
+
const digitPattern = format.match(/{\d+}/g);
|
65
|
+
|
66
|
+
if (!digitPattern) {
|
67
|
+
return "";
|
68
|
+
}
|
69
|
+
|
70
|
+
// calculate the maximum size of the given pattern based on the sum of the expected digits
|
71
|
+
const maxSize = digitPattern.reduce(
|
72
|
+
(total, p) => total + parseInt(p.replace("{", "").replace("}", "")),
|
73
|
+
0
|
74
|
+
);
|
75
|
+
|
76
|
+
// keep only digits
|
77
|
+
let rawValue = input.replace(/\D+/g, "");
|
78
|
+
|
79
|
+
// make sure the value is a number
|
80
|
+
//@ts-expect-error
|
81
|
+
if (parseInt(rawValue) != rawValue) {
|
82
|
+
return "";
|
83
|
+
}
|
84
|
+
|
85
|
+
// make sure the number of digits does not exceed the maximum size
|
86
|
+
if (rawValue.length > maxSize) {
|
87
|
+
rawValue = rawValue.substring(0, maxSize);
|
88
|
+
}
|
89
|
+
|
90
|
+
// build the regex based based on the expected digits in each part
|
91
|
+
const formatter = digitPattern.reduce((result, p) => result + `(\\d${p})`, "^");
|
92
|
+
|
93
|
+
// if the current digits match the pattern we have each group of digits in an array
|
94
|
+
let digits = new RegExp(formatter).exec(rawValue);
|
95
|
+
|
96
|
+
// no match, return the raw value without any format
|
97
|
+
if (!digits) {
|
98
|
+
return input;
|
99
|
+
}
|
100
|
+
|
101
|
+
let result = format;
|
102
|
+
|
103
|
+
// finally format the current digits accordingly to the given format
|
104
|
+
for (let i = 0; i < digitPattern.length; i++) {
|
105
|
+
result = result.replace(digitPattern[i], digits[i + 1]);
|
106
|
+
}
|
107
|
+
|
108
|
+
return result;
|
109
|
+
};
|