3.8.0 • Published 1 month ago

react-currency-input-field v3.8.0

Weekly downloads
8,051
License
MIT
Repository
github
Last release
1 month ago

React Currency Input Field Component

npm npm NPM Codecov Coverage Release build

Features

  • Allows abbreviations eg. 1k = 1,000 2.5m = 2,500,000
  • Prefix and Suffix options eg. £ or \$
  • Automatically inserts group separator
  • Accepts Intl locale config
  • Can use arrow down/up to step
  • Can allow/disallow decimals
  • Written in TypeScript and has type support
  • Does not use any third party packages

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="Please enter a number"
  defaultValue={1000}
  decimalsLimit={2}
  onValueChange={(value, name, values) => console.log(value, name, values)}
/>;

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

Props

NameTypeDefaultDescription
allowDecimalsbooleantrueAllow decimals
allowNegativeValuebooleantrueAllow user to enter negative value
defaultValuenumberDefault value
valuenumberProgrammatically set the value
onValueChangefunctionHandle change in value
placeholderstringPlaceholder if no value
decimalsLimitnumber2Limit length of decimals allowed
decimalScalenumberSpecify decimal scale for padding/trimming eg. 1.5 -> 1.50 or 1.234 -> 1.23 if decimal scale 2
fixedDecimalLengthnumberValue will always have the specified length of decimals
prefixstringInclude a prefix eg. £ or \$
suffixstringInclude a suffix eg. € or %
decimalSeparatorstringlocale defaultSeparator between integer part and fractional part of value
groupSeparatorstringlocale defaultSeparator between thousand, million and billion
intlConfigobjectInternational locale config
disabledbooleanfalseDisabled
disableAbbreviationsbooleanfalseDisable abbreviations eg. 1k -> 1,000, 2m -> 2,000,000
disableGroupSeparatorsbooleanfalseDisable auto adding the group separator between values, eg. 1000 -> 1,000
maxLengthnumberMaximum characters the user can enter
stepnumberIncremental value change on arrow down and arrow up key press
transformRawValuefunctionTransform the raw value from the input before parsing. Needs to return string.

onValueChange

Handle changes to the value.

onValueChange = (value, name, values) => void;

value

value will give you the value in a string format, without the prefix/suffix/separators.

Example: £123,456 -> 123456

name

name is the name you have passed to your component

values

values gives an object with three key values:

  • float: Value as float or null if empty. Example: "1.99" > 1.99
  • formatted: Value after applying formatting. Example: "1000000" > "1,000,0000"
  • value: Non formatted value as string, ie. same as first param.

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

This can be turned off by passing in disableAbbreviations.

Prefix and Suffix

You can add a prefix or suffix by passing in prefix or suffix.

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

<CurrencyInput prefix="£" value={123} />;
// £123

<CurrencyInput suffix="%" value={456} />;
// 456%

Note: Passing in prefix/suffix will override the intl locale config.

Separators

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

Example:

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

<CurrencyInput 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 disableGroupSeparators={true}.

Intl Locale Config

This component can also accept international locale config to format the currency to locale setting.

Examples:

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

<CurrencyInput intlConfig={{ locale: 'en-US', currency: 'GBP' }} />;

<CurrencyInput intlConfig={{ locale: 'ja-JP', currency: 'JPY' }} />;

<CurrencyInput intlConfig={{ locale: 'en-IN', currency: 'INR' }} />;

locale should be a BCP 47 language tag, such as "en-US" or "en-IN".

currency should be a ISO 4217 currency code, such as "USD" for the US dollar, "EUR" for the euro, or "CNY" for the Chinese RMB.

Any prefix, suffix, group separator and decimal separator options passed in will override the default locale settings.

Decimal Scale and Decimals Limit

decimalsLimit and decimalScale sound similar but have different usages.

decimalsLimit prevents the user from typing more than the limit, and decimalScale will format the decimals onBlur to the specified length, padding or trimming as necessary.

Example:

If decimalScale is 2

- 1.5 becomes 1.50 (padded)
- 1.234 becomes 1.23 (trimmed)

---

If decimalLimit is 2

- User enters 1.23
- User is then prevented from entering another value

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

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';

// Format using prefix, groupSeparator and decimalSeparator
const formattedValue1 = formatValue({
  value: '123456',
  groupSeparator: ',',
  decimalSeparator: '.',
  prefix: '$',
});

console.log(formattedValue1);
// $123,456

// Format using intl locale config
const formattedValue2 = formatValue({
  value: '500000',
  intlConfig: { locale: 'en-IN', currency: 'INR' },
});

console.log(formattedValue2);
// ₹5,00,000

v3.0.0 Release Notes

Breaking Changes

  • :warning: onChange renamed to onValueChange :warning:
  • onBlurValue has been removed.
  • turnOffAbbreviations renamed to disableAbbreviations.
  • turnOffSeparators renamed to disableGroupSeparators.
  • precision renamed to decimalScale

Improvements in v3

  • Intl locale config can be passed in. Please note: formatting where the currency symbol is placed after the value like a suffix eg. (1 234,56 €) might cause problems, this is still in development.
  • Group separator will default to browser locale if not specified.
  • Can pass ref to the component.
  • onChange and onBlur functions can be passed in and will be called with original event.

Reasoning

As this component grew in usage, I started getting more bug reports and feature requests. That wasn't a problem though, because I was always happy to fix any bugs and implement any features if I could.

However, this meant sometimes I was a bit trigger happy, and didn't always think about how the different options interacted with each other. I found that it was getting a bit convoluted for my liking, and choices I had made earlier in development, now seemed like it could be improved.

Therefore, I took the opportunity of v3 to do a bit of tidying up for the component, in order to make it more future proof and intuitive to use.

I apologize if any of the changes cause new bugs or issues. Please let me know and I will fix asap.

Issues

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

Contributing

If you want to contribute to this repository, please refer to the contributing doc.

@uponco/admin-uijkc-input@infinitebrahmanuniverse/nolb-react-curharuow-adminjovapos-pos@everything-registry/sub-chunk-2548smartjkcjkc-inputsmedusa-admin-ui-fa@aveonline/ui-react@availity/formassistpoint-de-controls@cryptofi/core-ui@devforth/adminjs-design-systembrokoli-ui@bonlineza/luxity-lib@bazyli/admin-ui@buyfood/components@givewp/form-builder-librarynwb-component-testorganism-next-uipoletto-bookstoreportfolio-simulatorportfolio-mgt-simulatorreact-gross-elementsuc-custom-medusajs-admin-uiuc-custon-medusajs-admin-uiupscale-formsupscale-forms-uiv4components-reacttj-componentsslowbro-uistorefront-icu_admin-uitest-admin-uireact-ui-controller@commercelayer/app-elementsctp-helpdesk-dashboardctp-helpdesk-editctp-helpdesk-entryctp-helpdesk-viewcustom-admin-ui@dr.cash/componentsdd360-ds01cm-new-layout@applifyer/admin-ui@adminjs/design-system@ajeebsa/admin-uidede-cbs-table-crud@flowind/ui@lambdacurry/gatsby-theme-medusa-admin@kukui/ui@medusajs/ui@medusajs/admin-ui@meksiabdou/react-input@minternaljs/admin-ui@minternaljs/ui@lrocher/medusa-admin-ui@novapo/medusajs-admin-ui@massalabs/react-ui-kit@nelomobile/medusa-admin-ui@netlambda/admin-ui@lnminh0710/price-tag@payglo/admin-ui@optidist/admin-ui@prosperitainova/mirage-ui@qlue-ui/ui-kit@qlue/ui-kit@raccord/ui-kit@shakeem/hakeem-input@sgftech/admin-ui@sellerartifact-medusajs/admin-ui@theazizmoh/admin-ui@theazizmoh/admin-ui-janatainfinity-forge@salomeg5_/components@rubensdeoliveira-ui/react@smartjkc/input@srclaunch/uiloon-bulma-react@truehome/landing-ui@truehome-tech/ui-landing@softsmart/input@softsmart/jkc-input@storozhok/adminjs-design-system@viniengelage/materialui-inputmaroc4products-admin-ui@unexex/uimedusajs-admin-uimedusa-adminmy-smart-payroll-calculatorewz-ui-test@zvonimirko/react-components-templatenext-crash-components-finaladmin-ui-longvb@zalastax/nolb-react-cuflexibull-memeflexibull2
3.8.0

1 month ago

3.7.1

1 month ago

3.7.0

2 months ago

3.6.14

3 months ago

3.6.13

3 months ago

3.6.12

5 months ago

3.6.11

9 months ago

3.6.10

1 year ago

3.6.6

1 year ago

3.6.9

1 year ago

3.6.8

1 year ago

3.6.7

1 year ago

3.6.5

2 years ago

3.6.2

2 years ago

3.6.1

2 years ago

3.6.4

2 years ago

3.6.3

2 years ago

3.6.0

2 years ago

3.5.1

3 years ago

3.5.0

3 years ago

3.4.3

3 years ago

3.4.2

3 years ago

3.4.0

3 years ago

3.4.1

3 years ago

3.3.2

3 years ago

3.3.1

3 years ago

3.3.0

3 years ago

3.2.5

3 years ago

3.2.4

3 years ago

3.2.3

3 years ago

3.2.2

3 years ago

3.2.1

3 years ago

3.2.0

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

3.0.0-beta.11

3 years ago

3.0.0-beta.10

3 years ago

3.0.0-beta.9

3 years ago

3.0.0-beta.8

3 years ago

3.0.0-beta.7

3 years ago

3.0.0-beta.6

3 years ago

2.7.1

3 years ago

3.0.0-beta.5

3 years ago

3.0.0-beta.4

3 years ago

3.0.0-beta.3

3 years ago

3.0.0-beta.2

3 years ago

3.0.0-beta.1

3 years ago

2.8.0-beta.1

3 years ago

2.7.0

3 years ago

2.6.0

3 years ago

2.5.0

3 years ago

2.4.1

3 years ago

2.4.0

3 years ago

2.3.6

3 years ago

2.3.5

3 years ago

2.3.4

3 years ago

2.3.3

3 years ago

2.3.2

4 years ago

2.3.1

4 years ago

2.3.0

4 years ago

2.2.0-beta.3

4 years ago

2.2.0

4 years ago

2.2.0-beta.2

4 years ago

2.2.0-beta.1

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.0.1

4 years ago

0.10.1

4 years ago

0.10.0

4 years ago

0.9.0

4 years ago

0.8.4

4 years ago

0.8.3

4 years ago

0.8.2

4 years ago

0.8.1

4 years ago

0.8.0

4 years ago

0.7.0

4 years ago

0.6.0

4 years ago

0.5.3

4 years ago

0.5.2

4 years ago

0.5.0

4 years ago

0.5.1

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.47

5 years ago

0.0.46

5 years ago

0.0.45

5 years ago

0.0.44

5 years ago

0.0.43

5 years ago

0.0.42

5 years ago

0.0.41

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

6 years ago

1.0.0

6 years ago