-
-
-
masked value:
-
unmasked value:
+
+
+
+
+
+
+
masked value:
+
unmasked value:
+
+
-
+
-
+
```
### **Vue**
@@ -47,11 +51,11 @@ And then use it in your `.vue` component:
```js
-
+
```
@@ -118,7 +122,7 @@ For modern browsers you can use ES modules build with (optional) [import maps](h
}
@@ -154,7 +158,7 @@ For modern browsers you can use ES modules build with (optional) [import maps](h
}
diff --git a/docs/v3/svelte.md b/docs/v3/svelte.md
index 3491784..a704a19 100644
--- a/docs/v3/svelte.md
+++ b/docs/v3/svelte.md
@@ -3,34 +3,38 @@
Maska provides simple Svelte action for use with input:
```html
-
+
```
-- `options` is object with default options
+- `value` could be one of:
+ - `string` for the mask value (should be enclosed in additional quotation marks: `{'#-#'}`)
+ - `object` with a default options
## Minimal example
-Apply `maska` action to the input along with `data-maska` attribite:
+Apply `maska` action to the input:
```svelte
-
+
```
+?> Please note that the mask value is enclosed in additional quotation marks: `"'#-#'"`.
## Set mask options
To set default [options](/options) for the mask, pass options via **directive value**:
```svelte
-
-
+
```
@@ -42,11 +44,12 @@ Import `vMaska` directive and apply it to the input along with `data-maska` attr
-
+
```
+?> Please note that the mask value is enclosed in additional quotation marks: `"'#-#'"`.
## Set mask options
@@ -56,12 +59,13 @@ To set default [options](/options) for the mask, pass options via **directive va
### **Composition API**
```vue
-
-
+
Masked value: {{ maskedValue }}
Unmasked value: {{ unmaskedValue }}
@@ -144,7 +148,7 @@ To get masked value you can use standard `v-model` directive on the input. But i
-
+
Masked value: {{ maskedValue }}
Unmasked value: {{ unmaskedValue }}
@@ -168,7 +172,7 @@ export default defineNuxtPlugin((nuxtApp) => {
Now you can use v-maska directive in your app:
```html
-
+
```
diff --git a/src/alpine/index.ts b/src/alpine/index.ts
index 0e44b18..39c0a56 100644
--- a/src/alpine/index.ts
+++ b/src/alpine/index.ts
@@ -9,11 +9,16 @@ export const xMaska = (Alpine: Alpine): void => {
if (input == null || input?.type === 'file') return
+ let opts: MaskInputOptions = {}
+
+ const evaluator = directive.expression !== ''
+ ? utilities.evaluateLater(directive.expression)
+ : () => {}
+
utilities.effect(() => {
- const opts =
- directive.expression !== ''
- ? { ...utilities.evaluate(directive.expression) }
- : {}
+ evaluator((expr) => {
+ opts = typeof expr === 'string' ? { mask: expr } : { ...expr }
+ })
if (directive.value != null) {
const updateArg = (detail: MaskaDetail): void => {
@@ -43,7 +48,5 @@ export const xMaska = (Alpine: Alpine): void => {
masks.set(input, new MaskInput(input, opts))
}
})
-
- utilities.cleanup(() => masks.get(input)?.destroy())
}).before('model')
}
diff --git a/src/svelte/index.ts b/src/svelte/index.ts
index 9053404..774b606 100644
--- a/src/svelte/index.ts
+++ b/src/svelte/index.ts
@@ -3,19 +3,29 @@ import { MaskaDetail, MaskInput, MaskInputOptions } from '..'
const masks = new WeakMap()
-type MaskaAction = Action) => void
}>
-export const maska: MaskaAction = (node, opts = {}) => {
+export const maska: MaskaAction = (node, value = {}) => {
const input = node instanceof HTMLInputElement ? node : node.querySelector('input')
if (input == null || input?.type === 'file') return
+ let opts = value
+
+ if (typeof opts === 'string') {
+ opts = { mask: opts }
+ }
+
masks.set(input, new MaskInput(input, opts))
return {
update (opts) {
+ if (typeof opts === 'string') {
+ opts = { mask: opts }
+ }
+
masks.get(input)?.update(opts)
},
diff --git a/src/vue/index.ts b/src/vue/index.ts
index 66b92ef..8492691 100644
--- a/src/vue/index.ts
+++ b/src/vue/index.ts
@@ -1,7 +1,7 @@
import { Directive, DirectiveBinding } from 'vue'
import { MaskaDetail, MaskInput, MaskInputOptions } from '..'
-type MaskaDirective = Directive
+type MaskaDirective = Directive
const masks = new WeakMap()
@@ -20,10 +20,15 @@ const setArg = (binding: DirectiveBinding, value: string | boolean): void => {
export const vMaska: MaskaDirective = (el, binding) => {
const input = el instanceof HTMLInputElement ? el : el.querySelector('input')
- const opts = binding.value != null ? { ...binding.value } : {}
if (input == null || input?.type === 'file') return
+ let opts: MaskInputOptions = {}
+
+ if (binding.value != null) {
+ opts = typeof binding.value === 'string' ? { mask: binding.value } : { ...binding.value }
+ }
+
if (binding.arg != null) {
const updateArg = (detail: MaskaDetail): void => {
const value = binding.modifiers.unmasked
diff --git a/test/alpine.test.ts b/test/alpine.test.ts
index 3720d71..9b5e6ce 100644
--- a/test/alpine.test.ts
+++ b/test/alpine.test.ts
@@ -20,6 +20,12 @@ const prepareInput = async (markup: string) => {
}
describe('init', () => {
+ test('with string', async () => {
+ input = await prepareInput(``)
+
+ expect(input).toHaveValue('1-2')
+ })
+
test('with data attr', async () => {
input = await prepareInput(``)
diff --git a/test/svelte/DataAttr.svelte b/test/svelte/DataAttr.svelte
new file mode 100644
index 0000000..63ae7df
--- /dev/null
+++ b/test/svelte/DataAttr.svelte
@@ -0,0 +1,7 @@
+
+
+
+
+
diff --git a/test/svelte/Demo.svelte b/test/svelte/Demo.svelte
index 9bc1690..2101900 100644
--- a/test/svelte/Demo.svelte
+++ b/test/svelte/Demo.svelte
@@ -1,5 +1,6 @@
-
+ Eager
diff --git a/test/svelte/Simple.svelte b/test/svelte/Simple.svelte
index 7e8daf2..1a3da9d 100644
--- a/test/svelte/Simple.svelte
+++ b/test/svelte/Simple.svelte
@@ -3,5 +3,5 @@
-
+
diff --git a/test/vue/Simple.vue b/test/vue/Simple.vue
index df1551c..d53dd90 100644
--- a/test/vue/Simple.vue
+++ b/test/vue/Simple.vue
@@ -3,5 +3,5 @@ import { vMaska } from '../../src/vue'
-
+
diff --git a/test/vue/Sink.vue b/test/vue/Sink.vue
index b88a6b8..ccc8a3b 100644
--- a/test/vue/Sink.vue
+++ b/test/vue/Sink.vue
@@ -1,62 +1,52 @@
-
-
+
-
+
+
+
-
+
+
+
-
+
+
+
-
-