I am trying to implement a feature in my app that allows users to search an address and select a suggestion. I used GooglePlacesAutocomplete to achieve this result. I have the code for autocomplete on a bottom sheet. The code works as intended on iOS but does not register suggestion selections on android.
My code for the bottomsheet component is as follows: (Omitting imports and styles)
const BottomSheet = forwardRef(({ activeHeight, children }, ref) => {
const { height } = useWindowDimensions();
const newActiveHeight = height - activeHeight;
const topAnimation = useSharedValue(height);
const open = useCallback(() => {
"worklet";
topAnimation.value = withSpring(newActiveHeight, {
damping: 100,
stiffness: 400,
});
}, []);
const close = useCallback(() => {
"worklet";
Keyboard.dismiss();
topAnimation.value = withSpring(height, {
damping: 100,
stiffness: 400,
});
}, []);
useImperativeHandle(
ref,
() => ({
open,
close,
}),
[open, close]
);
const animationStyle = useAnimatedStyle(() => {
const top = topAnimation.value;
return {
top,
};
});
const backDropAnimation = useAnimatedStyle(() => {
const opacity = interpolate(
topAnimation.value,
[height, newActiveHeight],
[0, 0.5]
);
const display = opacity === 0 ? "none" : "flex";
return {
opacity,
display,
};
});
const gestureHandler = useAnimatedGestureHandler({
onStart: (_, ctx) => {
ctx.startY = topAnimation.value;
},
onActive: (event, ctx) => {
if (event.translationY < 0) {
topAnimation.value = withSpring(newActiveHeight, {
damping: 100,
stiffness: 400,
});
} else {
topAnimation.value = withSpring(ctx.startY + event.translationY, {
damping: 100,
stiffness: 400,
});
}
},
onEnd: (_) => {
if (topAnimation.value > newActiveHeight + 50) {
topAnimation.value = withSpring(height, {
damping: 100,
stiffness: 400,
});
} else {
topAnimation.value = withSpring(newActiveHeight, {
damping: 100,
stiffness: 400,
});
}
},
});
return (
<>
<TouchableWithoutFeedback
onPress={() => {
close();
}}
>
<Animated.View style={[styles.backDrop, backDropAnimation]} />
</TouchableWithoutFeedback>
<PanGestureHandler onGestureEvent={gestureHandler}>
<Animated.View style={[styles.container, animationStyle]}>
<View style={styles.lineContainer}>
<View style={styles.line} />
</View>
{children}
</Animated.View>
</PanGestureHandler>
</>
);
});
export default BottomSheet;
My code for where the BottomSheet being triggered is as follows:
const ref = useRef();
const onPress = useCallback(() => {
ref.current.open();
}, []);
<Pressable style={styles.otherContainer} onPress={onPress}>
<Text style={styles.header}>Location</Text>
<CustomIcon name="Pin" width={35} height={35} />
</Pressable>
<BottomSheet
style={styles.location}
ref={ref}
activeHeight={height * 0.87}
>
<LocationDropdown style={styles.locationDropdown} />
</BottomSheet>
Finally, my code for the GooglePlacesAutoComplete (LocationDropdown) is as follows:
import React, { useState } from "react";
import {
View,
LayoutAnimation,
StyleSheet,
TouchableOpacity,
Text,
UIManager,
KeyboardAvoidingView,
} from "react-native";
import { GooglePlacesAutocomplete } from "react-native-google-places-autocomplete";
import Colors from "../../../../utils/colors";
if (
Platform.OS === "android" &&
UIManager.setLayoutAnimationEnabledExperimental
) {
UIManager.setLayoutAnimationEnabledExperimental(true);
}
const LocationDropdown = ({ modalWidth, modalHeight, ...props }) => {
const [searchPosition, setSearchPosition] = useState("minimized");
// TODO: Key should not be stored here
return (
<View style={{height:500}}>
<GooglePlacesAutocomplete
placeholder="Where is your event?"
onPress={(data, details = null) => {
console.log(data, details);
}}
query={{
key: "API_KEY",
language: "en",
}}
fetchDetails={true}
styles={{
textInputContainer: {
marginTop: 10,
},
textInput: {
fontFamily: "QuicksandMedium",
color: "black",
},
// placeholder: {
// color: "black",
// },
}}
/>
</View>
);
};
const styles = StyleSheet.create({
searchContainer: {
alignSelf: "center",
backgroundColor: Colors.primary500,
},
});
export default LocationDropdown;
On the android side, the auto complete places show up, but the touches are not being registered and data is not being logged to the console.
Thanks in advance!