@canutin/svelte-currency-input 0.5.3 → 0.6.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/CurrencyInput.svelte +14 -0
- package/README.md +1 -1
- package/package.json +1 -1
package/CurrencyInput.svelte
CHANGED
|
@@ -30,6 +30,7 @@ const handleKeyDown = (event) => {
|
|
|
30
30
|
if (!isDeletion && !isModifier && !isArrowKey && isInvalidCharacter)
|
|
31
31
|
event.preventDefault();
|
|
32
32
|
};
|
|
33
|
+
let inputTarget;
|
|
33
34
|
const currencyDecimal = new Intl.NumberFormat(locale).format(1.1).charAt(1); // '.' or ','
|
|
34
35
|
const isDecimalComma = currencyDecimal === ','; // Remove currency formatting from `formattedValue` so we can assign it to `value`
|
|
35
36
|
const currencySymbol = formatCurrency(0, 0)
|
|
@@ -45,6 +46,8 @@ const setUnformattedValue = (event) => {
|
|
|
45
46
|
const strippedUnformattedValue = formattedValue.replace(' ', '');
|
|
46
47
|
if (ignoreSymbols.includes(strippedUnformattedValue))
|
|
47
48
|
return;
|
|
49
|
+
// Set the starting caret positions
|
|
50
|
+
inputTarget = event.target;
|
|
48
51
|
// Remove all characters that arent: numbers, commas, periods (or minus signs if `isNegativeAllowed`)
|
|
49
52
|
let unformattedValue = isNegativeAllowed
|
|
50
53
|
? formattedValue.replace(/[^0-9,.-]/g, '')
|
|
@@ -65,7 +68,18 @@ const setUnformattedValue = (event) => {
|
|
|
65
68
|
}
|
|
66
69
|
};
|
|
67
70
|
const setFormattedValue = () => {
|
|
71
|
+
// Previous caret position
|
|
72
|
+
const startCaretPosition = inputTarget?.selectionStart || 0;
|
|
73
|
+
const previousFormattedValueLength = formattedValue.length;
|
|
74
|
+
// Apply formatting to input
|
|
68
75
|
formattedValue = isZero ? '' : formatCurrency(value, fractionDigits, 0);
|
|
76
|
+
// New caret position
|
|
77
|
+
const endCaretPosition = startCaretPosition + formattedValue.length - previousFormattedValueLength;
|
|
78
|
+
// HACK:
|
|
79
|
+
// Delay setting the new caret position until the input has been formatted
|
|
80
|
+
setTimeout(() => {
|
|
81
|
+
inputTarget?.setSelectionRange(endCaretPosition, endCaretPosition);
|
|
82
|
+
}, 0.1);
|
|
69
83
|
};
|
|
70
84
|
let formattedValue = '';
|
|
71
85
|
let formattedPlaceholder = placeholder !== null ? formatCurrency(placeholder, fractionDigits, fractionDigits) : '';
|
package/README.md
CHANGED
|
@@ -105,7 +105,7 @@ The [default styles](https://github.com/canutin/svelte-currency-input/blob/main/
|
|
|
105
105
|
Here's ways in which you can contribute:
|
|
106
106
|
|
|
107
107
|
- Found a bug? Open a [new issue](https://github.com/canutin/svelte-currency-input/issues/new)
|
|
108
|
-
-
|
|
108
|
+
- Comment or upvote [existing issues](https://github.com/canutin/svelte-currency-input/issues)
|
|
109
109
|
- Submit a [pull request](https://github.com/canutin/svelte-currency-input/pulls)
|
|
110
110
|
|
|
111
111
|
## Developing
|