如何获得下拉列表/自动完成建议在ReactJ中显示在模态之上?

发布于 2025-01-31 12:11:04 字数 2777 浏览 1 评论 0原文

我有一种模态,可以提示用户在输入字段中输入具有自动完成功能的某些位置。但是,自动完整建议在模式中显示

以下是我的模态组件的代码。我正在使用MUI和@ress/combobox

const ContainerStyle = {
    position: 'absolute',
    top: '50%',
    left: '50%',
    transform: 'translate(-50%, -50%)',
    display: 'flex',
    flexDirection: { xs: 'column', md: 'row' },
    alignItems: 'center',
    bgcolor: 'background.paper',
    border: '1px solid #000',
    boxShadow: 24,
    borderRadius: '12px',
    margin: 0,
};


export const WelcomeModal = () => {
    const [open, setOpen] = useState(true)

    return ReactDOM.createPortal(<>
        <Modal
            aria-labelledby="transition-modal-title"
            aria-describedby="transition-modal-description"
            open={open}
            onClose={() => setOpen(false)}
            closeAfterTransition
            BackdropComponent={Backdrop}
            BackdropProps={{
                timeout: 500,
            }}
        >
            <Box sx={ContainerStyle} >
                <StartPlaces />
                <EndPlaces />
            </Box>
        </Modal>
    </>, document.getElementById("portal"))
}

下面是我的startplace component的代码。 终点组件是相同的。

export const StartPlaces = ()=>{
    const dispatch = useDispatch()
    const {ready, value, setValue, suggestions: {status, data}, clearSuggestions} = usePlacesAutocomplete();

    const handleSelect = async (val)=>{
        
        setValue(val, false);
        clearSuggestions();

        const results = await getGeocode({address: val});
        // console.log(results[0])
        const {lat, lng} = await getLatLng(results[0])
        dispatch(setOrigin({
            coordinates: {lat, lng},
            name: val
        }))
    }

    return<>
        <h4>Enter Origin</h4>
        <Combobox onSelect={handleSelect}>
            <ComboboxInput value={value} onChange={(e) => setValue(e.target.value)} className="combobox-input" placeholder="Houston, Texas" disabled={!ready}/>
            <ComboboxPopover>
                <ComboboxList style={{zIndex: 3000}}>
                    {status === "OK" && data.map(({place_id, description}) =><ComboboxOption key={place_id} value={description}/>)}
                </ComboboxList>
            </ComboboxPopover>
        </Combobox>
    </>
}

由于我在Portal中渲染所有内容,因此我无法想到发生这种情况的原因。我尝试在zindex中弄乱comboboxlist,但似乎不起作用。任何帮助或建议将不胜感激:)预先感谢您。

I have a modal that prompts the users to enter some locations in an input field with autocomplete functionality. However, the autocomplete suggestions are displayed behind in the modal
enter image description here

Below is the code for my modal component. I am using MUI and @reach/combobox

const ContainerStyle = {
    position: 'absolute',
    top: '50%',
    left: '50%',
    transform: 'translate(-50%, -50%)',
    display: 'flex',
    flexDirection: { xs: 'column', md: 'row' },
    alignItems: 'center',
    bgcolor: 'background.paper',
    border: '1px solid #000',
    boxShadow: 24,
    borderRadius: '12px',
    margin: 0,
};


export const WelcomeModal = () => {
    const [open, setOpen] = useState(true)

    return ReactDOM.createPortal(<>
        <Modal
            aria-labelledby="transition-modal-title"
            aria-describedby="transition-modal-description"
            open={open}
            onClose={() => setOpen(false)}
            closeAfterTransition
            BackdropComponent={Backdrop}
            BackdropProps={{
                timeout: 500,
            }}
        >
            <Box sx={ContainerStyle} >
                <StartPlaces />
                <EndPlaces />
            </Box>
        </Modal>
    </>, document.getElementById("portal"))
}

Below is the code for my StartPlaces component. EndPlaces component is identical.

export const StartPlaces = ()=>{
    const dispatch = useDispatch()
    const {ready, value, setValue, suggestions: {status, data}, clearSuggestions} = usePlacesAutocomplete();

    const handleSelect = async (val)=>{
        
        setValue(val, false);
        clearSuggestions();

        const results = await getGeocode({address: val});
        // console.log(results[0])
        const {lat, lng} = await getLatLng(results[0])
        dispatch(setOrigin({
            coordinates: {lat, lng},
            name: val
        }))
    }

    return<>
        <h4>Enter Origin</h4>
        <Combobox onSelect={handleSelect}>
            <ComboboxInput value={value} onChange={(e) => setValue(e.target.value)} className="combobox-input" placeholder="Houston, Texas" disabled={!ready}/>
            <ComboboxPopover>
                <ComboboxList style={{zIndex: 3000}}>
                    {status === "OK" && data.map(({place_id, description}) =><ComboboxOption key={place_id} value={description}/>)}
                </ComboboxList>
            </ComboboxPopover>
        </Combobox>
    </>
}

I couldnt think of a reason why this is happening since I am rendering everything in the portal. I tried messing with the zIndex in my ComboboxList but it doesnt seem to work. Any help or suggestions would be greatly appreciated :) Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文