L
L
Lesha Fedoseev2014-11-06 16:14:33
JavaScript
Lesha Fedoseev, 2014-11-06 16:14:33

React: how to properly normalize cursor position in input?

There is a regular text input in which you need to replace the “dot” with an “exclamation mark” after each change.
First iteration: jsfiddle.net/0cnnyy74
If you enter a "dot" at the end of the line, then everything is ok. But if you make an arrow on the keyboard ← ← and put a “dot” in the middle, then of course it will turn into an “exclamation mark”, but the caret will bounce to the end. I don’t need to) I solve
it with the second iteration: jsfiddle.net/ruc56bbv
Attention, the question is: the second iteration is React-way? Or is it possible to do this in a more elegant way inside the component?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
KnightForce, 2017-05-26
@KnightForce

If you just need to replace all dots with an exclamation point when typing/changing input, then:

onChange(e) {
    e.target.value=e.target.value.replace(/\./g, "!");//Регулярное выражение, которое заменит все точки
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question