首页 > 解决方案 > 如何删除 React 电话号码输入中的国际选项?

问题描述

如何从react-phone-number-input包中的选择选项中删除国际选项?我试图将这些国家/地区限制在六个。我已经添加了defaultCountryandcountries属性,但它仍然允许输入其他国家的电话号码。这是我的使用方法:

<PhoneInput
   placeholder={placeholder}
   name={name}
   value={value}
   onChange={onValueChange}
   onBlur={handleInputBlur}
   onFocus={handleInputFocus}
   defaultCountry={country}
   countries={["NG", "MG", "SC", "KM", "BW", "MR"]}
 />

以下是它在没有在 props 中指定的情况下包含的国际选项的显示方式:

在此处输入图像描述

如何删除国际选项。

标签: reactjsvalidationlibphonenumber

解决方案


将国际道具设置为 false,以便将其删除。

通过文档 if country is US and international property is not passed then the phone number can only be input in the "national" format for US ((213) 373-4253). But if country is "US" and international property is true then the phone number can only be input in the "international" format for US (213 373 4253) without the "country calling code" part (+1)

你的代码应该是

<PhoneInput
   placeholder={placeholder}
   name={name}
   value={value}
   international = {false}
   onChange={onValueChange}
   onBlur={handleInputBlur}
   onFocus={handleInputFocus}
   defaultCountry={country}
   countries={["NG", "MG", "SC", "KM", "BW", "MR"]}
 />

推荐阅读