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
Asked
Active
Viewed 2.5k times
26
-
Not sure to understand, if the user can't type text in the text input, why not use a button? – MaximumLasagna Feb 15 '19 at 10:53
-
this is a special case – Savad Feb 15 '19 at 11:01
-
are you using any third party to open DatePicker? @StvNewsKaipamangalam – Patel Dhara Feb 15 '19 at 11:27
5 Answers
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
-
1this works for me because I also need it to be disable the TextInput, so only this answer works, thanks! – elblogdelbeto May 19 '21 at 07:13
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