在React项目中如何用回车键模拟Tab?

想要实现用回车键模拟按tab在输入框之间切换
目前尝试用event.keycode无效,尝试创建keyborderevent然后触发也无效,是不支持这种用法还是在React项目不能用这种方法。
如果不支持的话是否只能通过人为focus的方式实现

阅读 2.3k
1 个回答

请关注React的文档说明

关键词:onKeyDown

const handleKey: KeyboardEventHandler<HTMLInputElement> = (event) => {
    if (event.code === 'Enter') {
      nextInputRef.current?.focus()
    }
  }
<p className="mb-10">
    <Input type='text' ref={inputRef} onKeyDown={handleKey}/>
</p>
<p className="mb-10">
    <Input ref={nextInputRef}/>
</p>
推荐问题