question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Formatting of NumberInput and TextInput breaking after X chars pressed

See original GitHub issue

What you were expecting: In my case, user needs to enter revenue, based on which i will filter the results. Since revenue can be a huge number, i am trying to format the number using String.toLocaleString() so that it looks good and parse the value back to number when submitted to API

For this i have created 2 functions

const numberParser = (value) => (value ? parseInt(value, 10) : 0);
const numberFormatter = (value) => {
  return value?.toLocaleString();
};

and in my filters, i have defined a filter like this

<TextInput
  label="Min Revenue"
  source="revenue_min"
  key="revenue_min"
  format={numberFormatter}
  parse={numberParser} 
/>

Even tried to swap TextInput with NumberInput

What happened instead: Now with this implementation, the moment the user starts typing in, if i am using NumberInput, after 3 char, when 4th number is pressed, it resets the input field. For Text Input, 4 numbers are formatted, with 5th number it resets

Steps to reproduce:

To login, just use any username password

https://codesandbox.io/s/condescending-moore-jri1ug?file=/src/posts.js:1424-1588

I have prepared a code sandbox where if you go to the Post Page and try to add he Min Revenue Filter and start tying in the value, you will see the issue after the 5th char is inputed.

Related code: https://codesandbox.io/s/condescending-moore-jri1ug?file=/src/posts.js:1424-1588

Environment

  • React-admin version: ^3.9.0
  • Last version that did not exhibit the issue (if applicable): Not Sure
  • React version: 17
  • Browser: Chrome
  • Stack trace (in case of a JS error): No Error

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
achyutjhunjhunwalacommented, Mar 11, 2022

@CampingPanda You are a rockstar mate. Thank you so much for this help. This is exactly what i need. There is one caveat to this which i found when using the parse function now.

I am writing a logic in parse function which would convert this generated string by format function to Integer again before it sends to the backend.

But the parse function is overwriting whatever format does also in the UI. This mean the moment parse function executes, it converts the string to Integer and format functions fails because it always expect the value to be string. (.replaceAll only works with string and fails when it gets a number)

So I modified the code a bit like below to handle number input in format function

  const numberFormatter = (value: any) => {
    console.log('1  ' + value + '  ' + typeof value) // Standard input from the textbox
    if (value == null || value.length === 0) return value // No crash when empty textbox
    
    // ------------------CHANGED CODE -----------------------------
    
    if (typeof value === 'number') {
      value = value.toLocaleString() // Here i check if value is number (as parse function is converting it), and convert it to string so that it doesn't fails in the next step
    }
    
    // ------------------CHANGED CODE -----------------------------
    
    value = value.replaceAll('.', '').replaceAll(',', '') // Remove garbage so we can make a number
    console.log('2  ' + value + '  ' + typeof value)
    value = parseInt(value, 10) // toLocaleString needs a Number
    console.log('3  ' + value + '  ' + typeof value)
    if (isNaN(value)) return 0 // Cant make a number then 0
    return value?.toLocaleString('en-US') // Cast number to US locale
  }

  const numberParser = (value: any) => {
    return parseInt(value.replaceAll('.', '').replaceAll(',', ''), 10)
  }

With this change, now i don’t need any change on the backend

Once again @CampingPanda many thanks for your help here. Really really appreciate

1reaction
WiXSLcommented, Mar 11, 2022

Since this issue has been clarified not to be a react-admin bug, I’m closing it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Format a phone number as a user types using pure JavaScript
A function to format text to look like a phone number function phoneFormat(input){ // Strip all characters from the input except digits ...
Read more >
TextInput - React Native
Tells TextInput to automatically capitalize certain characters. This property is not supported by some keyboard types such as name-phone-pad ...
Read more >
Basic Input, Output, and String Formatting in Python
In this step-by-step Python tutorial, you'll learn how to take user input from the keyboard with the built-in function input(), how to display...
Read more >
<input>: The Input (Form Input) element - HTML
A single-line text field for entering search strings. Line-breaks are automatically removed from the input value. May include a delete icon in ...
Read more >
Python Input(): Take Input From User [Guide] - PYnative
Take integer, float, character, and string input from a user. Convert the user input to a different data type. Command-line input; How to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found