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

big-number-input

v1.0.3

Published

A BigNumberInput to use with decentralized applications and React

Downloads

577

Readme

big-number-input

A BigNumberInput to use with decentralized applications and React.

Build Status NPM version MIT licensed dependencies Status devDependencies Status

The problem

You are using big integer numbers that can be expressed as smaller fractional numbers and you want users to be able to enter them in this format. The originating use case for this library are ERC20 tokens, but a simpler example is having some amount expressed in millions. You want the user to input 1.5 but to receive a bignumber containing 1500000. In this case, you can use big-number-input with a decimals value of 6. See Usage below for more information.

Installation

npm install big-number-input

Usage

Import the component and pass the required props:

import React from 'react'
import { BigNumberInput } from 'big-number-input'

function App() {
  const [value, setValue] = React.useState('1000000')

  return (
    <div>
      <BigNumberInput
        decimals={6}
        onChange={setValue}
        value={value}
      />
      <div>{value || 'empty'}</div>
    </div>
  )
}

export default App

In this case the input value is 1000000 and the rendered value will be 1. If the user changes it to 2 in the UI the onChange will be called with 2000000.

The required props are:

  • decimals: The number of decimals you want to use
  • value: A string with the raw value that will be used by the input, eg. 1000000.
  • onChange: A function that receives the new raw value, or an empty string when the input is empty

Then you have a bunch of optional props you can use:

  • autofocus: Set to true if you want the input to obtain focus as soon as it's rendered
  • placeholder: A string to use as the input's placeholder
  • disabled: A boolean used to disable the input
  • min, max and step: The min and max values you want to use and the step used when you increase or decrease the number with the arrow keys or by clicking the arrows in the input. These values are also strings with the raw values and are interpreted with the same number of decimals as the main value.

Using a different input component

If you want to use a different input, for example the Input from Material UI, you can use the renderInput prop:

import React from 'react';
import { BigNumberInput } from 'big-number-input'
import { Input } from '@material-ui/core'


function App() {
  const [value, setValue] = React.useState('')

  return (
    <div>
      <BigNumberInput decimals={6} onChange={setValue} value={value} renderInput={
        props => <Input {...props} />
      }/>
      <div>{value || 'empty'}</div>
    </div>
  );
}

Converting to and from a big number

If you want to use some big number library to store your value, you can follow an approach like this:

import React from "react";
import Big from 'big.js'
import { BigNumberInput } from "big-number-input";

function App() {
  const [value, setValue] = React.useState(null);
  return (
    <div>
      <BigNumberInput
        autofocus={true}
        decimals={6}
        onChange={newValue => newValue ? setValue(new Big(newValue)) : setValue(null)}
        value={value ? value.toString() : ''}
      />
      <div>{value ? value.toString() : "empty"}</div>
    </div>
  );
}

Here we use null to indicate an empty input, and a big number instance when there is something entered in the input.