简体   繁体   English

React onChange 文本输入正在将 cursor 移动到输入字段中的文本末尾

[英]React onChange text input is moving cursor to end of text in input field

I have a bug when I set the value from the state and change the value in a input.当我设置 state 的值并更改输入中的值时出现错误。

when you write in the input and you try to correct the text the cursor move to the end.当您在输入中写入并尝试更正文本时,cursor 移至末尾。

This is my element这是我的元素

<div className="campo">
  <p>Nombre</p>
  <div className="campo-input">
    <input
     type="text"
     name="name"
     value={this.state.name}
     onChange={this.handleInputChangeUpperCase}
     />
  </div>
</div>

This is my function这是我的 function

handleInputChangeUpperCase = (e) => {
    let { name, value } = e.target;
    this.setState({ [name]: value.toUpperCase() });
  };

Working Snippet:工作片段:

 class App extends React.Component { state = { name: "Ronaldo", }; // This is the function handleInputChangeUpperCase = (e) => { let { name, value } = e.target; this.setState({ [name]: value.toUpperCase(), }); }; render() { // This is the Element return ( <div className="campo"> <p> Nombre </p> <div className="campo-input"> <input type="text" name="name" value={this.state.name} onChange={this.handleInputChangeUpperCase} /> {this.state.name} </div> </div> ); } } ReactDOM.render(<App />, document.getElementById("root"));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="root"></div>

To control where the cursor is in an input field, use selectionStart , which indicates where your selected text begins.要控制 cursor 在输入字段中的位置,请使用selectionStart ,它指示所选文本的开始位置。 As long as you don't set selectionEnd , you'll just move the cursor to the desired position.只要您不设置selectionEnd ,您只需将 cursor 移动到所需的 position。

For instance, let's move to the position "1", just after the first character.例如,让我们移动到 position“1”,就在第一个字符之后。 For input "123", that should be "1|23"...对于输入“123”,应该是“1|23”...

 document.getElementById('text-area').selectionStart = 1; document.getElementById('text-area').focus()
 <input id="text-area" type="text" value="123"/>

For your situation, if you want the cursor to move to the start of the string, set .selectionStart = 0 .对于您的情况,如果您希望 cursor 移动到字符串的开头,请设置.selectionStart = 0 If you want it to go back to where it was originally, save the value with var tempstart =...selectionStart , and then restore it after you do the data change, .selectionStart = tempstart .如果您希望它 go 回到原来的位置,请使用var tempstart =...selectionStart保存该值,然后在进行数据更改后将其恢复, .selectionStart = tempstart

Source: Developer.Mozilla.org: HTMLInputElement.setSelectionRange()资料来源: Developer.Mozilla.org:HTMLInputElement.setSelectionRange()

Based on the original answer: https://stackoverflow.com/a/49648061/7427111基于原始答案: https://stackoverflow.com/a/49648061/7427111

 class App extends React.Component { state = { name: "Ronaldo", }; handleInputChangeUpperCase = (e) => { // Here is the addition const pointer = e.target.selectionStart; const element = e.target; window.requestAnimationFrame(() => { element.selectionStart = pointer; element.selectionEnd = pointer; }); let { name, value } = e.target; this.setState({ [name]: value.toUpperCase(), }); }; render() { return ( <div className="campo"> <p> Nombre </p> <div className="campo-input"> <input type="text" name="name" value={this.state.name} onChange={this.handleInputChangeUpperCase} /> {this.state.name} </div> </div> ); } } ReactDOM.render(<App />, document.getElementById("root"));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="root"></div>

If you need just toUpperCase as formatting then you can use css instead.如果您只需要toUpperCase作为格式,则可以改用 css。 It seems the simplest solution is described here https://stackoverflow.com/a/3724990/12868043似乎这里描述了最简单的解决方案https://stackoverflow.com/a/3724990/12868043

That behaviour seems to be happening with .toUpperCase() ;这种行为似乎发生在.toUpperCase()上;

handleInputChangeUpperCase = e => {    
    this.setState({ [e.target.name]: e.target.value });
};

If you don't require the text to be upper case immediately on input, I would apply the method outside of the setState如果您不要求文本在输入时立即变为大写,我会在setState之外应用该方法

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

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