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

Dropdown List Appears Behind Modal and Cannot Be Selected #130

Open
lucasAzS opened this issue Jun 10, 2024 · 7 comments
Open

Dropdown List Appears Behind Modal and Cannot Be Selected #130

lucasAzS opened this issue Jun 10, 2024 · 7 comments
Labels
bug Something isn't working

Comments

@lucasAzS
Copy link

lucasAzS commented Jun 10, 2024

I am encountering an issue when using the react-native-autocomplete-dropdown library within a React Native modal. The dropdown list appears behind the modal and is not interactable. Additionally, when I attempt to modify the styles and positioning, the dropdown list either does not appear or remains below other elements, making it unusable.

Code Example:

import React, { useState } from 'react';
import { StyleSheet, Text, View, Modal, Button } from 'react-native';
import { AutocompleteDropdown } from 'react-native-autocomplete-dropdown';

const data = [
    { id: '77772', title: 'Isabel ' },
    { id: '77773', title: 'Elson ' },
    { id: '77774', title: 'Rodrigo ' },
    { id: '77775', title: 'Diogo ' },
    { id: '77792', title: 'Filipa ' },
    { id: '80830', title: 'Lucas' },
    { id: '99998', title: 'AutomatedTests' },
    { id: '99999', title: 'AutomatedTests' },
    { id: '121212', title: 'Karina ' },
    { id: '233217', title: 'Joaquim ' },
    { id: '999999', title: 'Processamento ' },
];

export const SandBoxMainScreen = React.memo(() => {
    const [modalVisible, setModalVisible] = useState(false);

    return (
        <View style={styles.container}>
            <Text>SandBox</Text>
            <Button title="Show Modal" onPress={() => setModalVisible(true)} />
            <Modal
                animationType="slide"
                transparent={true}
                visible={modalVisible}
                onRequestClose={() => {
                    setModalVisible(!modalVisible);
                }}
            >
                <View style={styles.modalView}>
                    <Text style={styles.modalText}>Hello, this is a modal!</Text>
                    <Button title="Close Modal" onPress={() => setModalVisible(false)} />

                    <AutocompleteDropdown dataSet={data} inputContainerStyle={styles.input} />
                </View>
            </Modal>
        </View>
    );
});

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#fff',
        alignItems: 'center',
        justifyContent: 'center',
    },
    modalView: {
        backgroundColor: 'white',
        borderRadius: 20,
        padding: 20,
        alignItems: 'center',
        shadowColor: '#000',
        shadowOffset: {
            width: 0,
            height: 2,
        },
        shadowOpacity: 0.25,
        shadowRadius: 4,
        elevation: 5,
        bottom: 0,
        left: 0,
        right: 0,
        zIndex: 1000,
        position: 'absolute',
        width: '100%',
        height: '50%',
    },
    modalText: {
        marginBottom: 15,
        textAlign: 'center',
    },
    input: {
        width: '100%',
        height: 40,
        padding: 10,
        borderWidth: 1,
        borderColor: 'black',
        marginBottom: 10,
        borderRadius: 5,
        backgroundColor: 'white',
    },
});

Screenshots
screenshot-1718056091668

@lucasAzS
Copy link
Author

I try to put the flatlist in the dropdown.js inside a modal, but then he fill all the screen

import React, { memo, useMemo } from 'react'
import { StyleSheet, FlatList, View, Keyboard, Modal } from 'react-native'

export const Dropdown = memo(
  ({ dataSet, suggestionsListMaxHeight, renderItem, ListEmptyComponent, ...props }) => {
    const ItemSeparatorComponent = useMemo(() => {
      return () =>
        props.ItemSeparatorComponent ?? <View style={{ height: 1, width: '100%', backgroundColor: '#ddd' }} />
    }, [props.ItemSeparatorComponent])

    return (
        <View
            style={{
                ...styles.listContainer,
                ...props.suggestionsListContainerStyle,
            }}
        >
            <Modal>
                <FlatList
                    keyboardDismissMode="on-drag"
                    keyboardShouldPersistTaps="handled"
                    nestedScrollEnabled={true}
                    onScrollBeginDrag={Keyboard.dismiss}
                    data={dataSet}
                    style={{ maxHeight: suggestionsListMaxHeight }}
                    renderItem={renderItem}
                    keyExtractor={(item) => item.id}
                    ListEmptyComponent={ListEmptyComponent}
                    ItemSeparatorComponent={ItemSeparatorComponent}
                    {...props.flatListProps}
                />
            </Modal>
        </View>
    );
  }
)

