ComponentGroup.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <div class="px-3 py-2 text-sm text-gray-700">
  3. {{ title }}
  4. </div>
  5. <Draggable tag="ul" class="m-0 p-3 pt-0 grid grid-cols-2 gap-1" item-key="type"
  6. :group="{ name: 'form-design', pull: 'clone', put: false }" :clone="cloneWidget" :sort="false" :list="list"
  7. :allList="allList" :move="moveStart" :choose="setListIndex(allList)" @add="stopMove()">
  8. <template #item="{ element }">
  9. <li v-if="fields.includes(element.type)"
  10. class="bg-gray-100 text-xs text-gray-700 hover:text-blue-500 leading-8 cursor-move -outline-offset-1 hover:outline-dashed outline-1 outline-blue-500 list-none flex items-center">
  11. <i :class="'custom:' + element.type" class="ml-2 mr-1" />
  12. <span>{{ element.label }}</span>
  13. </li>
  14. </template>
  15. </Draggable>
  16. </template>
  17. <script lang="ts" setup>
  18. import Draggable from 'vuedraggable'
  19. import { ElMessage } from 'element-plus'
  20. import { cloneDeep } from 'lodash-es'
  21. const props = defineProps<{ fields: any[]; title: string; list: any[], allList: any }>()
  22. let moveFlag = $ref(false)
  23. const moveStart = () => {
  24. // const flag = props.allList.list.length < 5
  25. const array = props.allList.list.map((item: any) => item.model)
  26. const plateNumbers = array.filter((item: any) => item.includes('plate')).map((item: any) => {
  27. const match = item.match(/plate(\d+)/);
  28. return match ? match[1] : null;
  29. });
  30. const flag = plateNumbers.length < 5
  31. if (!flag && !moveFlag) {
  32. moveFlag = true
  33. ElMessage.warning('最多添加五个')
  34. return false
  35. }
  36. return flag
  37. }
  38. const stopMove = () => {
  39. moveFlag = false
  40. }
  41. </script>
  42. <script lang="ts">
  43. let listIndex = 1
  44. function setListIndex(list: any) {
  45. if (list.list.length > 0) {
  46. const array = list.list.map((item: any) => item.model)
  47. const plateNumbers = array.filter((item: any) => item.includes('plate')).map((item: any) => {
  48. const match = item.match(/plate(\d+)/);
  49. return match ? match[1] : null;
  50. });
  51. listIndex = findMissingPositive(plateNumbers)
  52. }
  53. }
  54. export function cloneWidget(params: any) {
  55. const key = Math.random().toString(36).substring(2, 9)
  56. params = cloneDeep({
  57. ...params,
  58. key,
  59. // model: `${params.type}_${key}`,
  60. model: `plate${listIndex}`,
  61. })
  62. if (params.type === 'grid') {
  63. params.columns.forEach((i: any) => {
  64. i.list = i.list.map(cloneWidget)
  65. })
  66. }
  67. return params
  68. }
  69. function findMissingPositive(nums: any) {
  70. let numbers = nums.map(Number);
  71. numbers.sort((a:any, b:any) => a - b);
  72. let missing = 1;
  73. for (let i = 0; i < numbers.length; i++) {
  74. if (numbers[i] === missing) {
  75. missing++;
  76. }
  77. }
  78. return missing;
  79. }
  80. </script>