简体   繁体   English

如何将 cursor 聚焦到 React 可拖动菜单中的下一个 TextInput?

[英]How can I focus the cursor to the next TextInput in a React Draggable Menu?

I have a draggable context menu with three rows and three TextInput s.我有一个包含三行和三个TextInput的可拖动上下文菜单。 Right now I have two bugs:现在我有两个错误:

  1. When "tab" is hit, the menu closes当点击“tab”时,菜单关闭
  2. Even when I stop "tab" from closing (I return in the handleClose ), "tab" doesn't focus the cursor to the next TextInput .即使我停止“tab”关闭(我在handleClose中返回),“tab”也不会将 cursor 聚焦到下一个TextInput

I've tried adding tabIndex={0} to the three div s that surround each TextInput , but this didn't help with the tab issue.我尝试将tabIndex={0}添加到围绕每个TextInput的三个div ,但这对制表符问题没有帮助。

Does anyone know what might be wrong here?有谁知道这里可能出了什么问题?

Below is the simplified code (variables such as startNumber , endNumber , comments are state variables).下面是简化代码( startNumberendNumber等变量, comments是state个变量)。

const handleClose = (event?, reason?) => {
  if (reason && reason === 'tabKeyDown') {
    // Prevent the 'Tab' key from closing the window
    return;
  }
  setShouldOpen(false);
  otherStuffHere();
}

...

return (
<Draggable cancel="textarea">
<Menu
  open={shouldOpen}
  onClose={handleClose}
  anchorReference="anchorPosition"
  anchorPosition={{ top: y, left: x }}
  className={classes.myContextMenu}
>

  <div className={classes.row}>
    <div className={classes.title}>
      <Text>First number</Text>
    </div>
    <div className={classes.text} tabIndex={0}>
      <TextInput
        fullWidth
        value={startNumber}
        disabled={readOnly}
        onChange={handleStartNumberChange}
      />
    </div>
  </div>

  <div className={classes.row}>
    <div className={classes.title}>
      <Text>End number</Text>
    </div>
    <div className={classes.text} tabIndex={0}>
      <TextInput
        fullWidth
        value={endNumber}
        disabled={readOnly}
        onChange={handleEndNumberChange}
      />
    </div>
  </div>

  <div className={classes.row}>
    <div className={classes.title}>
      <Text>Comments</Text>
    </div>
    <div className={classes.text} tabIndex={0}>
      <TextInput
        fullWidth
        value={comments}
        disabled={readOnly}
        onChange={handleCommentsChange}
      />
    </div>
  </div>
</Menu>
</Draggable>
)

I wasn't able to find a solution while still using the Menu component, but I used the Popover component instead.我在仍然使用Menu组件的同时找不到解决方案,但我改用了Popover组件。

Using the same CSS and same everything else, I got the tab to work as intended without changing the UI.使用相同的 CSS 和相同的其他一切,我让选项卡按预期工作,而无需更改 UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM