Git Product home page Git Product logo

react-currency-input-field's Introduction

React Currency Input Field Component

npm NPM Codecov Coverage Release build

Features

  • Allows abbreviations eg. 1k = 1,000 2.5m = 2,500,000
  • Prefix option eg. £ or $
  • Can allow/disallow decimals
  • Automatically inserts comma separator
  • Only allows valid numbers
  • Lightweight and simple

Examples

Play with demo or view examples code

React Currency Input Demo

Install

npm install react-currency-input-field

or

yarn add react-currency-input-field

Usage

import CurrencyInput from 'react-currency-input-field';

<CurrencyInput
  id="input-example"
  name="input-name"
  placeholder="£1,000"
  defaultValue={1000}
  allowDecimals={true}
  decimalsLimit={2}
  onChange={(value, name) => console.log(value, name)}
/>;

Have a look in src/examples for more examples on implementing and validation.

Abbreviations

It can parse values with abbreviations k, m and b

Examples:

  • 1k = 1,000
  • 2.5m = 2,500,000
  • 3.456B = 3,456,000,000

Separators

You can change the decimal and group separators by passing in decimalSeparator and groupSeparator.

Example:

import CurrencyInput from 'react-currency-input-field';

<CurrencyInput
  id="input-example"
  defaultValue="123.456,89"
  decimalSeparator=","
  groupSeparator="."
/>;

Note: the separators cannot be a number, and decimalSeparator must be different to groupSeparator.

To turn off auto adding the group separator, add turnOffSeparators={true}.

Fixed Decimal Length

Use fixedDecimalLength so that the value will always have the specified length of decimals. This formatting happens onBlur.

Example if fixedDecimalLength was 2:

- 1 -> 1.00
- 123 -> 1.23
- 12.3 -> 12.30
- 12.34 -> 12.34

Props

Name Type Default Description
allowDecimals boolean true Allow decimals
allowNegativeValue boolean true Allow user to enter negative value
className string Class names
decimalsLimit number 2 Limit length of decimals allowed
defaultValue number Default value
value number Programmatically set the value
disabled boolean false Disabled
fixedDecimalLength number Value will always have the specified length of decimals
id string Component id
maxLength number Maximum characters the user can enter
onChange function Handle change in value
onBlurValue function Handle value onBlur
placeholder string Placeholder if no value
precision number Specify decimal precision for padding/trimming
prefix string Include a prefix eg. £ or $
step number Incremental value change on arrow down and arrow up key press
decimalSeparator string . Separator between integer part and fractional part of value
groupSeparator string , Separator between thousand, million and billion
turnOffAbbreviations boolean false Disable abbreviations eg. 1k > 1,000, 2m > 2,000,000
turnOffSeparators boolean false Disable auto adding the group separator between values, eg. 1000 > 1,000

Format values for display

Use the formatValue function to format the values to a more user friendly string. This is useful if you are displaying the value somewhere else ie. the total of multiple inputs.

import { formatValue } from 'react-currency-input-field';

const formattedValue = formatValue({
  value = 123456,
  groupSeparator = ',',
  decimalSeparator = '.',
  turnOffSeparators = false,
  prefix = '$',
});

Issues

Feel free to raise an issue on Github if you find a bug or have a feature request

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.