mirror of
https://github.com/tenrok/maska.git
synced 2026-06-17 19:21:21 +03:00
style: code format
This commit is contained in:
+6
-6
@@ -7,7 +7,7 @@ const masks = new WeakMap<HTMLInputElement, MaskInput>()
|
|||||||
|
|
||||||
// hacky way to update binding.arg without using defineExposed
|
// hacky way to update binding.arg without using defineExposed
|
||||||
const setArg = (binding: DirectiveBinding, value: string | boolean) => {
|
const setArg = (binding: DirectiveBinding, value: string | boolean) => {
|
||||||
if (!binding.arg || !binding.instance) return
|
if (!binding.arg || (binding.instance == null)) return
|
||||||
|
|
||||||
const inst = binding.instance as any
|
const inst = binding.instance as any
|
||||||
if (binding.arg in inst) {
|
if (binding.arg in inst) {
|
||||||
@@ -28,8 +28,8 @@ export const vMaska: MaskaDirective = (el, binding) => {
|
|||||||
const value = binding.modifiers.unmasked
|
const value = binding.modifiers.unmasked
|
||||||
? detail.unmasked
|
? detail.unmasked
|
||||||
: binding.modifiers.completed
|
: binding.modifiers.completed
|
||||||
? detail.completed
|
? detail.completed
|
||||||
: detail.masked
|
: detail.masked
|
||||||
setArg(binding, value)
|
setArg(binding, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,12 +37,12 @@ export const vMaska: MaskaDirective = (el, binding) => {
|
|||||||
opts.onMaska == null
|
opts.onMaska == null
|
||||||
? updateArg
|
? updateArg
|
||||||
: Array.isArray(opts.onMaska)
|
: Array.isArray(opts.onMaska)
|
||||||
? [...opts.onMaska, updateArg]
|
? [...opts.onMaska, updateArg]
|
||||||
: [opts.onMaska, updateArg]
|
: [opts.onMaska, updateArg]
|
||||||
}
|
}
|
||||||
|
|
||||||
let mask = masks.get(input)
|
let mask = masks.get(input)
|
||||||
if (mask) {
|
if (mask != null) {
|
||||||
mask.update(opts)
|
mask.update(opts)
|
||||||
} else {
|
} else {
|
||||||
mask = new MaskInput(input, opts)
|
mask = new MaskInput(input, opts)
|
||||||
|
|||||||
+10
-10
@@ -19,21 +19,21 @@ 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, readonly options: MaskInputOptions = {}) {
|
||||||
this.init(this.getInputs(target), this.getOptions(options))
|
this.init(this.getInputs(target), this.getOptions(options))
|
||||||
}
|
}
|
||||||
|
|
||||||
update(options: MaskInputOptions = {}): void {
|
update (options: MaskInputOptions = {}): void {
|
||||||
this.init(Array.from(this.items.keys()), this.getOptions(options))
|
this.init(Array.from(this.items.keys()), this.getOptions(options))
|
||||||
}
|
}
|
||||||
|
|
||||||
checkValue(input: HTMLInputElement) {
|
checkValue (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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy(): void {
|
destroy (): void {
|
||||||
for (const input of this.items.keys()) {
|
for (const input of this.items.keys()) {
|
||||||
input.removeEventListener('input', this.inputEvent)
|
input.removeEventListener('input', this.inputEvent)
|
||||||
input.removeEventListener('beforeinput', this.beforeinputEvent)
|
input.removeEventListener('beforeinput', this.beforeinputEvent)
|
||||||
@@ -41,7 +41,7 @@ export class MaskInput {
|
|||||||
this.items.clear()
|
this.items.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
private init(inputs: HTMLInputElement[], defaults: MaskOptions): void {
|
private init (inputs: HTMLInputElement[], defaults: MaskOptions): void {
|
||||||
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)
|
||||||
@@ -53,7 +53,7 @@ export class MaskInput {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private getInputs(target: MaskaTarget): HTMLInputElement[] {
|
private getInputs (target: MaskaTarget): HTMLInputElement[] {
|
||||||
if (typeof target === 'string') {
|
if (typeof target === 'string') {
|
||||||
return Array.from(document.querySelectorAll(target))
|
return Array.from(document.querySelectorAll(target))
|
||||||
}
|
}
|
||||||
@@ -61,7 +61,7 @@ export class MaskInput {
|
|||||||
return 'length' in target ? Array.from(target) : [target]
|
return 'length' in target ? Array.from(target) : [target]
|
||||||
}
|
}
|
||||||
|
|
||||||
private getOptions(options: MaskInputOptions): MaskOptions {
|
private getOptions (options: MaskInputOptions): MaskOptions {
|
||||||
const { onMaska, preProcess, postProcess, ...opts } = options
|
const { onMaska, preProcess, postProcess, ...opts } = options
|
||||||
|
|
||||||
return opts
|
return opts
|
||||||
@@ -107,7 +107,7 @@ export class MaskInput {
|
|||||||
this.updateCursor(e, selection, value)
|
this.updateCursor(e, selection, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateCursor(e: Event | InputEvent, s: number | null, value: string) {
|
private updateCursor (e: Event | InputEvent, s: number | null, value: string) {
|
||||||
if (!('inputType' in e) || s === null) return
|
if (!('inputType' in e) || s === null) return
|
||||||
|
|
||||||
const input = e.target as HTMLInputElement
|
const input = e.target as HTMLInputElement
|
||||||
@@ -120,7 +120,7 @@ export class MaskInput {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private setMaskedValue(input: HTMLInputElement, value: string): void {
|
private setMaskedValue (input: HTMLInputElement, value: string): void {
|
||||||
const detail = this.process(input, value)
|
const detail = this.process(input, value)
|
||||||
|
|
||||||
input.value = detail.masked
|
input.value = detail.masked
|
||||||
@@ -137,7 +137,7 @@ export class MaskInput {
|
|||||||
input.dispatchEvent(new CustomEvent('input', { detail: detail.masked }))
|
input.dispatchEvent(new CustomEvent('input', { detail: detail.masked }))
|
||||||
}
|
}
|
||||||
|
|
||||||
private process(input: HTMLInputElement, value?: string): MaskaDetail {
|
private process (input: HTMLInputElement, value?: string): MaskaDetail {
|
||||||
const mask = this.items.get(input) as Mask
|
const mask = this.items.get(input) as Mask
|
||||||
let valueNew = value ?? input.value
|
let valueNew = value ?? input.value
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user