@arcblock/ux 2.12.35 → 2.12.36
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.
@@ -59,7 +59,7 @@ const CountrySelectInner = /*#__PURE__*/forwardRef(({
|
|
59
59
|
sx: {
|
60
60
|
lineHeight: 1.5
|
61
61
|
},
|
62
|
-
children: hideDialCode ?
|
62
|
+
children: hideDialCode ? countryDetail?.name : `+${countryDetail?.dialCode}`
|
63
63
|
})]
|
64
64
|
});
|
65
65
|
if (preview) {
|
@@ -13,4 +13,6 @@ export interface PhoneInputProps extends Omit<TextFieldProps, 'value' | 'onChang
|
|
13
13
|
allowDial?: boolean;
|
14
14
|
}
|
15
15
|
export declare function validatePhoneNumber(phone: string): boolean;
|
16
|
+
export declare function getDialCodeByCountry(iso2: CountryIso2): string;
|
17
|
+
export declare function getCountryNameByCountry(iso2: CountryIso2): string;
|
16
18
|
export default function PhoneInput({ value, onChange, placeholder, countryDisplayOptions, preview, allowDial, ...props }: PhoneInputProps): import("react/jsx-runtime").JSX.Element;
|
package/lib/PhoneInput/index.js
CHANGED
@@ -6,7 +6,22 @@ import isMobilePhone from 'validator/lib/isMobilePhone';
|
|
6
6
|
import CountrySelect from './country-select';
|
7
7
|
import { mergeSx } from '../Util/style';
|
8
8
|
export function validatePhoneNumber(phone) {
|
9
|
-
|
9
|
+
// 如果没有输入电话号码,返回false
|
10
|
+
if (!phone) return false;
|
11
|
+
|
12
|
+
// 检查是否只有国家区号
|
13
|
+
// 电话号码格式通常为 +区号 后跟电话号码
|
14
|
+
// 如果去除所有格式化字符后,只剩下区号,则认为只有区号
|
15
|
+
const cleanedPhone = phone.replace(/[\s\-()]+/g, '');
|
16
|
+
|
17
|
+
// 使用正则表达式匹配:以+开头,后面全是数字,但长度不超过5(大多数国家区号在1-4位)
|
18
|
+
// 这表示电话号码只有区号部分
|
19
|
+
if (/^\+\d{1,5}$/.test(cleanedPhone)) {
|
20
|
+
return true; // 如果只有区号,则视为有效
|
21
|
+
}
|
22
|
+
|
23
|
+
// 否则使用validator进行完整校验
|
24
|
+
return isMobilePhone(cleanedPhone, 'any', {
|
10
25
|
strictMode: true
|
11
26
|
});
|
12
27
|
}
|
@@ -17,12 +32,12 @@ function extractPhoneWithoutCode(phone, dialCode) {
|
|
17
32
|
// 先去除区号
|
18
33
|
const phoneWithoutCode = phone.replace(new RegExp(`^\\+${dialCode}`), '');
|
19
34
|
// 去除非数字字符,但保留括号
|
20
|
-
return phoneWithoutCode.replace(/[^\d
|
35
|
+
return phoneWithoutCode.replace(/[^\d]/g, '');
|
21
36
|
}
|
22
37
|
|
23
38
|
// 添加区号到纯号码
|
24
39
|
function addCountryCodeToPhone(phone, dialCode) {
|
25
|
-
if (!phone) return
|
40
|
+
if (!phone) return `+${dialCode}`;
|
26
41
|
return phone.startsWith('+') ? phone : `+${dialCode}${phone}`;
|
27
42
|
}
|
28
43
|
|
@@ -30,6 +45,28 @@ function addCountryCodeToPhone(phone, dialCode) {
|
|
30
45
|
function getDialCodeWithoutPlus(dialCode) {
|
31
46
|
return dialCode.replace(/^\+/, '');
|
32
47
|
}
|
48
|
+
|
49
|
+
// 根据ISO2国家代码获取国家信息
|
50
|
+
function getCountryInfoByIso2(iso2, property) {
|
51
|
+
// 查找对应的国家数据
|
52
|
+
const countryItem = defaultCountries.find(country => parseCountry(country).iso2 === iso2);
|
53
|
+
if (!countryItem) {
|
54
|
+
return undefined;
|
55
|
+
}
|
56
|
+
|
57
|
+
// 解析国家信息并返回请求的属性
|
58
|
+
return parseCountry(countryItem)[property];
|
59
|
+
}
|
60
|
+
|
61
|
+
// 根据ISO2国家代码获取区号
|
62
|
+
export function getDialCodeByCountry(iso2) {
|
63
|
+
return getCountryInfoByIso2(iso2, 'dialCode') || '';
|
64
|
+
}
|
65
|
+
|
66
|
+
// 根据ISO2国家代码获取国家名称
|
67
|
+
export function getCountryNameByCountry(iso2) {
|
68
|
+
return getCountryInfoByIso2(iso2, 'name') || '';
|
69
|
+
}
|
33
70
|
export default function PhoneInput({
|
34
71
|
value = {
|
35
72
|
country: 'us',
|
@@ -55,7 +92,6 @@ export default function PhoneInput({
|
|
55
92
|
// 使用 react-international-phone 的 hook
|
56
93
|
const {
|
57
94
|
phone,
|
58
|
-
inputRef,
|
59
95
|
country
|
60
96
|
} = usePhoneInput({
|
61
97
|
defaultCountry: value.country,
|
@@ -158,7 +194,6 @@ export default function PhoneInput({
|
|
158
194
|
onChange: onPhoneChange,
|
159
195
|
placeholder: placeholder,
|
160
196
|
className: "phone-input",
|
161
|
-
inputRef: inputRef,
|
162
197
|
sx: mergeSx({
|
163
198
|
'& .MuiInputBase-input': {
|
164
199
|
padding: 1
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@arcblock/ux",
|
3
|
-
"version": "2.12.
|
3
|
+
"version": "2.12.36",
|
4
4
|
"description": "Common used react components for arcblock products",
|
5
5
|
"keywords": [
|
6
6
|
"react",
|
@@ -68,12 +68,12 @@
|
|
68
68
|
"react": ">=18.2.0",
|
69
69
|
"react-router-dom": ">=6.22.3"
|
70
70
|
},
|
71
|
-
"gitHead": "
|
71
|
+
"gitHead": "9cb69f5c89649426b7e204ed9c8697359abfec68",
|
72
72
|
"dependencies": {
|
73
73
|
"@arcblock/did-motif": "^1.1.13",
|
74
|
-
"@arcblock/icons": "^2.12.
|
75
|
-
"@arcblock/nft-display": "^2.12.
|
76
|
-
"@arcblock/react-hooks": "^2.12.
|
74
|
+
"@arcblock/icons": "^2.12.36",
|
75
|
+
"@arcblock/nft-display": "^2.12.36",
|
76
|
+
"@arcblock/react-hooks": "^2.12.36",
|
77
77
|
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
|
78
78
|
"@fontsource/inter": "^5.0.16",
|
79
79
|
"@fontsource/ubuntu-mono": "^5.0.18",
|
@@ -71,7 +71,7 @@ const CountrySelectInner = forwardRef(
|
|
71
71
|
sx={{ cursor: preview ? 'default' : 'pointer' }}>
|
72
72
|
{hideFlag ? null : <FlagEmoji iso2={value} style={{ display: 'flex', width: 24, color: 'inherit' }} />}
|
73
73
|
<Typography component="span" sx={{ lineHeight: 1.5 }}>
|
74
|
-
{hideDialCode ?
|
74
|
+
{hideDialCode ? countryDetail?.name : `+${countryDetail?.dialCode}`}
|
75
75
|
</Typography>
|
76
76
|
</Box>
|
77
77
|
);
|
package/src/PhoneInput/index.tsx
CHANGED
@@ -19,7 +19,22 @@ export interface PhoneInputProps extends Omit<TextFieldProps, 'value' | 'onChang
|
|
19
19
|
}
|
20
20
|
|
21
21
|
export function validatePhoneNumber(phone: string): boolean {
|
22
|
-
|
22
|
+
// 如果没有输入电话号码,返回false
|
23
|
+
if (!phone) return false;
|
24
|
+
|
25
|
+
// 检查是否只有国家区号
|
26
|
+
// 电话号码格式通常为 +区号 后跟电话号码
|
27
|
+
// 如果去除所有格式化字符后,只剩下区号,则认为只有区号
|
28
|
+
const cleanedPhone = phone.replace(/[\s\-()]+/g, '');
|
29
|
+
|
30
|
+
// 使用正则表达式匹配:以+开头,后面全是数字,但长度不超过5(大多数国家区号在1-4位)
|
31
|
+
// 这表示电话号码只有区号部分
|
32
|
+
if (/^\+\d{1,5}$/.test(cleanedPhone)) {
|
33
|
+
return true; // 如果只有区号,则视为有效
|
34
|
+
}
|
35
|
+
|
36
|
+
// 否则使用validator进行完整校验
|
37
|
+
return isMobilePhone(cleanedPhone, 'any', { strictMode: true });
|
23
38
|
}
|
24
39
|
|
25
40
|
// 从带区号的电话号码中提取纯号码部分
|
@@ -28,12 +43,12 @@ function extractPhoneWithoutCode(phone: string, dialCode: string): string {
|
|
28
43
|
// 先去除区号
|
29
44
|
const phoneWithoutCode = phone.replace(new RegExp(`^\\+${dialCode}`), '');
|
30
45
|
// 去除非数字字符,但保留括号
|
31
|
-
return phoneWithoutCode.replace(/[^\d
|
46
|
+
return phoneWithoutCode.replace(/[^\d]/g, '');
|
32
47
|
}
|
33
48
|
|
34
49
|
// 添加区号到纯号码
|
35
50
|
function addCountryCodeToPhone(phone: string, dialCode: string): string {
|
36
|
-
if (!phone) return
|
51
|
+
if (!phone) return `+${dialCode}`;
|
37
52
|
return phone.startsWith('+') ? phone : `+${dialCode}${phone}`;
|
38
53
|
}
|
39
54
|
|
@@ -42,6 +57,32 @@ function getDialCodeWithoutPlus(dialCode: string): string {
|
|
42
57
|
return dialCode.replace(/^\+/, '');
|
43
58
|
}
|
44
59
|
|
60
|
+
// 根据ISO2国家代码获取国家信息
|
61
|
+
function getCountryInfoByIso2<T extends keyof ReturnType<typeof parseCountry>>(
|
62
|
+
iso2: CountryIso2,
|
63
|
+
property: T
|
64
|
+
): ReturnType<typeof parseCountry>[T] | undefined {
|
65
|
+
// 查找对应的国家数据
|
66
|
+
const countryItem = defaultCountries.find((country) => parseCountry(country).iso2 === iso2);
|
67
|
+
|
68
|
+
if (!countryItem) {
|
69
|
+
return undefined;
|
70
|
+
}
|
71
|
+
|
72
|
+
// 解析国家信息并返回请求的属性
|
73
|
+
return parseCountry(countryItem)[property];
|
74
|
+
}
|
75
|
+
|
76
|
+
// 根据ISO2国家代码获取区号
|
77
|
+
export function getDialCodeByCountry(iso2: CountryIso2): string {
|
78
|
+
return getCountryInfoByIso2(iso2, 'dialCode') || '';
|
79
|
+
}
|
80
|
+
|
81
|
+
// 根据ISO2国家代码获取国家名称
|
82
|
+
export function getCountryNameByCountry(iso2: CountryIso2): string {
|
83
|
+
return getCountryInfoByIso2(iso2, 'name') || '';
|
84
|
+
}
|
85
|
+
|
45
86
|
export default function PhoneInput({
|
46
87
|
value = { country: 'us', phone: '' },
|
47
88
|
onChange,
|
@@ -62,7 +103,7 @@ export default function PhoneInput({
|
|
62
103
|
}, []);
|
63
104
|
|
64
105
|
// 使用 react-international-phone 的 hook
|
65
|
-
const { phone,
|
106
|
+
const { phone, country } = usePhoneInput({
|
66
107
|
defaultCountry: value.country,
|
67
108
|
value: value.phone,
|
68
109
|
countries: defaultCountries,
|
@@ -158,7 +199,6 @@ export default function PhoneInput({
|
|
158
199
|
onChange={onPhoneChange}
|
159
200
|
placeholder={placeholder}
|
160
201
|
className="phone-input"
|
161
|
-
inputRef={inputRef}
|
162
202
|
sx={mergeSx(
|
163
203
|
{
|
164
204
|
'& .MuiInputBase-input': {
|