ReactNative: Element appearing on Top but wont trigger onPress - react-native

I have created a dropdown. Its elements appearing on top of body, however, when I press them, it actually triggers onPress on elements underneath them.
You guys can see the issue:
Code for the Dropdown is:
<View style={[styles.container]}>
<TouchableOpacity style={styles.main_container} onPress={this.animate}>
<Text style={styles.text}>{this.state.selectedCategoryText}</Text>
<Image style={styles.image}
source={require('../../assets/icons/dropdown.png')}
/>
</TouchableOpacity>
<View >
<Animated.View style={[styles.animated_container,{opacity: this.opacity, zIndex:1, elevation:1}]}>
<ScrollView style={{height:60}}>
{ this.data.map( (item, i)=>
<TouchableOpacity
style={{}}
disabled={false}
key={i}
onPress={()=>this.selectItem(i)}>
<Text style={styles.item_text}>{item} </Text>
</TouchableOpacity>
)}
</ScrollView>
</Animated.View>
</View>
</View>
I have applied absolute positioning on the animated view which displays the dropdown items.
animated_container:{
backgroundColor: colors.secondaryWhiteColor,
position: 'absolute',
top: '100%',
width: '100%', height: 70
}

I have managed to know the source of the problem, however I still don't know what exactly is the problem. DrawerNavigator from React.Navigation is doing something. If I remove the screen containing dropdown from the DrawerNavigator, then it works properly.

try to use touchable opacity from gesture handler not react native

Related

How to make TouchableOpacity wrap its content when nested inside parent that has flex = 1

I know that as default TouchableOpacity always has width as wrap-content. But I place it inside a View that has flex: 1. TouchableOpacity's width become match-parent/take the full width of the screen.
<View style={ {flex:1} }>
<TouchableOpacity>
<Text>I'm button</Text>
</TouchableOpacity>
</View>
The question is how could I make the TouchableOpacity wrap-content while nested inside a View like that.
Thanks
You will have to use position: "absolute" style for the TouchableOpacity
<View style={{flex:1}}>
<TouchableOpacity style={{position: "absolute" }}>
<Text>I'm button</Text>
</TouchableOpacity>
</View>
You can also use
alignSelf:'flex-start' can use center or flex-end as well.
you can style TouchableOpacity like this so that TouchableOpacity will have it's own style
<View style={ {flex:1} }>
<TouchableOpacity style={{width:'80%',height:'20%'}}>
<Text>I'm button</Text>
</TouchableOpacity>
</View>

Cant Use onPress() Events on Animated.View set to Position: Absolute - React Native

I made a scrollable header for an app I am building for a company. When I try to use a TouchableHighlight to trigger an onPress() event it does not register because the position key is set to 'absolute':
Ive tried the following after scouring the internet for hours:
-raising the zIndex/elevation
-making sure TouchableHighlight is imported from 'react-native'
-Animated library from 'react-native-reanimated'
When I comment out the position:absolute in the main header it works perfectly, but the header leaves a view behind it when it scrolls up that I cant lay a view on top of.
I'm running out of options here...any help would be GREATLY appreciated!
<Animated.View style={globalStyles.tabNavbar}>
<TouchableHighlight
style={{backgroundColor: 'white', height: 50, width: 100, zIndex: 99999, elevation: 99999}}
underlayColor="#eee"
onPress={() => {
console.log('PLZ WORK');
this.updateTabs('projectsTab');
}} >
<Animated.View style={{ alignItems: 'center'}}>
<Text onPress={() => { this.updateTabs('projectsTab');}} style={{fontWeight: 'bold'}}>Close</Text>
</Animated.View>
</TouchableHighlight>
<TouchableHighlight
style={{color: 'white'}}
underlayColor="#eee"
onPress={() => {
console.log('PLZ WORK');
this.updateTabs('notificationTab');
}} >
<View style={{alignItems: 'center'}}>
<Text style={{fontWeight: 'bold'}}>Close</Text>
</View>
</TouchableHighlight>
</Animated.View>
For those wondering, If you alter the View order of the Components to where the component with position: absolute is last, the buttons will work. In this situation I had to put my header last in the order even though it appears at the top of my screen.

How do I let touch events propagate to TouchableOpacity wrapped text links?

