Skip to content

Commit a8ee88a

Browse files
committed
fix: refactor SbaKeyValueTable to use Composition API and add filtering for null values
1 parent 172cf96 commit a8ee88a

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed
Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<dl
3-
v-for="(value, key, index) in map"
3+
v-for="(value, key, index) in filteredMap"
44
:key="key"
55
class="px-4 py-3 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6"
66
:class="{ 'bg-white': index % 2 === 0, 'bg-gray-50': index % 2 !== 0 }"
@@ -17,25 +17,40 @@
1717
</dl>
1818
</template>
1919

20-
<script>
21-
export default {
22-
name: 'SbaKeyValueTable',
23-
props: {
24-
map: {
25-
type: Object,
26-
required: true,
27-
},
28-
},
29-
methods: {
30-
getSlotName(key, value) {
31-
return value?.id || key.replace(/[^a-zA-Z]/gi, '').toLowerCase();
32-
},
33-
getLabel(key, value) {
34-
return value?.label || key;
35-
},
36-
getValue(value) {
37-
return value?.value || value;
38-
},
39-
},
20+
<script setup lang="ts">
21+
import { computed } from 'vue';
22+
23+
const { map, skipNullValues = false } = defineProps<{
24+
map: Record<string, any>;
25+
skipNullValues?: boolean;
26+
}>();
27+
28+
const filteredMap = computed(() => {
29+
return Object.entries(map)
30+
.filter(([_, value]) => {
31+
if (skipNullValues) {
32+
return value && value.value !== null;
33+
}
34+
return true;
35+
})
36+
.reduce(
37+
(acc, [key, value]) => {
38+
acc[key] = value;
39+
return acc;
40+
},
41+
{} as Record<string, any>,
42+
);
43+
});
44+
45+
const getSlotName = (key: string, value: any) => {
46+
return value?.id || key.replace(/[^a-zA-Z]/gi, '').toLowerCase();
47+
};
48+
49+
const getLabel = (key: string, value: any) => {
50+
return value?.label || key;
51+
};
52+
53+
const getValue = (value: any) => {
54+
return value?.value || value;
4055
};
4156
</script>

0 commit comments

Comments
 (0)