Skip to content

Commit e6b2799

Browse files
committed
fix(vue): use defineExpose to set argument value
1 parent 0285532 commit e6b2799

File tree

9 files changed

+54
-38
lines changed

9 files changed

+54
-38
lines changed

docs/v3/vue.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,22 @@ To get masked value you can use standard `v-model` directive on the input. But i
112112
import { ref } from "vue"
113113
import { vMaska } from "maska/vue"
114114
115-
const maskedvalue = ref('')
116-
const unmaskedvalue = ref('')
115+
const maskedValue = ref('')
116+
const unmaskedValue = ref('')
117+
118+
defineExpose({ unmaskedValue }) // make sure you expose the bound variable
117119
</script>
118120
119121
<template>
120-
<input v-maska:unmaskedvalue.unmasked data-maska="#-#" v-model="maskedvalue">
122+
<input v-maska:unmaskedValue.unmasked data-maska="#-#" v-model="maskedValue">
121123
122-
Masked value: {{ maskedvalue }}
123-
Unmasked value: {{ unmaskedvalue }}
124+
Masked value: {{ maskedValue }}
125+
Unmasked value: {{ unmaskedValue }}
124126
</template>
125127
```
126128

129+
!> Make sure you expose the bound variable using `defineExpose` macro.
130+
127131
### **Options API**
128132

129133
```vue
@@ -133,23 +137,21 @@ To get masked value you can use standard `v-model` directive on the input. But i
133137
export default {
134138
directives: { maska: vMaska },
135139
data: () => ({
136-
maskedvalue: "",
137-
unmaskedvalue: ""
140+
maskedValue: "",
141+
unmaskedValue: ""
138142
})
139143
}
140144
</script>
141145
142146
<template>
143-
<input v-maska:unmaskedvalue.unmasked data-maska="#-#" v-model="maskedvalue">
147+
<input v-maska:unmaskedValue.unmasked data-maska="#-#" v-model="maskedValue">
144148
145-
Masked value: {{ maskedvalue }}
146-
Unmasked value: {{ unmaskedvalue }}
149+
Masked value: {{ maskedValue }}
150+
Unmasked value: {{ unmaskedValue }}
147151
</template>
148152
```
149153
<!-- tabs:end -->
150154

151-
!> 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`.
152-
153155

154156
## Usage with Nuxt
155157

src/vue/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ type MaskaDirective = Directive<HTMLElement, MaskInputOptions | undefined>
55

66
const masks = new WeakMap<HTMLInputElement, MaskInput>()
77

