GithubHelp home page GithubHelp logo

react-dynamic-number's Introduction

REACT Dynamic Number

Build Status Downloads Downloads NPM version

Highly customizable React form element component for numbers (prices). It validates inputs in realtime (if user press not acceptable character it wont appear in input field). This component may be configured for each input (you can set number of digits in integer part and number of digits in decimal part, you can set decimal separator, accept only positive or negative values).

The Big advantage of this component, is separation of view value and model value (value which is available to other javascript code). You can set comma as decimal separator (default is dot) for numbers. And then in input field there will be comma as separator, but your model value will have correct float number with dot separator.

It works at realtime, therefore this model value may be use in computation for other elements and they will change real time too.

This is port of Angular Dynamic Number

Demo:

link

Features:

  • config max numbers for integer part and decimal part.
  • config decimal separator (dot or comma)
  • config to accept positive, negative and both numbers.
  • model value is correct javascript number, but view value may be correct number for localities
  • allow to set thousand separator (dot, comma or space)

Installation:

npm

npm install react-dynamic-number

It needs react to work correctly. It is compiled without react at its source.

Quick start: How to use it

import React from 'react';
import ReactDOM from 'react-dom';
import DynamicNumber from 'react-dynamic-number';

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

  onChange(evt, modelValue, viewValue) {
    //modelValue has proper js value which you can process
    //viewValue it is string which is visible to user in form input
    console.log(modelValue);
    console.log(viewValue);
  };

  render() {
    return (
      <DynamicNumber className="form-control" onChange={this.onChange.bind(this)} separator={','} integer={5} fraction={5} />
    );
  }
}

//this render this component, but in normal situation you render only root component
ReactDOM.render(
  <SomeParentComponent />,
  document.getElementById('app')
);

Options:

<DynamicNumber
  value={0}
  separator={'.'}
  integer={10}
  fraction={10}
  positive={true}
  negative={true}
  thousand={' '}
  onChange={this.handleChange}
/>

value

  • type: Number or '' (empty string)
  • required: false
  • default: 0

Init value of input. If init value is '' then input is empty.

When you set this prop, then component became a Controlled Component

separator

  • type: String
  • required: false
  • default: '.'
  • options:
  • '.' - dot is decimal separator
  • ',' - comma is decimal separator

Define number decimal separator

integer

  • type: Number
  • required: false
  • default: 10

Set maximum numbers of digits integer part (digits before decimal separator)

fraction

  • type: Number
  • required: false
  • default: 10

Set maximum numbers of digits fraction part (digits after decimal separator)

positive

  • type: Boolean
  • required: false
  • default: true
  • options:
  • true - number can be positive
  • false - number may not be positive

Define if number may be positive

negative

  • type: Boolean
  • required: false
  • default: true
  • options:
  • true - number can be negative
  • false - number may not be negative

Define if number may be negative

thousand

  • type: boolean or ' ' (space)
  • required: false
  • default: false
  • options:
  • false - thousand separator is disabled
  • ' ' (space) - thousand separator is enabled and separate values by space
  • true - thousand separator is enabled.
  • If decimal separator is dot then thousand separator is comma.
  • If decimal separator is comma then thousand separator is dot.

Define number decimal separator

placeholder

  • type: string
  • required: false
  • default: none

Allow to set placeholder to empty input

onChange

  • type: Function (callback)
  • required: false
  • function attributes:
  • evt - react event
  • modelValue - correct javascript number
  • viewValue - string value visible in input

Define callback which will be trigger on any number change

FAQ

Is it works with ES5?

Yes it works but remember to use it :

var DynamicNumber = require('react-dynamic-number').default;

How to set focus on this element

First of all you need to get ref to this component. How to get ref:

  <DynamicNumber ref={input => { this.input = input }} ... 

Ref should be use in componentDidMount or componentDidUpdate because there we are sure that everything is ready.

Now you can call focus method on this ref.

  componentDidMount() {
    this.input.focus();
  }

Example from documentation Adding a Ref to a Class Component

Warning: As you can read here Refs and Functional Components

You may not use the ref attribute on functional components because they don't have instances:

