index.vue 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <template>
  2. <el-text type="info" size="small"> 拖动左上角的小圆点可对其排序 </el-text>
  3. <VueDraggable
  4. :list="formData"
  5. :force-fallback="true"
  6. :animation="200"
  7. handle=".drag-icon"
  8. class="m-t-8px"
  9. item-key="index"
  10. >
  11. <template #item="{ element, index }">
  12. <div
  13. class="mb-4px flex flex-col gap-4px border border-gray-2 border-rounded rounded border-solid p-8px"
  14. >
  15. <!-- 操作按钮区 -->
  16. <div class="m--8px m-b-4px flex flex-row items-center justify-between p-8px" style="background-color: var(--app-content-bg-color);">
  17. <el-tooltip content="拖动排序">
  18. <Icon icon="ic:round-drag-indicator" class="drag-icon cursor-move" style="color: #8a909c;" />
  19. </el-tooltip>
  20. <el-tooltip content="删除">
  21. <Icon
  22. icon="ep:delete"
  23. class="cursor-pointer text-red-5"
  24. v-if="formData.length > 1"
  25. @click="handleDelete(index)"
  26. />
  27. </el-tooltip>
  28. </div>
  29. <!-- 内容区 -->
  30. <slot :element="element" :index="index"></slot>
  31. </div>
  32. </template>
  33. </VueDraggable>
  34. <el-tooltip :disabled="limit < 1" :content="`最多添加${limit}个`">
  35. <el-button
  36. type="primary"
  37. plain
  38. class="m-t-4px w-full"
  39. :disabled="limit > 0 && formData.length >= limit"
  40. @click="handleAdd"
  41. >
  42. <Icon icon="ep:plus" /><span>添加</span>
  43. </el-button>
  44. </el-tooltip>
  45. </template>
  46. <script setup lang="ts">
  47. // 拖拽组件
  48. import VueDraggable from 'vuedraggable'
  49. import { usePropertyForm } from '@/components/DiyEditor/util'
  50. import { any, array } from 'vue-types'
  51. import { propTypes } from '@/utils/propTypes'
  52. import { cloneDeep } from 'lodash-es'
  53. // 拖拽组件封装
  54. defineOptions({ name: 'Draggable' })
  55. // 定义属性
  56. const props = defineProps({
  57. // 绑定值
  58. modelValue: array<any>().isRequired,
  59. // 空的元素:点击添加按钮时,创建元素并添加到列表;默认为空对象
  60. emptyItem: any<unknown>().def({}),
  61. // 数量限制:默认为0,表示不限制
  62. limit: propTypes.number.def(0)
  63. })
  64. // 定义事件
  65. const emit = defineEmits(['update:modelValue'])
  66. const { formData } = usePropertyForm(props.modelValue, emit)
  67. // 处理添加
  68. const handleAdd = () => formData.value.push(cloneDeep(props.emptyItem || {}))
  69. // 处理删除
  70. const handleDelete = (index: number) => formData.value.splice(index, 1)
  71. </script>
  72. <style scoped lang="scss"></style>