rifm
rifm copied to clipboard
Invalid cursor position
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
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 .
.
We found interesting solution. It will be another transformation prop with saving cursor position.
this works with new replace
api in 0.9
I could achive the described behavior only by:
- creating
format
function, which keeps original delimiter - 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:
- For parsing floats, I need to replace commas with dots in
format
function - Then I need to determine the initial delimiter and reconstruct formatted value with initial delimiter
- 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?
looks like good idea 👍