【问题标题】:Is there a way I can get the country info (code) from the flag selected in react-phone-input-2/material-ui-phone-number有没有办法从 react-phone-input-2/material-ui-phone-number 中选择的标志中获取国家信息(代码)
【发布时间】:2019-12-23 21:16:35
【问题描述】:

我正在使用从 react-phone-input 包开发的 https://www.npmjs.com/package/material-ui-phone-number 包,并希望从下拉列表中选择国家代码库。那可能吗?我确实看到了有关对象“国家/地区”的详细信息,但我无法获取国家/地区代码。

<MuiPhoneNumber value={phone || ''} defaultCountry={'us'}  preferredCountries={['gb', 'us']} disableAreaCodes={true} value={phone || ''} onChange={handleOnChange} />

我只能在 onChange 中获得目标值

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    您可以在material-ui-phone-number中的支持的事件中看到

    <MuiPhoneNumber onChange={(value, country) => console.log(country)} />
    

    第二个参数将为您提供一个国家数据对象,其中包括:姓名、拨号代码、国家代码

    还有

    国家数据对象不从 onKeyDown 事件返回

    【讨论】:

      【解决方案2】:

      我自己也遇到了这个问题,然后想通了……

      {<MuiPhoneNumber value={phone || ''} defaultCountry={'us'}  preferredCountries={['gb', 'us']} disableAreaCodes={true} value={phone || ''} onChange={(country) => handleOnChange(country.countryCode)} />}
      

      只需将 country 传递给 onchange 并使用 country.countryCode 获取代码,或使用 country.name 获取名称,使用 country.dialCode 获取拨号代码。

      干杯

      【讨论】:

        猜你喜欢
        • 2022-07-10
        • 2020-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-14
        • 1970-01-01
        • 2017-04-09
        • 1970-01-01
        相关资源
        最近更新 更多