mirror of
https://github.com/tenrok/maska.git
synced 2026-06-23 20:40:35 +03:00
fix(vue): use defineExpose to set argument value
This commit is contained in:
+14
-12
@@ -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 { ref } from "vue"
|
||||||
import { vMaska } from "maska/vue"
|
import { vMaska } from "maska/vue"
|
||||||
|
|
||||||
const maskedvalue = ref('')
|
const maskedValue = ref('')
|
||||||
const unmaskedvalue = ref('')
|
const unmaskedValue = ref('')
|
||||||
|
|
||||||
|
defineExpose({ unmaskedValue }) // make sure you expose the bound variable
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<input v-maska:unmaskedvalue.unmasked data-maska="#-#" v-model="maskedvalue">
|
<input v-maska:unmaskedValue.unmasked data-maska="#-#" v-model="maskedValue">
|
||||||
|
|
||||||
Masked value: {{ maskedvalue }}
|
Masked value: {{ maskedValue }}
|
||||||
Unmasked value: {{ unmaskedvalue }}
|
Unmasked value: {{ unmaskedValue }}
|
||||||
</template>
|
</template>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
!> Make sure you expose the bound variable using `defineExpose` macro.
|
||||||
|
|
||||||
### **Options API**
|
### **Options API**
|
||||||
|
|
||||||
```vue
|
```vue
|
||||||
@@ -133,23 +137,21 @@ To get masked value you can use standard `v-model` directive on the input. But i
|
|||||||
export default {
|
export default {
|
||||||
directives: { maska: vMaska },
|
directives: { maska: vMaska },
|
||||||
data: () => ({
|
data: () => ({
|
||||||
maskedvalue: "",
|
maskedValue: "",
|
||||||
unmaskedvalue: ""
|
unmaskedValue: ""
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<input v-maska:unmaskedvalue.unmasked data-maska="#-#" v-model="maskedvalue">
|
<input v-maska:unmaskedValue.unmasked data-maska="#-#" v-model="maskedValue">
|
||||||
|
|
||||||
Masked value: {{ maskedvalue }}
|
Masked value: {{ maskedValue }}
|
||||||
Unmasked value: {{ unmaskedvalue }}
|
Unmasked value: {{ unmaskedValue }}
|
||||||
</template>
|
</template>
|
||||||
```
|
```
|
||||||
<!-- tabs:end -->
|
<!-- tabs:end -->
|
||||||
|
|
||||||
!> 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
|
## Usage with Nuxt
|
||||||
|
|
||||||
|
|||||||
+6
-6
@@ -5,16 +5,16 @@ type MaskaDirective = Directive<HTMLElement, MaskInputOptions | undefined>
|
|||||||
|
|
||||||
const masks = new WeakMap<HTMLInputElement, MaskInput>()
|
const masks = new WeakMap<HTMLInputElement, MaskInput>()
|
||||||
|
|
||||||
// hacky way to update binding.arg without using defineExposed
|
|
||||||
const setArg = (binding: DirectiveBinding, value: string | boolean): void => {
|
const setArg = (binding: DirectiveBinding, value: string | boolean): void => {
|
||||||
if (binding.arg == null || (binding.instance == null)) return
|
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) {
|
if (binding.arg in binding.instance) {
|
||||||
inst[binding.arg] = value // options api
|
// @ts-expect-error
|
||||||
} else if (inst.$?.setupState != null && binding.arg in inst.$.setupState) {
|
binding.instance[binding.arg] = value
|
||||||
inst.$.setupState[binding.arg] = value // composition api
|
} else if (isComposition) {
|
||||||
|
console.warn('Maska: please expose `%s` using defineExpose', binding.arg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,11 +2,13 @@
|
|||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { vMaska } from '../../src/vue'
|
import { vMaska } from '../../src/vue'
|
||||||
|
|
||||||
const bound = ref(false)
|
const boundCompleted = ref(false)
|
||||||
|
|
||||||
|
defineExpose({ boundCompleted })
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<input v-maska:bound.completed data-maska="#-#-#" />
|
<input v-maska:boundCompleted.completed data-maska="#-#-#" />
|
||||||
<div v-if="bound === true">Completed</div>
|
<div v-if="boundCompleted === true">Completed</div>
|
||||||
<div v-else>Uncompleted</div>
|
<div v-else>Uncompleted</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -2,10 +2,12 @@
|
|||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { vMaska } from '../../src/vue'
|
import { vMaska } from '../../src/vue'
|
||||||
|
|
||||||
const bound = ref('')
|
const boundMasked = ref('')
|
||||||
|
|
||||||
|
defineExpose({ boundMasked })
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<input v-maska:bound.masked data-maska="#-#" />
|
<input v-maska:boundMasked.masked data-maska="#-#" />
|
||||||
<div>{{ bound }}</div>
|
<div>{{ boundMasked }}</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -2,12 +2,14 @@
|
|||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { vMaska } from '../../src/vue'
|
import { vMaska } from '../../src/vue'
|
||||||
|
|
||||||
const value = ref('123')
|
const valueMasked = ref('123')
|
||||||
const bound = ref('')
|
const boundUnmasked = ref('')
|
||||||
|
|
||||||
|
defineExpose({ boundUnmasked })
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<input v-maska:bound.unmasked data-maska="#-#" data-maska-eager v-model="value" />
|
<input v-maska:boundUnmasked.unmasked data-maska="#-#" data-maska-eager v-model="valueMasked" />
|
||||||
<div id="value1">{{ value }}</div>
|
<div id="value1">{{ valueMasked }}</div>
|
||||||
<div id="value2">{{ bound }}</div>
|
<div id="value2">{{ boundUnmasked }}</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -2,10 +2,12 @@
|
|||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { vMaska } from '../../src/vue'
|
import { vMaska } from '../../src/vue'
|
||||||
|
|
||||||
const bound = ref('')
|
const boundUnmasked = ref('')
|
||||||
|
|
||||||
|
defineExpose({ boundUnmasked })
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<input v-maska:bound.unmasked data-maska="#-#" />
|
<input v-maska:boundUnmasked.unmasked data-maska="#-#" />
|
||||||
<div>{{ bound }}</div>
|
<div>{{ boundUnmasked }}</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ const options2 = <MaskInputOptions>{
|
|||||||
(detail) => emit('mask3', detail),
|
(detail) => emit('mask3', detail),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defineExpose({ bound1, bound2 })
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
+5
-3
@@ -3,7 +3,7 @@ import { ref } from 'vue'
|
|||||||
import { MaskInputOptions } from '../../src'
|
import { MaskInputOptions } from '../../src'
|
||||||
import { vMaska } from '../../src/vue'
|
import { vMaska } from '../../src/vue'
|
||||||
|
|
||||||
const bound = ref('')
|
const boundMasked = ref('')
|
||||||
const config = <MaskInputOptions>{
|
const config = <MaskInputOptions>{
|
||||||
mask: 'A A',
|
mask: 'A A',
|
||||||
tokens: {
|
tokens: {
|
||||||
@@ -14,9 +14,11 @@ const config = <MaskInputOptions>{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defineExpose({ boundMasked })
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<input v-maska:bound="config" />
|
<input v-maska:boundMasked="config" />
|
||||||
<div>{{ bound }}</div>
|
<div>{{ boundMasked }}</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
+5
-3
@@ -6,7 +6,7 @@ const mask = ref('+1 (###) ###-####')
|
|||||||
const show = ref(true)
|
const show = ref(true)
|
||||||
const eager = ref(true)
|
const eager = ref(true)
|
||||||
const valueMasked = ref('1234567')
|
const valueMasked = ref('1234567')
|
||||||
const valueunmasked = ref('1')
|
const valueUnmasked = ref('1')
|
||||||
|
|
||||||
const onMaska = (e) => console.log(e.detail)
|
const onMaska = (e) => console.log(e.detail)
|
||||||
|
|
||||||
@@ -29,6 +29,8 @@ const options2 = {
|
|||||||
.slice(0, sub ? -sub : undefined)
|
.slice(0, sub ? -sub : undefined)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defineExpose({ valueUnmasked })
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -40,9 +42,9 @@ const options2 = {
|
|||||||
<div><input v-model="mask"></div>
|
<div><input v-model="mask"></div>
|
||||||
|
|
||||||
<div v-if="show">
|
<div v-if="show">
|
||||||
<input v-maska:valueunmasked.unmasked="options" v-model="valueMasked" @maska="onMaska">
|
<input v-maska:valueUnmasked.unmasked="options" v-model="valueMasked" @maska="onMaska">
|
||||||
<div>Masked: {{ valueMasked }}</div>
|
<div>Masked: {{ valueMasked }}</div>
|
||||||
<div>Unmasked: {{ valueunmasked }}</div>
|
<div>Unmasked: {{ valueUnmasked }}</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user