Skip to content

Commit 6c6e30b

Browse files
author
Guilherme de Oliveira
committed
fix: handles default value on render and null emits
1 parent fb56762 commit 6c6e30b

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

src/interface.vue

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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';
88
import { parseExpression } from './operations';
99
import { 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
});

src/operations.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export function parseExpression(exp: string, values: Ref | undefined): any {
1212
const { op, a, b } = opMatch;
1313
const valueA = parseExpression(a, values);
1414

15+
if (!valueA) {
16+
return '';
17+
}
18+
1519
// unary operators
1620
if (b === null) {
1721
if (op === 'INT') {

0 commit comments

Comments
 (0)