License

MIT

react-dynamic-number's People

Contributors

gdotdesign avatar haydnhkim avatar msander avatar uhlryk 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

Watchers

 avatar  avatar  avatar

react-dynamic-number's Issues

Support for unicode numerals

Currently it seems that this module has support for english numerals only.
Can it be further enhanced to allow input of unicode numerals as well?

check what if we pass float value and in props we set thousand with dot

below statement need to be checked.
this.calculator.calculate is designed to test raw string values if they have certain format and to change them to js numbers. For example If we set in props decimal separator comma, thousand separator dot. And we pass normal javascript float 1234.1234 to calculate it will treat this value as raw string and remove dot (because dot in this configuration means thousand separator).
If Im right then we need new method similar to calculate which transform correct js numbers to certain string view values.

Add id value

Hey, can you please add an id to the input field? Even if I add it to the DynamicNumber element, when I inspect the code, those values are not there.
For example:

<DynamicNumber
  thousand={','}
  integer={7}
  type='number'
  min={0}
  id='someIdHere'
  ref='someRefHere'
  onChange={this.valueChange}
  value={this.state.something} />

Generated input:
<input type='text' class='some-css-class' value='something'>

Thanks!

ES5 support

I get multiple errors when using the module as follows:
var DynamicNumer = require('react-dynamic-number')

I guess the reason is that module doesn't work with es5, commonjs?

Cheers!

Unable to focus using ref

I'm trying to focus on DynamicNumber, but it throws error as cannot read focus of null. I'm targeting from onChange of another component. Simple example of what I have done:

xyz = () => {
this.abc.focus();
}

<DynamicNumber
ref={(input) => { this.abc = input; }}
/>

React 16 Warning

Starting from react 16, I see that assigning values to "this.state" in componentWillReceiveProps, will raise a warning which suggests to use ".setState".

Can you please update this in the next version? Thanks in advance

Add focus support

There needs to be a focus method on the component so parent elements can focus on the input.

Duplicate module name: ViewportMetrics

Failed to build DependencyGraph: @providesModule naming collision:
  Duplicate module name: ViewportMetrics
  Paths: /node_modules/react-dynamic-number/node_modules/react/lib/ViewportMetrics.js collides with /node_modules/react/lib/ViewportMetrics.js

This error is caused by a @providesModule declaration with the same name accross two different files.
Error: @providesModule naming collision:
  Duplicate module name: ViewportMetrics
  Paths: /node_modules/react-dynamic-number/node_modules/react/lib/ViewportMetrics.js collides with /node_modules/react/lib/ViewportMetrics.js

This error is caused by a @providesModule declaration with the same name accross two different files.
    at HasteMap._updateHasteMap (/node_modules/react-native/node_modules/node-haste/lib/DependencyGraph/HasteMap.js:162:15)
    at /node_modules/react-native/node_modules/node-haste/lib/DependencyGraph/HasteMap.js:127:25

add tests

We can use mocha and test only logic

Refs Problem

Using refs seemed to work fine in our DEV environment set in the react-router. Though when put into production mode it no longer recognized this element component as a valid react component and threw an error due to the ref being added.

The error read:
Minified React error #120; visit http://facebook.gitbhub.io/react/docs/error-decoder.html?invariant=120 for the full message or use the non-minified dev environment for full errors......

PlaceHolder Values

The placeholder value seems to be set at the value 0 no matter what is placed in this element. Is there some way to make this value empty so the initial value is not 0.

move react to peerDependencies

Can you please move react to devDependencies or peerDependencies.
Otherwise react is included twice in any application that uses your component and version of React different than yours.
Another possible option is to make react dependency more loose. Like '>= 0.14.5' instead of '^0.14.5'

Fraction is error

Let's say I have 2 input fields. Both of them have fraction={1}. When I input in first field 2.35, in visual it is 2.3 and that's what I want. But when I move to second field, then I start to input some values, the first field's value become to 0 again. Then I reload page. Now, I input in first field 2.3, then I move to second field and I start to input some values, the first field is fine with value 2.3. I think there's something missed with fraction props. Is it possible to fix this?

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.