Files
docuseal/app/javascript/template_builder/fields.vue
T

130 lines
3.7 KiB
Vue
Raw Normal View History

2023-05-21 17:59:36 +03:00
<template>
2023-06-03 00:53:46 +03:00
<div class="mb-2">
2023-05-21 17:59:36 +03:00
<Field
v-for="field in fields"
:key="field.uuid"
:field="field"
2023-06-03 00:53:46 +03:00
:type-index="fields.filter((f) => f.type === field.type).indexOf(field)"
2023-05-23 00:03:26 +03:00
@remove="fields.splice(fields.indexOf($event), 1)"
2023-06-03 00:53:46 +03:00
@move-up="move(field, -1)"
@move-down="move(field, 1)"
@scroll-to="$emit('scroll-to-area', $event)"
2023-05-21 17:59:36 +03:00
@set-draw="$emit('set-draw', $event)"
/>
</div>
2023-06-03 00:53:46 +03:00
<div class="grid grid-cols-3 gap-1">
<button
v-for="item in fieldTypes"
:key="item.type"
draggable="true"
class="flex items-center justify-center border border-dashed border-gray-300 bg-base-100 w-full rounded relative"
@dragstart="onDragstart(item.value)"
@dragend="$emit('drag-end')"
@click="addField(item.value)"
2023-05-23 00:03:26 +03:00
>
2023-06-03 00:53:46 +03:00
<div class="w-0 absolute left-0">
<svg
xmlns="http://www.w3.org/2000/svg"
class="cursor-grab"
width="18"
height="18"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
fill="none"
stroke-linecap="round"
stroke-linejoin="round"
>
<path
stroke="none"
d="M0 0h24v24H0z"
fill="none"
/>
<path d="M9 5m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
<path d="M9 12m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
<path d="M9 19m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
<path d="M15 5m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
<path d="M15 12m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
<path d="M15 19m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
</svg>
</div>
<div class="flex items-center flex-col px-2 py-2">
<component :is="item.icon" />
<span class="text-xs mt-1">
{{ item.label }}
</span>
</div>
</button>
</div>
2023-05-21 17:59:36 +03:00
</template>
<script>
import Field from './field'
import { v4 } from 'uuid'
2023-06-03 00:53:46 +03:00
import { IconTextSize, IconWriting, IconCalendarEvent, IconPhoto, IconCheckbox, IconPaperclip, IconSelect, IconCircleDot } from '@tabler/icons-vue'
2023-05-21 17:59:36 +03:00
export default {
2023-05-31 23:19:20 +03:00
name: 'TemplateFields',
2023-05-21 17:59:36 +03:00
components: {
Field
},
props: {
fields: {
type: Array,
required: true
}
},
2023-06-03 00:53:46 +03:00
emits: ['set-draw', 'set-drag', 'drag-end', 'scroll-to-area'],
2023-05-21 17:59:36 +03:00
computed: {
fieldTypes () {
return [
2023-06-03 00:53:46 +03:00
{ label: 'Text', value: 'text', icon: IconTextSize },
{ label: 'Signature', value: 'signature', icon: IconWriting },
{ label: 'Date', value: 'date', icon: IconCalendarEvent },
{ label: 'Image', value: 'image', icon: IconPhoto },
{ label: 'File', value: 'attachment', icon: IconPaperclip },
{ label: 'Select', value: 'select', icon: IconSelect },
{ label: 'Checkbox', value: 'checkbox', icon: IconCheckbox },
{ label: 'Radio', value: 'radio', icon: IconCircleDot }
2023-05-21 17:59:36 +03:00
]
}
},
methods: {
2023-05-23 00:03:26 +03:00
onDragstart (fieldType) {
this.$emit('set-drag', fieldType)
},
2023-06-03 00:53:46 +03:00
move (field, direction) {
const currentIndex = this.fields.indexOf(field)
this.fields.splice(currentIndex, 1)
if (currentIndex + direction > this.fields.length) {
this.fields.unshift(field)
} else if (currentIndex + direction < 0) {
this.fields.push(field)
} else {
this.fields.splice(currentIndex + direction, 0, field)
}
},
2023-05-23 00:03:26 +03:00
addField (type, area = null) {
2023-05-21 17:59:36 +03:00
const field = {
name: type === 'signature' ? 'Signature' : '',
uuid: v4(),
required: true,
type
}
if (['select', 'checkbox', 'radio'].includes(type)) {
field.options = ['']
}
2023-05-23 00:03:26 +03:00
if (area) {
field.areas = [area]
}
2023-05-21 17:59:36 +03:00
this.fields.push(field)
}
}
}
</script>