|
| 1 | +<script setup lang="ts"> |
| 2 | +import { titleCase } from 'scule' |
| 3 | +import type { FormTree } from '../../../types' |
| 4 | +import type { PropType } from 'vue' |
| 5 | +import { computed } from 'vue' |
| 6 | +
|
| 7 | +const props = defineProps({ |
| 8 | + disabled: { |
| 9 | + type: Boolean, |
| 10 | + default: false, |
| 11 | + }, |
| 12 | + children: { |
| 13 | + type: Object as PropType<FormTree>, |
| 14 | + default: null, |
| 15 | + }, |
| 16 | +}) |
| 17 | +
|
| 18 | +const model = defineModel({ type: Object as PropType<Record<string, unknown>>, default: () => ({}) }) |
| 19 | +
|
| 20 | +const entries = computed(() => { |
| 21 | + if (!props.children) return [] |
| 22 | +
|
| 23 | + return Object.entries(props.children).map(([key, child]) => ({ |
| 24 | + key, |
| 25 | + label: titleCase(child.title || key), |
| 26 | + value: model.value?.[key] ?? child.default ?? '', |
| 27 | + type: child.type === 'number' ? 'number' : 'text', |
| 28 | + placeholder: child.type === 'number' ? '0' : `Enter ${(child.title || key).toLowerCase()}...`, |
| 29 | + })) |
| 30 | +}) |
| 31 | +
|
| 32 | +function updateValue(key: string, value: string | number) { |
| 33 | + model.value = { ...model.value, [key]: value } |
| 34 | +} |
| 35 | +</script> |
| 36 | + |
| 37 | +<template> |
| 38 | + <div class="space-y-2"> |
| 39 | + <template v-if="entries.length"> |
| 40 | + <div |
| 41 | + v-for="entry in entries" |
| 42 | + :key="entry.key" |
| 43 | + class="flex flex-col gap-1" |
| 44 | + > |
| 45 | + <label |
| 46 | + :for="`object-${entry.key}`" |
| 47 | + class="text-xs font-medium text-muted tracking-tight" |
| 48 | + > |
| 49 | + {{ entry.label }} |
| 50 | + </label> |
| 51 | + <UInput |
| 52 | + :id="`object-${entry.key}`" |
| 53 | + :model-value="entry.value" |
| 54 | + :placeholder="entry.placeholder" |
| 55 | + :type="entry.type" |
| 56 | + :disabled="disabled" |
| 57 | + class="w-full" |
| 58 | + @update:model-value="(v: string | number) => updateValue(entry.key, v)" |
| 59 | + /> |
| 60 | + </div> |
| 61 | + </template> |
| 62 | + |
| 63 | + <div |
| 64 | + v-else |
| 65 | + class="flex flex-col items-center justify-center py-6 rounded-md border border-dashed border-muted" |
| 66 | + > |
| 67 | + <UIcon |
| 68 | + name="i-lucide-box" |
| 69 | + class="size-5 text-muted mb-2" |
| 70 | + /> |
| 71 | + <p class="text-xs text-muted"> |
| 72 | + No properties defined |
| 73 | + </p> |
| 74 | + </div> |
| 75 | + </div> |
| 76 | +</template> |
0 commit comments