-
Notifications
You must be signed in to change notification settings - Fork 197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(React Native) Can this package be used in the React Native? #296
Comments
Update: Yes, "without country select" component. First, create import React, { useCallback } from 'react'
import PropTypes from 'prop-types'
import { TextInput } from 'react-native'
function PhoneTextInput({
placeholder,
autoComplete,
autoFocus,
value,
onChange
}, ref) {
// Instead of `onChangeText` it could use `onChange` and get `value` from `nativeEvent.text`.
const onChangeText = useCallback((value) => {
onChange({
preventDefault() { this.defaultPrevented = true },
target: { value }
})
}, [onChange])
return (
<TextInput
ref={ref}
placeholder={placeholder}
autoFocus={autoFocus}
autoCompleteType={autoComplete}
keyboardType="phone-pad"
onChangeText={onChangeText}
value={value}/>
)
}
PhoneTextInput = React.forwardRef(PhoneTextInput)
PhoneTextInput.propTypes = {
placeholder: PropTypes.string,
autoComplete: PropTypes.string,
autoFocus: PropTypes.bool,
value: PropTypes.string,
onChange: PropTypes.func.isRequired
}
export default PhoneTextInput Then, in a React Native application: import React, { useState } from 'react'
import PhoneInput from 'react-phone-number-input/input'
import PhoneTextInput from './PhoneTextInput'
function Example() {
const [value, setValue] = useState()
return (
<PhoneInput
style={...}
smartCaret={false}
inputComponent={PhoneTextInput}
defaultCountry="US"
value={value}
onChange={setValue} />
)
} Report in this issue if it works. |
Also see the previous question: #283 (comment) |
@catamphetamine "without country select" doesn't work with react native. Although I am trying out a simple example with text input and the libphonenumber-js to work out something. Till now what I have done has worked till some extent but it will be great if you could help me in understanding how the smart caret works in your react component. For now the caret has a jumping effect when it meets with a blank space or a bracket. |
How are you using it?
Smart caret is using |
I am using it as per the instructions in the readme for using "without country select"
Yes, the latest. 3.0.13.
Any suggestions on how to use with react native input or how you implement it with react input? |
Then it won't work because React Native most likely doesn't support
See the |
Yes you are correct.
You mean to pass in the react native TextInput component here? |
Yes. |
Maybe something like: import React from 'react'
import { TextInput } from 'react-native'
export default function PhoneTextInput({
placeholder,
autoComplete,
autoFocus,
value,
onChange
}) {
return (
<TextInput
placeholder={placeholder}
autoFocus={autoFocus}
autoCompleteType={autoComplete}
keyboardType="phone-pad"
onChangeText={onChange}
value={value}
/>
);
} Report if it works. |
Ok, copying the above as a separate component and calling it as following:
Let me know if my interpretation is correct. |
Yes, see if it works. |
@catamphetamine On Load I get a On typing in the first digit, I also get an error: |
The component uses
Actually, we didn't forward ref in See if this works: import React from 'react'
import PropTypes from 'prop-types'
import { TextInput } from 'react-native'
function PhoneTextInput({
placeholder,
autoComplete,
autoFocus,
value,
onChange
}, ref) {
return (
<TextInput
ref={ref}
placeholder={placeholder}
autoFocus={autoFocus}
autoCompleteType={autoComplete}
keyboardType="phone-pad"
onChangeText={onChange}
value={value}
/>
)
}
PhoneTextInput = React.forwardRef(PhoneTextInput)
PhoneTextInput.propTypes = {
placeholder: PropTypes.string,
autoComplete: PropTypes.string,
autoFocus: PropTypes.bool,
value: PropTypes.string,
onChange: PropTypes.func.isRequired
}
export default PhoneTextInput |
Do I need to install the |
No, it's installed automatically. |
Yes, the forward ref warning disappeared but I get the |
Doesn’t it show a stack trace?
…On Thu, 23 Jan 2020 at 18:23, Ayan Dey ***@***.***> wrote:
Yes, the forward ref warning disappeared but I get the TypeError.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#296?email_source=notifications&email_token=AADUP3ZZJFBH2G4LJ3EFHGTQ7GZAXA5CNFSM4JMIZADKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEJXXIII#issuecomment-577729569>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AADUP33YW75QSG3MMW455OLQ7GZAXANCNFSM4JMIZADA>
.
|
So, the error originates at that function: I read a bit about React Native, and found out that even if the react-phone-number-input/source/PhoneInput.js Line 103 in d84e5ad
So, see if such code works: import PhoneInput from 'react-phone-number-input/input`
<PhoneInput smartCaret={false} .../> |
On adding
|
Oh, yes, change `onChangeText` to `onChange` in `<PhoneTextInput/>`.
…On Fri, 24 Jan 2020 at 09:10, Ayan Dey ***@***.***> wrote:
On adding smartCaret: { false } I receive a TypeError.
[image: Screenshot_2020-01-24-11-38-40-638_com furry]
<https://user-images.githubusercontent.com/8104493/73048198-4c0e9380-3e9e-11ea-974a-45ba4b51a49e.jpg>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#296?email_source=notifications&email_token=AADUP35VM5E462NIODWTGOTQ7KA5RA5CNFSM4JMIZADKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEJZ2OUY#issuecomment-578004819>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AADUP3ZMANUMVUAR77VMTKDQ7KA5RANCNFSM4JMIZADA>
.
|
So
You can add some kind of console.log(event.target.value) in onChange to see what's the value there.
<PhoneTextInput onChange={event => { console.log(event); onChange(event); }}/> |
The log turns out to be huge! So I am pasting the
|
Huh, so <PhoneTextInput onTextChange={value => onChange({
preventDefault() { this.defaultPrevented = true },
target: { value }
})}/> It creates an Perhaps there're more correct ways. |
I guess we get the values from |
Where should I try the above? |
Maybe. That could be a second route. Not yet though.
I meant: <TextInput onTextChange={(value) => onChange({
preventDefault() { this.defaultPrevented = true },
target: { value }
})}/> |
My current implementation was as following:
where PhoneTextInput is:
I am not sure if this is the way you mean. On a side note, I have been trying out to implement this on my own without |
Remove
I won't assist with any custom components, only with parts of |
@catamphetamine for now, I don't have the code as this part was delaying my progress with my app. But as far as I remember I did spot his mistake and tried with BTW, just a thought, why don't you publish a new package for RN? It would be just great! ;) |
I see.
Because I haven't worked with React Native, and so I'm not a specialist in it, and developing a React Native version of this package would better be done by someone being an expert in React Native. |
Neither am I. :(
True for everyone. :) |
Not sure if I have the time for this, but I'll take a look, as I might need to use this in my RN project. @catamphetamine This would require me refactoring the styles to get rid of CSS to make it work in RN. Should I just fork into a separate library? |
I've posted the theoretically supposedly working React Native "without country select" component in the first comment in this issue. |
Hey @catamphetamine, I can confirm that the snippets in your first comment work on React Native. I implemented my own country selector dropdown to go along with it using react-native-picker-select and it works great. I couldn't figure out how to replicate the behaviour in the "Force international format" example though, where the country code is part of the input field. Maybe there should be an additional flag in the props for that? Since setting a country using the |
@ianmartorell I see, so it works then. I guess I'll release it as a subpackage then.
There currently is no such mode because I personally thought that it's a better UX when the country code is outside of the input field. |
I ended up implementing it with the country code inside the picker, but I think it would've looked good too with the country code in the field and just a flag in the picker. Maybe it's better UX like you said, but I guess it's good to let the user choose. |
Published
Also added a new |
We're using
We're getting the error: |
@seamive See if |
@catamphetamine Hi! Could you add |
@vpodolyan All |
Parent props aren't being passed to children, This solves import React, { useCallback } from 'react'
import PropTypes from 'prop-types'
import { TextInput } from 'react-native'
/**
* This is an _experimental_ React Native component.
* Feedback thread: https://github.com/catamphetamine/react-phone-number-input/issues/296
*/
function PhoneTextInput({
autoComplete,
onChange,
...rest
}, ref) {
// Instead of `onChangeText` it could use `onChange` and get `value` from `nativeEvent.text`.
const onChangeText = useCallback((value) => {
onChange({
preventDefault() { this.defaultPrevented = true },
target: { value }
})
}, [onChange])
return (
<TextInput
ref={ref}
autoCompleteType={autoComplete}
keyboardType="phone-pad"
onChangeText={onChangeText}
{...rest}/>
)
}
PhoneTextInput = React.forwardRef(PhoneTextInput)
PhoneTextInput.propTypes = {
autoComplete: PropTypes.string,
onChange: PropTypes.func.isRequired
}
export default PhoneTextInput |
@gabrielmar cool! |
Published |
Thank you very much! |
Added a PR to allow inputComponent to be specified. |
the import statement "import PhoneInput from 'react-phone-number-input/react-native-input';" is giving a "could not find declaration file for module" error. How can I work around/fix this issue? |
Is that actually a functional issue? I think typescript just wants you to add a |
to what? theres a @types/react-phone-number-input but I don't see an equivalent for the react-native-input |
There isn't one, hence the error. Might be worth a PR but that's not causing functional issues is it? |
I've followed the example exactly but the component isn't showing up on the emulator and that's the only issue vsc is highlighting |
I'm seeing the same warning but the control is working fine on the iOS simulator and react native web at the least, so it's likely something else. Any chance you can throw up a snack that shows your issue https://snack.expo.dev/? |
Update: added |
@cgrady3 I was planning to add TypeScript "typings" to this library this weekend. Maybe tomorrow. |
@cgrady3 |
@catamphetamine thank you for that. However, now it's giving the same error for every file it references (PhoneTextInput, PhoneInput, InputBasic, PropTypes, etc.) |
I just wanted to mention something, I tried to implement using refs as suggested before but I didn't quite get it working. ChatGPT suggest using a prop I wasn't aware of called |
Can this package be used in the react native
The text was updated successfully, but these errors were encountered: