@arcblock/ux 2.12.41 → 2.12.43
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/lib/PhoneInput/index.js +35 -40
- package/package.json +5 -5
- package/src/PhoneInput/index.tsx +43 -37
package/lib/PhoneInput/index.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
2
|
-
import { useMemo } from 'react';
|
2
|
+
import { useCallback, useMemo, useState } from 'react';
|
3
3
|
import { Box, TextField, Typography } from '@mui/material';
|
4
4
|
import { defaultCountries, usePhoneInput, parseCountry } from 'react-international-phone';
|
5
5
|
import isMobilePhone from 'validator/lib/isMobilePhone';
|
@@ -88,60 +88,55 @@ export default function PhoneInput({
|
|
88
88
|
});
|
89
89
|
return map;
|
90
90
|
}, []);
|
91
|
+
const [previewValue, setPreviewValue] = useState(value);
|
92
|
+
const handleValueChange = useCallback(data => {
|
93
|
+
if (!preview && onChange) {
|
94
|
+
const newValue = {
|
95
|
+
...value,
|
96
|
+
...data
|
97
|
+
};
|
98
|
+
const countryData = countryMap.get(newValue.country);
|
99
|
+
if (countryData) {
|
100
|
+
newValue.phone = addCountryCodeToPhone(newValue.phone, getDialCodeWithoutPlus(countryData.dialCode));
|
101
|
+
}
|
102
|
+
onChange(newValue);
|
103
|
+
}
|
104
|
+
}, [onChange, preview, value, countryMap]);
|
105
|
+
|
106
|
+
// 获取当前国家的区号(不带+号)
|
107
|
+
const currentDialCode = useMemo(() => {
|
108
|
+
const countryData = countryMap.get(value.country);
|
109
|
+
return countryData ? getDialCodeWithoutPlus(countryData.dialCode) : '';
|
110
|
+
}, [value.country, countryMap]);
|
91
111
|
|
92
112
|
// 使用 react-international-phone 的 hook
|
93
113
|
const {
|
94
114
|
phone,
|
95
|
-
country
|
115
|
+
country,
|
116
|
+
handlePhoneValueChange,
|
117
|
+
setCountry
|
96
118
|
} = usePhoneInput({
|
97
119
|
defaultCountry: value.country,
|
98
|
-
value: value.phone,
|
120
|
+
value: extractPhoneWithoutCode(value.phone, currentDialCode),
|
99
121
|
countries: defaultCountries,
|
122
|
+
disableDialCodeAndPrefix: true,
|
100
123
|
onChange: data => {
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
124
|
+
const newData = {
|
125
|
+
...data,
|
126
|
+
phone: previewValue.country !== data.country && !data.phone ? extractPhoneWithoutCode(value.phone, currentDialCode) : data.phone
|
127
|
+
};
|
128
|
+
setPreviewValue(newData);
|
129
|
+
handleValueChange(newData);
|
107
130
|
}
|
108
131
|
});
|
109
132
|
|
110
|
-
// 获取当前国家的区号(不带+号)
|
111
|
-
const currentDialCode = useMemo(() => {
|
112
|
-
const countryData = countryMap.get(country);
|
113
|
-
return countryData ? getDialCodeWithoutPlus(countryData.dialCode) : '';
|
114
|
-
}, [country, countryMap]);
|
115
|
-
|
116
133
|
// 从完整电话号码中提取不带区号的部分用于显示,并去除格式化字符
|
117
|
-
const displayPhone = useMemo(() => extractPhoneWithoutCode(phone, currentDialCode), [phone, currentDialCode]);
|
118
|
-
|
119
|
-
// 创建更新后的 PhoneValue 对象
|
120
|
-
const createUpdatedPhoneValue = (countryIso2, phoneWithoutCode) => {
|
121
|
-
const countryData = countryMap.get(countryIso2);
|
122
|
-
if (!countryData) return {
|
123
|
-
country: countryIso2,
|
124
|
-
phone: ''
|
125
|
-
};
|
126
|
-
const dialCode = getDialCodeWithoutPlus(countryData.dialCode);
|
127
|
-
return {
|
128
|
-
country: countryIso2,
|
129
|
-
phone: addCountryCodeToPhone(phoneWithoutCode, dialCode)
|
130
|
-
};
|
131
|
-
};
|
132
|
-
|
133
|
-
// 处理电话号码变更
|
134
|
-
const onPhoneChange = e => {
|
135
|
-
if (preview) return;
|
136
|
-
// 去除用户输入中的非数字字符,但保留括号
|
137
|
-
const cleanedValue = e.target.value.replace(/[^\d()]/g, '');
|
138
|
-
onChange?.(createUpdatedPhoneValue(country, cleanedValue));
|
139
|
-
};
|
134
|
+
const displayPhone = useMemo(() => phone ? phone.replace(/[^\d]/g, '') : extractPhoneWithoutCode(value.phone, currentDialCode), [phone, value.phone, currentDialCode]);
|
140
135
|
|
141
136
|
// 处理国家变更
|
142
137
|
const onCountryChange = newCountry => {
|
143
138
|
if (preview) return;
|
144
|
-
|
139
|
+
setCountry(newCountry);
|
145
140
|
};
|
146
141
|
|
147
142
|
// 预览模式
|
@@ -191,7 +186,7 @@ export default function PhoneInput({
|
|
191
186
|
}), /*#__PURE__*/_jsx(TextField, {
|
192
187
|
...props,
|
193
188
|
value: displayPhone,
|
194
|
-
onChange:
|
189
|
+
onChange: handlePhoneValueChange,
|
195
190
|
placeholder: placeholder,
|
196
191
|
className: "phone-input",
|
197
192
|
sx: mergeSx({
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@arcblock/ux",
|
3
|
-
"version": "2.12.
|
3
|
+
"version": "2.12.43",
|
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": "dfe5344490260529ec3b7a997d9925ee25c9613f",
|
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.43",
|
75
|
+
"@arcblock/nft-display": "^2.12.43",
|
76
|
+
"@arcblock/react-hooks": "^2.12.43",
|
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",
|
package/src/PhoneInput/index.tsx
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { useMemo } from 'react';
|
1
|
+
import { useCallback, useMemo, useState } from 'react';
|
2
2
|
import { Box, TextField, TextFieldProps, Typography } from '@mui/material';
|
3
3
|
import { defaultCountries, CountryIso2, usePhoneInput, parseCountry } from 'react-international-phone';
|
4
4
|
import isMobilePhone from 'validator/lib/isMobilePhone';
|
@@ -102,54 +102,60 @@ export default function PhoneInput({
|
|
102
102
|
return map;
|
103
103
|
}, []);
|
104
104
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
countries: defaultCountries,
|
110
|
-
onChange: (data) => {
|
105
|
+
const [previewValue, setPreviewValue] = useState(value);
|
106
|
+
|
107
|
+
const handleValueChange = useCallback(
|
108
|
+
(data: PhoneValue) => {
|
111
109
|
if (!preview && onChange) {
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
}
|
110
|
+
const newValue = {
|
111
|
+
...value,
|
112
|
+
...data,
|
113
|
+
};
|
114
|
+
const countryData = countryMap.get(newValue.country);
|
115
|
+
if (countryData) {
|
116
|
+
newValue.phone = addCountryCodeToPhone(newValue.phone, getDialCodeWithoutPlus(countryData.dialCode));
|
117
|
+
}
|
118
|
+
onChange(newValue);
|
116
119
|
}
|
117
120
|
},
|
118
|
-
|
121
|
+
[onChange, preview, value, countryMap]
|
122
|
+
);
|
119
123
|
|
120
124
|
// 获取当前国家的区号(不带+号)
|
121
125
|
const currentDialCode = useMemo(() => {
|
122
|
-
const countryData = countryMap.get(country);
|
126
|
+
const countryData = countryMap.get(value.country);
|
123
127
|
return countryData ? getDialCodeWithoutPlus(countryData.dialCode) : '';
|
124
|
-
}, [country, countryMap]);
|
128
|
+
}, [value.country, countryMap]);
|
125
129
|
|
126
|
-
//
|
127
|
-
const
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
130
|
+
// 使用 react-international-phone 的 hook
|
131
|
+
const { phone, country, handlePhoneValueChange, setCountry } = usePhoneInput({
|
132
|
+
defaultCountry: value.country,
|
133
|
+
value: extractPhoneWithoutCode(value.phone, currentDialCode),
|
134
|
+
countries: defaultCountries,
|
135
|
+
disableDialCodeAndPrefix: true,
|
136
|
+
onChange: (data) => {
|
137
|
+
const newData = {
|
138
|
+
...data,
|
139
|
+
phone:
|
140
|
+
previewValue.country !== data.country && !data.phone
|
141
|
+
? extractPhoneWithoutCode(value.phone, currentDialCode)
|
142
|
+
: data.phone,
|
143
|
+
};
|
144
|
+
setPreviewValue(newData);
|
145
|
+
handleValueChange(newData);
|
146
|
+
},
|
147
|
+
});
|
140
148
|
|
141
|
-
//
|
142
|
-
const
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
onChange?.(createUpdatedPhoneValue(country, cleanedValue));
|
147
|
-
};
|
149
|
+
// 从完整电话号码中提取不带区号的部分用于显示,并去除格式化字符
|
150
|
+
const displayPhone = useMemo(
|
151
|
+
() => (phone ? phone.replace(/[^\d]/g, '') : extractPhoneWithoutCode(value.phone, currentDialCode)),
|
152
|
+
[phone, value.phone, currentDialCode]
|
153
|
+
);
|
148
154
|
|
149
155
|
// 处理国家变更
|
150
156
|
const onCountryChange = (newCountry: CountryIso2) => {
|
151
157
|
if (preview) return;
|
152
|
-
|
158
|
+
setCountry(newCountry);
|
153
159
|
};
|
154
160
|
|
155
161
|
// 预览模式
|
@@ -196,7 +202,7 @@ export default function PhoneInput({
|
|
196
202
|
<TextField
|
197
203
|
{...props}
|
198
204
|
value={displayPhone}
|
199
|
-
onChange={
|
205
|
+
onChange={handlePhoneValueChange}
|
200
206
|
placeholder={placeholder}
|
201
207
|
className="phone-input"
|
202
208
|
sx={mergeSx(
|