react-phone-input-2 icon indicating copy to clipboard operation
react-phone-input-2 copied to clipboard

Issue with Canada/USA, Russia/Kazakhstan, countries with same calling code

Open ghost opened this issue 5 years ago • 9 comments

Hi, If from the dropdown we select Canada which has the same calling code as the USA. The component will show USA flag. Same for other countries with the same callin code.

Here is a link for sandbox: https://codesandbox.io/s/boring-lake-fe5o5?file=/src/App.js

ghost avatar Jan 26 '21 08:01 ghost

https://github.com/bl00mber/react-phone-input-2/issues/398

Manimall avatar Mar 24 '21 11:03 Manimall

Hi, If from the dropdown we select Canada which has the same calling code as the USA. The component will show USA flag. Same for other countries with the same callin code.

Here is a link for sandbox: https://codesandbox.io/s/boring-lake-fe5o5?file=/src/App.js

definitely, it's a huge bug

Manimall avatar Mar 24 '21 11:03 Manimall

i found i kinda-of solution there, it's not pretty, but in the end - it works)

We can use fourth argument in onChange fn - and create a wrapper component to avoid this problem -

const PhoneInputWrapper: FC<PhoneInputWrapperProps > = ({
  value,
  onChange,
  ...props
}) => {
  const [phoneValue, setPhoneValue] = useState(value);

  const handlePhoneChange = useCallback(
    (
      value: string,
      _data: Record<string, never>,
      _e: ChangeEvent<HTMLInputElement>,
      formattedValue: string,
    ) => {
      // saving and sending unformatted value to backend
      onChange(value);

      // changing the component state correctly
      setPhoneValue(formattedValue);
    }, []);

  return (
    <PhoneInput
      { ...props }
      value={phoneValue}
      onChange={handlePhoneChange} />
  );
};

export default PhoneInputWrapper;

Manimall avatar May 14 '21 17:05 Manimall

I found a similar problem and its solution https://github.com/bl00mber/react-phone-input-2/issues/221

Anton-SN avatar Jan 13 '22 09:01 Anton-SN

any buddy got solution for this issue?

dev-mani7 avatar May 24 '22 15:05 dev-mani7

I add this areaCodes={{ kz: [7, 6] }} and now it works fine for Kazahstan +76...., +77... - now it Kazahstan (not Russia)

TheEagleGame avatar May 29 '23 08:05 TheEagleGame

just add disableCountryGuess= {true}

hafidFrn avatar Sep 03 '23 09:09 hafidFrn

In input, we display only 2 country have a problem when enter Canada number, after component re-render country change to the USA.

valentynay06 avatar Sep 18 '23 16:09 valentynay06