admin管理员组文章数量:1387344
I'm writing a React app that will be used on mobile devices.
One of the pages has an <input>
text that the user inputs with the keyboard. Entering text in the field has an immediate effect, and thus no button needs to be pushed afterwards. I'd like to have the soft keyboard 'enter' key simply dismiss the keyboard without taking any action. Is this at all possible? It sounds so simple, yet I couldn't find how to do it.
For what it's worth, I tried adding enterKeyHint="done"
attribute (as described here), but other than changing the enter title from 'done' to a 'v' sign it does not dismiss the keyboard.
Any ideas? Thanks in advance!
I'm writing a React app that will be used on mobile devices.
One of the pages has an <input>
text that the user inputs with the keyboard. Entering text in the field has an immediate effect, and thus no button needs to be pushed afterwards. I'd like to have the soft keyboard 'enter' key simply dismiss the keyboard without taking any action. Is this at all possible? It sounds so simple, yet I couldn't find how to do it.
For what it's worth, I tried adding enterKeyHint="done"
attribute (as described here), but other than changing the enter title from 'done' to a 'v' sign it does not dismiss the keyboard.
Any ideas? Thanks in advance!
Share Improve this question asked Apr 30, 2021 at 3:48 chakazchakaz 3113 silver badges14 bronze badges1 Answer
Reset to default 6Sounds like it's simply a matter of triggering blur()
from the element itself.
You could either trigger blur()
from the event handler or by using useRef
.
Make sure to e.stopPropagation()
or e.preventDefault()
if you don't want your input to bubble the event upwards, or if it's inside of a form
export default function App() {
const onEnter = (e) => {
if (e.key === "Enter") {
e.target.blur();
}
};
return (
<div className="App">
<input onKeyUp={onEnter} />
</div>
);
}
Demo: https://codesandbox.io/s/relaxed-antonelli-j6c5d?file=/src/App.js
本文标签: javascriptReact ltinputgt Dismiss soft keyboard on enterStack Overflow
版权声明:本文标题:javascript - React <input>: Dismiss soft keyboard on enter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744495963a2609028.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论