npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-dynamic-number

v1.7.1

Published

Highly customizable react component for numbers

Downloads

28,498

Readme

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