自动对焦在 Material UI v5 中带有按钮组件的打开表单对话框中不起作用

问题描述 投票:0回答:1

有人知道为什么 TextField 中的自动对焦无法在带有按钮组件的打开对话框表单中工作吗?

      <TextField
        autoFocus
        margin="dense"
        id="name"
        label="Email Address"
        type="email"
        fullWidth
        variant="standard"
      />

参见这里,它正在工作!

参见这里,它不起作用!

但是如果您使用组件链接,它就可以工作! 请参阅这里

reactjs material-ui
1个回答
10
投票

看看这个:https://github.com/mui/material-ui/issues/33004#issuecomment-1455260156。 将

disableRestoreFocus
添加到
<Dialog>
对我有用!

这是一个例子。

  1. 对话框应该有
    disableRestoreFocus
    属性。
  2. 对话框中的文本字段应具有
    autoFocus
    属性。
<Dialog disableRestoreFocus {...otherProps}>
  ...
  <TextField autoFocus {...otherProps}/>
</Dialog>
© www.soinside.com 2019 - 2024. All rights reserved.