Quellcode durchsuchen

Fix x-mask $money with bad input

Caleb Porzio vor 3 Jahren
Ursprung
Commit
3e7ef77a87
2 geänderte Dateien mit 12 neuen und 2 gelöschten Zeilen
  1. 2 2
      packages/mask/src/index.js
  2. 10 0
      tests/cypress/integration/plugins/mask.spec.js

+ 2 - 2
packages/mask/src/index.js

@@ -183,8 +183,8 @@ function formatMoney(input, delimeter = '.', thousands) {
         return output
     }
 
-    let nothousands = input.replaceAll(thousands, '')
-    let template = Array.from({ length: nothousands.split(delimeter)[0].length }).fill('9').join('')
+    let strippedInput = input.replaceAll(new RegExp(`[^0-9\\${delimeter}]`, 'g'), '')
+    let template = Array.from({ length: strippedInput.split(delimeter)[0].length }).fill('9').join('')
 
     template = addThousands(template, thousands)
 

+ 10 - 0
tests/cypress/integration/plugins/mask.spec.js

@@ -119,3 +119,13 @@ test('$money swapping commas and periods',
         get('input').type(',89').should(haveValue('1.234.567,89'))
     },
 )
+
+test('$money works with permenant inserted at beginning',
+    [html`<input x-data x-mask:dynamic="$money">`],
+    ({ get }) => {
+        get('input').type('40.00').should(haveValue('40.00'))
+        get('input').type('{leftArrow}{leftArrow}{leftArrow}{leftArrow}{leftArrow}')
+        get('input').type('$')
+        get('input').should(haveValue('40.00'))
+    }
+)