Socket
Socket
Sign inDemoInstall

react-amount

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-amount

React amount component


Version published
Weekly downloads
12
decreased by-66.67%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

React component formatting numbers in an input field

GitHub open issues MIT Coverage Status

Live demo

Live demo on CodeSandbox

Installation

yarn add react-amount

or

npm install react-amount

Usage

import React, { useState } from 'react';
import { Amount } from 'react-amount';

import '~/react-amount/dist/style/index.min.css';

interface MyComponentProps {
  value: string | number | undefined;
}

const MyComponent = (props: MyComponentProps): React.Element => {
  const { value } = props;

  const [currentValue, setCurrentValue] = useState<string | number | undefined>(
    value,
  );

  return (
    <Amount
      value={currentValue}
      suffix="€"
      onChange={(newValue) => setCurrentValue(newValue.raw)}
      decimalSeparator=","
      thousandSeparator=" "
    />
  );
};

export default MyComponent;

Options

OptionTypeDefault valueDescription
valuestring | numberundefinedInitial value of the control
readOnlybooleanfalseInput value is not editable
disabledbooleanfalseInput value is disabled
textOnlybooleanfalseInput value is displayed as formatted text only value
namestringName of the input field
classNamestringundefinedClass to be added to the wrapper of the component
onChange(update: FormattedValues) => voidundefinedCallback function to handle value changes
decimalsnumber2Number of decimals
decimalSeparatorstring"."Decimal separator
thousandSeparatorstring","Thousand separator
thousandGroupingThousandGroupingStyle: "thousand" | "wan" | "lakh""thousand"Thousand grouping style
displayOnInvalidstring"-"Value displayed on invalid input in textOnly
dataTestIdstringundefinedId value for testing
requiredbooleanfalseRequired of the input field
prefixstringundefinedPrefix
suffixstringundefinedSuffix

Contributing

We very much welcome contributions.

  • Submit GitHub issues to report bugs or ask questions.
  • Propose Pull Request to improve our code.

Types

FormattedValues

export interface FormattedValues {
  formatted: string;
  float: number;
  raw: string;
}

This structure is used in the onChange handler to provide flexibility on the format.

  • formatted is for display purpose.
  • float is for numerical representation, but suffer from precision limitations.
  • raw is for numerical representation as a string with full precision.

ThousandGroupingStyle

export enum ThousandGroupingStyle {
  THOUSAND = 'thousand',
  WAN = 'wan',
  LAKH = 'lakh',
}
  • THOUSAND: groups of 3 digits example: 123,456,789
  • WAN: 1 group of 4 digits, then groups of 3 digits example: 12,345,6789
  • LAKH: 1 group of 3 digits, then groups of 2 digits example: 12,34,56,789

Keywords

FAQs

Last updated on 19 Apr 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc