使用React使用onkeydown在文本结束时使用cart/text-cursor进行焦点输入元素
我的组件中有两个输入字段:
const MyComponent = () => {
const onKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
if (event.key === 'ArrowLeft') {
ref.current?.setSelectionRange(value.length, value.length)
ref.current?.focus()
}
}
const [value, setValue] = useState('1234')
const ref = useRef<HTMLInputElement>(null)
return (
<>
<input
onChange={({target}) => setValue(target.value)}
value={value}
ref={ref}
type={'text'}/>
<input
onKeyDown={onKeyDown}
type={'text'}/>
</>
)
}
当我在第二个输入中击中左箭头键时,应该集中第一个输入,并且光标应在输入文本的末尾。
但是光标处于错误的位置。为什么这不起作用?
I have two input fields in my component:
const MyComponent = () => {
const onKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
if (event.key === 'ArrowLeft') {
ref.current?.setSelectionRange(value.length, value.length)
ref.current?.focus()
}
}
const [value, setValue] = useState('1234')
const ref = useRef<HTMLInputElement>(null)
return (
<>
<input
onChange={({target}) => setValue(target.value)}
value={value}
ref={ref}
type={'text'}/>
<input
onKeyDown={onKeyDown}
type={'text'}/>
</>
)
}
When i hit the left arrow-key on the second input, the first input should be focused, and the cursor should be at the end of the input text.
But the cursor is at the wrong place. Why is this not working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
光标是否向最后一个角色的左侧移动一个位置?
有趣的是,当使用
onkeyup
(键的发布)而不是onkeydown
时,问题似乎消失了。我列出了该解决方案,然后还有其他几个示例,其中包括下面的解释。解决方案
https://codesandbox.io/s/cursor-cursor-cursor-endor-endor-end-end of -input-onkeyup-x6ekke
说明
我的猜测是因为
onkeyup
自然遵循onkeydown
,当我们在React中接收OnKeydown
事件时,根据您的示例代码,发生以下顺序(或一般说类似这样的事情):内部
onkeydown
...我们的光标已移至文本输入的末尾。<<<<<<<<<<< /p>
ref.current?.setselectionrange(value.length,value.length)
文本输入接收焦点。
ref.current?.focus()
然后,左箭头键的释放
onkeyup
要在DOM中运行的事件(我们没有在React中执行此操作) em>现在,由于上述步骤2的结果,焦点是在文本输入上。输入重点是将左箭头键按/释放左箭头键的默认行为是将光标一个位置移至左侧,将其从输入文本末端放置一个位置。其他示例/解决方案,
如果您坚持使用
onkeydown
,这里还有其他几个示例。event.preventdefault()
settimeout()
https://codesandbox.io/s/cursor-end-of-input-example-h1yrdr
我的猜测是,这些解决方法有效地阻止了浏览器,从而超出了本机键向下触发或延迟我们的处理程序从运行到 分别启动了本机事件。
Was the cursor moving one position to the left of the last character?
Interestingly, when using
onKeyUp
(the release of the key) rather thanonKeyDown
the issue seems to go away. I've listed that solution followed by a couple other examples with explanations below.Solution
https://codesandbox.io/s/cursor-end-of-input-onkeyup-x6ekke
Explanation
My guess is that because
onKeyUp
naturally followsonKeyDown
, when we receive theonKeyDown
event in React, per your example code, the following sequence occurs (or generally speaking something like this is happening):Inside
onKeyDown
...Our cursor is moved to the very end of the text input.
ref.current?.setSelectionRange(value.length, value.length)
The text input receives focus.
ref.current?.focus()
Then, the release of the left arrow key causes
onKeyUp
event to run in the DOM (we haven't done anything to handle this in React) while the focus is now on the text input as a result of step 2 above. The default behavior pressing/releasing the left arrow key while the input has focus is to move the cursor one position to the left, placing it one position from the end of the input text.Other Examples/Solutions
If you stick with the use of
onKeyDown
, here are a couple other examples.event.preventDefault()
setTimeout()
https://codesandbox.io/s/cursor-end-of-input-example-h1yrdr
My guess is that these workarounds effectively block the browser from firing the native key down and up events altogether or delay our handler from running until after the native events have fired, respectively.