New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

input-currency-field

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

input-currency-field

Boilerplate for currency input field. You might not need react-*-mask lib.

latest
Source
npmnpm
Version
2.0.1
Version published
Maintainers
1
Created
Source

Input currency field

⚡️ Live demo

Helpers for currency input

Install

npm install input-currency-field

Use

import currencyField from 'input-currency-field'

const onChangeHandler = (event) => {
  const value = event.target.value
  if (value === null || value.length < 1) {
    return
  }
  // fill in config with possible options
  const config = {
    decimalSymbol: ',',
    allowedDecimalSymbols: ',.;',
    postfix: ' $',
    thousandsSeparator: ' ',
  }
  // apply mask/config to value
  const displayValue = currencyField.format(value, config)
  // calculate next caret/cursor position
  const nextCaretPosition = currencyField.getNextCaretPosition({
    value,
    displayValue,
    postfix: config.postfix,
    caretPosition: event.target.selectionStart,
  })
  // get math-ready value
  const mathValue = currencyField.parse(displayValue, config)

  // update DOM
  event.target.value = displayValue
  currencyField.setNextCaretPosition(event.target, nextCaretPosition)
  document.getElementById('realValue').value = mathValue
}

document.getElementById('currency').addEventListener('input', onChangeHandler)

❕ Many npm libs cannot, but this js could use custom

  • decimal symbol (even range of them)
  • postfix
  • thousand separator
  • onChange event. Use your own! Separating displayValue and value with no problem

⚠️ Before you copy and paste into prod

  • onChangeHandler should be debounced or throttled

📚 MDN

Keywords

purejs

FAQs

Package last updated on 04 Feb 2022

Did you know?

Socket

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