1.5.1 • Published 4 months ago

@vinzdf89/react-currency-field v1.5.1

Weekly downloads
-
License
-
Repository
github
Last release
4 months ago

React Currency Field

Image

This package provides a React component with an input field to be used in React projects, where the value directly typed by the user is automatically formatted in the local or in a specific currency format.

1.5.1 patch release notes

  • Fixed security issue by updating vite.

1.5.0 minor release notes

  • Added new attributes "onMouseDown" and "onFocus" to allow the usage of custom event functions.
  • Small optimization: if the value consists of only 1 character, when the input gets the focus the value gets always entirely selected.

Installation

npm i @vinzdf89/react-currency-field

Example

A simple usage of this component would be:

import { useState } from 'react'
import CurrencyField from "@vinzdf89/react-currency-field"

function App() {
    const initialAmount = 5000;

    const [amount, setAmount] = useState<string | number>(initialAmount);
    const [numericalAmount, setNumericalAmount] = useState<number>(initialAmount);

    function changeAmount(e: React.FormEvent<HTMLInputElement>) {
        setAmount(e.currentTarget.value);
    }

    function changeNumericalAmount(newValue: number) {
        setNumericalAmount(newValue);
    }

    return (
        <>
            <CurrencyField value={amount} onChange={changeAmount} onNumericalChange={changeNumericalAmount} />
            <p>String value: {amount}</p>
            <p>Numerical value: {numericalAmount}</p>
        </>
    )
}

export default App;

A more complex example can be found in the App.tsx file from the GitHub project.

Attributes / Properties

NameTypeDefaultDescription
refRef\<HTMLInputElement>undefinedUsed for binding a Ref object to the input field.
idstringundefinedFor setting the standard HTML "id" attribute.
namestringundefinedFor setting the standard HTML "name" attribute.
valuestring | numberundefinedUseful for setting a default value, this should be a value provided by a "useState" hook. It can be either a number or a string (which has to comply with the local format set!). Number is recommended.
placeholderstringundefinedFor setting the standard HTML "placeholder" attribute.
onChange(e: React.ChangeEvent) => voidundefinedFor setting a custom event handler on "change" event, which should update the state passed to the "value" attribute. Look at the example above.
onBlur(e: React.FocusEvent) => voidundefinedFor setting a custom event handler on "blur" event, which will be executed after the internal handler has run.
onPaste(e: React.ClipboardEvent) => voidundefinedFor setting a custom event handler on "paste" event, which will be executed after the internal handler has run.
classNamestringundefinedUsed to set classes to the input field.
localestringundefinedSet the number format. If the attribute is not provided, the format will be fetched from the browser's language preferences. If even this fails, it will be set to 'en-US'.
symbolstring'$'Set the currency symbol to use.
symbolPosition'start' | 'end''start'Move the symbol before or after the input field.
decimalsnumber2Number of decimals allowed.
maxnumber999999999The input field will not allow a value that is greater than the one specified by this attribute, and it will call the function (if any) specified by the "onMaxFails" attribute.
minnumber0If a value to this attribute is provided and it's greater than zero, any value will be allowed in the input field, but in case it's lower than the one specified by this attribute, it will call the function (if any) specified by the "onMinFails" attribute.
disableAutoSymbolPositioningbooleanfalseBy default the currency symbol will be translated to the right, so that it will be graphically shown up as if it were inside the input field (at the beginning or at the end depending on the "symbolPosition" attribute). If set to true, the chosen currency symbol will not be moved, so that the developer would have more freedom to style the component in a different and more precise way in order to make it more suitable and consistent with their graphics.
onNumericalChange(newValue: number) => voidundefinedWhenever the user interacts with the input field, the function passed to this attribute will be called and it will contain a parameter representing the value of the field but of number type.
onMaxFails(newValue: boolean) => voidundefinedIf set, the function will be called every time the user tries to set a number greater than the one specified by the "max" attribute.
onMinFails(newValue: boolean) => voidundefinedIf set, the function will be called every time the user interacts with the input field, and its value is lower than the one specified by the "min" attribute.

Previous releases

1.4.5 patch release notes

  • Both numerical and string values are now updated when the value of the "value" attribute is externally modified.

1.4.4 patch release notes

  • Fixed the formatting of the value when changing it externally (through the "value" attribute).

1.4.3 patch release notes

  • Fixed the wrong positioning and the formatting of the value that was occurring in specific cases.

1.4.2 patch release notes

  • Fixed the problem that prevented updating the field value by externally calling the set function for the "numericalValue" attribute.
  • "numericalValue" attribute marked as deprecated.

1.4.1 patch release notes

  • Fixed the problem that prevented updating the field value by externally calling the set function.

1.4.0 minor release notes

  • On blur, the input value is set to zero if empty.
  • Automatic symbol positioning logic optimized. Now the symbol position can be changed without graphical issues.

1.3.2 minor release notes

  • Optimized the value's formatting when deleting the decimal part. Now the decimal separator is kept until it is explicitly removed.

1.3.1 patch release notes

  • Optimized the cursor position for fields using the "numericalValue" attribute instead of "value"

1.3.0 minor release notes

  • Optimized the automatic setting of the input's padding where the symbol is placed.
  • Added the more generic "disableAutoSymbolPositioning" attribute to use instead of "disableAutoCurrencyPositioning", which is now marked as deprecated.

1.2.1 patch release notes

  • Fixed vulnerability from the Babel dependency.

1.2.0 minor release notes

  • Automatic symbol positioning optimized for input fields with full width.
  • Symbol now cannot be selected so it doesn't interfere with the input field anymore.
  • When typing non-allowed characters (such as letters), the cursor will now keep its position.
  • If using only "numericalValue" instead of "value" attribute, or if using "value" and passing it a correct formatted string value, the input field will not trigger an additional rendering on initialization anymore (if you don't need to get the formatted value, it's always recommended to use "numericalValue" attribute).
  • When deleting the first digit, the rest of the number is now kept in order to not lose it.
  • Several other small changes and improvements + a light refactoring of the code.

1.1.0 minor release notes

  • Added the more generic "symbol" attribute to use instead of "currency", which is now marked as deprecated.
  • Added the new "symbolPosition" attribute, which allows to move the symbol after the field if set to "end" (default is "start").
  • Small optimization on keyboard cursor's positioning.
  • Code refactoring.
1.5.1

4 months ago

1.5.0

4 months ago

1.4.5

5 months ago

1.4.4

5 months ago

1.4.3

5 months ago

1.4.2

5 months ago

1.4.1

5 months ago

1.4.0

7 months ago

1.3.2

7 months ago

1.3.1

7 months ago

1.3.0

7 months ago

1.2.1

7 months ago

1.2.0

7 months ago

1.1.0

8 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago