mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-14 20:19:34 +01:00
feat(module): devtools integration (#2196)
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
76
devtools/app/components/inputs/ArrayInput.vue
Normal file
76
devtools/app/components/inputs/ArrayInput.vue
Normal file
@@ -0,0 +1,76 @@
|
||||
<script lang="ts">
|
||||
import { z } from 'zod'
|
||||
|
||||
export const arrayInputSchema = z.object({
|
||||
kind: z.literal('array'),
|
||||
schema: z.array(z.any({}))
|
||||
.or(z.record(z.number(), z.any({})).transform(t => Object.values(t)))
|
||||
.transform((t) => {
|
||||
return t.filter(s => s !== 'undefined')
|
||||
}).pipe(z.array(z.any()).max(1))
|
||||
})
|
||||
|
||||
export type ArrayInputSchema = z.infer<typeof arrayInputSchema>
|
||||
</script>
|
||||
|
||||
<script setup lang="ts">
|
||||
const props = defineProps<{
|
||||
schema: ArrayInputSchema
|
||||
}>()
|
||||
|
||||
const modelValue = defineModel<Array<any>>({})
|
||||
|
||||
const itemSchema = computed(() => {
|
||||
return props.schema?.schema[0]
|
||||
})
|
||||
|
||||
function removeArrayItem(index: number) {
|
||||
if (!modelValue.value) return
|
||||
modelValue.value.splice(index, 1)
|
||||
}
|
||||
|
||||
function addArrayItem() {
|
||||
if (!modelValue.value) {
|
||||
modelValue.value = [{}]
|
||||
} else {
|
||||
modelValue.value.push({})
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<div v-for="value, index in modelValue" :key="index" class="relative">
|
||||
<ComponentPropInput
|
||||
:model-value="value"
|
||||
:meta="{ schema: itemSchema }"
|
||||
/>
|
||||
|
||||
<UPopover>
|
||||
<UButton variant="ghost" color="neutral" icon="i-heroicons-ellipsis-vertical" class="absolute top-4 right-1" />
|
||||
<template #content>
|
||||
<UButton
|
||||
variant="ghost"
|
||||
color="error"
|
||||
icon="i-heroicons-trash"
|
||||
block
|
||||
@click="removeArrayItem(index)"
|
||||
>
|
||||
Remove
|
||||
</UButton>
|
||||
</template>
|
||||
</UPopover>
|
||||
</div>
|
||||
|
||||
<UButton
|
||||
icon="i-heroicons-plus"
|
||||
color="neutral"
|
||||
variant="ghost"
|
||||
block
|
||||
class="justify-center mt-4"
|
||||
@click="addArrayItem()"
|
||||
>
|
||||
Add value
|
||||
</UButton>
|
||||
</div>
|
||||
</template>
|
||||
Reference in New Issue
Block a user