|
|
@@ -5,7 +5,8 @@ import Image from "next/image";
|
|
|
import clsx from "clsx";
|
|
|
import {
|
|
|
COUNTRY_PHONECODE,
|
|
|
- formatFinalPhoneValue
|
|
|
+ formatFinalPhoneValue,
|
|
|
+ getPhoneCodeBycountryCode
|
|
|
} from "./phoneCodeMetaData";
|
|
|
import { IS_VALID_PHONECODE } from "@utils/constants";
|
|
|
|
|
|
@@ -34,17 +35,7 @@ function parsePhoneValue(value: string|undefined) {
|
|
|
/**
|
|
|
* 受控组件
|
|
|
*/
|
|
|
-function PhoneNumberInput({
|
|
|
- value,
|
|
|
- placeholder,
|
|
|
- onChange,
|
|
|
- onBlur,
|
|
|
- name,
|
|
|
- error,
|
|
|
- ref,
|
|
|
- countryCode = "US",
|
|
|
- phoneCodeChange
|
|
|
-}: {
|
|
|
+interface PhoneNumberInputProps {
|
|
|
// 用于react-hook-form时,推荐使用useForm的 defaultValues 对整个表单设置默认值
|
|
|
value?: string;
|
|
|
placeholder: string;
|
|
|
@@ -54,9 +45,22 @@ function PhoneNumberInput({
|
|
|
name: string;
|
|
|
error?: string;
|
|
|
ref?: React.Ref<HTMLInputElement>;
|
|
|
- countryCode: string;
|
|
|
- phoneCodeChange: (e: string) => void;
|
|
|
-}) {
|
|
|
+ countryCode: string;//父组件表单国家字段
|
|
|
+ onHasUserSelectedPhoneCode: () => void;
|
|
|
+ hasUserSelectedPhoneCode: boolean;
|
|
|
+}
|
|
|
+function PhoneNumberInput({
|
|
|
+ value,
|
|
|
+ placeholder,
|
|
|
+ onChange,
|
|
|
+ onBlur,
|
|
|
+ name,
|
|
|
+ error,
|
|
|
+ ref,
|
|
|
+ countryCode,
|
|
|
+ onHasUserSelectedPhoneCode,
|
|
|
+ hasUserSelectedPhoneCode
|
|
|
+}: PhoneNumberInputProps) {
|
|
|
// const DEFAULT_COUNTRY_CODE = "US";
|
|
|
const phoneCodeList = COUNTRY_PHONECODE;
|
|
|
|
|
|
@@ -64,45 +68,18 @@ function PhoneNumberInput({
|
|
|
return parsePhoneValue(value);
|
|
|
}, [value]);
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- // const [finalPhone, setFinalPhone] = useState("");
|
|
|
- // const [phoneCode, setPhoneCode] = useState(DEFAULT_PHONE_CODE);
|
|
|
- // const [phoneNumber, setPhoneNumber] = useState("");
|
|
|
-
|
|
|
- // useEffect(() => {
|
|
|
- // // +1-345 / +44
|
|
|
- // if(value) {
|
|
|
- // const regPhoneCode = IS_VALID_PHONECODE;
|
|
|
- // const codePart = regPhoneCode.exec(value);
|
|
|
- // let code = DEFAULT_PHONE_CODE;
|
|
|
- // let phone = '';
|
|
|
-
|
|
|
-
|
|
|
- // if(codePart) {
|
|
|
- // code = codePart[0].trim() || DEFAULT_PHONE_CODE;
|
|
|
- // phone = value.replace(codePart[0], '');
|
|
|
- // } else {
|
|
|
- // phone = value;
|
|
|
- // }
|
|
|
- // // const finalPhoneValue = formatFinalPhoneValue(code, phone);
|
|
|
- // setPhoneCode(code);
|
|
|
- // setPhoneNumber(phone);
|
|
|
- // // setFinalPhone(finalPhoneValue);
|
|
|
-
|
|
|
- // }
|
|
|
-
|
|
|
- // }, [value]);
|
|
|
-
|
|
|
-
|
|
|
- // let flagSvg = '';
|
|
|
- // countryCode = DEFAULT_COUNTRY_CODE;
|
|
|
- // if(phoneCode) {
|
|
|
- // countryCode = getCountryCodeByPhoneCode(phoneCode);
|
|
|
- // }
|
|
|
- const flagSvg = `/image/flags/4x3/${countryCode.toLowerCase()}.svg`;
|
|
|
|
|
|
+ const [selectedPhoneCode, setSelectedPhoneCode] = useState<string | null>(null);
|
|
|
+ const [selectedCountryForPhoneCode, setSelectedCountryForPhoneCode] = useState<string | null>(null);
|
|
|
|
|
|
+ const displayPhonecode = useMemo(() => {
|
|
|
+ return selectedPhoneCode ?? getPhoneCodeBycountryCode(countryCode);
|
|
|
+ },[selectedPhoneCode,countryCode]);
|
|
|
+ const displayCountryCode = useMemo(() => {
|
|
|
+ return selectedCountryForPhoneCode ?? countryCode;
|
|
|
+ }, [selectedCountryForPhoneCode,countryCode]);
|
|
|
+
|
|
|
+ ////////////////////////////////
|
|
|
const rootRef = useRef<HTMLDivElement>(null);
|
|
|
const [rootBounding, setRootBounding] = useState<{
|
|
|
top: number;
|
|
|
@@ -155,20 +132,27 @@ function PhoneNumberInput({
|
|
|
}
|
|
|
setShowCodeList(!showCodeList);
|
|
|
};
|
|
|
+ ///////////////////////////
|
|
|
|
|
|
- const phoneCodeSelect = (phoneCode: string) => {
|
|
|
- onChange(formatFinalPhoneValue(phoneCode, phoneNumber));
|
|
|
- // setPhoneCode(phoneCode);
|
|
|
+ const phoneCodeSelect = (pCode: string, ccode: string) => {
|
|
|
+ onChange(formatFinalPhoneValue(pCode, phoneNumber));
|
|
|
+ setSelectedPhoneCode(pCode);
|
|
|
+ setSelectedCountryForPhoneCode(ccode);
|
|
|
+
|
|
|
+ if(!hasUserSelectedPhoneCode) {
|
|
|
+ onHasUserSelectedPhoneCode(); // 标记为用户手动选择过
|
|
|
+ }
|
|
|
setShowCodeList(false);
|
|
|
};
|
|
|
|
|
|
const phoneNumberChange = (value: string) => {
|
|
|
+
|
|
|
onChange(formatFinalPhoneValue(phoneCode, value));
|
|
|
- // setPhoneNumber(value);
|
|
|
+
|
|
|
};
|
|
|
const phoneNumberBlur = (value: string) => {
|
|
|
onBlur(formatFinalPhoneValue(phoneCode, value));
|
|
|
- // setPhoneNumber(value);
|
|
|
+
|
|
|
};
|
|
|
// const phoneNumberInput = (value: string) => {
|
|
|
// setPhoneNumber(value);
|
|
|
@@ -186,9 +170,10 @@ function PhoneNumberInput({
|
|
|
<div className="h-11.5 text-ly-12 flex items-center">
|
|
|
|
|
|
<Image alt="china flag" width={24} height={18} className="w-6 h-4.5 mr-2"
|
|
|
- src={flagSvg}
|
|
|
+ src={`/image/flags/4x3/${displayCountryCode.toLowerCase()}.svg`}
|
|
|
/>
|
|
|
- {phoneCode}
|
|
|
+ {/* {phoneCode} */}
|
|
|
+ {displayPhonecode}
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
@@ -214,8 +199,8 @@ function PhoneNumberInput({
|
|
|
{phoneCodeList.map((item) => {
|
|
|
return (<li className="text-ly-12 flex items-center h-8 gap-2" key={item.code}
|
|
|
onClick={() => {
|
|
|
- phoneCodeSelect(item.phoneCode);
|
|
|
- phoneCodeChange(item.code);
|
|
|
+ phoneCodeSelect(item.phoneCode, item.code);
|
|
|
+ // phoneCodeChange(item.code);
|
|
|
}}
|
|
|
>
|
|
|
<Image alt="china flag" width={20} height={15} className="w-5 h-3.75"
|