@fiscozen/composables 0.1.17 → 0.1.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fiscozen/composables",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.19",
|
|
4
4
|
"description": "Design System utility composables",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -25,9 +25,9 @@
|
|
|
25
25
|
"vite": "^5.0.10",
|
|
26
26
|
"vitest": "^1.2.0",
|
|
27
27
|
"vue-tsc": "^1.8.25",
|
|
28
|
-
"@fiscozen/
|
|
28
|
+
"@fiscozen/tsconfig": "^0.1.0",
|
|
29
29
|
"@fiscozen/prettier-config": "^0.1.0",
|
|
30
|
-
"@fiscozen/
|
|
30
|
+
"@fiscozen/eslint-config": "^0.1.0"
|
|
31
31
|
},
|
|
32
32
|
"license": "ISC",
|
|
33
33
|
"scripts": {
|
package/src/composables/index.ts
CHANGED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import {Ref, watch, getCurrentInstance, computed, ref, onMounted} from 'vue'
|
|
2
|
+
|
|
3
|
+
export const useCurrency = () => {
|
|
4
|
+
const inputRef: Ref<HTMLInputElement|null> = ref(null)
|
|
5
|
+
const vm = getCurrentInstance()
|
|
6
|
+
|
|
7
|
+
const modelValue = computed<number|undefined>(() => vm?.props.modelValue as unknown as number|undefined)
|
|
8
|
+
|
|
9
|
+
const format = (input: number) => {
|
|
10
|
+
return input.toLocaleString('it-IT', {
|
|
11
|
+
minimumFractionDigits: 2,
|
|
12
|
+
maximumFractionDigits: 2,
|
|
13
|
+
})
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const parse = (text: string) => {
|
|
17
|
+
// strip currency, handle edge cases...
|
|
18
|
+
return parseFloat(text)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const setValue = (val: number) => {
|
|
22
|
+
if (Number.isNaN(val)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
vm && vm.emit('update:modelValue', val)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const onInput = (el: HTMLInputElement) => (e: Event) => {
|
|
30
|
+
if (!inputRef.value || !e.target) {
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
let {value} = el;
|
|
34
|
+
value = value.replace(/[^0-9,.]/g, '')
|
|
35
|
+
inputRef.value.value = value;
|
|
36
|
+
const number = parse(value)
|
|
37
|
+
setValue(Number.isNaN(number) ? 0 : number)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const onBlur = (e: FocusEvent) => {
|
|
41
|
+
if (!inputRef.value || !e.target) {
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
let number = parse((e.target as HTMLInputElement).value.replace(/,/g,"."))
|
|
45
|
+
if (Number.isNaN(number)) {
|
|
46
|
+
number = 0;
|
|
47
|
+
}
|
|
48
|
+
const text = format(number)
|
|
49
|
+
|
|
50
|
+
inputRef.value.value = text;
|
|
51
|
+
setValue(number)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
watch(inputRef, (newVal, oldVal) => {
|
|
55
|
+
if(!newVal) {
|
|
56
|
+
return
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (oldVal) {
|
|
60
|
+
oldVal?.removeEventListener('input', onInput(newVal))
|
|
61
|
+
oldVal?.removeEventListener('blur', onBlur)
|
|
62
|
+
}
|
|
63
|
+
newVal.addEventListener('input', onInput(newVal))
|
|
64
|
+
newVal.addEventListener('blur', onBlur)
|
|
65
|
+
|
|
66
|
+
if (modelValue.value) {
|
|
67
|
+
newVal.value = format(modelValue.value)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
inputRef,
|
|
76
|
+
parse,
|
|
77
|
+
format,
|
|
78
|
+
setValue
|
|
79
|
+
}
|
|
80
|
+
}
|