Skip to content

Commit 1b2d87c

Browse files
Do not send the 'input' event when the maskedvalue is the same as the unmasked value
Fixes #2
1 parent 2132e46 commit 1b2d87c

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

dist/directive.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ function getConfig(binding) {
4747
}
4848
function run(el, eventName, config, vnode) {
4949
// Handle when initial value is not set
50-
var val = el.value === 'undefined' ? '' : el.value;
50+
var beforeValue = el.value === 'undefined' ? '' : el.value;
5151
var position = el.selectionEnd;
5252
// save the character just inserted
53-
var digit = val[position - 1];
54-
el.value = masker_1["default"](val, config.mask, config.masked, config.tokens);
53+
var digit = beforeValue[position - 1];
54+
el.value = masker_1["default"](beforeValue, config.mask, config.masked, config.tokens);
5555
// if the digit was changed, increment position until find the digit again
5656
while (position < el.value.length &&
5757
el.value.charAt(position - 1) !== digit) {
@@ -81,8 +81,10 @@ function run(el, eventName, config, vnode) {
8181
}
8282
}
8383
}
84-
// Notify listeners
85-
el.dispatchEvent(event(eventName));
84+
// Notify listeners only if value changed (ie send an extra 'input' event)
85+
if (beforeValue !== el.value) {
86+
el.dispatchEvent(event(eventName));
87+
}
8688
}
8789
// Vue.js directive hooks
8890
function bind(el, binding, vnode) {

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@titou10/v-mask",
33
"description": "mask directive for vue.js that exposes the unmasked value",
4-
"version": "1.0.4",
4+
"version": "1.0.5",
55
"author": "Denis Forveille <[email protected]>",
66
"license": "MIT",
77
"keywords": [

src/directive.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ function getConfig(binding) {
5151
function run(el , eventName: string, config, vnode) {
5252

5353
// Handle when initial value is not set
54-
const val = el.value === 'undefined' ? '' : el.value;
54+
const beforeValue = el.value === 'undefined' ? '' : el.value;
5555

5656
let position = el.selectionEnd;
5757
// save the character just inserted
58-
const digit = val[position - 1];
59-
el.value = masker(val, config.mask, config.masked, config.tokens);
58+
const digit = beforeValue[position - 1];
59+
el.value = masker(beforeValue, config.mask, config.masked, config.tokens);
6060
// if the digit was changed, increment position until find the digit again
6161
while (position < el.value.length &&
6262
el.value.charAt(position - 1) !== digit) {
@@ -88,8 +88,10 @@ function run(el , eventName: string, config, vnode) {
8888
}
8989
}
9090

91-
// Notify listeners
92-
el.dispatchEvent(event(eventName));
91+
// Notify listeners only if value changed (ie send an extra 'input' event)
92+
if (beforeValue !== el.value) {
93+
el.dispatchEvent(event(eventName));
94+
}
9395
}
9496

9597
// Vue.js directive hooks

0 commit comments

Comments
 (0)