Giter Club home page Giter Club logo

react-number-input's Introduction

Build Status

React Number Input component

HTML input element clone with support for post-edit formatting of number values. An input of 1000000 will format to 1,000,000 under en-AU locale.

Usage

yarn add react-number-input

By default it points to dist/index.js which is a pre-built using babel and compatible with CommonJS or ES6.

Demo (Storybook)

git clone [email protected]:hongymagic/react-number-input.git
cd react-number-input
yarn
yarn storybook

Screencast demo

in flight

Screencast demo has the following configuration:

* Optionally format to 2 decimal places
* No minimum
* No maximum

Install

npm install --save react-number-input

or

yarn add react-number-input --save

Use

Use it like a normal react form element.

<NumberInput
	id="price"
	type="tel"                      // optional, input[type]. Defaults to "tel" to allow non numeric characters
	onChange={this.onPriceChange}   // function (value: number | null, event: Event)
	value={this.state.price}        // normal react input binding
	placeholder="Enter price"       // all other input properties are supported
	min={0}                         // optional, set minimum allowed value
	max={100}                       // optional, set maximum allowed value
	format="0,0[.]00"               // optional, numbro.js format string. Defaults to "0,0[.][00]"
/>

All properties supplied to NumberInput will also be transferred to the resulting input element (e.g., style or className).

Supported event handlers

All event handlers supported by <input /> are supported. Except onChange received the current number as its first argument and the Event object as second argument.

Contributing / Developing / Running demo

If you're thinking of contributing, there are a number of npm scripts you can run to speed up the process.

Building react-number-input

yarn
yarn build

Testing react-number-input

yarn flow
yarn test

Building and running the storybook

yarn build
yarn storybook

Then head to http://localhost:6006/ to see the demo.

Note

  • Renders input[type=tel] element, can be overriden by providing type property
  • Number formatting is removed on focus
  • Number formatting is applied on blur
  • If input contains all zeroes, zeroes are left in place until blur

TODO

  • Add support for different locales

react-number-input's People

Contributors

blackpost38 avatar chee avatar cmschuetz avatar dwendo avatar freekmurze avatar hongymagic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-number-input's Issues

Focus formatting change interferes with cursor insertion or selection

Steps to reproduce:

  1. Visit demo page.
  2. Interact with Standard integer with formatting field as follows:
    1. Unfocus the field.
    2. Observe the value is 1,500,000.
    3. Click between the comma and the hundreds place.
    4. Observe that when the input field switches to edit mode, the cursor blinks between the hundreds and tens place.

On same page and Standard integer with formatting field:

  1. Unfocus the field.
  2. Observe the value is 1,500,000.
  3. Attempt to select/highlight 500 by click-dragging the mouse.
  4. Observe that the selection highlight in the focused field (edit mode) starts after the 5.

Possible Solution

Not sure how to fix this. Maybe this way?

When user clicks on formatted field,
determine location of cursor in formatted value,
map location of cursor to unformatted value.  something like...

    1 , 5 0 0 , 0 0 0       formatted value
   │ └─┤ │ │ ├─┘ │ │ │
   └─┐ │ │ │ │ ┌─┘ │ │
     │ │ │ │ │ │ ┌─┘ │
     │ │ │ │ │ │ │ ┌─┘
      1 5 0 0 0 0 0         unformatted value (edit mode)

each side of a comma would map to the same edit-mode cursor location.

And something similar with the selection issue, though I imagine if the cursor starts at the correct position in edit mode kicks in, then dragging the selection highlight would Just Work™ without further effort.

Request: Format while editing

The experience of having the text switch between unformatted and formatted as you focus and blur isn't expected by most users.

React component methods may only be bound to the component instance.

Getting the following error:

bind(): React component methods may only be bound to the component instance. See exports

This is how I'm using the component:

<NumberInput
    className = { styles.NumberInput }
    max = { 999 }
    min = { 1 }
    onChange = { this.onAppCountChange }
    type = 'number'
    value = { this.state.subscriptions.renewalCount }
/>

… and the onChange function:

onAppCountChange(event) {
    var value = event.target.value
    var currentAppCount = this.state.subscriptions.appAccountTitles.length
    SubscriptionActions.updateAppRenewalCount(value)
    this.setState({
        errorMessage: value < currentAppCount ? 'You currently have ' + currentAppCount + ' active ' + _('app').pluralize(currentAppCount) + '. Tktk remove some' : null
    })
}

Any idea where this error is coming from?

autoFocus property doesn't work

NumberInput seems to disregard "autoFocus" prop. I was previously using refs to manage focus, but ran into issues when I started using react-dnd (drag and drop). Also, using refs is frowned upon in React.

Any chance that you can add autoFocus support?

react@16

Any reason peer dependency doesn't allow react@16?

Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs

Hello,

Thanks for doing this -- when it works, it's fantastic, and a major time-saver.

However, I'm getting this problem when I've installed this in a React 15 application.

Looking here it appears that this happens when you get more than one instance of React installed in your application. Someone in that thread found out that some code they npm installd had its own copy of React.

I checked, and sure enough, I found node_modules/react-number-input/node_modules/react. I removed that directory and the problem went away.

Thanks!

Allow `format` to be a function

It would be convenient if the format prop could be a function instead of a string, so that using the numbro formatting strings wouldn't be mandatory. The format() function could be called instead of numbro.format() inside of the internal toFormattedString() method when it's a function, otherwise it could just function the same way that it does now.

Fix dependencies for react

Need to fix dependencies for react :

  • Set react and react-dom as devDependencies instead of dependencies
  • Set react and react-dom as peerDependencies
  • Add react v16 compatibility

Which one is correct Default format?

In read.me, Default format is 0,0[.][00]

<NumberInput
	id="price"
	type="tel"                      // optional, input[type]. Defaults to "tel" to allow non numeric characters
	onChange={this.onPriceChange}   // function (value: number | null, event: Event)
	value={this.state.price}        // normal react input binding
	placeholder="Enter price"       // all other input properties are supported
	min={0}                         // optional, set minimum allowed value
	max={100}                       // optional, set maximum allowed value
	format="0,0[.]00"               // optional, numbro.js format string. Defaults to "0,0[.][00]"
/>

But in source, Default format is 0,0.

refs: https://github.com/hongymagic/react-number-input/blob/master/index.js#L15

Which one is default format?

If 0,0[.][00] is correct default format, I'll fix default format and send P.R.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.