11<template >
2- <div v-if =" displayOnly" >{{ computedValue }}</div >
2+ <div v-if =" displayOnly" >{{ value }}</div >
33 <v-input v-else v-model =" value" />
44</template >
55
66<script lang="ts">
7- import { ComputedRef , defineComponent , inject , ref , watch } from ' vue' ;
7+ import { ComputedRef , defineComponent , inject , watch } from ' vue' ;
88import { parseExpression } from ' ./operations' ;
99import { useDeepValues , useCollectionRelations } from ' ./utils' ;
1010
@@ -37,7 +37,6 @@ export default defineComponent({
3737 },
3838 emits: [' input' ],
3939 setup(props , { emit }) {
40- const computedValue = ref (' ' );
4140 const relations = useCollectionRelations (props .collection );
4241 const values = useDeepValues (
4342 inject <ComputedRef <Record <string , any >>>(' values' )! ,
@@ -48,32 +47,24 @@ export default defineComponent({
4847 props .template
4948 );
5049
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 );
5454 }
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+ });
7156
7257 function compute() {
73- return props .template .replace (/ {{. *? }}/ g , (match ) => {
58+ const computedValue = props .template .replace (/ {{. *? }}/ g , (match ) => {
7459 const expression = match .slice (2 , - 2 ).trim ();
7560 return parseExpression (expression , values );
7661 });
62+
63+ if (! computedValue .length ) {
64+ return null ;
65+ }
66+
67+ return computedValue ;
7768 }
7869 },
7970});
0 commit comments