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

115 lines
3.0 KiB
Vue
Raw Normal View History

2023-05-21 17:59:36 +03:00
<template>
2023-06-04 01:08:45 +03:00
<div class="mb-1">
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
2023-06-04 01:08:45 +03:00
v-for="(icon, type) in fieldIcons"
:key="type"
2023-06-03 00:53:46 +03:00
draggable="true"
class="flex items-center justify-center border border-dashed border-gray-300 bg-base-100 w-full rounded relative"
2023-06-04 01:08:45 +03:00
@dragstart="onDragstart(type)"
2023-06-03 00:53:46 +03:00
@dragend="$emit('drag-end')"
2023-06-04 01:08:45 +03:00
@click="addField(type)"
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">
2023-06-04 01:08:45 +03:00
<component :is="icon" />
2023-06-03 00:53:46 +03:00
<span class="text-xs mt-1">
2023-06-04 01:08:45 +03:00
{{ $t(type) }}
2023-06-03 00:53:46 +03:00
</span>
</div>
</button>
</div>
2023-05-21 17:59:36 +03:00
</template>
<script>
import Field from './field'
import { v4 } from 'uuid'
2023-06-04 01:08:45 +03:00
import FieldType from './field_type'
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: {
2023-06-04 01:08:45 +03:00
fieldIcons: FieldType.computed.fieldIcons
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 = {
2023-06-04 01:08:45 +03:00
name: '',
2023-05-21 17:59:36 +03:00
uuid: v4(),
required: true,
type
}
if (['select', 'checkbox', 'radio'].includes(type)) {
field.options = ['']
}
this.fields.push(field)
}
}
}
</script>