From 1b2d87ceb18b2ad4d6f5c87499461a2b735d66f6 Mon Sep 17 00:00:00 2001 From: Denis Forveille Date: Fri, 20 Sep 2019 08:50:38 -0400 Subject: [PATCH] Do not send the 'input' event when the maskedvalue is the same as the unmasked value Fixes #2 --- dist/directive.js | 12 +++++++----- package-lock.json | 2 +- package.json | 2 +- src/directive.ts | 12 +++++++----- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/dist/directive.js b/dist/directive.js index 757913a..58f43db 100644 --- a/dist/directive.js +++ b/dist/directive.js @@ -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) { @@ -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) { diff --git a/package-lock.json b/package-lock.json index 67a4d6f..9065a6a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@titou10/v-mask", - "version": "1.0.4", + "version": "1.0.5", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 600a688..ed176f0 100644 --- a/package.json +++ b/package.json @@ -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 ", "license": "MIT", "keywords": [ diff --git a/src/directive.ts b/src/directive.ts index 3997b39..6be65f3 100644 --- a/src/directive.ts +++ b/src/directive.ts @@ -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) { @@ -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