Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

[#511] Compare resolved old and new masks after component update when deciding if the value should be updated #512

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,17 @@ describe('directive usage', () => {
await wrapper.vm.$nextTick();
expect(wrapper.vm.$el.value).toBe('19:32');
});

it('should not trigger input events if resolved mask is the same as previous one', async () => {
const wrapper = mountWithMask({
props: { triggerUpdate: { required: false } },
data: () => ({ mask: () => [/\d/, /\d/], value: null }),
template: '<input v-mask="mask" v-model="value" v-on:input="$emit(\'input\', $event)" />',
});

await wrapper.setProps({ triggerUpdate: true });
expect(wrapper.emitted().input).toBeFalsy();
});
});

describe('filter usage', () => {
Expand Down
32 changes: 28 additions & 4 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ function triggerInputUpdate(el) {
trigger(el, 'input');
}

function updateElementWithMaskedValue(el, maskedValue) {
if (maskedValue === el.value) {
return;
}

el.value = maskedValue;
triggerInputUpdate(el);
}

/**
* Event handler
* @param {HTMLInputElement} el
Expand All @@ -35,8 +44,8 @@ function updateValue(el, force = false) {

if ((force || isUpdateNeeded) && mask) {
const { conformedValue } = conformToMask(value, mask, { guide: false });
el.value = conformedValue;
triggerInputUpdate(el);

updateElementWithMaskedValue(el, conformedValue);
}

options.partiallyUpdate(el, { previousValue: value });
Expand Down Expand Up @@ -95,6 +104,22 @@ function maskToString(mask) {
return filteredMaskArray.toString();
}

/**
* Check if previous mask has been different than current one
* @param {String|Array.<String|RegExp>} mask
*/
function hasMaskFromBindingChanged(el, oldValue, currentValue) {
const previousMask = isFunction(oldValue)
? maskToString(oldValue(options.get(el).previousValue))
: maskToString(oldValue);

const currentMask = isFunction(currentValue)
? maskToString(currentValue(el.value))
: maskToString(currentValue);

return previousMask !== currentMask;
}

/**
* Create the Vue directive
* @param {Object} directiveOptions
Expand Down Expand Up @@ -139,8 +164,7 @@ export function createDirective(directiveOptions = {}) {
componentUpdated(el, { value, oldValue }) {
el = queryInputElementInside(el);

const isMaskChanged = isFunction(value)
|| maskToString(oldValue) !== maskToString(value);
const isMaskChanged = hasMaskFromBindingChanged(el, oldValue, value);

if (isMaskChanged) {
updateMask(el, value, instanceMaskReplacers);
Expand Down