Skip to content
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

How to apply click event on TextInput in react native #15570

Closed
ajaysaini-sgvu opened this issue Aug 20, 2017 · 15 comments
Closed

How to apply click event on TextInput in react native #15570

ajaysaini-sgvu opened this issue Aug 20, 2017 · 15 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@ajaysaini-sgvu
Copy link

ajaysaini-sgvu commented Aug 20, 2017

Hi I am using TextInput in my react native application. I want to open Date Dialog on clicking on TextInput. I am using TouchableOpacity to do some tricky stuff. I observed that it automatically calls onPress on loading my component every time but it doesn't when I explicitly press on it.

<TouchableOpacity onPress={console.log("Pressed")}>
  <TextInput
    style={{
      height: 40,
      borderColor: "gray",
      borderWidth: 1,
      marginTop: 8
    }}
    underlineColorAndroid="transparent"
    placeholder={strings.schedule_date}
  />
</TouchableOpacity>

Does anyone know any work around for this issue ?

@Amwam
Copy link

Amwam commented Aug 20, 2017

@ajaysaini-sgvu This is a question better suited for stack overflow

However, to answer your question, onPress accepts a function

<TouchableOpacity onPress={() => console.log("Pressed")}>
...

The function is called when the component is pressed.
In your example you are assigning the output of console.log("Pressed") to the onPress handler, so it is evaluated when the components are rendered.

@ajaysaini-sgvu
Copy link
Author

@Amwam Thanks for your reply. I already tried suggested solution. It didn't work for me. Looks like something is broken with react native itself.

<TouchableOpacity onPress={() => console.log("Pressed")}>
  <TextInput
    style={{
      height: 40,
      borderColor: "gray",
      borderWidth: 1,
      marginTop: 8
    }}
    underlineColorAndroid="transparent"
    placeholder={strings.schedule_date}
  />
</TouchableOpacity>    

@Amwam
Copy link

Amwam commented Aug 20, 2017

@ajaysaini-sgvu I've just noticed you are using a TextInput, you should be using just Text for this, as the TextInput will likely have its own handling for press events (i.e. on the native side showing the keyboard).

If your are still having problems, a post on stack overflow will allow for better feedback.

@Mehran-khan-memon
Copy link

Add pointerEvents="none" on TextInput

Example

<TouchableOpacity onPress={() => console.log("Pressed")}>
  <TextInput
   pointerEvents="none"
  />
</TouchableOpacity>

@bezsinnyidmytro
Copy link

Need to do something like this:

<TouchableOpacity onPress={() => { this.refs['input'].focus() }}>
  <TextInput
   pointerEvents="none"
   onEndEditing={() => { change state value }}
  />
  <Image /> // for any icon-picture
</TouchableOpacity>

thus Opacity work on any touch, but pointerEvents='none' doesn't work in this case, when tap on TextInput opacity efect doesn't appear.
Any suggestions, how to fix or workaround?

@azwar
Copy link

azwar commented Apr 30, 2018

You can trigger event using onTouchStart. So TouchableOpacity is not needed. Here it is:

 <TextInput
    onTouchStart={()=>  alert("Hello...")}
    editable={false}
    placeholder="Please enter your text" />

@Laslo89
Copy link

Laslo89 commented May 4, 2018

Thx @azwar ! that really saved me. I somehow missed this method. Answering old posts does matter! ;)

@azwar
Copy link

azwar commented May 4, 2018

You are welcome @Laslo89 . Be careful when you use it on Android. onTouchStart seems not working on Android.

@prdpklyn
Copy link

prdpklyn commented May 30, 2018

Thanks @azwar any work around for android??

@azwar
Copy link

azwar commented May 30, 2018

@prdpsdfc The best method is using TouchableOpacity

@prdpklyn
Copy link

Thanks @azwar i started with TouchableOpacity however the place holder text is showing only few characters

@azwar
Copy link

azwar commented May 31, 2018

@prdpsdfc you can set editable InputText as false and set the text programatically rather than using placeholder, if it is not a must.

@snehaChaudhari
Copy link

thanx @azwar It's working.!!!

@wesleyfc
Copy link

thank you @azwar TouchableOpacity worked on Android and onTouchStart worked on IOS, so i used both in my code and got the results. :)

<TouchableOpacity onPress={() => this.setState({ isTimeStartVisible: true })}>
<Input editable={false} onTouchStart={() => this.setState({ isTimeStartVisible: true })} value={this.state.timeStart} />

@pppluto
Copy link

pppluto commented Aug 15, 2018

@Mehran-khan-memon 🍺

@facebook facebook locked as resolved and limited conversation to collaborators Aug 20, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Aug 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests