2
0
mirror of https://github.com/tenrok/maska.git synced 2026-06-20 20:00:34 +03:00

fix: rework updating value

This commit is contained in:
Alexander Shabunevich
2024-04-06 11:59:33 +03:00
parent 81a748e891
commit 150ee77c48
2 changed files with 17 additions and 8 deletions
+3 -2
View File
@@ -47,7 +47,8 @@ export const vMaska: MaskaDirective = (el, binding) => {
} else { } else {
mask = new MaskInput(input, opts) mask = new MaskInput(input, opts)
masks.set(input, mask) masks.set(input, mask)
// delay for possible v-model change
setTimeout(() => mask?.checkValue(input))
} }
// delay for possible v-model change
setTimeout(() => mask?.updateValue(input))
} }
+14 -6
View File
@@ -19,15 +19,18 @@ export interface MaskaDetail {
export class MaskInput { export class MaskInput {
readonly items = new Map<HTMLInputElement, Mask>() readonly items = new Map<HTMLInputElement, Mask>()
constructor (target: MaskaTarget, readonly options: MaskInputOptions = {}) { constructor (target: MaskaTarget, private options: MaskInputOptions = {}) {
this.init(this.getInputs(target), this.getOptions(options)) this.init(this.getInputs(target))
} }
update (options: MaskInputOptions = {}): void { update (options: MaskInputOptions = {}): void {
this.init(Array.from(this.items.keys()), this.getOptions(options)) const needUpdate = JSON.stringify(options) !== JSON.stringify(this.options)
this.options = options
this.init(Array.from(this.items.keys()), needUpdate)
} }
checkValue (input: HTMLInputElement) { updateValue (input: HTMLInputElement) {
if (input.value && input.value !== this.process(input).masked) { if (input.value && input.value !== this.process(input).masked) {
this.setMaskedValue(input, input.value) this.setMaskedValue(input, input.value)
} }
@@ -41,7 +44,9 @@ export class MaskInput {
this.items.clear() this.items.clear()
} }
private init (inputs: HTMLInputElement[], defaults: MaskOptions): void { private init (inputs: HTMLInputElement[], update = true): void {
const defaults = this.getOptions(this.options)
for (const input of inputs) { for (const input of inputs) {
if (!this.items.has(input)) { if (!this.items.has(input)) {
input.addEventListener('input', this.inputEvent) input.addEventListener('input', this.inputEvent)
@@ -49,7 +54,10 @@ export class MaskInput {
} }
this.items.set(input, new Mask(parseInput(input, defaults))) this.items.set(input, new Mask(parseInput(input, defaults)))
this.checkValue(input)
if (update) {
this.updateValue(input)
}
} }
} }