@canutin/svelte-currency-input 0.11.3 → 0.11.4

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.
@@ -50,8 +50,13 @@ const handleKeyDown = (event) => {
50
50
  if (isPunctuationDuplicated() || !isDeletion && !isModifier && !isArrowKey && isInvalidCharacter && !isTab)
51
51
  event.preventDefault();
52
52
  };
53
- const handleOnBlur = () => setFormattedValue(true);
54
- onMount(() => setFormattedValue(true));
53
+ const handleOnBlur = () => setFormattedValue();
54
+ let dom;
55
+ let inputElement;
56
+ onMount(() => {
57
+ dom = document;
58
+ setFormattedValue();
59
+ });
55
60
  let inputTarget;
56
61
  const currencyDecimal = new Intl.NumberFormat(locale).format(1.1).charAt(1);
57
62
  const isDecimalComma = currencyDecimal === ",";
@@ -88,17 +93,23 @@ const setUnformattedValue = (event) => {
88
93
  }
89
94
  }
90
95
  };
91
- const setFormattedValue = (hasMinFractionDigits) => {
92
- const startCaretPosition = inputTarget?.selectionStart || 0;
96
+ const setFormattedValue = () => {
97
+ if (!dom)
98
+ return;
99
+ const startCaretPosition = inputElement?.selectionStart || 0;
93
100
  const previousFormattedValueLength = formattedValue.length;
94
- formattedValue = isZero && !isZeroNullish ? "" : formatCurrency(value, fractionDigits, hasMinFractionDigits ? fractionDigits : 0);
101
+ formattedValue = isZero && !isZeroNullish ? "" : formatCurrency(
102
+ value,
103
+ fractionDigits,
104
+ dom.activeElement === inputElement ? 0 : fractionDigits
105
+ );
95
106
  setUnformattedValue();
96
107
  let retries = 0;
97
108
  while (previousFormattedValueLength === formattedValue.length && retries < 10)
98
109
  retries++;
99
110
  if (previousFormattedValueLength !== formattedValue.length) {
100
111
  const endCaretPosition = startCaretPosition + formattedValue.length - previousFormattedValueLength;
101
- inputTarget?.setSelectionRange(endCaretPosition, endCaretPosition);
112
+ inputElement?.setSelectionRange(endCaretPosition, endCaretPosition);
102
113
  }
103
114
  onValueChange(value);
104
115
  };
@@ -140,12 +151,13 @@ $:
140
151
  : ''}
141
152
  "
142
153
  type="text"
143
- inputmode={fractionDigits > 0 ? "decimal" : "numeric"}
154
+ inputmode={fractionDigits > 0 ? 'decimal' : 'numeric'}
144
155
  name={`formatted-${name}`}
145
156
  required={required && !isZero}
146
157
  placeholder={handlePlaceholder(placeholder)}
147
158
  {autocomplete}
148
159
  {disabled}
160
+ bind:this={inputElement}
149
161
  bind:value={formattedValue}
150
162
  on:keydown={handleKeyDown}
151
163
  on:keyup={setUnformattedValue}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canutin/svelte-currency-input",
3
- "version": "0.11.3",
3
+ "version": "0.11.4",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "vite build",