diff --git a/src/directive.ts b/src/directive.ts index 456b21b..e5b948d 100644 --- a/src/directive.ts +++ b/src/directive.ts @@ -1,13 +1,16 @@ import { Directive } from 'vue' import { MaskaDetail, MaskInput, MaskInputOptions } from './mask-input' -type MaskaDirective = Directive +type MaskaDirective = Directive const masks = new WeakMap() export const vMaska: MaskaDirective = (el, binding) => { - if (masks.get(el) != null) { - masks.get(el)?.destroy() + const input = el instanceof HTMLInputElement ? el : el.querySelector('input') + if (input == null) return + + if (masks.get(input) != null) { + masks.get(input)?.destroy() } const opts = { ...(binding.arg as MaskInputOptions) } ?? {} @@ -21,5 +24,5 @@ export const vMaska: MaskaDirective = (el, binding) => { } } - masks.set(el, new MaskInput(el, opts)) + masks.set(input, new MaskInput(input, opts)) } diff --git a/test/components/Parent.vue b/test/components/Parent.vue new file mode 100644 index 0000000..dd9f559 --- /dev/null +++ b/test/components/Parent.vue @@ -0,0 +1,13 @@ + + + diff --git a/test/directive.test.ts b/test/directive.test.ts index 7db945e..4b3daae 100644 --- a/test/directive.test.ts +++ b/test/directive.test.ts @@ -12,6 +12,7 @@ import Dynamic from './components/Dynamic.vue' import Events from './components/Events.vue' import Hooks from './components/Hooks.vue' import Options from './components/Options.vue' +import Parent from './components/Parent.vue' import Simple from './components/Simple.vue' test('simple directive', async () => { @@ -34,6 +35,17 @@ test('data-attr', async () => { expect(input.element.value).toBe('1-2') }) +test('parent element', async () => { + const wrapper = mount(Parent) + const input = wrapper.get('input') + + await input.setValue('1') + expect(input.element.value).toBe('1-') + + await input.setValue('123') + expect(input.element.value).toBe('1-2') +}) + test('dynamic mask', async () => { const wrapper = mount(Dynamic) const input = wrapper.get('input')