@appcorp/fusion-storybook 0.5.29 → 0.5.33
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/base-modules/admission/context/use-admission-module.js +23 -29
- package/base-modules/admission/form.js +5 -19
- package/base-modules/admission/validate.d.ts +3 -2
- package/base-modules/admission/validate.js +10 -9
- package/base-modules/campus/context/use-campus-module.js +1 -2
- package/base-modules/campus/form.js +3 -5
- package/base-modules/campus/validate.d.ts +2 -2
- package/base-modules/campus/validate.js +3 -8
- package/base-modules/family/page.js +2 -1
- package/base-modules/family-member/context.js +2 -3
- package/base-modules/family-member/form.js +5 -8
- package/base-modules/family-member/page.js +6 -1
- package/base-modules/family-member/validate.d.ts +3 -2
- package/base-modules/family-member/validate.js +4 -3
- package/base-modules/push-campaign/form.js +13 -5
- package/base-modules/school/context.js +4 -6
- package/base-modules/school/form.js +2 -5
- package/base-modules/school/validate.d.ts +2 -1
- package/base-modules/school/validate.js +3 -6
- package/base-modules/student-profile/context/use-student-profile-module.js +2 -3
- package/base-modules/student-profile/form.js +3 -9
- package/base-modules/student-profile/validate.d.ts +2 -2
- package/base-modules/student-profile/validate.js +3 -9
- package/base-modules/teacher/form.js +5 -8
- package/base-modules/teacher/page.js +6 -1
- package/base-modules/teacher/validate.d.ts +3 -2
- package/base-modules/teacher/validate.js +4 -11
- package/base-modules/user/context/use-user-module.js +1 -2
- package/base-modules/user/form.js +5 -5
- package/base-modules/user/page.js +2 -1
- package/base-modules/user/validate.d.ts +1 -1
- package/base-modules/user/validate.js +2 -5
- package/components/phone-input.d.ts +21 -0
- package/components/phone-input.js +51 -0
- package/package.json +4 -1
- package/tsconfig.build.tsbuildinfo +1 -1
- package/utils/phone-validation.d.ts +15 -0
- package/utils/phone-validation.js +35 -0
- package/utils/phone.d.ts +25 -0
- package/utils/phone.js +145 -0
- package/utils/to-e164-phone.d.ts +9 -0
- package/utils/to-e164-phone.js +17 -5
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
interface PhoneSchemaOptions {
|
|
3
|
+
required?: boolean;
|
|
4
|
+
requiredErrorKey?: string;
|
|
5
|
+
maxLength?: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Zod schema for a phone field backed by `PhoneInput`.
|
|
9
|
+
*
|
|
10
|
+
* - Empty values are allowed unless `required` is true.
|
|
11
|
+
* - Non-empty values must parse as a valid international phone number.
|
|
12
|
+
* - Valid values are transformed to E.164 for the API/database.
|
|
13
|
+
*/
|
|
14
|
+
export declare function phoneSchema(errorKey: string, { required, requiredErrorKey, maxLength, }?: PhoneSchemaOptions): z.ZodPreprocess<z.ZodPipe<z.ZodNullable<z.ZodOptional<z.ZodString>>, z.ZodTransform<string | null | undefined, string | null | undefined>>, unknown>;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { toE164Phone } from "./to-e164-phone";
|
|
3
|
+
import { isValidPhone } from "./phone";
|
|
4
|
+
/**
|
|
5
|
+
* Zod schema for a phone field backed by `PhoneInput`.
|
|
6
|
+
*
|
|
7
|
+
* - Empty values are allowed unless `required` is true.
|
|
8
|
+
* - Non-empty values must parse as a valid international phone number.
|
|
9
|
+
* - Valid values are transformed to E.164 for the API/database.
|
|
10
|
+
*/
|
|
11
|
+
export function phoneSchema(errorKey, { required = false, requiredErrorKey, maxLength, } = {}) {
|
|
12
|
+
return z.preprocess((value) => (typeof value === "string" ? value.trim() : value), z
|
|
13
|
+
.string()
|
|
14
|
+
.optional()
|
|
15
|
+
.nullable()
|
|
16
|
+
.superRefine((value, ctx) => {
|
|
17
|
+
if (!value) {
|
|
18
|
+
if (required) {
|
|
19
|
+
ctx.addIssue({
|
|
20
|
+
code: "custom",
|
|
21
|
+
message: requiredErrorKey !== null && requiredErrorKey !== void 0 ? requiredErrorKey : errorKey,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (maxLength && value.length > maxLength) {
|
|
27
|
+
ctx.addIssue({ code: "custom", message: errorKey });
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if (!isValidPhone(value)) {
|
|
31
|
+
ctx.addIssue({ code: "custom", message: errorKey });
|
|
32
|
+
}
|
|
33
|
+
})
|
|
34
|
+
.transform((value) => { var _a; return (value ? ((_a = toE164Phone(value)) !== null && _a !== void 0 ? _a : value) : value); }));
|
|
35
|
+
}
|
package/utils/phone.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared phone helpers for the two-field phone input.
|
|
3
|
+
*
|
|
4
|
+
* The form state and database always store one full E.164 string
|
|
5
|
+
* (`+923212434302`). These helpers only split that value into a country and a
|
|
6
|
+
* national number for display, and join it back on change.
|
|
7
|
+
*/
|
|
8
|
+
export declare const FALLBACK_COUNTRY = "PK";
|
|
9
|
+
export declare function flagForCountry(code: string): string;
|
|
10
|
+
export declare function countryNameFor(code: string, fallback?: string): string;
|
|
11
|
+
export declare function dialCodeFor(country: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* True when the value is empty or starts with the selected country's dial
|
|
14
|
+
* code. Used to preserve an explicit country selection for shared dial codes
|
|
15
|
+
* (for example, US vs Canada both use `+1`).
|
|
16
|
+
*/
|
|
17
|
+
export declare function isDialCodeCompatible(value: string | null | undefined, country: string): boolean;
|
|
18
|
+
export declare function toIsoCountry(value?: string | null): string;
|
|
19
|
+
export declare function splitPhoneValue(value: string | null | undefined, defaultCountry?: string | null): {
|
|
20
|
+
country: string;
|
|
21
|
+
national: string;
|
|
22
|
+
};
|
|
23
|
+
export declare function joinPhoneValue(country: string, national: string): string;
|
|
24
|
+
export declare function formatNationalNumber(country: string, national: string): string;
|
|
25
|
+
export declare function isValidPhone(value: string): boolean;
|
package/utils/phone.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { AsYouType, parsePhoneNumberFromString, } from "libphonenumber-js";
|
|
2
|
+
import { dialCodes } from "@react-pakistan/util-functions/constants/dial-codes";
|
|
3
|
+
/**
|
|
4
|
+
* Shared phone helpers for the two-field phone input.
|
|
5
|
+
*
|
|
6
|
+
* The form state and database always store one full E.164 string
|
|
7
|
+
* (`+923212434302`). These helpers only split that value into a country and a
|
|
8
|
+
* national number for display, and join it back on change.
|
|
9
|
+
*/
|
|
10
|
+
export const FALLBACK_COUNTRY = "PK";
|
|
11
|
+
const displayNames = new Intl.DisplayNames(["en"], { type: "region" });
|
|
12
|
+
const COUNTRY_ALIASES = {
|
|
13
|
+
uk: "GB",
|
|
14
|
+
"u.k.": "GB",
|
|
15
|
+
"great britain": "GB",
|
|
16
|
+
usa: "US",
|
|
17
|
+
"u.s.a.": "US",
|
|
18
|
+
"united states of america": "US",
|
|
19
|
+
uae: "AE",
|
|
20
|
+
"u.a.e.": "AE",
|
|
21
|
+
ksa: "SA",
|
|
22
|
+
"saudi arabia": "SA",
|
|
23
|
+
};
|
|
24
|
+
export function flagForCountry(code) {
|
|
25
|
+
if (!/^[A-Za-z]{2}$/.test(code))
|
|
26
|
+
return "🏳️";
|
|
27
|
+
return String.fromCodePoint(...code
|
|
28
|
+
.toUpperCase()
|
|
29
|
+
.split("")
|
|
30
|
+
.map((char) => 127397 + char.charCodeAt(0)));
|
|
31
|
+
}
|
|
32
|
+
export function countryNameFor(code, fallback) {
|
|
33
|
+
var _a, _b;
|
|
34
|
+
const normalized = code.toUpperCase();
|
|
35
|
+
try {
|
|
36
|
+
return (_b = (_a = displayNames.of(normalized)) !== null && _a !== void 0 ? _a : fallback) !== null && _b !== void 0 ? _b : normalized;
|
|
37
|
+
}
|
|
38
|
+
catch (_c) {
|
|
39
|
+
return fallback !== null && fallback !== void 0 ? fallback : normalized;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export function dialCodeFor(country) {
|
|
43
|
+
var _a, _b;
|
|
44
|
+
const code = country.toUpperCase();
|
|
45
|
+
return (_b = (_a = dialCodes.find((entry) => entry.code === code)) === null || _a === void 0 ? void 0 : _a.dial_code) !== null && _b !== void 0 ? _b : "+1";
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* True when the value is empty or starts with the selected country's dial
|
|
49
|
+
* code. Used to preserve an explicit country selection for shared dial codes
|
|
50
|
+
* (for example, US vs Canada both use `+1`).
|
|
51
|
+
*/
|
|
52
|
+
export function isDialCodeCompatible(value, country) {
|
|
53
|
+
if (!value)
|
|
54
|
+
return true;
|
|
55
|
+
const raw = String(value).trim();
|
|
56
|
+
if (!raw)
|
|
57
|
+
return true;
|
|
58
|
+
return raw.startsWith(dialCodeFor(country));
|
|
59
|
+
}
|
|
60
|
+
export function toIsoCountry(value) {
|
|
61
|
+
var _a;
|
|
62
|
+
if (!value)
|
|
63
|
+
return FALLBACK_COUNTRY;
|
|
64
|
+
const trimmed = value.trim();
|
|
65
|
+
if (!trimmed)
|
|
66
|
+
return FALLBACK_COUNTRY;
|
|
67
|
+
const alias = COUNTRY_ALIASES[trimmed.toLowerCase()];
|
|
68
|
+
if (alias)
|
|
69
|
+
return alias;
|
|
70
|
+
if (/^[A-Za-z]{2}$/.test(trimmed)) {
|
|
71
|
+
const code = trimmed.toUpperCase();
|
|
72
|
+
if (dialCodes.some((entry) => entry.code === code))
|
|
73
|
+
return code;
|
|
74
|
+
}
|
|
75
|
+
const normalized = trimmed.toLowerCase().replace(/\s+/g, " ");
|
|
76
|
+
const match = dialCodes.find((entry) => {
|
|
77
|
+
const display = countryNameFor(entry.code, entry.name)
|
|
78
|
+
.toLowerCase()
|
|
79
|
+
.replace(/\s+/g, " ");
|
|
80
|
+
const raw = entry.name.toLowerCase().replace(/\s+/g, " ");
|
|
81
|
+
return display === normalized || raw === normalized;
|
|
82
|
+
});
|
|
83
|
+
return (_a = match === null || match === void 0 ? void 0 : match.code) !== null && _a !== void 0 ? _a : FALLBACK_COUNTRY;
|
|
84
|
+
}
|
|
85
|
+
export function splitPhoneValue(value, defaultCountry) {
|
|
86
|
+
var _a;
|
|
87
|
+
const fallback = toIsoCountry(defaultCountry);
|
|
88
|
+
if (!value)
|
|
89
|
+
return { country: fallback, national: "" };
|
|
90
|
+
const raw = String(value).trim();
|
|
91
|
+
if (!raw)
|
|
92
|
+
return { country: fallback, national: "" };
|
|
93
|
+
const parsed = raw.startsWith("+")
|
|
94
|
+
? parsePhoneNumberFromString(raw)
|
|
95
|
+
: ((_a = parsePhoneNumberFromString(raw, fallback)) !== null && _a !== void 0 ? _a : parsePhoneNumberFromString(`+${raw}`));
|
|
96
|
+
if (parsed === null || parsed === void 0 ? void 0 : parsed.country) {
|
|
97
|
+
return { country: parsed.country, national: parsed.nationalNumber };
|
|
98
|
+
}
|
|
99
|
+
if (raw.startsWith("+")) {
|
|
100
|
+
const match = [...dialCodes]
|
|
101
|
+
.sort((a, b) => b.dial_code.length - a.dial_code.length)
|
|
102
|
+
.find((entry) => raw.startsWith(entry.dial_code));
|
|
103
|
+
if (match) {
|
|
104
|
+
return {
|
|
105
|
+
country: match.code,
|
|
106
|
+
national: raw.slice(match.dial_code.length).replace(/\D/g, ""),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return { country: fallback, national: raw.replace(/\D/g, "") };
|
|
111
|
+
}
|
|
112
|
+
export function joinPhoneValue(country, national) {
|
|
113
|
+
const digits = national.replace(/\D/g, "");
|
|
114
|
+
if (!digits)
|
|
115
|
+
return "";
|
|
116
|
+
const iso = toIsoCountry(country);
|
|
117
|
+
const parsedNational = parsePhoneNumberFromString(digits, iso);
|
|
118
|
+
if (parsedNational === null || parsedNational === void 0 ? void 0 : parsedNational.isValid()) {
|
|
119
|
+
return parsedNational.format("E.164");
|
|
120
|
+
}
|
|
121
|
+
const full = `${dialCodeFor(iso)}${digits}`;
|
|
122
|
+
const parsed = parsePhoneNumberFromString(full);
|
|
123
|
+
return (parsed === null || parsed === void 0 ? void 0 : parsed.isValid()) ? parsed.format("E.164") : full;
|
|
124
|
+
}
|
|
125
|
+
export function formatNationalNumber(country, national) {
|
|
126
|
+
const digits = national.replace(/\D/g, "");
|
|
127
|
+
if (!digits)
|
|
128
|
+
return "";
|
|
129
|
+
const iso = toIsoCountry(country);
|
|
130
|
+
const parsed = parsePhoneNumberFromString(digits, iso);
|
|
131
|
+
if (parsed) {
|
|
132
|
+
const dial = dialCodeFor(iso);
|
|
133
|
+
const international = parsed.formatInternational();
|
|
134
|
+
return international.startsWith(dial)
|
|
135
|
+
? international.slice(dial.length).trim()
|
|
136
|
+
: international;
|
|
137
|
+
}
|
|
138
|
+
return new AsYouType(iso).input(digits);
|
|
139
|
+
}
|
|
140
|
+
export function isValidPhone(value) {
|
|
141
|
+
if (!value)
|
|
142
|
+
return false;
|
|
143
|
+
const parsed = parsePhoneNumberFromString(value);
|
|
144
|
+
return Boolean(parsed === null || parsed === void 0 ? void 0 : parsed.isValid());
|
|
145
|
+
}
|
package/utils/to-e164-phone.d.ts
CHANGED
|
@@ -1 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalize a phone number to E.164.
|
|
3
|
+
*
|
|
4
|
+
* - `null` stays `null`
|
|
5
|
+
* - non-string values return `undefined`
|
|
6
|
+
* - empty strings return `null`
|
|
7
|
+
* - valid international or national numbers return E.164
|
|
8
|
+
* - invalid numbers return `null`
|
|
9
|
+
*/
|
|
1
10
|
export declare function toE164Phone(value: unknown, country?: string | null): string | null | undefined;
|
package/utils/to-e164-phone.js
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { parsePhoneNumberFromString, } from "libphonenumber-js";
|
|
2
|
+
/**
|
|
3
|
+
* Normalize a phone number to E.164.
|
|
4
|
+
*
|
|
5
|
+
* - `null` stays `null`
|
|
6
|
+
* - non-string values return `undefined`
|
|
7
|
+
* - empty strings return `null`
|
|
8
|
+
* - valid international or national numbers return E.164
|
|
9
|
+
* - invalid numbers return `null`
|
|
10
|
+
*/
|
|
2
11
|
export function toE164Phone(value, country) {
|
|
3
|
-
var _a;
|
|
4
12
|
if (value === null)
|
|
5
13
|
return null;
|
|
6
14
|
if (typeof value !== "string")
|
|
7
15
|
return undefined;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
16
|
+
const raw = value.trim();
|
|
17
|
+
if (!raw)
|
|
18
|
+
return null;
|
|
19
|
+
const parsed = raw.startsWith("+")
|
|
20
|
+
? parsePhoneNumberFromString(raw)
|
|
21
|
+
: parsePhoneNumberFromString(raw, (country || "PK"));
|
|
22
|
+
return (parsed === null || parsed === void 0 ? void 0 : parsed.isValid()) ? parsed.format("E.164") : null;
|
|
11
23
|
}
|