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
import { useDeepValues , useCollectionRelations } from ' ./utils' ;
10
10
@@ -37,7 +37,6 @@ export default defineComponent({
37
37
},
38
38
emits: [' input' ],
39
39
setup(props , { emit }) {
40
- const computedValue = ref (' ' );
41
40
const relations = useCollectionRelations (props .collection );
42
41
const values = useDeepValues (
43
42
inject <ComputedRef <Record <string , any >>>(' values' )! ,
@@ -48,32 +47,24 @@ export default defineComponent({
48
47
props .template
49
48
);
50
49
51
- if (values ) {
52
- if (props .displayOnly ) {
53
- computedValue .value = compute ();
50
+ watch (values , () => {
51
+ const val = compute ();
52
+ if (val !== props .value ) {
53
+ emit (' input' , val ?? null );
54
54
}
55
-
56
- watch (values , () => {
57
- if (props .displayOnly ) {
58
- computedValue .value = compute ();
59
- } else {
60
- const newValue = compute ();
61
- if (newValue !== props .value ) {
62
- emit (' input' , newValue );
63
- }
64
- }
65
- });
66
- }
67
-
68
- return {
69
- computedValue ,
70
- };
55
+ });
71
56
72
57
function compute() {
73
- return props .template .replace (/ {{. *? }}/ g , (match ) => {
58
+ const computedValue = props .template .replace (/ {{. *? }}/ g , (match ) => {
74
59
const expression = match .slice (2 , - 2 ).trim ();
75
60
return parseExpression (expression , values );
76
61
});
62
+
63
+ if (! computedValue .length ) {
64
+ return null ;
65
+ }
66
+
67
+ return computedValue ;
77
68
}
78
69
},
79
70
});
0 commit comments