|
| 1 | +import { useFormKitSchema } from './useFormKitSchema' |
| 2 | + |
| 3 | +export function useFormKitRepeater() { |
| 4 | + const { addElement, addComponent, addElementsInOuterDiv } = useFormKitSchema() |
| 5 | + |
| 6 | + function addInsertButton(label: string = 'Add', innerClass: string = '', outerClass: string = '', buttonClass: string = 'p-button-sm', iconClass: string = 'pi pi-plus') { |
| 7 | + return addElementsInOuterDiv([ |
| 8 | + addComponent('Button', { onClick: '$addNode($node)', label, class: buttonClass, icon: iconClass }, '$node.value.length == 0'), |
| 9 | + ], innerClass, outerClass) |
| 10 | + } |
| 11 | + |
| 12 | + function addListGroupFunctions(data: any, addNodeDefaultObject: object = {}) { |
| 13 | + const swapElements = (array: any[], index1: number, index2: number) => { |
| 14 | + array[index1] = array.splice(index2, 1, array[index1])[0] |
| 15 | + return array |
| 16 | + } |
| 17 | + |
| 18 | + data.addNode = (parentNode: any) => (): void => { |
| 19 | + const newArray: any[] = [...parentNode.value, addNodeDefaultObject] |
| 20 | + parentNode.input(newArray, false) |
| 21 | + } |
| 22 | + data.removeNode = (parentNode: any, index: number) => (): void => { |
| 23 | + parentNode.input(parentNode._value.filter((_: any, i: number): boolean => i !== index), false) |
| 24 | + } |
| 25 | + data.moveNodeUp = (parentNode: any, index: number) => (): void => { |
| 26 | + const array: any[] = [...parentNode.value] |
| 27 | + if (index > 0) |
| 28 | + parentNode.input(swapElements(array, index - 1, index), false) |
| 29 | + } |
| 30 | + data.moveNodeDown = (parentNode: any, index: number) => (): void => { |
| 31 | + const array: any[] = [...parentNode.value] |
| 32 | + if (index < array.length - 1) |
| 33 | + parentNode.input(swapElements(array, index, index + 1), false) |
| 34 | + } |
| 35 | + data.copyNode = (parentNode: any, index: number) => (): void => { |
| 36 | + const obj: any = parentNode.value[index] |
| 37 | + const newArray: any[] = [...parentNode.value, { ...obj }] |
| 38 | + parentNode.input(newArray, false) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + function addGroupButtons(innerClass: string = 'mt-1', outerClass: string = 'col-4', label: string = 'Actions', help: string = '', render: string = 'true') { |
| 43 | + const addButtonComponent = (onClick: string = '', label: string = '', icon: string = '', severity: string = '', render: string = 'true', styleClass: string = 'p-button-sm mr-2'): object => { |
| 44 | + return addComponent('Button', { onClick, label, icon, class: styleClass, severity }, render) |
| 45 | + } |
| 46 | + |
| 47 | + return addElementsInOuterDiv([ |
| 48 | + addButtonComponent('$removeNode($node, $index)', '', 'pi pi-times', 'danger'), |
| 49 | + addButtonComponent('$copyNode($node, $index)', '', 'pi pi-plus'), |
| 50 | + addButtonComponent('$moveNodeUp($node, $index)', '', 'pi pi-arrow-up', 'secondary', '$index != 0'), |
| 51 | + addElement('span', [], { class: 'mr-12' }, '$index == 0'), |
| 52 | + addButtonComponent('$moveNodeDown($node, $index)', '', 'pi pi-arrow-down', 'secondary', '$index < $node.value.length -1'), |
| 53 | + addElement('span', [], { class: 'mr-12' }, '$index == $node.value.length -1'), |
| 54 | + ], innerClass, outerClass, label, help, render) |
| 55 | + } |
| 56 | + |
| 57 | + return { addInsertButton, addGroupButtons, addListGroupFunctions } |
| 58 | +} |
0 commit comments