@canutin/svelte-currency-input 0.2.0 → 0.2.2
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/CurrencyInput.svelte +5 -7
- package/README.md +1 -1
- package/package.json +1 -1
package/CurrencyInput.svelte
CHANGED
|
@@ -19,7 +19,9 @@ const formatCurrency = (value, maximumFractionDigits, minimumFractionDigits) =>
|
|
|
19
19
|
}).format(value);
|
|
20
20
|
};
|
|
21
21
|
const placeholder = formatCurrency(0, 2, 2); // e.g. '$0.00'
|
|
22
|
-
const currencySymbol = formatCurrency(0, 0)
|
|
22
|
+
const currencySymbol = formatCurrency(0, 0)
|
|
23
|
+
.replace('0', '') // e.g. '$0' > '$'
|
|
24
|
+
.replace(/\u00A0/, ''); // e.g '0 €' > '€'
|
|
23
25
|
const currencyDecimal = new Intl.NumberFormat(locale).format(1.1).charAt(1); // '.' or ','
|
|
24
26
|
// Updates `value` by stripping away the currency formatting
|
|
25
27
|
const setValue = (event) => {
|
|
@@ -43,10 +45,11 @@ const setValue = (event) => {
|
|
|
43
45
|
value = 0;
|
|
44
46
|
}
|
|
45
47
|
else {
|
|
48
|
+
// The order of the following operations is *critical*
|
|
46
49
|
const isDecimalComma = currencyDecimal === ','; // Remove currency formatting from `formattedValue` so we can assign it to `value`
|
|
50
|
+
unformattedValue = unformattedValue.replace(isDecimalComma ? /\./g : /\,/g, ''); // Remove all group symbols
|
|
47
51
|
if (isDecimalComma)
|
|
48
52
|
unformattedValue = unformattedValue.replace(',', '.'); // If the decimal point is a comma, replace it with a period
|
|
49
|
-
unformattedValue = unformattedValue.replace(isDecimalComma ? /\./g : /\,/g, ''); // Remove all group symbols
|
|
50
53
|
value = parseFloat(unformattedValue);
|
|
51
54
|
}
|
|
52
55
|
};
|
|
@@ -76,11 +79,6 @@ const applyFormatting = () => {
|
|
|
76
79
|
</div>
|
|
77
80
|
|
|
78
81
|
<style>
|
|
79
|
-
div.currencyInput {
|
|
80
|
-
display: flex;
|
|
81
|
-
flex-direction: column;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
82
|
input.currencyInput__formatted {
|
|
85
83
|
border: 1px solid #e2e2e2;
|
|
86
84
|
padding: 10px;
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# svelte-currency-input
|
|
2
2
|
|
|
3
|
-
A form input that converts numbers to
|
|
3
|
+
A form input that converts numbers to localized currency formats as you type
|
|
4
4
|
|
|
5
5
|
[<img width="1059" alt="image" src="https://user-images.githubusercontent.com/1434675/190315136-c1d310ab-0ef1-441d-a80c-2b3727d74f59.png">](https://svelte.dev/repl/d8f7d22e5b384555b430f62b157ac503?version=3.50.1)
|
|
6
6
|
|