I have some text with onPress wrapped in a TouchableOpacity.
<TouchableOpacity onPress={doNavigateToComment}>
<View>
<Text>
<Text onPress={doNavigateToUser}>
#{user.username}
</Text>{" "}
liked your comment.
</Text>
</View>
... more stuff ...
</TouchableOpacity>
Tapping anywhere on that text will trigger doNavigateToComment. When the user taps the username it should call doNavigateToUser Is there a way to give the underlying Text onPress priority? It seems to only recognize the outer touchable wrapper.
I modified your code a little bit, but I am getting exactly your intended behavior.
doNavigateToComment() {
console.log('doNavigateComment');
}
doNavigateToUser() {
console.log('doNavigateToUser');
}
<View style={styles.container}>
<TouchableOpacity
onPress={() => this.doNavigateToComment()}>
<View style={{ backgroundColor: 'red', height: 40, justifyContent: 'center', alignItems: 'center' }} >
<Text>
<Text
style={{ backgroundColor: 'blue' }}
onPress={() => this.doNavigateToUser()}>
Username
</Text>{' '}
liked your comment.
</Text>
</View>
</TouchableOpacity>
</View>
Pressing on Username (blue area) will print doNavigateToUser and pressing the red area will print doNavigateComment.
You probably just need to adapt your styling.
Screenshot:
Working expo:
https://snack.expo.io/SkOzaxdiV
I figured it out. I was importing TouchableOpacity from react-native-gesture-handler instead of react-native. Once I fixed that import the taps started working correctly.

React Native Absolute Positioned Component elements onPress events not working

I created a custom dropdown component which has a transparent Scrollview with ToucableOpacity elements. However the onPress on them are not working, even though, zIndex is making them appear on top of everything else.
In the main screen, I created two elements, 1 header containing the Dropdown and 2nd one is the body which contains the rest. Its code is given below:
<View style={styles.body, {zIndex:0}}>
<View style={[styles.header,{zIndex:1}]}>
<Text style={styles.header_text}>Search In </Text>
<Dropdown items={CATEGORIES}/>
</View>
<View style={[styles.main,{zIndex:-1}]}>
<Text style={{backgroundColor:'blue'}}>I am the body</Text>
</View>
<View>
I have included the render event markup of the Dropdown element. Its code is given below:
<View style={styles.container}>
<TouchableOpacity style={styles.main_container} onPress={this.animate}>
<Text style={styles.text}>{this.state.selectedCategoryText}</Text>
<Text>-V-</Text>
</TouchableOpacity>
<View style={{zIndex:3}}>
<Animated.View style={[styles.animated_container,{opacity: this.opacity, zIndex:4}]}>
<ScrollView style={{height:60,zIndex:4}}>
{ this.data.map( (item, i)=>
<TouchableOpacity
style={{zIndex:5}}
disabled={this.state.disabled}
key={i}
onPress={()=>this.selectItem(i)}>
<Text >{item} {i}</Text>
</TouchableOpacity>
)}
</ScrollView>
</Animated.View>
</View>
</View>
Styles for Dropdown are:
container:{
zIndex:2
},
main_container:{
flexDirection: 'row',zIndex:2
},
text:{
fontFamily: fontStyles.PoppinsRegular,
fontSize: moderateScale(14)
},
animated_container:{
backgroundColor: colors.secondaryWhiteColor,
position: 'absolute',
top: '100%',
width: '100%',
zIndex:3
}
Screenshot is given as. zIndex makes the elements appear on top but i cannot trigger onPress on Toucable opacities.

How react native scroll text?

How react native scroll text?
I have a long text in a fixed height container. I want to show a part of text in that container and scroll it when I want to see more.
I have try overflow scroll but react native seems not support overflow scroll.
Thanks.
You can use ScrollView for this case. Just put your text inside it and set height to scroll view.
I'm used the following code. Take ScrollView and put your any element inside ScrollView.
render() {
return(
<View style={styles.container}>
<ScrollView>
<Text>
Add your text
</Text>
</ScrollView>
</View>
);
}
There is currently a limitation in React Native where you can't set height directly on the ScrollView style prop. So, I suggest you do the following if you also want to set the height:
<View style={{ height: 100 }}>
<ScrollView>
<Text>
Your text goes here.
</Text>
</ScrollView>
</View>
Or alternatively using flexGrow:
<ScrollView style={{ flexGrow: 0.2 }}>
<Text>
Your text goes here.
</Text>
</ScrollView>
Relevant issue I found: https://github.com/facebook/react-native/issues/3422
<View style={{ height: hp(3)}}>
<ScrollView>
<Text style={{ fontSize: hp(2) }}>
Your text goes here
</Text>
</ScrollView>