Skip to content

Commit

Permalink
Do not send the 'input' event when the maskedvalue is the same as the…
Browse files Browse the repository at this point in the history
… unmasked value

Fixes #2
  • Loading branch information
titou10titou10 committed Sep 20, 2019
1 parent 2132e46 commit 1b2d87c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
12 changes: 7 additions & 5 deletions dist/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ function getConfig(binding) {
}
function run(el, eventName, config, vnode) {
// Handle when initial value is not set
var val = el.value === 'undefined' ? '' : el.value;
var beforeValue = el.value === 'undefined' ? '' : el.value;
var position = el.selectionEnd;
// save the character just inserted
var digit = val[position - 1];
el.value = masker_1["default"](val, config.mask, config.masked, config.tokens);
var digit = beforeValue[position - 1];
el.value = masker_1["default"](beforeValue, config.mask, config.masked, config.tokens);
// if the digit was changed, increment position until find the digit again
while (position < el.value.length &&
el.value.charAt(position - 1) !== digit) {
Expand Down Expand Up @@ -81,8 +81,10 @@ function run(el, eventName, config, vnode) {
}
}
}
// Notify listeners
el.dispatchEvent(event(eventName));
// Notify listeners only if value changed (ie send an extra 'input' event)
if (beforeValue !== el.value) {
el.dispatchEvent(event(eventName));
}
}
// Vue.js directive hooks
function bind(el, binding, vnode) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@titou10/v-mask",
"description": "mask directive for vue.js that exposes the unmasked value",
"version": "1.0.4",
"version": "1.0.5",
"author": "Denis Forveille <titou10.titou10@gmail.com>",
"license": "MIT",
"keywords": [
Expand Down
12 changes: 7 additions & 5 deletions src/directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ function getConfig(binding) {
function run(el , eventName: string, config, vnode) {

// Handle when initial value is not set
const val = el.value === 'undefined' ? '' : el.value;
const beforeValue = el.value === 'undefined' ? '' : el.value;

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

// Notify listeners
el.dispatchEvent(event(eventName));
// Notify listeners only if value changed (ie send an extra 'input' event)
if (beforeValue !== el.value) {
el.dispatchEvent(event(eventName));
}
}

// Vue.js directive hooks
Expand Down

0 comments on commit 1b2d87c

Please sign in to comment.