@canutin/svelte-currency-input 0.4.0 → 0.5.0
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.
- package/README.md +1 -0
- package/package.json +1 -1
- package/src/lib/CurrencyInput.svelte +12 -8
- package/src/routes/+page.svelte +1 -1
- package/tests/svelte-currency-input.test.ts +31 -10
package/README.md
CHANGED
|
@@ -68,6 +68,7 @@ This is more or less what `<CurrencyInput />` looks like under the hood:
|
|
|
68
68
|
| disabled | `boolean` | `false` | Marks the inputs as disabled |
|
|
69
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
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` |
|
|
71
72
|
|
|
72
73
|
## Styling
|
|
73
74
|
|
package/package.json
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const DEFAULT_CURRENCY = 'USD';
|
|
4
4
|
const DEFAULT_NAME = 'total';
|
|
5
5
|
const DEFAULT_VALUE = 0;
|
|
6
|
+
const DEFAULT_FRACTION_DIGITS = 2;
|
|
6
7
|
|
|
7
8
|
export let value: number = DEFAULT_VALUE;
|
|
8
9
|
export let locale: string = DEFAULT_LOCALE;
|
|
@@ -12,6 +13,7 @@
|
|
|
12
13
|
export let disabled: boolean = false;
|
|
13
14
|
export let placeholder: number | null = DEFAULT_VALUE;
|
|
14
15
|
export let isNegativeAllowed: boolean = true;
|
|
16
|
+
export let fractionDigits: number = DEFAULT_FRACTION_DIGITS;
|
|
15
17
|
|
|
16
18
|
// Formats value as: e.g. $1,523.00 | -$1,523.00
|
|
17
19
|
const formatCurrency = (
|
|
@@ -37,16 +39,18 @@
|
|
|
37
39
|
if (!isDeletion && !isModifier && !isArrowKey && isInvalidCharacter) event.preventDefault();
|
|
38
40
|
};
|
|
39
41
|
|
|
42
|
+
const currencyDecimal = new Intl.NumberFormat(locale).format(1.1).charAt(1); // '.' or ','
|
|
43
|
+
const isDecimalComma = currencyDecimal === ','; // Remove currency formatting from `formattedValue` so we can assign it to `value`
|
|
44
|
+
const currencySymbol = formatCurrency(0, 0)
|
|
45
|
+
.replace('0', '') // e.g. '$0' > '$'
|
|
46
|
+
.replace(/\u00A0/, ''); // e.g '0 €' > '€'
|
|
47
|
+
|
|
40
48
|
// Updates `value` by stripping away the currency formatting
|
|
41
49
|
const setUnformattedValue = (event: KeyboardEvent) => {
|
|
42
50
|
// Don't format if the user is typing a `currencyDecimal` point
|
|
43
|
-
const currencyDecimal = new Intl.NumberFormat(locale).format(1.1).charAt(1); // '.' or ','
|
|
44
51
|
if (event.key === currencyDecimal) return;
|
|
45
52
|
|
|
46
|
-
//
|
|
47
|
-
const currencySymbol = formatCurrency(0, 0)
|
|
48
|
-
.replace('0', '') // e.g. '$0' > '$'
|
|
49
|
-
.replace(/\u00A0/, ''); // e.g '0 €' > '€'
|
|
53
|
+
// Don't format if `formattedValue` is ['$', '-$', "-"]
|
|
50
54
|
const ignoreSymbols = [currencySymbol, `-${currencySymbol}`, '-'];
|
|
51
55
|
const strippedUnformattedValue = formattedValue.replace(' ', '');
|
|
52
56
|
if (ignoreSymbols.includes(strippedUnformattedValue)) return;
|
|
@@ -64,7 +68,6 @@
|
|
|
64
68
|
value = 0;
|
|
65
69
|
} else {
|
|
66
70
|
// The order of the following operations is *critical*
|
|
67
|
-
const isDecimalComma = currencyDecimal === ','; // Remove currency formatting from `formattedValue` so we can assign it to `value`
|
|
68
71
|
unformattedValue = unformattedValue.replace(isDecimalComma ? /\./g : /\,/g, ''); // Remove all group symbols
|
|
69
72
|
if (isDecimalComma) unformattedValue = unformattedValue.replace(',', '.'); // If the decimal point is a comma, replace it with a period
|
|
70
73
|
value = parseFloat(unformattedValue);
|
|
@@ -72,11 +75,12 @@
|
|
|
72
75
|
};
|
|
73
76
|
|
|
74
77
|
const setFormattedValue = () => {
|
|
75
|
-
formattedValue = isZero ? '' : formatCurrency(value,
|
|
78
|
+
formattedValue = isZero ? '' : formatCurrency(value, fractionDigits, 0);
|
|
76
79
|
};
|
|
77
80
|
|
|
78
81
|
let formattedValue = '';
|
|
79
|
-
let formattedPlaceholder =
|
|
82
|
+
let formattedPlaceholder =
|
|
83
|
+
placeholder !== null ? formatCurrency(placeholder, fractionDigits, fractionDigits) : '';
|
|
80
84
|
$: isZero = value === 0;
|
|
81
85
|
$: isNegative = value < 0;
|
|
82
86
|
$: value, setFormattedValue();
|
package/src/routes/+page.svelte
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
<CurrencyInput name="total" value={-42069.69} />
|
|
20
20
|
<CurrencyInput name="rent" />
|
|
21
21
|
<CurrencyInput name="balance" value={1234.56} isNegativeAllowed={false} placeholder={null} />
|
|
22
|
-
<CurrencyInput name="
|
|
22
|
+
<CurrencyInput name="btc" value={0.87654321} fractionDigits={8} />
|
|
23
23
|
|
|
24
24
|
<CurrencyInput name="amount" value={5678.9} {locale} {currency} />
|
|
25
25
|
<CurrencyInput name="loss" value={97532.95} disabled={true} {locale} {currency} />
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import { expect, test } from '@playwright/test';
|
|
1
|
+
import { expect, test, type Page } from '@playwright/test';
|
|
2
|
+
|
|
3
|
+
const isMacOs = process.platform === 'darwin';
|
|
4
|
+
const selectAll = async (page: Page) => {
|
|
5
|
+
isMacOs ? await page.keyboard.press('Meta+A') : await page.keyboard.press('Control+A');
|
|
6
|
+
};
|
|
2
7
|
|
|
3
8
|
test.describe('CurrencyInput', () => {
|
|
4
9
|
test('Default behavior is correct', async ({ page }) => {
|
|
@@ -73,8 +78,8 @@ test.describe('CurrencyInput', () => {
|
|
|
73
78
|
'formatted-rent': '',
|
|
74
79
|
balance: '1234.56',
|
|
75
80
|
'formatted-balance': '$1,234.56',
|
|
76
|
-
|
|
77
|
-
'formatted-
|
|
81
|
+
btc: '0.87654321',
|
|
82
|
+
'formatted-btc': '$0.87654321',
|
|
78
83
|
amount: '5678.9',
|
|
79
84
|
'formatted-amount': '€ 5.678,9',
|
|
80
85
|
cost: '-42069.69',
|
|
@@ -139,7 +144,6 @@ test.describe('CurrencyInput', () => {
|
|
|
139
144
|
test("Incorrect characters can't be entered", async ({ page }) => {
|
|
140
145
|
await page.goto('/');
|
|
141
146
|
|
|
142
|
-
const isMacOs = process.platform === 'darwin';
|
|
143
147
|
const rentUnformattedInput = page.locator('.currencyInput__unformatted[name=rent]');
|
|
144
148
|
const rentFormattedInput = page.locator('.currencyInput__formatted[name="formatted-rent"]');
|
|
145
149
|
|
|
@@ -162,10 +166,8 @@ test.describe('CurrencyInput', () => {
|
|
|
162
166
|
await expect(rentFormattedInput).toHaveValue('$420.69');
|
|
163
167
|
await expect(rentUnformattedInput).toHaveValue('420.69');
|
|
164
168
|
|
|
165
|
-
// Select all
|
|
166
|
-
isMacOs ? await page.keyboard.press('Meta+A') : await page.keyboard.press('Control+A');
|
|
167
|
-
|
|
168
169
|
// Check "Backspace" works
|
|
170
|
+
await selectAll(page);
|
|
169
171
|
await page.keyboard.press('Backspace');
|
|
170
172
|
await expect(rentUnformattedInput).toHaveValue('0');
|
|
171
173
|
await expect(rentFormattedInput).toHaveValue('');
|
|
@@ -175,10 +177,8 @@ test.describe('CurrencyInput', () => {
|
|
|
175
177
|
await expect(rentFormattedInput).toHaveValue('-$420.69');
|
|
176
178
|
await expect(rentUnformattedInput).toHaveValue('-420.69');
|
|
177
179
|
|
|
178
|
-
// Select all
|
|
179
|
-
isMacOs ? await page.keyboard.press('Meta+A') : await page.keyboard.press('Control+A');
|
|
180
|
-
|
|
181
180
|
// Check "Delete" also works
|
|
181
|
+
await selectAll(page);
|
|
182
182
|
await page.keyboard.press('Delete');
|
|
183
183
|
await expect(rentUnformattedInput).toHaveValue('0');
|
|
184
184
|
await expect(rentFormattedInput).toHaveValue('');
|
|
@@ -204,6 +204,27 @@ test.describe('CurrencyInput', () => {
|
|
|
204
204
|
await expect(deficitFormattedInput).toHaveAttribute('placeholder', '€ 1.234,56'); // The space is `%A0`, not `%20`
|
|
205
205
|
});
|
|
206
206
|
|
|
207
|
+
test('Fraction digits can be overriden', async ({ page }) => {
|
|
208
|
+
await page.goto('/');
|
|
209
|
+
|
|
210
|
+
const btcUnformattedInput = page.locator('.currencyInput__unformatted[name=btc]');
|
|
211
|
+
const btcFormattedInput = page.locator('.currencyInput__formatted[name="formatted-btc"]');
|
|
212
|
+
|
|
213
|
+
await expect(btcUnformattedInput).toHaveValue('0.87654321');
|
|
214
|
+
await expect(btcFormattedInput).toHaveValue('$0.87654321');
|
|
215
|
+
await expect(btcFormattedInput).toHaveAttribute('placeholder', '$0.00000000');
|
|
216
|
+
|
|
217
|
+
await btcFormattedInput.focus();
|
|
218
|
+
await selectAll(page);
|
|
219
|
+
await page.keyboard.press('Backspace');
|
|
220
|
+
await expect(btcUnformattedInput).toHaveValue('0');
|
|
221
|
+
await expect(btcFormattedInput).toHaveValue('');
|
|
222
|
+
|
|
223
|
+
await page.keyboard.type('-0.987654321');
|
|
224
|
+
await expect(btcUnformattedInput).toHaveValue('-0.987654321');
|
|
225
|
+
await expect(btcFormattedInput).toHaveValue('-$0.98765432');
|
|
226
|
+
});
|
|
227
|
+
|
|
207
228
|
test.skip('Updating chained inputs have the correct behavior', async () => {
|
|
208
229
|
// TODO
|
|
209
230
|
});
|