Socket
Socket
Sign inDemoInstall

react-hook-currency

Package Overview
Dependencies
3
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-hook-currency

> This libraries propose to introduce two pseudo hooks, one capable of format currencies and another capable to simulate a cash register.


Version published
Weekly downloads
164
increased by31.2%
Maintainers
1
Install size
135 kB
Created
Weekly downloads
 

Readme

Source

React Currency Hooks

This libraries propose to introduce two pseudo hooks, one capable of format currencies and another capable to simulate a cash register.

Live Demo

On Code Sandbox

Install

  npm install react-hook-currency  # or yarn add react-hook-currency

Browser compatibility (Tested)

ChromeEdgeFirefoxSafariIE 11
:heavy_check_mark::heavy_check_mark::heavy_check_mark::heavy_check_mark::heavy_check_mark:

How to use

demo currency-hook

Currency formatter

Controlled Input
import React from 'react';
import { useCurrency } from 'react-hook-currency';

const ControlledInputCurrency = () => {
  const { onClick, onChange, onKeyDown, format, toNumber } = React.useMemo(
    () => useCurrency(),
    []
  );
  const [value, setValue] = React.useState(format('000'));

  return (
    <input
      type="text"
      onChange={e => {
        onChange(e);
        console.log(toNumber(e.target.value)); // Number
        setValue(e.target.value);
      }}
      onKeyDown={onKeyDown}
      onClick={onClick}
      value={value}
    />
  );
};
Uncontrolled Input
const UncontrolledInputCurrency = () => {
  const { onClick, onChange, onKeyDown, format } = React.useMemo(
    () => useCurrency(),
    []
  );
  const ref = React.useRef<HTMLInputElement>(null);

  return (
    <input
      type="text"
      onChange={onChange}
      onKeyDown={onKeyDown}
      onClick={onClick}
      defaultValue={format('000')}
      ref={ref}
    />
  );
};

Cash Register

Uncontrolled Input
import React from 'react';
import { useCurrencyRegister } from 'react-hook-currency';

const UncontrolledInputCashRegister = () => {
  const { onClick, onChange, onKeyDown, format } = useCurrencyRegister();
  const ref = React.useRef<HTMLInputElement>(null);

  return (
    <input
      type="text"
      onClick={onClick}
      onChange={onChange}
      onKeyDown={onKeyDown}
      defaultValue={format('000')}
      autoFocus={true}
      ref={ref}
    />
  );
};

Options

Options hooks

PropsDefault ValueDescription
precision2Fraction Digits
stylecurrencycurrency or decimal(Remove symbol)
localept-BRCountry Code Reference(Currency symbol)
currencyBRLString i18n(Format Type)
negative *allowallow, always, never

Note: *not present in useCurrencyRegister

Return hooks

ReturnType
onClickFunction => void
onChangeFunction => void
onKeyDownFunction => void
formatFunction => void
decimalSeparatorstring
toNumberFunction => number
triggerChange *Function => void

Note: *not present in useCurrency

Contributing

Fell free to contribute. Create a branch, add commits, and open a pull request.

Keywords

FAQs

Last updated on 05 Jun 2021

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