Skip to content

Instantly share code, notes, and snippets.

@mcmxc
Created January 8, 2020 12:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcmxc/c43d9a2967e56fde5b76332fdc1fa83d to your computer and use it in GitHub Desktop.
Save mcmxc/c43d9a2967e56fde5b76332fdc1fa83d to your computer and use it in GitHub Desktop.
react-phone-number-input + ant design (antd) Input
import React, { useState, useCallback, forwardRef } from 'react';
import { Input } from 'antd';
import PhoneInput from 'react-phone-number-input/min';
import 'react-phone-number-input/style.css';
const PhoneInputComponent = forwardRef(({ onChange, ...props }, ref) => {
const handleChange = useCallback(e => onChange(e.target.value), [onChange]);
return <Input ref={ref} {...props} onChange={handleChange} />;
});
const App = () => {
const [phoneNumber, setPhoneNumber] = useState();
return (
<div className="app">
<PhoneInput
value={phoneNumber}
onChange={setPhoneNumber}
inputComponent={PhoneInputComponent}
/>
</div>
);
};
export default App;
@liyaodong
Copy link

Hey dude, I think this might be wrong. since the <Input ref={ref} is not correct. ref still means ant design Input rather than HTML input so it will cause some issue on react-phone-number-input library.

@liyaodong
Copy link

Or in another word, this solution doesn't work on ant design v4.

@Darian-Certn
Copy link

Any solution you guys know of with v4?

@c01nd01r
Copy link

c01nd01r commented May 4, 2021

We can extract the original input element from <Input /> instance.
Example with imask.js:
uNmAnNeR/imaskjs#163 (comment)

@mcmxc
Copy link
Author

mcmxc commented May 5, 2021

just checked, this peace of code still works with v4

@ovchinnikov
Copy link

doesn't work with the latest Antd https://stackblitz.com/edit/react-dg4xxp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment