|
| 1 | +<script setup lang='ts'> |
| 2 | +import { useFormKitSchema } from 'my-library' |
| 3 | +
|
| 4 | +const { addElement, addList, addListGroup, addComponent, addListGroupFunctions } = useFormKitSchema() |
| 5 | +function addFlexElement(children: any[]) { |
| 6 | + return addElement('div', children, { class: 'min-w-40rem flex gap-4' }) |
| 7 | +} |
| 8 | +
|
| 9 | +function addGroupButtons() { |
| 10 | + const addButtonComponent = (onClick: string = '', label: string = '', icon: string = '', severity: string = '', render: string = 'true', styleClass: string = 'p-button-sm ml-2'): object => { |
| 11 | + return addComponent('Button', { onClick, label, icon, class: styleClass, severity }, render) |
| 12 | + } |
| 13 | +
|
| 14 | + return addElement('div', [ |
| 15 | + addButtonComponent('$removeNode($node, $index)', '', 'pi pi-times', 'danger'), |
| 16 | + addButtonComponent('$copyNode($node, $index)', '', 'pi pi-plus'), |
| 17 | + addButtonComponent('$moveNodeUp($node, $index)', '', 'pi pi-arrow-up', 'secondary', '$index != 0'), |
| 18 | + addElement('span', [], { class: 'ml-2 mr-10' }, '$index == 0'), |
| 19 | + addButtonComponent('$moveNodeDown($node, $index)', '', 'pi pi-arrow-down', 'secondary', '$index < $node.value.length -1'), |
| 20 | + addElement('span', [], { class: 'ml-2 mr-10' }, '$index == $node.value.length -1'), |
| 21 | + ], { class: 'pt-6' }) |
| 22 | +} |
| 23 | +
|
| 24 | +const data = ref() |
| 25 | +
|
| 26 | +function createDefaultValue(): object { |
| 27 | + return { name: 'Sword', damage: '2D20' } |
| 28 | +} |
| 29 | +
|
| 30 | +onMounted(() => { |
| 31 | + const defaultData = { name: 'Fighter', attacks: [{ name: 'Sword', damage: '2D20' }, { name: 'Dagger', damage: '2D6' }] } |
| 32 | + addListGroupFunctions(defaultData, createDefaultValue()) |
| 33 | + data.value = defaultData |
| 34 | +}) |
| 35 | +
|
| 36 | +const schema |
| 37 | + = [ |
| 38 | + { |
| 39 | + $formkit: 'primeInputText', |
| 40 | + label: 'Name', |
| 41 | + name: 'name', |
| 42 | + }, |
| 43 | + addElement('hr', [], { class: 'mt-4 mb-4' }), |
| 44 | + addList('attacks', [ |
| 45 | + addFlexElement([ |
| 46 | + addElement('div', ['Attacks'], { class: 'text-xl mb-2' }), |
| 47 | + addComponent('Button', { onClick: '$addNode($node)', label: 'Add', class: 'p-button-sm', icon: 'pi pi-plus' }, '$node.value.length == 0'), |
| 48 | + ]), |
| 49 | + addListGroup( |
| 50 | + [ |
| 51 | + addFlexElement([ |
| 52 | + { |
| 53 | + $formkit: 'primeInputText', |
| 54 | + label: 'Name', |
| 55 | + name: 'name', |
| 56 | + }, |
| 57 | + { |
| 58 | + $formkit: 'primeInputText', |
| 59 | + label: 'Damage', |
| 60 | + name: 'damage', |
| 61 | + style: 'width:70px;', |
| 62 | + }, |
| 63 | + addGroupButtons(), |
| 64 | + ]), |
| 65 | + ], |
| 66 | + ), |
| 67 | + ], true, 'true'), |
| 68 | + ] |
| 69 | +</script> |
| 70 | + |
| 71 | +<template> |
| 72 | + <div v-if="data" class=""> |
| 73 | + <PrimeInput |
| 74 | + header="Repeater" :schema="schema" :data="data" |
| 75 | + /> |
| 76 | + </div> |
| 77 | +</template> |
| 78 | + |
| 79 | +<style lang='scss' scoped> |
| 80 | +
|
| 81 | +</style> |
0 commit comments