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

onInvalid never triggered #114

Open
pandaorb opened this issue Jun 4, 2019 · 2 comments
Open

onInvalid never triggered #114

pandaorb opened this issue Jun 4, 2019 · 2 comments

Comments

@pandaorb
Copy link

pandaorb commented Jun 4, 2019

Hi!
I’m having trouble triggering the onInvalid event in my NumericInput, which is a simple react-numeric-input.
The documentation (https://www.npmjs.com/package/react-numeric-input) states that onInvalid will be called with errorMessage, valueAsNumber, and valueAsString. I use valueAsNumber in the onChange event in an attempt to trigger the onInvalid event, but the breakpoint in _elevationInvalid is never hit even when valueAsNumber returns NaN.
Any guidance you could provide on how to properly use this event would be great!

public render() {
  return (
    <NumericInput value={this.state.elevation} onChange={this._elevationChange} onInvalid={this._elevationInvalid} />
  );
}

private _elevationChange = (_value: number | null, _stringValue: string, input: HTMLInputElement) => {
  const validatedValue = input.valueAsNumber;
  // do stuff
}

private _elevationInvalid = (error: string, _value: number | null, stringValue: string) => {
  // do stuff
}

@pandaorb pandaorb changed the title How to use onInvalid onInvalid never triggered Jun 12, 2019
@Nileshdcool
Copy link

any update on this .. how to mark control as invalid?

@rafaelperozin
Copy link

I had the same issue with onInvalid, the onChange is not fired when the user tries to add more than max.
Using observer of mobx ovbserver or useStateI lose the current value due to re-render the NumericInput

So... after some work, I found an alternative solution for warning messages.

  1. I created a specific component for the <NumericInput /> and I called it as QuantityPicker.
export const QuantityPicker: React.FC<QuantityPickerProps> = ({value, min, max, newValue}) => {
  const inputValue = value ? value : 0;
  const inputMin = min ? min : 0;
  const inputMax = max ? max : 10;

  return (
    <div>
      <div style={{maxWidth: 112}}>
        {min && max && min === max ? (
          <input type="number" value={1} style={{maxWidth: 112, textAlign: 'center'}} disabled />
        ) : (
          <NumericInput
            className="form-control"
            value={inputValue}
            min={inputMin}
            max={inputMax}
            onChange={(value: number | null) => newValue({value, min: inputMin, max: inputMax})}
            size={5}
            mobile
            required
          />
        )}
      </div>
    </div>
  );
};
  1. onChange calls a callback function on the parent component.
onChange={(value: number | null) => newValue({value, min: inputMin, max: inputMax})}
  1. At the parent component I do handleQuantityMessage when the call back is fired.
<QuantityPicker value={0} min={modifier.minQty} max={modifier.maxQty} newValue={handleQuantityError} />
const handleQuantityMessage = ({
  value,
  min,
  max,
}: QuantityMessageProps): void => {
  if (value === max) {
    console.log("## WARNING: You on the MAX limit");
  }
  if (value === min) {
    console.log("## WARNING: You on the MIN limit");
  }
  console.log("FINE:", value, min, max);
};

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

3 participants