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