8-
// hacky way to update binding.arg without using defineExposed
98
const setArg = (binding: DirectiveBinding, value: string | boolean): void => {
109
if (binding.arg == null || (binding.instance == null)) return
1110

12-
const inst = binding.instance as any
11+
const isComposition = 'setup' in binding.instance.$.type
1312

14-
if (binding.arg in inst) {
15-
inst[binding.arg] = value // options api
16-
} else if (inst.$?.setupState != null && binding.arg in inst.$.setupState) {
17-
inst.$.setupState[binding.arg] = value // composition api
13+
if (binding.arg in binding.instance) {
14+
// @ts-expect-error
15+
binding.instance[binding.arg] = value
16+
} else if (isComposition) {
17+
console.warn('Maska: please expose `%s` using defineExpose', binding.arg)
1818
}
1919
}
2020

test/vue/BindCompleted.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
import { ref } from 'vue'
33
import { vMaska } from '../../src/vue'
44
5-
const bound = ref(false)
5+
const boundCompleted = ref(false)
6+
7+
defineExpose({ boundCompleted })
68
</script>
79

810
<template>
9-
<input v-maska:bound.completed data-maska="#-#-#" />
10-
<div v-if="bound === true">Completed</div>
11+
<input v-maska:boundCompleted.completed data-maska="#-#-#" />
12+
<div v-if="boundCompleted === true">Completed</div>
1113
<div v-else>Uncompleted</div>
1214
</template>

test/vue/BindMasked.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
import { ref } from 'vue'
33
import { vMaska } from '../../src/vue'
44
5-
const bound = ref('')
5+
const boundMasked = ref('')
6+
7+
defineExpose({ boundMasked })
68
</script>
79

810
<template>
9-
<input v-maska:bound.masked data-maska="#-#" />
10-
<div>{{ bound }}</div>
11+
<input v-maska:boundMasked.masked data-maska="#-#" />
12+
<div>{{ boundMasked }}</div>
1113
</template>

test/vue/BindModel.vue

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
import { ref } from 'vue'
33
import { vMaska } from '../../src/vue'
44
5-
const value = ref('123')
6-
const bound = ref('')
5+
const valueMasked = ref('123')
6+
const boundUnmasked = ref('')
7+
8+
defineExpose({ boundUnmasked })
79
</script>
810

911
<template>
10-
<input v-maska:bound.unmasked data-maska="#-#" data-maska-eager v-model="value" />
11-
<div id="value1">{{ value }}</div>
12-
<div id="value2">{{ bound }}</div>
12+
<input v-maska:boundUnmasked.unmasked data-maska="#-#" data-maska-eager v-model="valueMasked" />
13+
<div id="value1">{{ valueMasked }}</div>
14+
<div id="value2">{{ boundUnmasked }}</div>
1315
</template>

test/vue/BindUnmasked.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
import { ref } from 'vue'
33
import { vMaska } from '../../src/vue'
44
5-
const bound = ref('')
5+
const boundUnmasked = ref('')
6+
7+
defineExpose({ boundUnmasked })
68
</script>
79

810
<template>
9-
<input v-maska:bound.unmasked data-maska="#-#" />
10-
<div>{{ bound }}</div>
11+
<input v-maska:boundUnmasked.unmasked data-maska="#-#" />
12+
<div>{{ boundUnmasked }}</div>
1113
</template>

test/vue/Callbacks.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const options2 = <MaskInputOptions>{
1717
(detail) => emit('mask3', detail),
1818
]
1919
}
20+
21+
defineExpose({ bound1, bound2 })
2022
</script>
2123

2224
<template>

test/vue/Config.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ref } from 'vue'
33
import { MaskInputOptions } from '../../src'
44
import { vMaska } from '../../src/vue'
55
6-
const bound = ref('')
6+
const boundMasked = ref('')
77
const config = <MaskInputOptions>{
88
mask: 'A A',
99
tokens: {
@@ -14,9 +14,11 @@ const config = <MaskInputOptions>{
1414
}
1515
}
1616
}
17+
18+
defineExpose({ boundMasked })
1719
</script>
1820

1921
<template>
20-
<input v-maska:bound="config" />
21-
<div>{{ bound }}</div>
22+
<input v-maska:boundMasked="config" />
23+
<div>{{ boundMasked }}</div>
2224
</template>

test/vue/Sink.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const mask = ref('+1 (###) ###-####')
66
const show = ref(true)
77
const eager = ref(true)
88
const valueMasked = ref('1234567')
9-
const valueunmasked = ref('1')
9+
const valueUnmasked = ref('1')
1010
1111
const onMaska = (e) => console.log(e.detail)
1212
@@ -29,6 +29,8 @@ const options2 = {
2929
.slice(0, sub ? -sub : undefined)
3030
}
3131
}
32+
33+
defineExpose({ valueUnmasked })
3234
</script>
3335

3436
<template>
@@ -40,9 +42,9 @@ const options2 = {
4042
<div><input v-model="mask"></div>
4143

4244
<div v-if="show">
43-
<input v-maska:valueunmasked.unmasked="options" v-model="valueMasked" @maska="onMaska">
45+
<input v-maska:valueUnmasked.unmasked="options" v-model="valueMasked" @maska="onMaska">
4446
<div>Masked: {{ valueMasked }}</div>
45-
<div>Unmasked: {{ valueunmasked }}</div>
47+
<div>Unmasked: {{ valueUnmasked }}</div>
4648
</div>
4749
</div>
4850

0 commit comments

Comments
 (0)