Skip to content

Commit 9b4eaf5

Browse files
author
Guilherme de Oliveira
committed
fix: handles default value on render and null emits
1 parent c074b4f commit 9b4eaf5

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

src/interface.vue

+12-20
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
1010
export 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
});

src/operations.ts

+4
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)