@canutin/svelte-currency-input 0.9.3 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
- <script>const DEFAULT_LOCALE = 'en-US';
1
+ <script>import { onMount } from "svelte";
2
+ const DEFAULT_LOCALE = 'en-US';
2
3
  const DEFAULT_CURRENCY = 'USD';
3
4
  const DEFAULT_NAME = 'total';
4
5
  const DEFAULT_VALUE = 0;
@@ -16,6 +17,7 @@ export let name = DEFAULT_NAME;
16
17
  export let required = false;
17
18
  export let disabled = false;
18
19
  export let placeholder = DEFAULT_VALUE;
20
+ export let autocomplete = undefined;
19
21
  export let isNegativeAllowed = true;
20
22
  export let fractionDigits = DEFAULT_FRACTION_DIGITS;
21
23
  export let inputClasses = null;
@@ -39,6 +41,11 @@ const handleKeyDown = (event) => {
39
41
  if (!isDeletion && !isModifier && !isArrowKey && isInvalidCharacter && !isTab)
40
42
  event.preventDefault();
41
43
  };
44
+ // Formats the value when the input loses focus and sets the correct number of
45
+ // fraction digits when the value is zero
46
+ const handleOnBlur = () => setFormattedValue(true);
47
+ // Also set the correct fraction digits when the value is zero on initial load
48
+ onMount(() => setFormattedValue(true));
42
49
  let inputTarget;
43
50
  const currencyDecimal = new Intl.NumberFormat(locale).format(1.1).charAt(1); // '.' or ','
44
51
  const isDecimalComma = currencyDecimal === ',';
@@ -95,12 +102,12 @@ const setUnformattedValue = (event) => {
95
102
  }
96
103
  }
97
104
  };
98
- const setFormattedValue = () => {
105
+ const setFormattedValue = (hasMinFractionDigits) => {
99
106
  // Previous caret position
100
107
  const startCaretPosition = inputTarget?.selectionStart || 0;
101
108
  const previousFormattedValueLength = formattedValue.length;
102
109
  // Apply formatting to input
103
- formattedValue = isZero ? '' : formatCurrency(value, fractionDigits, 0);
110
+ formattedValue = isZero ? '' : formatCurrency(value, fractionDigits, hasMinFractionDigits ? fractionDigits : 0);
104
111
  // Update `value` after formatting
105
112
  setUnformattedValue();
106
113
  let retries = 0;
@@ -144,11 +151,12 @@ $: value, setFormattedValue();
144
151
  name={`formatted-${name}`}
145
152
  required={required && !isZero}
146
153
  placeholder={formattedPlaceholder}
154
+ {autocomplete}
147
155
  {disabled}
148
156
  bind:value={formattedValue}
149
157
  on:keydown={handleKeyDown}
150
158
  on:keyup={setUnformattedValue}
151
- on:blur={setFormattedValue}
159
+ on:blur={handleOnBlur}
152
160
  />
153
161
  </div>
154
162
 
@@ -8,6 +8,7 @@ declare const __propDef: {
8
8
  required?: boolean | undefined;
9
9
  disabled?: boolean | undefined;
10
10
  placeholder?: number | null | undefined;
11
+ autocomplete?: string | null | undefined;
11
12
  isNegativeAllowed?: boolean | undefined;
12
13
  fractionDigits?: number | undefined;
13
14
  inputClasses?: {
package/README.md CHANGED
@@ -58,19 +58,20 @@ This is more or less what `<CurrencyInput />` looks like under the hood:
58
58
 
59
59
  ## API
60
60
 
61
- | Option | Type | Default | Description |
62
- | ----------------- | --------------- | ----------- | ----------- |
63
- | value | `number` | `undefined` | Initial value. If left `undefined` a formatted value of `0` is visible as a placeholder |
64
- | locale | `string` | `en-US` | Overrides default locale. [Examples](https://gist.github.com/ncreated/9934896) |
65
- | currency | `string` | `USD` | Overrides default currency. [Examples](https://www.xe.com/symbols/) |
66
- | name | `string` | `total` | Applies the name to the [input fields](#how-it-works) for _unformatted_ (e.g `[name=total]`) and _formatted_ (e.g. `[name=formatted-total]`) values |
67
- | required | `boolean` | `false` | Marks the inputs as required |
68
- | disabled | `boolean` | `false` | Marks the inputs as disabled |
69
- | placeholder | `number` `null` | `0` | Overrides the default placeholder. Setting the value to a `number` will display it as formatted. Setting it to `null` will not show a placeholder |
70
- | isNegativeAllowed | `boolean` | `true` | If `false`, forces formatting only to positive values and ignores `--positive` and `--negative` styling modifiers |
71
- | fractionDigits | `number` | `2` | Sets `maximumFractionDigits` in [`Intl.NumberFormat()` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#minimumfractiondigits) used for formatting the currency. Supported digits: `0` to `20` |
72
- | inputClasses | `object` | [See below](#Styling) | Selectively overrides any class names passed |
73
- | onValueChange | `Callback` | `undefined` | Runs a callback function after the value changes |
61
+ Option | Type | Default | Description |
62
+ ----------------- | --------------- | ----------- | ----------- |
63
+ value | `number` | `undefined` | Initial value. If left `undefined` a formatted value of `0` is visible as a placeholder |
64
+ locale | `string` | `en-US` | Overrides default locale. [Examples](https://gist.github.com/ncreated/9934896) |
65
+ currency | `string` | `USD` | Overrides default currency. [Examples](https://www.xe.com/symbols/) |
66
+ name | `string` | `total` | Applies the name to the [input fields](#how-it-works) for _unformatted_ (e.g `[name=total]`) and _formatted_ (e.g. `[name=formatted-total]`) values |
67
+ required | `boolean` | `false` | Marks the inputs as required |
68
+ disabled | `boolean` | `false` | Marks the inputs as disabled |
69
+ placeholder | `number` `null` | `0` | Overrides the default placeholder. Setting the value to a `number` will display it as formatted. Setting it to `null` will not show a placeholder |
70
+ autocomplete | `string` | `undefined` | Sets the autocomplete attribute. Accepts any valid HTML [autocomplete attribute values](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete#values) |
71
+ isNegativeAllowed | `boolean` | `true` | If `false`, forces formatting only to positive values and ignores `--positive` and `--negative` styling modifiers |
72
+ fractionDigits | `number` | `2` | Sets `maximumFractionDigits` in [`Intl.NumberFormat()` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#minimumfractiondigits) used for formatting the currency. Supported digits: `0` to `20` |
73
+ inputClasses | `object` | [See below](#Styling) | Selectively overrides any class names passed |
74
+ onValueChange | `Callback` | `undefined` | Runs a callback function after the value changes |
74
75
 
75
76
  ## Styling
76
77
 
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@canutin/svelte-currency-input",
3
- "version": "0.9.3",
3
+ "version": "0.10.1",
4
4
  "exports": {
5
5
  "./package.json": "./package.json",
6
6
  ".": "./index.js",
7
7
  "./CurrencyInput.svelte": "./CurrencyInput.svelte"
8
8
  },
9
9
  "devDependencies": {
10
- "@playwright/test": "^1.25.0",
10
+ "@playwright/test": "^1.37.1",
11
11
  "@sveltejs/adapter-auto": "next",
12
12
  "@sveltejs/kit": "next",
13
13
  "@sveltejs/package": "next",