Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid cursor position #65

Open
mashafomasha opened this issue May 29, 2019 · 5 comments
Open

Invalid cursor position #65

mashafomasha opened this issue May 29, 2019 · 5 comments

Comments

@mashafomasha
Copy link

We want to allow user to type any digit, dot or comma symbols and replace comma with dot on the fly.

const numberAccept = /[\d.,]+/g;

const parseNumber = string =>
  (string.replace(",", ".").match(numberAccept) || []).join("");

But when user types comma symbol cursor visually jumps to the wrong position before new dot symbol.

Sandbox example:
https://codesandbox.io/s/istarkovrifm-numberformat-e6jfu?fontsize=14

@TrySound
Copy link
Contributor

Hi, currently we handle similar case with toLowerCase. Though it's hardcoded now.
https://github.com/istarkov/rifm/blob/master/src/Rifm.js#L81-L96

We will probably add isEqual prop so you could provide own check considering , and ..

@TrySound
Copy link
Contributor

TrySound commented Jun 2, 2019

We found interesting solution. It will be another transformation prop with saving cursor position.

@TrySound
Copy link
Contributor

this works with new replace api in 0.9

@pastushenkoy
Copy link

pastushenkoy commented Apr 9, 2020

I could achive the described behavior only by:

  1. creating format function, which keeps original delimiter
  2. creating replace function, which replaces original delimiter with the desired one.

If this is the only way I can do it, then it's quite sad because:

  1. For parsing floats, I need to replace commas with dots in format function
  2. Then I need to determine the initial delimiter and reconstruct formatted value with initial delimiter
  3. Here I can apply replace and get what I want

A better solution would be introducing a preprocessor function. A good candidate for it is the accept prop. If it is a function then, instead of applying it as a regex, it should be called on an input value and change the input before applying format.

What do you think about it?

@istarkov
Copy link
Contributor

istarkov commented Apr 9, 2020

looks like good idea 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants