react-number-format icon indicating copy to clipboard operation
react-number-format copied to clipboard

allowLeadingZeros should not allow leading zeros

Open Enlitened opened this issue 3 years ago • 7 comments

Setting allowLeadingZeros only removes the leading zeros when focus is lost.

Setting allowLeadingZeros should not allow leading zeros (similar to allowNegative). This interferes with a defaultValue of '0', the leading zero is never cleared.

Enlitened avatar Nov 02 '21 05:11 Enlitened

defaultValue is something that can be looked into. But while typing leading zero is indeed needed. For instance, you typed 1000002, then you want to delete the first char (1) and update it to (2). If we clear it while the user is in focus. It would create a usability issue.

s-yadav avatar Nov 03 '21 03:11 s-yadav

Would it be possible to separate this into two settings - allowLeadingZeros and removeLeadingZeros? allowLeadingZeros = false would not allow you type the first character as a zero, removeLeadingZeros would remove the zeros when focus is lost.

Enlitened avatar Nov 03 '21 04:11 Enlitened

IMO not allowing typing breaks the UX, and not in favour of providing a breaking behaviour prop, given that it gets consistent with what the user is expecting at the end without preventing them to do something. (Here by user I refer to end-user)

s-yadav avatar Nov 03 '21 06:11 s-yadav

Is it not the same functionality as allowNegative - it also prevents the user from typing a minus? I just think that not allowing leading zeros would be a very common use case - basically any currency input with formatting would use it. As it is now, it will still format currency with zeros in front, that seems incorrect?

Enlitened avatar Nov 03 '21 22:11 Enlitened

What if allowLeadingZeroes is respected on the first change (maybe not so much first change but when that is a length of 2)? If I click to the right of the 0 and type 2 I get 02. That's obviously not correct based on allowLeadingZeroes being false. I could see the usability case you mention, but in the initial number piece the setting is valid and that 0 should be removed.

vernonk avatar Jan 12 '22 17:01 vernonk

In my code:

const rangeOfLimit = (values: NumberFormatValues) => {
const { formattedValue, value, floatValue } = values as RangeOfLimitProps;
  if (value.charAt(0) === '0') {
    if (value.charAt(1) && value.charAt(1) != '.') {
      return false;
    }
  }
  return formattedValue === '' || (floatValue <= 99_999.99 && floatValue >= 0);
  }
 

Props

<NumberFormat
  isAllowed={rangeOfLimit}

Source: https://stackoverflow.com/a/53214578/18722313

ipatieff avatar Apr 06 '22 07:04 ipatieff

What if allowLeadingZeroes is respected on the first change (maybe not so much first change but when that is a length of 2)? If I click to the right of the 0 and type 2 I get 02. That's obviously not correct based on allowLeadingZeroes being false. I could see the usability case you mention, but in the initial number piece the setting is valid and that 0 should be removed.

let value = e.target.value.replace(/['.-]/g, ""); while (value.length > 1 && value.startsWith("0")) { value = value.substring(1); }

MantasGulbinskas avatar Dec 22 '23 10:12 MantasGulbinskas