@ericblade/react-currency-input

中文文档
1.4.6发布于 2026/10/20MITTypeScriptESM + CJS构建来源已验证近 30 天下载 6050

React component for inputting currency amounts

原始使用文档
文档目录10 节
  1. Testing Across React Versions
  2. Codepen Demonstrations
  3. Pre-fork README below
  4. Changes
  5. v1.3.0
  6. Installation
  7. Integration
  8. Separators and Precision
  9. Currency
  10. Options

@ericblade/react-currency-input

Rolling Versions

Modern React component for currency input. Fork from https://github.com/jsillitoe/react-currency-input

Supports custom decimal and thousand separators as well as precision.

Uniquely formats while inputting, including decimal and thousands separators, precision, prefixes and suffixes. I have not found any other currency inputs that do this, and the original hasn't been touched for quite a long time, so that is why I fork this.

Changes since the original

  • Now in TypeScript
  • Supports React 16, 17, 18, 19
  • Totally new automated testing setup with Cypress Playwright (new in 1.4.2+)
  • Automated Testing setup much more thorough
  • Caret selection redone and seems to work everywhere
  • Uses react-device-detect to workaround issues with Gboard

Testing Across React Versions

This package can be tested against React 16 through 19 locally:

npm run test:react16
npm run test:react17
npm run test:react18
npm run test:react19

Run all of them in sequence:

npm run test:matrix

Codepen Demonstrations

There is a CodePen available which you may use to see the component in action, and play with all of it's various parameters to see how it works. It is written with React and TypeScript. Codepen Link

There is a second CodePen, which mirrors the demonstration application in the "examples" directory here in the source: Codepen Link

Pre-fork README below

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 @ericblade/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 '@ericblade/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 '@ericblade/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

OptionDefault ValueDescription
value0The initial currency value
onChangeEventn/aCallback function to handle value changes
precision2Number 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*
allowNegativefalseAllows negative numbers in the input
allowEmptyfalseIf no value is given, defines if it starts as null (true) or '' (false)
selectAllOnFocusfalseSelects all text on focus or does not
prefix''Currency prefix
suffix''Currency suffix
autoFocusfalseAutofocus
onClicknonePassed through to input
onFocusnoneCalled after internal focus handling
onBlurnoneCalled after internal blur handling
stylenonePassed through to input
idnonePassed through to input
tabIndexnonePassed through to input

*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.

版本与发布记录

@ericblade/react-currency-input 共发布 18 个版本,首次发布于 2022/01/09;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.4.6latest2026/10/2024 天前
1.4.52025/12/1610 个月前
1.4.42024/07/222 年前
1.4.32024/07/222 年前
1.4.12023/07/063 年前
1.4.02023/07/033 年前

关于 @ericblade/react-currency-input 的常见问题

@ericblade/react-currency-input 是什么?

React component for inputting currency amounts

如何安装 @ericblade/react-currency-input?

在项目目录运行 npm install @ericblade/react-currency-input,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=16.14.0 <20)、react-dom(>=16.14.0 <20)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ericblade/react-currency-input 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ericblade/react-currency-input 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ericblade/react-currency-input 可以商用吗?

@ericblade/react-currency-input 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ericblade/react-currency-input 的下载量有多少?

根据 npm 官方下载统计,@ericblade/react-currency-input 在 2026/10/14 – 2026/11/12 这 30 天内被下载 6,050 次,日均约 202 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ericblade/react-currency-input 还在维护吗?

最新版本 1.4.6 发布于 2026/10/20(24 天前);至今共发布 18 个版本,其中过去 12 个月发布了 2 个;首次发布于 2022/01/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。