在下方,您可以找到有关自动完成功能的MUI文档的示例,在该示例中,我已经在选项列表之前将链接传递给了google。但是,我无法单击该选项,事件目标只是MuiAutocomplete,而不是<a>我正在传递的事件。
<a>
import React from "react"; import TextField from "@material-ui/core/TextField"; import Paper from "@material-ui/core/Paper"; import Autocomplete from "@material-ui/lab/Autocomplete"; const Link = ({ children }) => ( <Paper> <a href="https://www.google.com/" rel="nofollow" target="_blank"> Go to Google </a> {children} </Paper> ); export default function ComboBox() { return ( <Autocomplete id="combo-box-demo" options={top100Films} getOptionLabel={option => option.title} style={{ width: 300 }} renderInput={params => ( <TextField {...params} label="Combo box" variant="outlined" fullWidth /> )} PaperComponent={Link} /> ); }
https://codesandbox.io/s/material-demo- egi6p
有趣的是,对自动完成功能开放
<Autocomplete open // add this prop id="combo-box-demo" options={top100Films}
使它能够按预期工作。
目前,我正在使用onMouseDown来完成这项工作,但是觉得这可能是一个不好的解决方案。
您可以通过添加以下内容来解决此问题:
onMouseDown={event => { event.preventDefault(); }}
到您的链接,以便您的Link组件看起来像:
Link
const Link = ({ children, ...other }) => ( <Paper {...other}> <a onMouseDown={event => { event.preventDefault(); }} href="https://www.google.com/" rel="nofollow" target="_blank" > Go to Google </a> {children} </Paper> );
我包含的一个单独的修复程序是通过任何其他道具传递给Paper组件(通过...other)。该Popper组件通过道具到Paper控制其定位部件,这样所述定位将不正确,而不这种变化。
Paper
...other
Popper
之所以event.preventDefault()需要这样做,是因为重点在于单击之前的输入。鼠标向下移动的默认效果之一是将焦点从输入更改为链接。输入的onBlur将触发的列表框部分的关闭,Autocomplete这意味着该链接将不再存在,并且不会继续onClick该链接的行为。调用event.preventDefault()可防止发生焦点更改。
event.preventDefault()
Autocomplete
onClick