From c86fcff13a3fcc0244442db5a46dbaf9d659bae7 Mon Sep 17 00:00:00 2001 From: Jonathan Martins Date: Sat, 27 Jan 2024 08:02:38 -0300 Subject: [PATCH] #88 - refactoring --- src/Utils.ts | 14 ---------- src/directive.ts | 71 +++++++++++++++++++++++++++++------------------- 2 files changed, 43 insertions(+), 42 deletions(-) diff --git a/src/Utils.ts b/src/Utils.ts index f917e1c..2657602 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -134,17 +134,3 @@ export function event(name: string): Event { export function debug({ debug = false }: VMoneyOptions | ExtractPropTypes, ...args: any): void { if (debug) console.log(...args); } - -export const getValidatedInputElement = (el: HTMLInputElement): HTMLInputElement => { - // v-money3 used on a component that's not an input - if (el.tagName.toLocaleUpperCase() !== 'INPUT') { - const els = el.getElementsByTagName('input'); - if (els.length !== 1) { - throw new Error(`v-money3 requires 1 input, found ${els.length} elements.`); - } else { - // eslint-disable-next-line prefer-destructuring - return els[0]; - } - } - return el; -}; diff --git a/src/directive.ts b/src/directive.ts index e78f953..33d76e1 100644 --- a/src/directive.ts +++ b/src/directive.ts @@ -7,7 +7,6 @@ import { setCursor, validateRestrictedOptions, event, - getValidatedInputElement, } from './Utils'; import format from './format'; import unformat from './unformat'; @@ -16,8 +15,11 @@ import defaults, { VMoneyOptions } from './options'; // this option is used for ALL directive instances // let opt: VMoneyOptions = defaults; -// eslint-disable-next-line max-len -const setValue = (el: HTMLInputElement, opt: VMoneyOptions | ExtractPropTypes, caller: string) => { +const setValue = ( + el: HTMLInputElement, + opt: VMoneyOptions | ExtractPropTypes, + caller: string, +) => { debug(opt, 'directive setValue() - caller', caller); if (!validateRestrictedOptions(opt)) { @@ -93,29 +95,50 @@ const onFocus = (e: Event, opt: VMoneyOptions | ExtractPropTypes) => { } }; +const getValidatedInputElement = (el: HTMLInputElement): HTMLInputElement => { + // v-money3 used on a component that's not an input + if (el.tagName.toLocaleUpperCase() !== 'INPUT') { + const els = el.getElementsByTagName('input'); + if (els.length !== 1) { + throw new Error(`v-money3 requires 1 input, found ${els.length} elements.`); + } else { + // eslint-disable-next-line prefer-destructuring + return els[0]; + } + } + return el; +}; + +const registerListeners = (el: HTMLInputElement, opt: VMoneyOptions | ExtractPropTypes) => { + el.onkeydown = (e: KeyboardEvent) => { + onKeyDown(e, opt); + }; + + el.oninput = (e: Event) => { + onInput(e, opt); + }; + + el.onfocus = (e: Event) => { + onFocus(e, opt); + }; +}; + export default { mounted(el: HTMLInputElement, binding: DirectiveBinding): void { if (!binding.value) { return; } - const opt = filterOptRestrictions({ ...defaults, ...binding.value }); + const opt: VMoneyOptions | ExtractPropTypes = filterOptRestrictions({ + ...defaults, + ...binding.value, + }); debug(opt, 'directive mounted() - opt', opt); el = getValidatedInputElement(el); - el.onkeydown = (e: KeyboardEvent) => { - onKeyDown(e, opt); - }; - - el.oninput = (e: Event) => { - onInput(e, opt); - }; - - el.onfocus = (e: Event) => { - onFocus(e, opt); - }; + registerListeners(el, opt); debug(opt, 'directive mounted() - el.value', el.value); setValue(el, opt, 'directive mounted'); @@ -124,22 +147,14 @@ export default { if (!binding.value) { return; } - const opt = filterOptRestrictions({ ...defaults, ...binding.value }); - - el.onkeydown = (e: KeyboardEvent) => { - onKeyDown(e, opt); - }; - el.oninput = (e: Event) => { - onInput(e, opt); - }; + const opt = filterOptRestrictions({ + ...defaults, + ...binding.value, + }); - el.onfocus = (e: Event) => { - onFocus(e, opt); - }; - - debug(opt, 'directive updated() - el.value', el.value); debug(opt, 'directive updated() - opt', opt); + debug(opt, 'directive updated() - el.value', el.value); setValue(el, opt, 'directive updated'); }, beforeUnmount(el: HTMLInputElement): void {