mirror of
https://github.com/tenrok/maska.git
synced 2026-05-30 15:24:06 +03:00
fix: rework updating value
This commit is contained in:
+3
-2
@@ -47,7 +47,8 @@ export const vMaska: MaskaDirective = (el, binding) => {
|
||||
} else {
|
||||
mask = new MaskInput(input, opts)
|
||||
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
@@ -19,15 +19,18 @@ export interface MaskaDetail {
|
||||
export class MaskInput {
|
||||
readonly items = new Map<HTMLInputElement, Mask>()
|
||||
|
||||
constructor (target: MaskaTarget, readonly options: MaskInputOptions = {}) {
|
||||
this.init(this.getInputs(target), this.getOptions(options))
|
||||
constructor (target: MaskaTarget, private options: MaskInputOptions = {}) {
|
||||
this.init(this.getInputs(target))
|
||||
}
|
||||
|
||||
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) {
|
||||
this.setMaskedValue(input, input.value)
|
||||
}
|
||||
@@ -41,7 +44,9 @@ export class MaskInput {
|
||||
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) {
|
||||
if (!this.items.has(input)) {
|
||||
input.addEventListener('input', this.inputEvent)
|
||||
@@ -49,7 +54,10 @@ export class MaskInput {
|
||||
}
|
||||
|
||||
this.items.set(input, new Mask(parseInput(input, defaults)))
|
||||
this.checkValue(input)
|
||||
|
||||
if (update) {
|
||||
this.updateValue(input)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user