Skip to content

Commit e2b40ac

Browse files
committed
array of strings
1 parent 5f4c26d commit e2b40ac

File tree

5 files changed

+89
-4
lines changed

5 files changed

+89
-4
lines changed

playground/docus/content/authors/farnabaz.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ modules:
88
- studio
99
- content
1010
- mdc
11+
- hub
1112
---

src/app/src/components/form/FormPanelInput.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ const inputType = computed(() => {
3636
}
3737
})
3838
39+
const isArrayType = computed(() => props.formItem.type === 'array')
40+
3941
// Initialize model value
4042
const model = ref(computeValue(props.formItem))
4143
@@ -46,7 +48,7 @@ watch(model, (newValue) => {
4648
}
4749
4850
form.value = applyValueById(form.value, props.formItem.id, newValue)
49-
})
51+
}, { deep: true })
5052
5153
// Watch for external form item changes
5254
watch(() => props.formItem, (newFormItem) => {
@@ -87,7 +89,13 @@ function computeValue(formItem: FormItem): unknown {
8789
label: 'text-xs font-medium tracking-tight',
8890
}"
8991
>
92+
<FormInputArray
93+
v-if="isArrayType"
94+
v-model="model"
95+
:form-item="formItem.arrayItemForm"
96+
/>
9097
<UInput
98+
v-else
9199
:id="formItem.id"
92100
v-model="model"
93101
:placeholder="placeholder"
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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>

src/app/src/types/form.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ export type FormItem = {
2020
// Not in schema, created manually by user
2121
custom?: boolean
2222
// Items for array type
23-
items?: FormItem
23+
arrayItemForm?: FormItem
2424
}

src/app/src/utils/form.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export const buildFormTreeFromSchema = (treeKey: string, schema: Draft07): FormT
9595
id,
9696
title: upperFirst(itemKey),
9797
type: 'array',
98-
items: buildFormTreeItem(def.items, `#${itemKey}/items`)!,
98+
arrayItemForm: buildFormTreeItem(def.items, `#${itemKey}/items`)!,
9999
}
100100
}
101101

@@ -127,7 +127,7 @@ export const buildFormTreeFromSchema = (treeKey: string, schema: Draft07): FormT
127127
}
128128

129129
if (type === 'array' && def.items) {
130-
item.items = buildFormTreeItem(def.items, `#${itemKey}/items`)!
130+
item.arrayItemForm = buildFormTreeItem(def.items, `#${itemKey}/items`)!
131131
}
132132

133133
if (def.enum && Array.isArray(def.enum) && def.enum.length > 0) {

0 commit comments

Comments
 (0)