浏览代码

Allow disabling x-mask with a `falsy` value (#2945)

* allow disabling x-mask with a `falsy` value

* add tests

* allow string falsy value
stepanjakl 3 年之前
父节点
当前提交
763e287842
共有 2 个文件被更改,包括 28 次插入1 次删除
  1. 3 0
      packages/mask/src/index.js
  2. 25 1
      tests/cypress/integration/plugins/mask.spec.js

+ 3 - 0
packages/mask/src/index.js

@@ -48,6 +48,9 @@ export default function (Alpine) {
 
             let template = templateFn(input)
 
+            // If a template value is `falsy`, then don't process the input value
+            if(!template || template === 'false') return false
+
             // If they hit backspace, don't process input.
             if (lastInputValue.length - el.value.length === 1) {
                 return lastInputValue = el.value

+ 25 - 1
tests/cypress/integration/plugins/mask.spec.js

@@ -60,6 +60,30 @@ test('x-mask with x-model',
     },
 )
 
+test('x-mask with a falsy input',
+    [html`<input x-data x-mask="">`],
+    ({ get }) => {
+	    get('input').type('1').should(haveValue('1'))
+	    get('input').type('2').should(haveValue('12'))
+	    get('input').type('ua').should(haveValue('12ua'))
+	    get('input').type('/').should(haveValue('12ua/'))
+	    get('input').type('cs').should(haveValue('12ua/cs'))
+	    get('input').type('  3').should(haveValue('12ua/cs  3'))
+    }
+)
+
+test('x-mask with a falsy string input',
+    [html`<input x-data x-mask="false">`],
+    ({ get }) => {
+	    get('input').type('1').should(haveValue('1'))
+	    get('input').type('2').should(haveValue('12'))
+	    get('input').type('ua').should(haveValue('12ua'))
+	    get('input').type('/').should(haveValue('12ua/'))
+	    get('input').type('cs').should(haveValue('12ua/cs'))
+	    get('input').type('  3').should(haveValue('12ua/cs  3'))
+    }
+)
+
 test('x-mask with non wildcard alpha-numeric characters (b)',
     [html`<input x-data x-mask="ba9*b">`],
     ({ get }) => {
@@ -68,7 +92,7 @@ test('x-mask with non wildcard alpha-numeric characters (b)',
         get('input').type('3').should(haveValue('ba3'))
         get('input').type('z').should(haveValue('ba3zb'))
         get('input').type('{backspace}{backspace}4').should(haveValue('ba34b'))
-    },
+    }
 )
 
 test('x-mask:dynamic',