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' ;
99
1010export default defineComponent ({
@@ -30,31 +30,17 @@ export default defineComponent({
3030 setup(props , { emit }) {
3131 const values = inject <ComputedRef <Record <string , any >>>(' values' );
3232
33- const computedValue = ref (' ' );
34-
3533 if (values ) {
36- if (props .displayOnly ) {
37- computedValue .value = compute ();
38- }
39-
4034 watch (values , (val , oldVal ) => {
4135 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 );
4939 }
5040 }
5141 });
5242 }
5343
54- return {
55- computedValue ,
56- };
57-
5844 /** Simple check which fields are used */
5945 function shouldUpdate(val : Record <string , any >, oldVal : Record <string , any >) {
6046 for (const key of Object .keys (val )) {
@@ -71,10 +57,16 @@ export default defineComponent({
7157 }
7258
7359 function compute() {
74- return props .template .replace (/ {{. *? }}/ g , (match ) => {
60+ const computedValue = props .template .replace (/ {{. *? }}/ g , (match ) => {
7561 const expression = match .slice (2 , - 2 ).trim ();
7662 return parseExpression (expression , values );
7763 });
64+
65+ if (! computedValue .length ) {
66+ return null ;
67+ }
68+
69+ return computedValue ;
7870 }
7971 },
8072});
0 commit comments