0.11.4 • Published 3 months ago

@canutin/svelte-currency-input v0.11.4

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

svelte-currency-input

A form input that converts numbers to localized currency formats as you type


Features

  • Formats positive and negative values
  • Leverages Intl.NumberFormat for localizing currency denominations and masking the input
  • Simple API
  • Minimal default styling, easy to customize

Usage

npm install @canutin/svelte-currency-input --save
<script lang="ts">
  import CurrencyInput from '@canutin/svelte-currency-input';
</script>

<CurrencyInput name="total" value={-420.69} locale="nl-NL" currency="EUR" />

How it works

When the form is submitted you get unformatted or formatted values from two <input />'s. This is more or less what <CurrencyInput /> looks like under the hood:

<div class="currencyInput">
  <!-- Unformatted value -->
  <input
    class="currencyInput__unformatted"
    type="hidden"
    name="total"
    value="-420.69"
  />

  <!-- Formatted value -->
  <input
    class="currencyInput__formatted"
    type="text"
    name="formatted-total"
    value="€ -420,69"
  />
</div>

API

OptionTypeDefaultDescription
valuenumberundefinedInitial value. If left undefined a formatted value of 0 is visible as a placeholder
localestringen-USOverrides default locale. Examples
currencystringUSDOverrides default currency. Examples
namestringtotalApplies the name to the input fields for unformatted (e.g [name=total]) and formatted (e.g. [name=formatted-total]) values
requiredbooleanfalseMarks the inputs as required
disabledbooleanfalseMarks the inputs as disabled
placeholderstring number null0A string will override the default placeholder. A number will override it by formatting it to the set currency. Setting it to null will not show a placeholder
isZeroNullishbooleanfalseIf true and when the value is 0, it will override the default placeholder and render the formatted value in the field like any other value. Note: this option might become the default in future versions
autocompletestringundefinedSets the autocomplete attribute. Accepts any valid HTML autocomplete attribute values
isNegativeAllowedbooleantrueIf false, forces formatting only to positive values and ignores --positive and --negative styling modifiers
fractionDigitsnumber2Sets maximumFractionDigits in Intl.NumberFormat() constructor used for formatting the currency. Supported digits: 0 to 20
inputClassesobjectSee belowSelectively overrides any class names passed
onValueChangeCallbackundefinedRuns a callback function after the value changes

Styling

There are two ways of customizing the styling of the input: 1. Passing it your own CSS classes 2. Overriding the styles using the existing class names

You can override all of the class names by passing an object to inputClasses that has one or more of these properties:

interface InputClasses {
  wrapper?: string; // <div> that contains the two <input> elements
  unformatted?: string; // <input type="hidden"> that contains the unformatted value
  formatted?: string; // <input type="text"> that contains the formatted value
  formattedPositive?: string; // Class added when the formatted input is positive
  formattedNegative?: string; // Class added when the formatted input is negative
  formattedZero?: string; // Class added when the formatted input is zero
}

Usage (with Tailwind CSS as an example):

<CurrencyInput name="total" value="{420.69}" inputClasses={
  { 
    wrapper: "form-control block",
    formatted: 'py-1.5 text-gray-700',
    formattedPositive: 'text-green-700',
    formattedNegative: 'text-red-700'
  }
} />

Alternatively you can write your own CSS by overriding the default styles which use BEM naming conventions. To do so apply your styles as shown below:

<div class="my-currency-input">
  <CurrencyInput name="total" value="{420.69}" />
</div>

<style>
  /* Container */
  div.my-currency-input :global(div.currencyInput) { /* ... */ }

  /* Formatted input */
  div.my-currency-input :global(input.currencyInput__formatted) { /* ... */ }

  /* Formatted input when the it's disabled */
  div.my-currency-input :global(input.currencyInput__formatted:disabled) { /* ... */ }

  /* Formatted input when the value is zero */
  div.my-currency-input :global(input.currencyInput__formatted--zero) { /* ... */ }

  /* Formatted input when the value is positive */
  div.my-currency-input :global(input.currencyInput__formatted--positive) { /* ... */ }

  /* Formatted input when the value is negative */
  div.my-currency-input :global(input.currencyInput__formatted--negative) { /* ... */ }
</style>

Contributing

Here's ways in which you can contribute:

Developing

This package was generated with SvelteKit. Install dependencies with npm install, then start a development server:

npm run dev

# or start the server and open the app in a new browser tab
npm run dev -- --open

Integration tests

The component is tested using Playwright. You can find the tests in tests/svelte-currency-input.test.ts

To run all tests on Chromium, Firefox and Webkit:

npm run test

To run all tests on a specific browser (e.g. Webkit):

npx playwright test --project=webkit

Additional debug commands can be found on Playwright's documentation.

0.11.4

3 months ago

0.9.3

9 months ago

0.11.0

7 months ago

0.10.1

7 months ago

0.11.1

7 months ago

0.11.2

6 months ago

0.11.3

6 months ago

0.10.0

9 months ago

0.9.0

1 year ago

0.9.2

1 year ago

0.9.1

1 year ago

0.8.0

1 year ago

0.7.2

2 years ago

0.7.1

2 years ago

0.7.0

2 years ago

0.6.0

2 years ago

0.5.3

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago

0.5.0

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago