@swan-io/shared-business 8.8.4 → 8.9.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/package.json
CHANGED
|
@@ -2,16 +2,19 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
2
2
|
import { Flag } from "@swan-io/lake/src/components/Flag";
|
|
3
3
|
import { LakeSelect } from "@swan-io/lake/src/components/LakeSelect";
|
|
4
4
|
import { forwardRef, useMemo } from "react";
|
|
5
|
-
import {
|
|
5
|
+
import { getCountryByCCA3 } from "../constants/countries";
|
|
6
6
|
const CountryPickerWithRef = ({ onValueChange, value, countries, readOnly, id, error, placeholder, disabled, hideErrors, }, forwardedRef) => {
|
|
7
7
|
const items = useMemo(() => {
|
|
8
8
|
return countries
|
|
9
9
|
.filter((item, index, array) => array.indexOf(item) === index) // deduplicate
|
|
10
|
-
.map(cca3 =>
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
.map(cca3 => {
|
|
11
|
+
const country = getCountryByCCA3(cca3);
|
|
12
|
+
return {
|
|
13
|
+
name: country.name,
|
|
14
|
+
icon: _jsx(Flag, { width: 14, cca2: country.cca2 }),
|
|
15
|
+
value: cca3,
|
|
16
|
+
};
|
|
17
|
+
})
|
|
15
18
|
.sort((a, b) => a.name.localeCompare(b.name));
|
|
16
19
|
}, [countries]);
|
|
17
20
|
return (_jsx(LakeSelect, { readOnly: readOnly, id: id, ref: forwardedRef, error: error, items: items, placeholder: placeholder, value: value, onValueChange: onValueChange, disabled: disabled, hideErrors: hideErrors }));
|
|
@@ -1486,8 +1486,10 @@ export type Country = Simplify<Pick<(typeof readonlyCountries)[number], "cca2" |
|
|
|
1486
1486
|
export type CountryCCA2 = Country["cca2"];
|
|
1487
1487
|
export type CountryCCA3 = Country["cca3"];
|
|
1488
1488
|
export declare const countries: Country[];
|
|
1489
|
-
export declare const
|
|
1490
|
-
export declare const
|
|
1489
|
+
export declare const getCountryByCCA2: (cca2: CountryCCA2) => Country;
|
|
1490
|
+
export declare const getCountryByCCA3: (cca3: CountryCCA3) => Country;
|
|
1491
|
+
export declare const getCCA2forCCA3: (cca3: CountryCCA3) => CountryCCA2;
|
|
1492
|
+
export declare const getCCA3forCCA2: (cca2: CountryCCA2) => CountryCCA3;
|
|
1491
1493
|
export declare const companyWithUboCountries: readonly ["BEL", "DEU", "FRA", "ITA", "LTU", "LUX", "NLD"];
|
|
1492
1494
|
export type CompanyWithUboCountryCCA3 = (typeof companyWithUboCountries)[number];
|
|
1493
1495
|
export declare const allCountries: ("ALA" | "ASM" | "AND" | "AGO" | "AIA" | "ATA" | "ATG" | "ARG" | "ABW" | "AUS" | "AZE" | "BHS" | "BGD" | "BRB" | "BEL" | "BLZ" | "BEN" | "BMU" | "BOL" | "BES" | "BIH" | "BWA" | "BVT" | "BRA" | "VGB" | "BFA" | "BDI" | "CPV" | "CMR" | "CAN" | "CYM" | "CZE" | "CHL" | "CXR" | "CCK" | "COL" | "COK" | "CRI" | "CIV" | "CUB" | "CUW" | "DNK" | "DEU" | "DJI" | "DMA" | "ECU" | "EST" | "IRL" | "SLV" | "ESP" | "FLK" | "FJI" | "FRO" | "FRA" | "GAB" | "GMB" | "GHA" | "GIB" | "GRD" | "GLP" | "GUM" | "GTM" | "GGY" | "GNB" | "GNQ" | "GIN" | "GUY" | "GUF" | "HTI" | "HMD" | "HND" | "HRV" | "IDN" | "ISL" | "IMN" | "ITA" | "JAM" | "JEY" | "GRL" | "KHM" | "KAZ" | "KEN" | "KIR" | "CAF" | "COM" | "REU" | "LVA" | "LSO" | "LBR" | "LIE" | "LTU" | "LUX" | "MDG" | "HUN" | "MHL" | "MWI" | "MYS" | "MDV" | "MLI" | "MLT" | "MTQ" | "MUS" | "MYT" | "MEX" | "FSM" | "MOZ" | "MDA" | "MCO" | "MSR" | "NAM" | "NLD" | "BRN" | "NZL" | "NIC" | "NER" | "NGA" | "NIU" | "NFK" | "NOR" | "MNP" | "NCL" | "AUT" | "UZB" | "PAK" | "PLW" | "PAN" | "PNG" | "PRY" | "PER" | "PHL" | "PCN" | "POL" | "PYF" | "PRT" | "PRI" | "DOM" | "XKX" | "COD" | "COG" | "ROU" | "RWA" | "KNA" | "LCA" | "VCT" | "BLM" | "MAF" | "SPM" | "WSM" | "SMR" | "STP" | "CHE" | "SEN" | "SYC" | "ALB" | "SLE" | "SGP" | "SVN" | "SVK" | "SLB" | "SOM" | "ZAF" | "SGS" | "SSD" | "LKA" | "FIN" | "SUR" | "SWE" | "SWZ" | "TZA" | "TCD" | "ATF" | "TLS" | "TGO" | "TON" | "TTO" | "TUR" | "TKM" | "TCA" | "TUV" | "UGA" | "GBR" | "USA" | "UMI" | "VIR" | "URY" | "VUT" | "VAT" | "VEN" | "VNM" | "WLF" | "ZMB" | "ZWE" | "BTN" | "GRC" | "CYP" | "BLR" | "BGR" | "KGZ" | "MKD" | "MNG" | "RUS" | "SRB" | "TJK" | "UKR" | "MNE" | "ARM" | "ISR" | "AFG" | "JOR" | "DZA" | "SDN" | "ESH" | "IRQ" | "SAU" | "KWT" | "MAR" | "YEM" | "IRN" | "TUN" | "ARE" | "SYR" | "OMN" | "PSE" | "QAT" | "LBN" | "EGY" | "MRT" | "NPL" | "IND" | "THA" | "LAO" | "MMR" | "GEO" | "ETH" | "ERI" | "BHR" | "LBY" | "CHN" | "JPN" | "MAC" | "TWN" | "HKG" | "KOR" | "PRK")[];
|
|
@@ -1729,9 +1729,7 @@ const readonlyCountries = [
|
|
|
1729
1729
|
const countryNameResolver = Result.fromExecution(() => {
|
|
1730
1730
|
const DisplayNames = Intl.DisplayNames;
|
|
1731
1731
|
// https://github.com/formatjs/formatjs/blob/%40formatjs/intl-localematcher%400.5.3/packages/intl-displaynames/should-polyfill.ts
|
|
1732
|
-
const hasMissingICUBug = new DisplayNames(["en"], {
|
|
1733
|
-
type: "region",
|
|
1734
|
-
}).of("CA") === "CA";
|
|
1732
|
+
const hasMissingICUBug = new DisplayNames(["en"], { type: "region" }).of("CA") === "CA";
|
|
1735
1733
|
if (hasMissingICUBug) {
|
|
1736
1734
|
throw new Error("Missing Intl APIs");
|
|
1737
1735
|
}
|
|
@@ -1742,27 +1740,34 @@ const countryNameResolver = Result.fromExecution(() => {
|
|
|
1742
1740
|
}
|
|
1743
1741
|
return new DisplayNames(locales, { type: "region" });
|
|
1744
1742
|
});
|
|
1743
|
+
const countriesByCCA2 = {};
|
|
1744
|
+
const countriesByCCA3 = {};
|
|
1745
1745
|
export const countries = readonlyCountries
|
|
1746
|
-
.map(
|
|
1746
|
+
.map(item => {
|
|
1747
1747
|
const name = countryNameResolver.match({
|
|
1748
|
-
Ok: resolver => { var _a; return (_a = resolver.of(
|
|
1749
|
-
Error: () =>
|
|
1748
|
+
Ok: resolver => { var _a; return (_a = resolver.of(item.cca2)) !== null && _a !== void 0 ? _a : item.name; },
|
|
1749
|
+
Error: () => item.name,
|
|
1750
1750
|
});
|
|
1751
|
-
|
|
1752
|
-
...
|
|
1751
|
+
const country = {
|
|
1752
|
+
...item,
|
|
1753
1753
|
name,
|
|
1754
1754
|
deburr: deburr(name.toLowerCase()),
|
|
1755
|
-
uid: `${
|
|
1755
|
+
uid: `${item.cca2}-${item.idd}`,
|
|
1756
1756
|
};
|
|
1757
|
+
countriesByCCA2[country.cca2] = country;
|
|
1758
|
+
countriesByCCA3[country.cca3] = country;
|
|
1759
|
+
return country;
|
|
1757
1760
|
})
|
|
1758
1761
|
.sort((countryA, countryB) => {
|
|
1759
1762
|
return countryA.name.localeCompare(countryB.name);
|
|
1760
1763
|
});
|
|
1761
|
-
export const
|
|
1762
|
-
export const
|
|
1764
|
+
export const getCountryByCCA2 = (cca2) => countriesByCCA2[cca2];
|
|
1765
|
+
export const getCountryByCCA3 = (cca3) => countriesByCCA3[cca3];
|
|
1766
|
+
export const getCCA2forCCA3 = (cca3) => countriesByCCA3[cca3].cca2;
|
|
1767
|
+
export const getCCA3forCCA2 = (cca2) => countriesByCCA2[cca2].cca3;
|
|
1763
1768
|
export const companyWithUboCountries = ["BEL", "DEU", "FRA", "ITA", "LTU", "LUX", "NLD"];
|
|
1764
1769
|
export const allCountries = countries.map(country => country.cca3);
|
|
1765
|
-
export const france =
|
|
1770
|
+
export const france = countriesByCCA3["FRA"];
|
|
1766
1771
|
export const individualCountries = [
|
|
1767
1772
|
"AUT",
|
|
1768
1773
|
"BEL",
|
|
@@ -1833,8 +1838,8 @@ export const companyCountries = [
|
|
|
1833
1838
|
export const countriesWithMultipleCCA3 = {
|
|
1834
1839
|
FRA: ["FRA", "GUF", "REU", "MTQ", "GLP"], // France, French Guiana, Réunion, Martinique, Guadeloupe
|
|
1835
1840
|
};
|
|
1836
|
-
export const isCountryCCA3 = (value) =>
|
|
1837
|
-
export const isCountryCCA2 = (value) =>
|
|
1841
|
+
export const isCountryCCA3 = (value) => typeof value === "string" && value in countriesByCCA3;
|
|
1842
|
+
export const isCountryCCA2 = (value) => typeof value === "string" && value in countriesByCCA2;
|
|
1838
1843
|
export const isIndividualCountryCCA3 = (value) => individualCountries.includes(value);
|
|
1839
1844
|
export const isCompanyCountryCCA3 = (value) => companyCountries.includes(value);
|
|
1840
1845
|
export const isCompanyWithUboCountryCCA3 = (value) => companyWithUboCountries.includes(value);
|
|
@@ -1842,10 +1847,12 @@ const navigatorCountries = navigator.languages.reduce((acc, lang) => {
|
|
|
1842
1847
|
const countryCode = lang.split("-")[1];
|
|
1843
1848
|
if (countryCode != null && isCountryCCA2(countryCode)) {
|
|
1844
1849
|
const countryCCA3 = getCCA3forCCA2(countryCode);
|
|
1845
|
-
countryCCA3 != null
|
|
1850
|
+
if (countryCCA3 != null) {
|
|
1851
|
+
acc.push(countryCCA3);
|
|
1852
|
+
}
|
|
1846
1853
|
}
|
|
1847
1854
|
return acc;
|
|
1848
1855
|
}, []);
|
|
1849
1856
|
export const companyFallbackCountry = (_a = companyCountries.find(cca3 => navigatorCountries.includes(cca3))) !== null && _a !== void 0 ? _a : "FRA";
|
|
1850
1857
|
export const individualFallbackCountry = (_b = individualCountries.find(cca3 => navigatorCountries.includes(cca3))) !== null && _b !== void 0 ? _b : "FRA";
|
|
1851
|
-
export const getCountryName = (cca3) =>
|
|
1858
|
+
export const getCountryName = (cca3) => countriesByCCA3[cca3].name;
|