Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

material-ui-number-input

NoHomey331MIT5.0.24TypeScript support: included

The better TextField for number inputs.

react, material-ui, input, number, ux, material-design, input-number, user-expects

readme

material-ui-number-input

The better TextField for number inputs.

Install

NPM

Changelog

Check Change log for changes.

Properties

Name Type Default TextField Description
children node | ✓
className string | ✓ The css class name of the root element.
disabled bool false Disables the input field if set to true.
floatingLabelFixed bool false If true, the floating label will float even when there is no value.
id string | ✓ The id prop for the input field.
name string | ✓ Name applied to the input.
fullWidth bool false If true, the field receives the property width 100%.
underlineShow bool true If true, shows the underline for the input field.
defaultValue number | ✓ The number to use for the default value. Must be in range [min, max] if any is setted.
strategy 'ignore' | 'warn' | 'allow' 'allow' Strategy to use when user presses key and when value prop change it's value.
min number | ❌ The number used as minimum value limit. Must be less than max.
max number | ❌ The number used as maximum value limit. Must be greater than min.
required bool false If true and if input is left empty than instead of 'clean', 'required' error will be emited throughout onError handler if useStrategy is not 'ignore'.
value string | ✓ The value of the input field.
onChange function | ✓ Callback function that is fired when input filed must change it's value. Signature: function(event: React.FormEvent<HTMLInputElement>, value: string) => void.
onError function | ❌ Callback function that is fired when input error status changes and strategy is not 'ignore'. Signature: `function(error: 'none' 'invalidSymbol'
onValid function | ❌ Callback function that is fired when input's value is a valid number. Signature: function(value: number) => void
onRequestValue* function | ❌ Callback function that is fired when strategy is 'warn' or 'ignore', input is controlled and an invalid number value is passed. It provides valid number value which needs to be setted. Signature: function(value: string) => void
errorText node | ✓ The error content to display.
errorStyle object | ✓ The style object to use to override error styles.
floatingLabelFocusStyle object | ✓ The style object to use to override floating label styles when focused.
floatingLabelStyle object | ✓ The style object to use to override floating label styles.
floatingLabelText node | ✓ The content to use for the floating label element.
hintStyle object | ✓ Override the inline-styles of the TextField's hint text element.
hintText node | ✓ The hint content to display.
inputStyle object | ✓ Override the inline-styles of the TextField's input element. When multiLine is false: define the style of the input element. When multiLine is true: define the style of the container of the textarea.
style object | ✓ Override the inline-styles of the root element.
underlineDisabledStyle object | ✓ Override the inline-styles of the TextField's underline element when disabled.
underlineFocusStyle object | ✓ Override the inline-styles of the TextField's underline element when focussed.
underlineStyle object | ✓ Override the inline-styles of the TextField's underline element.

* onRequestValue is required when strategy is 'warn' or 'ignore' and input is controlled in order to ensure correct strategy behaviour.

Strategies

strategy onError fired onRequestValue fired
'allow'
'warn' ✓*
'ignore' | ✓*

* Fired when input is controlled (value is provided). If input is not controlled it's value will be automaticlly corrected when it get's invalid number value.

Errors

'none'

Fired when input's value is valid (there is no error).

'required'

Fired when required prop is true and user leaves empty the input or it gets cleared.

'clean'

Fired when required prop is false and user leaves empty the input or it gets cleared.

'invalidSymbol'

Fired when user enters none special key which is different than -,.,[0-9].

'incompleteNumber'

Fired wehn user enters - as first char in the input or when user enters the first ..

'singleMinus'

Fired when user enters - not as a first char.

'singleFloatingPoint'

Fired when user enters . and there is already one entered.

'singleZero'

Fired when user has entered 0 as first char and enters a digit key.

'min'

Fired when user enters number less than min prop value.

'max'

Fired when user enters number greater than max prop value.

public methods

NumberInput re-exposes public method getInputNode(): HTMLInputElement from TextField.

TextField methods: blur, focus, select and getValue are not exposed as they and getInputNode will be removed in material-ui 0.16 and replaced with public member input which is public and now but getInputNode is prefered until 0.16 is released. If you want to use any of those methods call them on input retunrned from getInputNode with the excpetion of getValue instead use value property.

Example

import * as React from 'react';
import NumberInput from 'material-ui-number-input';

class Demo extends React.Component {
  constructor(props) {
  super(props);

  this.onKeyDown = (event) => {
    console.log(`onKeyDown ${event.key}`);
  };

  this.onChange = (event, value) => {
    const e = event;
    console.log(`onChange ${e.target.value}, ${value}`);
  };

  this.onError = (error) => {
    let errorText;
    console.log(error);
    switch (error) {
      case 'required':
        errorText = 'This field is required';
        break;
      case 'invalidSymbol':
        errorText = 'You are tring to enter none number symbol';
        break;
      case 'incompleteNumber':
        errorText = 'Number is incomplete';
        break;
      case 'singleMinus':
        errorText = 'Minus can be use only for negativity';
        break;
      case 'singleFloatingPoint':
        errorText = 'There is already a floating point';
        break;
      case 'singleZero':
        errorText = 'Floating point is expected';
        break;
      case 'min':
        errorText = 'You are tring to enter number less than -10';
        break;
      case 'max':
          errorText = 'You are tring to enter number greater than 12';
          break;
      }
      this.setState({ errorText: errorText });
    };

    this.onValid = (value) => {
      console.debug(`${value} is a valid number`);
    };

    this.onRequestValue = (value) => {
      console.log(`request ${JSON.stringify(value)}`);
      this.setState({ value: value })
    }
  }

  render() {
    const { state, onChange, onError, onKeyDown, onValid, onRequestValue } = this; 
    return (
      <NumberInput
        id="num"
        value={state.value}
        required
        defaultValue={9}
        min={-10}
        max={12}
        strategy="warn"
        errorText={state.errorText}
        onValid={onValid}
        onChange={onChange}
        onError={onError}
        onRequestValue={onRequestValue}
        onKeyDown={onKeyDown} />
    );
  }
}

Written in Typescript and Typescript Ready! (check example)

Supports propTypes for regular JavaScript users

Testing

Tests will be added soon

Contributing

  1. Fork the repository
  2. npm install
  3. npm run typings
  4. Make changes
  5. npm start
  6. open http://localhost:3000
  7. Make a Pull Request

changelog

Change Log

v2.0.0

Properties

  • value is now of type 'string'
  • onChange changed signature to function (event: React.FormEvent, value: string, complete: boolean) => void
  • error is new prop of type React.PropTypes.oneOf(['none', 'invalidSymbol', 'incompleteNumber', 'singleMinus', 'singleFloatingPoint', 'singleZero', 'min', 'max', 'required']) and it's the controlled error so onError can be called only when error changes. Default value is 'none'

Changes

  • replacing state double with controlled input
  • value is now watched for changes
  • onChange is now called every time when input value must change, third argument provides infomration is value a complete number.

Bug fixes

  • onKeyDown is now called when . and - are pressed and emitted error is 'incompleteNumber'

Implementation

  • shouldComponentUpdate is now not overrided

v3.0.0

Properties

Deprecated

  • showDefaultValue
  • error

New

  • defaultValue is of type number and is the same as TextField and input defaultValue prop
  • onValidis function with signature function(value: number) => void called when input's value is a valid number
  • useStrategy is of type React.PropTypes.oneOf(['ignore', 'warn', 'allow']) with defualt value 'allow' and sets used error strategy refer to Strategy and Errors

Changed

  • onError signature changed to function(event: React.FromEvent, value: string) => void
  • onChange signature changed to function(error: 'none' | 'invalidSymbol' | 'incompleteNumber' | 'singleMinus' | 'singleFloatingPoint' | 'singleZero'| 'min' | 'max' | 'required' | 'clean';) => void

Errors

  • 'clean' equivalent of 'required' when required prop is false

Implementation

  • error is moved from props to state
  • re-exposing public method getInputNode of TextField
  • using polyfillied Object.assign ('object-assign')

v3.1.0

Properties

Deprecated

  • useStrategy has been renamed to strategy

README changes

v3.1.2

Bug fixes

  • fixing when event.preventDefault() is called and when event is delegated

v3.2.0

Breaking changes

  • When strategy is 'ignore' and error is min or max instead of clearing input field it's value is overwritten with String(props[error])

v4.0.0

Bug fixes

  • Fixing all bugs introduced after v3.0.0 which prevented valid numbers to be entered when strategy is 'ignore' or 'warn'.
  • Fixing a bug after v3.2.0. Now onValid is emitted when input's value is beeing overwritten when ''min'' or 'max' errors are cathced and strategy is not 'allow'.
  • Fixing a bug after v3.2.0 where input value was not overwritten when 'min' or 'max' errors are cathced and strategy is not 'allow' and input is uncontrolled.
  • Fixing a bug where input's value is not beeing validated before and after initial render depending on that is the input controlled or not.

v4.0.1

Bug fixes

  • Fixing a bug where valid numbers are prevented from beeing entered. This bug occures when (min * 10) < max (not fixed in v4.0.0)
  • Fixing a bug where valid numbers are prevented from beeing entered. This bug occures when Number(checkedValue) === 0 (not fixed in v4.0.0)

v4.0.2

Bug fixes

  • Ensure onBlur is called if a handler is passed via props

New

  • Adding new public method getTextField with signature () => TextField which returns underling TextField ref

v5.0.0

Properties

New

  • onRequestValueis function with signature function(value: string) => void called with correct number value when strategy is 'warn' or 'ignore' and value is provided.

Implementation

  • Droping alot of the logic for correcting value when strategy is 'warn' or 'ignore' and simplified it by introducing new prop onRequestValue which should ensure correct behavior when consumed by third party libraries such as 'react-material-ui-keyboard'

v5.0.1

  • Re-exporting default from './NumberInput'

v5.0.2

  • Using npm badge for README.md#Install

v5.0.3

  • Fixing spelling bug onReqestValue -> onRequestValue, Opps sry ...

v5.0.4

  • Performance improvment

v5.0.5

  • Performance improvments and memory optimizations

Bug fixes

  • If strategy is not 'allow' and entered number is less than min but it is decimal number, input value will be overriden if it's not controlled else onRequestValue will be emitted and 'min' error will be emitted if strategy is 'warn'

  • If strategy is not 'allow' and min is greater than 0 and entered number is 0, input value will be overriden if it's not controlled else onRequestValue will be emitted and 'min' error will be emitted if strategy is 'warn'

v5.0.6

TypeScript Users Only

  • propTypes is now React.ValidationMap<NumberInputProps> instead of just Object making React.createElement callable

v5.0.7

Bug fixes

  • Fixing a bug which caused input value overriding to be skipped when strategy is 'ignore' instead of when it's 'allow'

v5.0.8

  • NPM dependencies update

v5.0.9

  • NPM dependencies update
  • Moving to TypeScript v2 and replacing typings with @types

v5.0.10

Bug fixes

  • Fixing when 'singleZero' is emitted. 'singleZero' is emited accordingly to Errors now. In all other cases when 'singleZero' was emitted now 'invalidSymbol' is emitted instead.

v5.0.11

Bug fixes

  • Fixing 'allow' to properly be masked as 'min' when error is emitted onBlur.
  • If there is error onBlur it will be emitted no matter dose the strategy is 'warn' or 'allow'.

v5.0.12

Bug fixes

  • Fixing, in addition of value the following props are checked for changes: min, max,required and strategy before take decision to call onError, onValid and onRequestValue accordingly when componentWillReceiveProps.

v5.0.13

Bug fixes

  • Fixing the bug that should have been fixed with v5.0.12 and the one introduced with v5.0.12 which caused events to be one state behind actual input.

v5.0.14

Bug fixes

  • Fixing a bug which caused onError to be called based on old props.
  • Fixing the bug that should be fixed with 5.0.12

v5.0.15

Bug fixes

  • Fixing Cannot resolve module 'material-ui-number-input' due to $ npm run npm command has not been ran before releasing previos version.

v5.0.16

Properties

  • Displayed by the browser keyboard is now configurable (adding support for the input inputMode prop)

v5.0.17

Bug fixes

  • Allowing negative min values, closes #7

v5.0.18

  • Syncing npm and github versions.

v5.0.19

Bug fixes

  • Fixing empty npm package, closes #9

v5.0.20

  • Dummy version

v5.0.21

  • Shipping #13 (Fixes #12)

v5.0.22

Bug fixes

  • Fixed:
Uncaught TypeError: value.match is not a function
    at Function.NumberInput.validateValue (NumberInput.js:103)

(closed #14)

v5.0.23

  • Updating package dependencies

  • Switching from React.PropTypes to prop-types package