From e6b2799880c6c4c2519fd279bd4c2675b0e27022 Mon Sep 17 00:00:00 2001 From: Alexander Shabunevich Date: Tue, 11 Jun 2024 10:17:10 +0300 Subject: [PATCH] fix(vue): use defineExpose to set argument value --- docs/v3/vue.md | 26 ++++++++++++++------------ src/vue/index.ts | 12 ++++++------ test/vue/BindCompleted.vue | 8 +++++--- test/vue/BindMasked.vue | 8 +++++--- test/vue/BindModel.vue | 12 +++++++----- test/vue/BindUnmasked.vue | 8 +++++--- test/vue/Callbacks.vue | 2 ++ test/vue/Config.vue | 8 +++++--- test/vue/Sink.vue | 8 +++++--- 9 files changed, 54 insertions(+), 38 deletions(-) diff --git a/docs/v3/vue.md b/docs/v3/vue.md index c2c3d5a..608a814 100644 --- a/docs/v3/vue.md +++ b/docs/v3/vue.md @@ -112,18 +112,22 @@ To get masked value you can use standard `v-model` directive on the input. But i import { ref } from "vue" import { vMaska } from "maska/vue" - const maskedvalue = ref('') - const unmaskedvalue = ref('') + const maskedValue = ref('') + const unmaskedValue = ref('') + + defineExpose({ unmaskedValue }) // make sure you expose the bound variable ``` +!> Make sure you expose the bound variable using `defineExpose` macro. + ### **Options API** ```vue @@ -133,23 +137,21 @@ To get masked value you can use standard `v-model` directive on the input. But i export default { directives: { maska: vMaska }, data: () => ({ - maskedvalue: "", - unmaskedvalue: "" + maskedValue: "", + unmaskedValue: "" }) } ``` -!> For correct work as directive argument, name of the bounded variable should be in **lower case**. So instead of `unmaskedValue` use `unmaskedvalue` or `unmasked_value`. - ## Usage with Nuxt diff --git a/src/vue/index.ts b/src/vue/index.ts index 42f9970..66b92ef 100644 --- a/src/vue/index.ts +++ b/src/vue/index.ts @@ -5,16 +5,16 @@ type MaskaDirective = Directive const masks = new WeakMap() -// hacky way to update binding.arg without using defineExposed const setArg = (binding: DirectiveBinding, value: string | boolean): void => { if (binding.arg == null || (binding.instance == null)) return - const inst = binding.instance as any + const isComposition = 'setup' in binding.instance.$.type - if (binding.arg in inst) { - inst[binding.arg] = value // options api - } else if (inst.$?.setupState != null && binding.arg in inst.$.setupState) { - inst.$.setupState[binding.arg] = value // composition api + if (binding.arg in binding.instance) { + // @ts-expect-error + binding.instance[binding.arg] = value + } else if (isComposition) { + console.warn('Maska: please expose `%s` using defineExpose', binding.arg) } } diff --git a/test/vue/BindCompleted.vue b/test/vue/BindCompleted.vue index d96bca9..3b0a571 100644 --- a/test/vue/BindCompleted.vue +++ b/test/vue/BindCompleted.vue @@ -2,11 +2,13 @@ import { ref } from 'vue' import { vMaska } from '../../src/vue' -const bound = ref(false) +const boundCompleted = ref(false) + +defineExpose({ boundCompleted }) diff --git a/test/vue/BindMasked.vue b/test/vue/BindMasked.vue index 9753587..4e23ecf 100644 --- a/test/vue/BindMasked.vue +++ b/test/vue/BindMasked.vue @@ -2,10 +2,12 @@ import { ref } from 'vue' import { vMaska } from '../../src/vue' -const bound = ref('') +const boundMasked = ref('') + +defineExpose({ boundMasked }) diff --git a/test/vue/BindModel.vue b/test/vue/BindModel.vue index b63a904..7b4a2bb 100644 --- a/test/vue/BindModel.vue +++ b/test/vue/BindModel.vue @@ -2,12 +2,14 @@ import { ref } from 'vue' import { vMaska } from '../../src/vue' -const value = ref('123') -const bound = ref('') +const valueMasked = ref('123') +const boundUnmasked = ref('') + +defineExpose({ boundUnmasked }) diff --git a/test/vue/BindUnmasked.vue b/test/vue/BindUnmasked.vue index 223ce81..8c768d0 100644 --- a/test/vue/BindUnmasked.vue +++ b/test/vue/BindUnmasked.vue @@ -2,10 +2,12 @@ import { ref } from 'vue' import { vMaska } from '../../src/vue' -const bound = ref('') +const boundUnmasked = ref('') + +defineExpose({ boundUnmasked }) diff --git a/test/vue/Callbacks.vue b/test/vue/Callbacks.vue index 479ed6e..ebe6597 100644 --- a/test/vue/Callbacks.vue +++ b/test/vue/Callbacks.vue @@ -17,6 +17,8 @@ const options2 = { (detail) => emit('mask3', detail), ] } + +defineExpose({ bound1, bound2 })