const styles = StyleSheet.create({
  container: {},
  listContainer: {
    backgroundColor: '#fff',
    width: '100%',
    zIndex: 9,
    borderRadius: 5,
    shadowColor: '#00000099',
    shadowOffset: {
      width: 0,
      height: 12
    },
    shadowOpacity: 0.3,
    shadowRadius: 15.46,

    elevation: 20
  }
})
screencapture-1718056688248.mp4

@lucasAzS
Copy link
Author

lucasAzS commented Jun 10, 2024

Ok, I see in the examples that using AutocompleteDropdownContextProvider inside the modal fixes the issue. I think that example should be included in the README.

import React, { useState } from 'react';
import { StyleSheet, Text, View, Modal, Button } from 'react-native';
import {
    AutocompleteDropdown,
    AutocompleteDropdownContextProvider,
} from 'react-native-autocomplete-dropdown';

const data = [
    { id: '77772', title: 'Isabel ' },
    { id: '77773', title: 'Elson ' },
    { id: '77774', title: 'Rodrigo ' },
    { id: '77775', title: 'Diogo ' },
    { id: '77792', title: 'Filipa ' },
    { id: '80830', title: 'Lucas' },
    { id: '99998', title: 'AutomatedTests' },
    { id: '99999', title: 'AutomatedTests' },
    { id: '121212', title: 'Karina ' },
    { id: '233217', title: 'Joaquim ' },
    { id: '999999', title: 'Processamento ' },
];

export const SandBoxMainScreen = React.memo(() => {
    const [modalVisible, setModalVisible] = useState(false);

    return (
        <View style={styles.container}>
            <Text>SandBox</Text>
            <Button title="Show Modal" onPress={() => setModalVisible(true)} />
            <Modal
                animationType="slide"
                transparent={true}
                visible={modalVisible}
                onRequestClose={() => {
                    setModalVisible(!modalVisible);
                }}
            >
                <AutocompleteDropdownContextProvider>
                    <View style={styles.modalView}>
                        <AutocompleteDropdown
                            dataSet={data}
                            inputContainerStyle={styles.input}
                            direction="up"
                        />
                        <Text style={styles.modalText}>Hello, this is a modal!</Text>
                        <Button title="Close Modal" onPress={() => setModalVisible(false)} />
                    </View>
                </AutocompleteDropdownContextProvider>
            </Modal>
        </View>
    );
});

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#fff',
        alignItems: 'center',
        justifyContent: 'center',
    },
    modalView: {
        backgroundColor: 'white',
        borderRadius: 20,
        padding: 20,
        alignItems: 'center',
        shadowColor: '#000',
        shadowOffset: {
            width: 0,
            height: 2,
        },
        shadowOpacity: 0.25,
        shadowRadius: 4,
        elevation: 5,
        bottom: 0,
        left: 0,
        right: 0,
        // zIndex: 1000,
        position: 'absolute',
        width: '100%',
        height: '50%',
    },
    modalText: {
        marginBottom: 15,
        textAlign: 'center',
    },
    input: {
        width: '100%',
        height: 40,
        padding: 10,
        borderWidth: 1,
        borderColor: 'black',
        marginBottom: 10,
        borderRadius: 5,
        backgroundColor: 'white',
    },
});

@cristianfavaro
Copy link

I was trying to use it with React Navigation Modal Stack, but no luck to... Your workaround make the list element appears, but it loses all the references to the element bar and open far away.

@mzorzella
Copy link

I'm using this component inside a tamagui Sheet component. I added the AutocompleteDropdownContextProvider inside <Sheet.Frame> and the dropdown appears apart from the text box. See image below.
image

@onmotion onmotion added the bug Something isn't working label Jul 31, 2024
@pwetherbee
Copy link

I'm seeing the same issue with the expo-router Stack.Screen component when presentation is "modal"

@ishanAhuja
Copy link

+1 for expo-router Stack.Screen

@mutschler
Copy link

Same for me, when presentation of the Stack is set to modal the popup doesn't appear

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants