【发布时间】:2022-07-10 01:47:46
【问题描述】:
最近我实现了一个自定义反应输入电话,但我无法在选项和所选国家/地区显示国家/地区标志。 实际上,我不能直接使用 PhoneInput,因为我需要提取国家并将其标签发送到服务器(例如:'US')。 如果您对在自定义输入中显示标志或在默认 PhoneInput 中提取选定国家/地区标签有任何想法,请回答。
谢谢
这是我的代码行:
import Input, {
getCountries,
getCountryCallingCode,
} from "react-phone-number-input/input";
import en from "react-phone-number-input/locale/en.json";
import "react-phone-number-input/style.css";
const RegisterForm = () => {
const [onFocuseInput, setOnFocuseInput] = useState("");
const [phoneNumber, setPhoneNumber] = useState();
const [country, setCountry] = useState();
const CountrySelect = ({ value, onChange, labels, ...rest }) => (
<select
{...rest}
value={value}
onChange={(event) => {
onChange(event.target.value || undefined);
}}
>
<option value="">country</option>
{getCountries().map((country) => (
<option key={country} value={country}>
{labels[country]} +{getCountryCallingCode(country)}
</option>
))}
</select>
);
<div className="mb-6 flex">
<CountrySelect
className={`border-b-2 bg-none outline-none w-1/4 text-xs ${
onFocuseInput === "country"
? "border-blue-700 "
: "border-gray-300"
}`}
labels={en}
value={country}
onChange={setCountry}
name="countrySelect"
onFocus={() => setOnFocuseInput("country")}
/>
<Input
className={`${
onFocuseInput === "phoneNumber"
? "focusedInput w-full"
: "registerInput w-full"
}`}
placeholder="phoneNumber"
dir="ltr"
country={country}
value={phoneNumber}
onChange={setPhoneNumber}
name="phoneNumber"
onFocus={() => setOnFocuseInput("phoneNumber")}
required
/>
</div>
{loading ? (
<div className="flex justify-center items-center my-5 bg-red-600 p-4 rounded-full">
<div
className="spinner-border animate-spin inline-block w-8 h-8 border-4 border-blue-700 border-t-white rounded-full"
role="status"
></div>
</div>
);
};
export default RegisterForm;
【问题讨论】:
标签: javascript reactjs typescript