|
| 1 | +<template> |
| 2 | + <UFormField |
| 3 | + :name="formItem.id" |
| 4 | + :label="label" |
| 5 | + :ui="{ |
| 6 | + root: 'w-full', |
| 7 | + label: 'text-xs font-semibold tracking-tight', |
| 8 | + container: 'mt-1', |
| 9 | + }" |
| 10 | + > |
| 11 | + <UInput |
| 12 | + :id="formItem.id" |
| 13 | + v-model="model" |
| 14 | + :placeholder="placeholder" |
| 15 | + :type="inputType" |
| 16 | + class="w-full" |
| 17 | + /> |
| 18 | + </UFormField> |
| 19 | +</template> |
| 20 | + |
| 21 | +<script setup lang="ts"> |
| 22 | +import { titleCase } from 'scule' |
| 23 | +import type { FormItem, FormTree } from '../../../types' |
| 24 | +import type { PropType } from 'vue' |
| 25 | +import { computed, ref, watch } from 'vue' |
| 26 | +
|
| 27 | +const props = defineProps({ |
| 28 | + formItem: { |
| 29 | + type: Object as PropType<FormItem>, |
| 30 | + required: true, |
| 31 | + }, |
| 32 | +}) |
| 33 | +
|
| 34 | +const form = defineModel({ type: Object as PropType<FormTree>, default: () => ({}) }) |
| 35 | +
|
| 36 | +const label = computed(() => titleCase(props.formItem.title)) |
| 37 | +
|
| 38 | +const placeholder = computed(() => { |
| 39 | + switch (props.formItem.type) { |
| 40 | + case 'string': |
| 41 | + return `Enter ${props.formItem.title.toLowerCase()}...` |
| 42 | + case 'number': |
| 43 | + return '0' |
| 44 | + default: |
| 45 | + return '' |
| 46 | + } |
| 47 | +}) |
| 48 | +
|
| 49 | +const inputType = computed(() => { |
| 50 | + switch (props.formItem.type) { |
| 51 | + case 'number': |
| 52 | + return 'number' |
| 53 | + default: |
| 54 | + return 'text' |
| 55 | + } |
| 56 | +}) |
| 57 | +
|
| 58 | +// Initialize model value |
| 59 | +const model = ref(computeValue(props.formItem)) |
| 60 | +
|
| 61 | +// Sync changes back to parent form |
| 62 | +watch(model, (newValue) => { |
| 63 | + form.value = applyValueById(form.value, props.formItem.id, newValue) |
| 64 | +}) |
| 65 | +
|
| 66 | +// Watch for external form item changes |
| 67 | +watch(() => props.formItem, (newFormItem) => { |
| 68 | + model.value = computeValue(newFormItem) |
| 69 | +}, { deep: true }) |
| 70 | +
|
| 71 | +function computeValue(formItem: FormItem): unknown { |
| 72 | + if (formItem.value !== undefined) { |
| 73 | + return formItem.value |
| 74 | + } |
| 75 | +
|
| 76 | + switch (formItem.type) { |
| 77 | + case 'string': |
| 78 | + case 'icon': |
| 79 | + case 'media': |
| 80 | + case 'file': |
| 81 | + return '' |
| 82 | + case 'boolean': |
| 83 | + return false |
| 84 | + case 'number': |
| 85 | + return 0 |
| 86 | + case 'array': |
| 87 | + return [] |
| 88 | + case 'object': |
| 89 | + return {} |
| 90 | + default: |
| 91 | + return null |
| 92 | + } |
| 93 | +} |
| 94 | +
|
| 95 | +function applyValueById(tree: FormTree, id: string, value: unknown): FormTree { |
| 96 | + const result = { ...tree } |
| 97 | + const paths = id.split('/').filter(Boolean) |
| 98 | +
|
| 99 | + let current: Record<string, unknown> = result |
| 100 | + for (let i = 0; i < paths.length - 1; i++) { |
| 101 | + const key = paths[i] |
| 102 | + if (!current[key] || typeof current[key] !== 'object') { |
| 103 | + current[key] = {} |
| 104 | + } |
| 105 | + current = current[key] as Record<string, unknown> |
| 106 | + } |
| 107 | +
|
| 108 | + const lastKey = paths[paths.length - 1] |
| 109 | + current[lastKey] = value |
| 110 | +
|
| 111 | + return result |
| 112 | +} |
| 113 | +</script> |
0 commit comments