26

I am developing an app on react native. I want to call a date picker when i press on a Text-Input and after selecting a date it should show on the Text-Input

Savad
  • 1,240
  • 3
  • 21
  • 50

5 Answers5

56

Wrap TextInput into a view and set pointerEvents as none. Now you can use Pressable component from react-native to listen to the onpress event.

<Pressable onPress={() => alert('Hi!')}>
 <View pointerEvents="none">
  <TextInput />
 </View>
</Pressable>
Kuldeep Saxena
  • 1,803
  • 1
  • 14
  • 17
19

You cannot explicitly call onPress for TextInput. You could only use. onFocus which will be called when you press the input box to get cursor over there. No need to focus on onBlur as your use case doesn't required.. Handle close within onFocus if possible.

onFocus = () => {
  // do something
}

render() {
  <TextInput onFocus={onFocus} />
}
Naveen Vignesh
  • 1,351
  • 10
  • 21
3

What you have to do is use onFocus and onBlur of TextInput.

<TextInput 
    onFocus={this.onFocus}
    onBlur={this.onBlur}
/>

onFocus = () => {
 // Open date picker
}

onBlur = () => {
 // Close date picker and add value to textinput
}
Samitha Nanayakkara
  • 2,529
  • 2
  • 10
  • 23
3

You can make text input field read only by providing

 editable={false}
 pointerEvents="none"

prop to TextInput.

Ram Rana
  • 194
  • 6
0

Wrap TextInput inside View and set pointerEvents="none", wrap that view with TouchableOpacity or any other Touchable Component. Please see below example!

<TouchableOpacity
  onPress={() => {
                    alert('hello');
                }}>
                <View pointerEvents="none">
                    <TextInput
                        onChange={() => {}}
                        value={''}
                        placeholder={waterMark}
                        editable={!isDisabled}
                        selectTextOnFocus={!isDisabled}
                    />
                </View>
</TouchableOpacity>
Lovekush Vishwakarma
  • 3,035
  • 23
  • 25