Skip to content

Commit a61c49f

Browse files
authored
Merge pull request #21 from rezo-labs/feature/enhance_display
Enhance display of computed value
2 parents dd32174 + a8773dc commit a61c49f

File tree

3 files changed

+81
-8
lines changed

3 files changed

+81
-8
lines changed

src/index.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default defineInterface({
77
icon: 'calculate',
88
description: 'Perform computed value based on other fields',
99
component: InterfaceComponent,
10-
types: ['string', 'text', 'integer', 'decimal', 'bigInteger', 'float', 'boolean'],
10+
types: ['string', 'text', 'integer', 'decimal', 'bigInteger', 'float', 'boolean', 'alias'],
1111
group: 'other',
1212
options: [
1313
{
@@ -24,7 +24,7 @@ export default defineInterface({
2424
name: 'Field Mode',
2525
type: 'string',
2626
meta: {
27-
width: 'half',
27+
width: 'full',
2828
interface: 'select-dropdown',
2929
options: {
3030
allowNone: true,
@@ -35,5 +35,41 @@ export default defineInterface({
3535
},
3636
},
3737
},
38+
{
39+
field: 'prefix',
40+
name: 'Prefix',
41+
type: 'string',
42+
meta: {
43+
width: 'half',
44+
interface: 'system-input-translated-string',
45+
options: {
46+
trim: false,
47+
},
48+
},
49+
},
50+
{
51+
field: 'suffix',
52+
name: 'Suffix',
53+
type: 'string',
54+
meta: {
55+
width: 'half',
56+
interface: 'system-input-translated-string',
57+
options: {
58+
trim: false,
59+
},
60+
},
61+
},
62+
{
63+
field: 'customCss',
64+
name: 'Custom CSS',
65+
type: 'json',
66+
meta: {
67+
width: 'full',
68+
interface: 'input-code',
69+
options: {
70+
language: 'json',
71+
},
72+
},
73+
},
3874
],
3975
});

src/interface.vue

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<template>
2-
<div v-if="mode">{{ computedValue }}</div>
2+
<div v-if="mode" :style="customCss">
3+
<span class="prefix">{{ prefix }}</span>
4+
<span class="computed-value">{{ computedValue }}</span>
5+
<span class="suffix">{{ suffix }}</span>
6+
</div>
37
<v-input v-else v-model="value" />
8+
<v-notice v-if="errorMsg" type="danger">{{ errorMsg }}</v-notice>
49
</template>
510

611
<script lang="ts">
@@ -18,6 +23,10 @@ export default defineComponent({
1823
type: String,
1924
default: null,
2025
},
26+
type: {
27+
type: String,
28+
default: null,
29+
},
2130
collection: {
2231
type: String,
2332
default: '',
@@ -34,10 +43,22 @@ export default defineComponent({
3443
type: String,
3544
default: null,
3645
},
46+
prefix: {
47+
type: String,
48+
default: null,
49+
},
50+
suffix: {
51+
type: String,
52+
default: null,
53+
},
54+
customCss: {
55+
type: Object,
56+
default: null,
57+
},
3758
},
3859
emits: ['input'],
3960
setup(props, { emit }) {
40-
const computedValue = ref(props.value);
61+
const computedValue = ref<string | number | null>(props.value);
4162
const relations = useCollectionRelations(props.collection);
4263
const values = useDeepValues(
4364
inject<ComputedRef<Record<string, any>>>('values')!,
@@ -47,6 +68,7 @@ export default defineComponent({
4768
props.primaryKey,
4869
props.template
4970
);
71+
const errorMsg = ref<string | null>(null);
5072
5173
if (values) {
5274
watch(values, () => {
@@ -64,13 +86,26 @@ export default defineComponent({
6486
6587
return {
6688
computedValue,
89+
errorMsg,
6790
};
6891
6992
function compute() {
70-
return props.template.replace(/{{.*?}}/g, (match) => {
71-
const expression = match.slice(2, -2).trim();
72-
return parseExpression(expression, values.value);
73-
});
93+
try {
94+
const res = props.template.replace(/{{.*?}}/g, (match) => {
95+
const expression = match.slice(2, -2).trim();
96+
return parseExpression(expression, values.value);
97+
});
98+
if (['integer', 'decimal', 'bigInteger'].includes(props.type)) {
99+
return parseInt(res) || 0;
100+
}
101+
if (['float'].includes(props.type)) {
102+
return parseFloat(res) || 0;
103+
}
104+
return res;
105+
} catch (err) {
106+
errorMsg.value = err.message ?? 'Unknown error';
107+
return null;
108+
}
74109
}
75110
},
76111
});

src/operations.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ export function parseExpression(exp: string, values: Record<string, any>): any {
190190
if (!isNaN(parseFloat(exp))) {
191191
return parseFloat(exp);
192192
}
193+
194+
throw new Error(`Cannot parse expression: ${exp}`);
193195
}
194196
return '';
195197
}

0 commit comments

Comments
 (0)