mirror of
https://github.com/tenrok/maska.git
synced 2026-06-02 16:04:05 +03:00
refactor: always update value inside init
To universal process of value change not only in v-model, but in all cases
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
</head>
|
||||
<body>
|
||||
<div id="app">
|
||||
<input data-maska="#-#">
|
||||
<input data-maska="#-#" value="123">
|
||||
</div>
|
||||
|
||||
<script type="module">
|
||||
|
||||
@@ -24,10 +24,8 @@ export class MaskInput {
|
||||
}
|
||||
|
||||
update (options: MaskInputOptions = {}): void {
|
||||
const needUpdate = JSON.stringify(options) !== JSON.stringify(this.options)
|
||||
this.options = options
|
||||
|
||||
this.init(Array.from(this.items.keys()), needUpdate)
|
||||
this.options = { ...options }
|
||||
this.init(Array.from(this.items.keys()))
|
||||
}
|
||||
|
||||
updateValue (input: HTMLInputElement): void {
|
||||
@@ -43,7 +41,7 @@ export class MaskInput {
|
||||
this.items.clear()
|
||||
}
|
||||
|
||||
private init (inputs: HTMLInputElement[], update = true): void {
|
||||
private init (inputs: HTMLInputElement[]): void {
|
||||
const defaults = this.getOptions(this.options)
|
||||
|
||||
for (const input of inputs) {
|
||||
@@ -54,12 +52,10 @@ export class MaskInput {
|
||||
const mask = new Mask(parseInput(input, defaults))
|
||||
this.items.set(input, mask)
|
||||
|
||||
if (update) {
|
||||
this.updateValue(input)
|
||||
queueMicrotask(() => this.updateValue(input))
|
||||
|
||||
if (input.selectionStart === null && mask.isEager()) {
|
||||
console.warn('Maska: input of `%s` type is not supported', input.type)
|
||||
}
|
||||
if (input.selectionStart === null && mask.isEager()) {
|
||||
console.warn('Maska: input of `%s` type is not supported', input.type)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,14 +43,9 @@ export const vMaska: MaskaDirective = (el, binding) => {
|
||||
: [opts.onMaska, updateArg]
|
||||
}
|
||||
|
||||
let mask = masks.get(input)
|
||||
if (mask != null) {
|
||||
mask.update(opts)
|
||||
if (masks.has(input)) {
|
||||
masks.get(input)?.update(opts)
|
||||
} else {
|
||||
mask = new MaskInput(input, opts)
|
||||
masks.set(input, mask)
|
||||
masks.set(input, new MaskInput(input, opts))
|
||||
}
|
||||
|
||||
// delay for possible v-model change
|
||||
queueMicrotask(() => mask?.updateValue(input))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user