Commit 20e70347 authored by Roma Nikulin's avatar Roma Nikulin

Mask as object feature added

Prevent useless initMask calls
parent af38d151
...@@ -23,23 +23,45 @@ The following format characters define editable parts of the mask (see [inputmas ...@@ -23,23 +23,45 @@ The following format characters define editable parts of the mask (see [inputmas
* `#` - alphanumeric, forced to upper case when entered * `#` - alphanumeric, forced to upper case when entered
* `+` - any character * `+` - any character
### Static characters
If you need to include one of these characters as a static part of the mask, you can escape them with a preceding backslash: If you need to include one of these characters as a static part of the mask, you can escape them with a preceding backslash:
```vue ```vue
<masked-input v-model="phone" mask="\+\1 (111) 111-1111" placeholder="Phone number" type="tel" /> <masked-input v-model="phone" mask="\+\1 (111) 111-1111" placeholder="Phone number" type="tel" />
``` ```
### Raw input
You can also get a raw user input text if you want. Instead of using v-model you might need second argument of the input event: You can also get a raw user input text if you want. Instead of using v-model you might need second argument of the input event:
```vue ```vue
<masked-input mask="\+\1 (111) 1111-11" placeholder="Phone" @input="rawVal = arguments[1]" /> <masked-input mask="\+\1 (111) 1111-11" placeholder="Phone" @input="rawVal = arguments[1]" />
``` ```
### Placeholder character
Placeholder character is customizable by `placeholder-char` attribute: Placeholder character is customizable by `placeholder-char` attribute:
```vue ```vue
<masked-input v-model="phone" mask="\+\1 (111) 111-1111" placeholder-char="-" placeholder="Phone number" type="tel" /> <masked-input v-model="phone" mask="\+\1 (111) 111-1111" placeholder-char="-" placeholder="Phone number" type="tel" />
``` ```
### Custom mask object
You can use your own mask options object, it will be passed to the [inputmask-core](https://github.com/insin/inputmask-core#inputmask-options) constructor:
```vue
<masked-input
v-model="custom"
placeholder="Custom"
:mask="{
pattern: 'VVVV',
formatCharacters: {
'V': {
validate: char => /[a-jA-J]/.test(char),
transform: char => char.toUpperCase(),
},
},
}"
/>
```
## Known issues/TODO ## Known issues/TODO
* Cut in mobile Chrome * Cut in mobile Chrome
* Cyrillic chars are not supported in mobile Chrome * Cyrillic chars are not supported in mobile Chrome
* Backspace problems in mobile Chrome
Found more? It's open for feedback and pull requests Found more? It's open for feedback and pull requests
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -44,10 +44,9 @@ export default { ...@@ -44,10 +44,9 @@ export default {
type: String type: String
}, },
mask: { mask: {
type: String,
required: true, required: true,
validator: function validator(value) { validator: function validator(value) {
return !!(value && value.length >= 1); return !!(value && value.length >= 1 || value instanceof Object);
} }
}, },
placeholderChar: { placeholderChar: {
...@@ -64,8 +63,10 @@ export default { ...@@ -64,8 +63,10 @@ export default {
}, },
watch: { watch: {
mask: function mask() { mask: function mask(newValue, oldValue) {
if (JSON.stringify(newValue) !== JSON.stringify(oldValue)) {
this.initMask(); this.initMask();
}
}, },
value: function value(newValue) { value: function value(newValue) {
if (this.maskCore) this.maskCore.setValue(newValue); // For multiple inputs support if (this.maskCore) this.maskCore.setValue(newValue); // For multiple inputs support
...@@ -82,6 +83,9 @@ export default { ...@@ -82,6 +83,9 @@ export default {
var _this = this; var _this = this;
try { try {
if (this.mask instanceof Object) {
this.maskCore = new InputMask(this.mask);
} else {
this.maskCore = new InputMask({ this.maskCore = new InputMask({
pattern: this.mask, pattern: this.mask,
value: '', value: '',
...@@ -125,6 +129,7 @@ export default { ...@@ -125,6 +129,7 @@ export default {
} }
} }
}); });
}
[].concat(_toConsumableArray(this.$refs.input.value)).reduce(function (memo, item) { [].concat(_toConsumableArray(this.$refs.input.value)).reduce(function (memo, item) {
return _this.maskCore.input(item); return _this.maskCore.input(item);
}, null); }, null);
......
{ {
"name": "vue-masked-input", "name": "vue-masked-input",
"description": "Masked input component for Vue.js 2.X", "description": "Masked input component for Vue.js 2.X",
"version": "0.5.1", "version": "0.5.2",
"author": "niksmr", "author": "niksmr",
"license": "MIT", "license": "MIT",
"homepage": "https://github.com/niksmr/vue-masked-input", "homepage": "https://github.com/niksmr/vue-masked-input",
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
</ul> </ul>
<h4>Date: </h4> <h4>Date: </h4>
<masked-input v-model="date" mask="11 / 11 / 1111" placeholder="Date"/><span>{{ date }}</span> <masked-input v-model="date" mask="11 / 11 / 1111" placeholder="Date" /><span>{{ date }}</span>
<p class="code"> <p class="code">
&lt;masked-input v-model="date" mask="11 / 11 / 1111" placeholder="Date" /&gt; &lt;masked-input v-model="date" mask="11 / 11 / 1111" placeholder="Date" /&gt;
</p> </p>
......
...@@ -40,7 +40,7 @@ export default { ...@@ -40,7 +40,7 @@ export default {
}, },
mask: { mask: {
required: true, required: true,
validator: value => !! ((value && value.length >= 1) || value instanceof Object) validator: value => !!((value && value.length >= 1) || value instanceof Object),
}, },
placeholderChar: { placeholderChar: {
type: String, type: String,
...@@ -54,8 +54,10 @@ export default { ...@@ -54,8 +54,10 @@ export default {
}, },
watch: { watch: {
mask() { mask(newValue, oldValue) {
if (JSON.stringify(newValue) !== JSON.stringify(oldValue)) {
this.initMask(); this.initMask();
}
}, },
value(newValue) { value(newValue) {
if (this.maskCore) this.maskCore.setValue(newValue); // For multiple inputs support if (this.maskCore) this.maskCore.setValue(newValue); // For multiple inputs support
...@@ -70,7 +72,7 @@ export default { ...@@ -70,7 +72,7 @@ export default {
initMask() { initMask() {
try { try {
if (this.mask instanceof Object) { if (this.mask instanceof Object) {
this.maskСore = new InputMask(this.mask) this.maskCore = new InputMask(this.mask);
} else { } else {
this.maskCore = new InputMask({ this.maskCore = new InputMask({
pattern: this.mask, pattern: this.mask,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment