@canutin/svelte-currency-input 0.3.1 → 0.4.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
CHANGED
|
@@ -58,15 +58,16 @@ This is more or less what `<CurrencyInput />` looks like under the hood:
|
|
|
58
58
|
|
|
59
59
|
## API
|
|
60
60
|
|
|
61
|
-
| Option | Type
|
|
62
|
-
| ----------------- |
|
|
63
|
-
| value | `number`
|
|
64
|
-
| locale | `string`
|
|
65
|
-
| currency | `string`
|
|
66
|
-
| name | `string`
|
|
67
|
-
| required | `boolean`
|
|
68
|
-
| disabled | `boolean`
|
|
69
|
-
|
|
|
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://github.com/datasets/currency-codes/blob/master/data/codes-all.csv) |
|
|
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 |
|
|
70
71
|
|
|
71
72
|
## Styling
|
|
72
73
|
|
package/package.json
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
export let name: string = DEFAULT_NAME;
|
|
11
11
|
export let required: boolean = false;
|
|
12
12
|
export let disabled: boolean = false;
|
|
13
|
+
export let placeholder: number | null = DEFAULT_VALUE;
|
|
13
14
|
export let isNegativeAllowed: boolean = true;
|
|
14
15
|
|
|
15
16
|
// Formats value as: e.g. $1,523.00 | -$1,523.00
|
|
@@ -75,11 +76,10 @@
|
|
|
75
76
|
};
|
|
76
77
|
|
|
77
78
|
let formattedValue = '';
|
|
79
|
+
let formattedPlaceholder = placeholder !== null ? formatCurrency(placeholder, 2, 2) : '';
|
|
78
80
|
$: isZero = value === 0;
|
|
79
81
|
$: isNegative = value < 0;
|
|
80
82
|
$: value, setFormattedValue();
|
|
81
|
-
|
|
82
|
-
const placeholder = formatCurrency(DEFAULT_VALUE, 2, 2); // e.g. '$0.00'
|
|
83
83
|
</script>
|
|
84
84
|
|
|
85
85
|
<div class="currencyInput">
|
|
@@ -95,7 +95,7 @@
|
|
|
95
95
|
inputmode="numeric"
|
|
96
96
|
name={`formatted-${name}`}
|
|
97
97
|
required={required && !isZero}
|
|
98
|
-
{
|
|
98
|
+
placeholder={formattedPlaceholder}
|
|
99
99
|
{disabled}
|
|
100
100
|
bind:value={formattedValue}
|
|
101
101
|
on:keydown={handleKeyDown}
|
package/src/routes/+page.svelte
CHANGED
|
@@ -18,13 +18,19 @@
|
|
|
18
18
|
<div class="demoForm__container">
|
|
19
19
|
<CurrencyInput name="total" value={-42069.69} />
|
|
20
20
|
<CurrencyInput name="rent" />
|
|
21
|
+
<CurrencyInput name="balance" value={1234.56} isNegativeAllowed={false} placeholder={null} />
|
|
21
22
|
<CurrencyInput name="cashflow" value={5678.9} />
|
|
22
|
-
<CurrencyInput name="balance" value={1234.56} isNegativeAllowed={false} />
|
|
23
23
|
|
|
24
24
|
<CurrencyInput name="amount" value={5678.9} {locale} {currency} />
|
|
25
|
-
<CurrencyInput name="deficit" value={1234.56} isNegativeAllowed={false} {locale} {currency} />
|
|
26
25
|
<CurrencyInput name="loss" value={97532.95} disabled={true} {locale} {currency} />
|
|
27
26
|
<CurrencyInput name="cost" value={-42069.69} {locale} {currency} />
|
|
27
|
+
<CurrencyInput
|
|
28
|
+
name="deficit"
|
|
29
|
+
placeholder={1234.56}
|
|
30
|
+
isNegativeAllowed={false}
|
|
31
|
+
{locale}
|
|
32
|
+
{currency}
|
|
33
|
+
/>
|
|
28
34
|
</div>
|
|
29
35
|
|
|
30
36
|
<nav class="demoForm__output">
|
|
@@ -71,16 +71,16 @@ test.describe('CurrencyInput', () => {
|
|
|
71
71
|
'formatted-total': '-$42,069.69',
|
|
72
72
|
rent: '0',
|
|
73
73
|
'formatted-rent': '',
|
|
74
|
-
cashflow: '5678.9',
|
|
75
|
-
'formatted-cashflow': '$5,678.9',
|
|
76
74
|
balance: '1234.56',
|
|
77
75
|
'formatted-balance': '$1,234.56',
|
|
76
|
+
cashflow: '5678.9',
|
|
77
|
+
'formatted-cashflow': '$5,678.9',
|
|
78
78
|
amount: '5678.9',
|
|
79
79
|
'formatted-amount': '€ 5.678,9',
|
|
80
|
-
deficit: '1234.56',
|
|
81
|
-
'formatted-deficit': '€ 1.234,56',
|
|
82
80
|
cost: '-42069.69',
|
|
83
|
-
'formatted-cost': '€ -42.069,69'
|
|
81
|
+
'formatted-cost': '€ -42.069,69',
|
|
82
|
+
deficit: '0',
|
|
83
|
+
'formatted-deficit': ''
|
|
84
84
|
},
|
|
85
85
|
null,
|
|
86
86
|
2
|
|
@@ -184,6 +184,26 @@ test.describe('CurrencyInput', () => {
|
|
|
184
184
|
await expect(rentFormattedInput).toHaveValue('');
|
|
185
185
|
});
|
|
186
186
|
|
|
187
|
+
test('Placeholders can be overriden', async ({ page }) => {
|
|
188
|
+
await page.goto('/');
|
|
189
|
+
|
|
190
|
+
// Default placeholder
|
|
191
|
+
const rentFormattedInput = page.locator('.currencyInput__formatted[name="formatted-rent"]');
|
|
192
|
+
await expect(rentFormattedInput).toHaveAttribute('placeholder', '$0.00');
|
|
193
|
+
|
|
194
|
+
// Null placeholder
|
|
195
|
+
const balanceFormattedInput = page.locator(
|
|
196
|
+
'.currencyInput__formatted[name="formatted-balance"]'
|
|
197
|
+
);
|
|
198
|
+
await expect(balanceFormattedInput).toHaveAttribute('placeholder', '');
|
|
199
|
+
|
|
200
|
+
// Overriden placeholder
|
|
201
|
+
const deficitFormattedInput = page.locator(
|
|
202
|
+
'.currencyInput__formatted[name="formatted-deficit"]'
|
|
203
|
+
);
|
|
204
|
+
await expect(deficitFormattedInput).toHaveAttribute('placeholder', '€ 1.234,56'); // The space is `%A0`, not `%20`
|
|
205
|
+
});
|
|
206
|
+
|
|
187
207
|
test.skip('Updating chained inputs have the correct behavior', async () => {
|
|
188
208
|
// TODO
|
|
189
209
|
});
|