Merge pull request #3714 from primefaces/issue-3698

InputMask: Dynamic mask change issue fixed
pull/3743/head
Tuğçe Küçükoğlu 2023-03-13 11:55:04 +03:00 committed by GitHub
commit 892095f074
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 60 additions and 50 deletions

View File

@ -31,57 +31,15 @@ export default {
default: false
}
},
watch: {
mask(newMask, oldMask) {
if (oldMask !== newMask) {
this.initMask();
}
}
},
mounted() {
this.tests = [];
this.partialPosition = this.mask.length;
this.len = this.mask.length;
this.firstNonMaskPos = null;
this.defs = {
9: '[0-9]',
a: '[A-Za-z]',
'*': '[A-Za-z0-9]'
};
let ua = DomHandler.getUserAgent();
this.androidChrome = /chrome/i.test(ua) && /android/i.test(ua);
let maskTokens = this.mask.split('');
for (let i = 0; i < maskTokens.length; i++) {
let c = maskTokens[i];
if (c === '?') {
this.len--;
this.partialPosition = i;
} else if (this.defs[c]) {
this.tests.push(new RegExp(this.defs[c]));
if (this.firstNonMaskPos === null) {
this.firstNonMaskPos = this.tests.length - 1;
}
if (i < this.partialPosition) {
this.lastRequiredNonMaskPos = this.tests.length - 1;
}
} else {
this.tests.push(null);
}
}
this.buffer = [];
for (let i = 0; i < maskTokens.length; i++) {
let c = maskTokens[i];
if (c !== '?') {
if (this.defs[c]) this.buffer.push(this.getPlaceholder(i));
else this.buffer.push(c);
}
}
this.defaultBuffer = this.buffer.join('');
this.updateValue(false);
this.initMask();
},
updated() {
if (this.isValueUpdated()) {
@ -500,6 +458,58 @@ export default {
this.focusText = this.$el.value;
}
},
initMask() {
this.tests = [];
this.partialPosition = this.mask.length;
this.len = this.mask.length;
this.firstNonMaskPos = null;
this.defs = {
9: '[0-9]',
a: '[A-Za-z]',
'*': '[A-Za-z0-9]'
};
let ua = DomHandler.getUserAgent();
this.androidChrome = /chrome/i.test(ua) && /android/i.test(ua);
let maskTokens = this.mask.split('');
for (let i = 0; i < maskTokens.length; i++) {
let c = maskTokens[i];
if (c === '?') {
this.len--;
this.partialPosition = i;
} else if (this.defs[c]) {
this.tests.push(new RegExp(this.defs[c]));
if (this.firstNonMaskPos === null) {
this.firstNonMaskPos = this.tests.length - 1;
}
if (i < this.partialPosition) {
this.lastRequiredNonMaskPos = this.tests.length - 1;
}
} else {
this.tests.push(null);
}
}
this.buffer = [];
for (let i = 0; i < maskTokens.length; i++) {
let c = maskTokens[i];
if (c !== '?') {
if (this.defs[c]) this.buffer.push(this.getPlaceholder(i));
else this.buffer.push(c);
}
}
this.defaultBuffer = this.buffer.join('');
this.updateValue(false);
},
isValueUpdated() {
return this.unmask ? this.modelValue != this.getUnmaskedValue() : this.defaultBuffer !== this.$el.value && this.$el.value !== this.modelValue;
}