Giter VIP home page Giter VIP logo

react-currency-input's Introduction

react-currency-input

An ES2015 react component for currency. Supports custom decimal and thousand separators as well as precision.

Build Status

Changes

v1.3.0:

  • Deprecated "onChange" option in favor of "onChangeEvent". This fixes the argument order to better match React's default input handling
  • Updated dependencies to React 15
  • Added parseFloat polyfill
  • Persist events to deal with an issue of event pooling
  • Other bug fixes.

Installation

npm install react-currency-input --save

Integration

You can store the value passed in to the change handler in your state.

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

const MyApp = React.createClass({
    getInitialState(){
        return ({amount: "0.00"});
    },

    handleChange(event, maskedvalue, floatvalue){
        this.setState({amount: maskedvalue});
    },
    render() {
        return (
            <div>
                <CurrencyInput value={this.state.amount} onChangeEvent={this.handleChange}/>
            </div>
        );
    }
});
export default MyApp

You can also assign a reference then access the value using a call to getMaskedValue().

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

const MyApp = React.createClass({
    handleSubmit(event){
        event.preventDefault();
        console.log(this.refs.myinput.getMaskedValue())
    },
    render() {
        return (
            <form onSubmit={this.handleSubmit}>
                <CurrencyInput ref="myinput" />
            </form>
        );
    }
});
export default MyApp

Separators and Precision

Specify custom decimal and thousand separators:

    // 1.234.567,89
    <CurrencyInput decimalSeparator="," thousandSeparator="." />

Specify a specific precision:

    // 123,456.789
    <CurrencyInput precision="3" />
    // 123,456,789
    <CurrencyInput precision="0" />

Currency

Optionally set a currency symbol as a prefix or suffix

    // $1,234,567.89
    <CurrencyInput prefix="$" />
    // 1,234,567.89 kr
    <CurrencyInput suffix=" kr" />

Negative signs come before the prefix

    // -$20.00
    <CurrencyInput prefix="$" value="-20.00" />

All other attributes are applied to the input element. For example, you can integrate bootstrap styling:

    <CurrencyInput className="form-control" />

Options

Option Default Value Description
value 0 The initial currency value
onChange n/a Callback function to handle value changes. Deprecated, use onChangeEvent.
onChangeEvent n/a Callback function to handle value changes
precision 2 Number of digits after the decimal separator
decimalSeparator '.' The decimal separator
thousandSeparator ',' The thousand separator
inputType "text" Input field tag type. You may want to use number or tel*
allowNegative false Allows negative numbers in the input
allowEmpty false If no value is given, defines if it starts as null (true) or '' (false)
selectAllOnFocus false Selects all text on focus or does not
prefix '' Currency prefix
suffix '' Currency suffix

*Note: Enabling any mask-related features such as prefix, suffix or separators with an inputType="number" or "tel" could trigger errors. Most of those characters would be invalid in such input types.

react-currency-input's People

Contributors

aesopwolf avatar becousae avatar darostreet avatar gektorgrom avatar igorsantos07 avatar jsillitoe avatar malbernaz avatar panupan avatar sevos avatar tncalvert avatar viniciusdacal avatar zhuangya avatar

Watchers

 avatar  avatar

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.