| 1 | <template> |
| 2 | <div class="@container"> |
| 3 | <div class="my-1 grid grid-cols-1 gap-4 @md:grid-cols-2 @xl:grid-cols-3 @3xl:grid-cols-4"> |
| 4 | <slot name="prefix" /> |
| 5 | <CardKV |
| 6 | v-for="(field, index) in fields" |
| 7 | :key="field.key || field.label || index" |
| 8 | :label="field.label || field.key || index" |
| 9 | :value="getFieldValue(field)" |
| 10 | /> |
| 11 | <slot name="suffix" /> |
| 12 | </div> |
| 13 | </div> |
| 14 | </template> |
| 15 | |
| 16 | <script setup lang="ts"> |
| 17 | import type { SafeAny } from "@/types/utils" |
| 18 | import CardKV from "@/components/common/cards/CardKV.vue" |
| 19 | |
| 20 | export interface Field { |
| 21 | label?: string |
| 22 | key?: string | number |
| 23 | value?: SafeAny |
| 24 | formatter?: (value: any) => string | null |
| 25 | } |
| 26 | |
| 27 | const { fields } = defineProps<{ fields: Field[] }>() |
| 28 | |
| 29 | function getFieldValue(field: Field): string { |
| 30 | const value = field.value |
| 31 | |
| 32 | if (field.formatter) { |
| 33 | return field.formatter(value) || "—" |
| 34 | } |
| 35 | |
| 36 | if (value === undefined) { |
| 37 | return "—" |
| 38 | } |
| 39 | |
| 40 | if (typeof value === "string") { |
| 41 | return value.trim() || "—" |
| 42 | } |
| 43 | |
| 44 | return String(value) |
| 45 | } |
| 46 | </script> |