1
1
<template >
2
- <div v-if =" displayOnly" >{{ computedValue }}</div >
2
+ <div v-if =" displayOnly" >{{ value }}</div >
3
3
<v-input v-else v-model =" value" />
4
4
</template >
5
5
6
6
<script lang="ts">
7
- import { ComputedRef , defineComponent , inject , ref , watch } from ' vue' ;
7
+ import { ComputedRef , defineComponent , inject , watch } from ' vue' ;
8
8
import { parseExpression } from ' ./operations' ;
9
9
10
10
export default defineComponent ({
@@ -30,31 +30,17 @@ export default defineComponent({
30
30
setup(props , { emit }) {
31
31
const values = inject <ComputedRef <Record <string , any >>>(' values' );
32
32
33
- const computedValue = ref (' ' );
34
-
35
33
if (values ) {
36
- if (props .displayOnly ) {
37
- computedValue .value = compute ();
38
- }
39
-
40
34
watch (values , (val , oldVal ) => {
41
35
if (shouldUpdate (val , oldVal )) {
42
- if (props .displayOnly ) {
43
- computedValue .value = compute ();
44
- } else {
45
- const newValue = compute ();
46
- if (newValue !== props .value ) {
47
- emit (' input' , newValue );
48
- }
36
+ const val = compute ();
37
+ if (val !== props .value ) {
38
+ emit (' input' , val ?? null );
49
39
}
50
40
}
51
41
});
52
42
}
53
43
54
- return {
55
- computedValue ,
56
- };
57
-
58
44
/** Simple check which fields are used */
59
45
function shouldUpdate(val : Record <string , any >, oldVal : Record <string , any >) {
60
46
for (const key of Object .keys (val )) {
@@ -71,10 +57,16 @@ export default defineComponent({
71
57
}
72
58
73
59
function compute() {
74
- return props .template .replace (/ {{. *? }}/ g , (match ) => {
60
+ const computedValue = props .template .replace (/ {{. *? }}/ g , (match ) => {
75
61
const expression = match .slice (2 , - 2 ).trim ();
76
62
return parseExpression (expression , values );
77
63
});
64
+
65
+ if (! computedValue .length ) {
66
+ return null ;
67
+ }
68
+
69
+ return computedValue ;
78
70
}
79
71
},
80
72
});
0 commit comments