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.

NumberInput cannot be controlled in case an invalid value has been entered directly

See original GitHub issue

The NumberControl ignores the value property if invalid data has been entered in the input field directly

Detailed description

Using a simple

currentValue = 2;
<NumberInput
   min={0}
   max={10}
   value={currentValue}
   onChange={...}
/>

If a user enters something invalid like ,, into the input field directly, it is not possible to ignore such input in onChange by keeping the currently used value property. Which means that NumberControl cannot be properly controlled if it gets into invalid state.

Additional information

Code wise this is due to the implementation of

static getDerivedStateFromProps({ min, value }, state) {
    const { prevValue } = state;
    return prevValue === value
      ? null
      : {
          value: isNaN(min) ? value : Math.max(min, value),
          prevValue: value,
        };
  }

This method is the only place where state.prevValue is changed. Given the condition prevValue === value, this method will only ever update the state if prevValue !== value. Which means it is not possible to ignore input because ignoring means to provide the same value property all the time, which causes prevValue === value to hold.

At the same time

 handleChange = evt => {
    if (!this.props.disabled) {
      evt.persist();
      evt.imaginaryTarget = this._inputRef;
      this.setState(
        {
          value: evt.target.value,
        },
        () => {
          this.props.onChange(evt);
        }
      );
    }
  };

ensures that even invalid data entered into the input field updates the internal state (which cannot be patched from outside due to the finding above).

Expected behavior: It should be possible to enforce a certain value using properties.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
asudohcommented, Jul 12, 2019

Current code does not seem to do min/max capping before onChange() fires. I’d recommend min/max capping yourself given you specify min/max.

1reaction
asudohcommented, Mar 29, 2019

Hi @anschoen 👋 thank you for reporting - Dropped the following code into https://codesandbox.io/s/github/IBM/carbon-components-react/tree/master/examples/codesandbox but couldn’t reproduce the issue (comma couldn’t be entered there):

import React from 'react';
import { render } from 'react-dom';
import { NumberInput } from 'carbon-components-react';

let currentValue = 2;

const App = () => (
  <div>
    <NumberInput
      min={0}
      max={10}
      value={currentValue}
      onChange={evt => {
        console.log('Event:', evt);
      }}
    />
  </div>
);

render(<App />, document.getElementById('root'));

That said, do you want to create a reduced case like that? Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why the number input is the worst input - Stack Overflow Blog
This is the flipside of the invalid number value issue: the number input allows you to enter values that are technically acceptable numbers,...
Read more >
How to block +,-,e in input type Number? - Stack Overflow
Try preventing the default behaviour if you don't like the incoming key value: document.querySelector(".your_class").addEventListener("keypress", function ...
Read more >
How to handle invalid user inputs in React forms for UX ...
When there is only one <input> element within the <form> element, we need to disable what's known as implicit submission: hitting the Enter...
Read more >
Input Components - React-admin - Marmelab
React-admin uses react-hook-form to control form inputs. ... Value of the input if the record has no value for the source .
Read more >
<input>: The Input (Form Input) element - HTML
A control for entering a time value with no time zone. ... A field for entering a URL. Looks like a text input,...
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