index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <design-framework>
  3. <template #components>
  4. <table-components :group-list="groupList"></table-components>
  5. </template>
  6. <template #toolbar>
  7. <el-button type="text" size="small" @click="previewJson">预览JSON</el-button>
  8. </template>
  9. <div class="form-drawing-container">
  10. <el-form class="form-item-wrap" :size="tableConfig.tableSize||'small'" :label-width="`${tableConfig.labelWidth}px`" :label-position="tableConfig.labelPosition">
  11. <form-drawing-table field-key="tableData"
  12. :select-id="selectId"
  13. :config="tableConfig.config"
  14. @update:config="updateConfig"
  15. @click="handleSelectItem(tableConfig)"
  16. @onSelectItem="handleSelectItem"></form-drawing-table>
  17. </el-form>
  18. </div>
  19. <template #property>
  20. <table-property :selectItem="selectItem"
  21. :data="formConfig"
  22. :property-tabs="propertyTabs"
  23. @update:selectItem="updateSelectItem"
  24. ></table-property>
  25. </template>
  26. <cip-dialog v-model="previewJsonVisible" title="预览JSON" top="5vh" :show-only="true">
  27. <cip-code-mirror :modelValue="previewJsonResult"></cip-code-mirror>
  28. </cip-dialog>
  29. </design-framework>
  30. </template>
  31. <script>
  32. import { computed, ref } from 'vue'
  33. import { ElButton, ElForm } from 'element-plus'
  34. import DesignFramework from './framework'
  35. import TableComponents from './table-components'
  36. import TableProperty from './form-property'
  37. import FormDrawingTable from './form-drawing'
  38. import CipDialog from '@cip/components/cip-dialog'
  39. import CipCodeMirror from '@cip/components/cip-code-mirror'
  40. import { formConfigListFlat, except } from './util'
  41. import { componentsGroupList } from './components-config.js'
  42. export default {
  43. components: { ElForm, DesignFramework, TableComponents, TableProperty, ElButton, CipDialog, FormDrawingTable, CipCodeMirror },
  44. props: {
  45. formConfig: Object,
  46. isDefaultConfig: {
  47. type: Boolean,
  48. default: true
  49. },
  50. tableDefaultConfig: Object,
  51. componentsGroupList: { // 字段组件配置
  52. type: Array,
  53. default: () => componentsGroupList
  54. },
  55. propertyTabs: Array // 需要的属性面板tab列表
  56. },
  57. emits: ['update:tableConfig'],
  58. setup (props, { emit }) {
  59. const previewFormVisible = ref(false)
  60. const preview = () => {
  61. previewFormVisible.value = true
  62. }
  63. const previewJsonVisible = ref(false)
  64. const previewJsonResult = ref('')
  65. const previewJson = () => {
  66. previewJsonResult.value = props.formConfig
  67. previewJsonVisible.value = true
  68. }
  69. /**
  70. * 转成列表待用组件
  71. * 筛出子表单和文字组件,将布局组件中的字段提出
  72. */
  73. const selectItem = ref({})
  74. const selectId = ref('')
  75. // 拿到表单设计组件配置项,使用里边的label来做默认值
  76. const componentsList = ref([])
  77. props.componentsGroupList.forEach(i => {
  78. componentsList.value = componentsList.value.concat(i.components)
  79. })
  80. const groupList = computed(() => {
  81. if (!props.isDefaultConfig) {
  82. const list = toGroupList(props.formConfig.list) ?? []
  83. list[0].components.map(i => {
  84. if (i.label === '') i.label = componentsList.value.find(v => v.type === i.type)?.label ?? ''
  85. return i
  86. })
  87. return list
  88. } else {
  89. return props.componentsGroupList
  90. }
  91. })
  92. // 备选表单字段保留的字段格式
  93. const assignConfig = (i) => {
  94. return {
  95. key: i.key,
  96. id: i.id,
  97. type: i.config.type,
  98. label: i.config.label,
  99. search: i.config.search ?? true,
  100. fieldAlias: i.config.fieldAlias ?? i.config.label ?? ''
  101. }
  102. }
  103. const toGroupList = (formData) => {
  104. return [{
  105. groupName: 'basic',
  106. label: '表单字段',
  107. components: formConfigListFlat(formData.list).filter(v => !except.includes(v.config.type)).map(i => assignConfig(i))
  108. }]
  109. }
  110. /**
  111. * 列表配置项
  112. */
  113. const tableConfig = ref({
  114. config: {
  115. type: 'table',
  116. class: 'disabled-table',
  117. icon: 'el-icon-edit',
  118. list: props.formConfig.list ?? []
  119. }
  120. })
  121. const updateConfig = (val) => {
  122. emit('update:tableConfig', val.config)
  123. }
  124. const handleSelectItem = (item) => {
  125. selectItem.value = item
  126. selectId.value = item.id
  127. }
  128. const updateSelectItem = val => {
  129. selectItem.value.key = val.key || ''
  130. selectItem.value.config = val
  131. // eslint-disable-next-line
  132. for (const i in tableConfig.value?.config?.list) {
  133. if (tableConfig.value.config.list[i].key === val.key) {
  134. tableConfig.value.config.list[i] = { config: val, key: val.key, id: val.id }
  135. }
  136. }
  137. updateConfig(tableConfig.value)
  138. }
  139. return {
  140. groupList,
  141. previewFormVisible,
  142. preview,
  143. previewJsonVisible,
  144. previewJsonResult,
  145. previewJson,
  146. tableConfig: tableConfig.value,
  147. updateConfig,
  148. selectItem,
  149. selectId,
  150. handleSelectItem,
  151. updateSelectItem
  152. }
  153. }
  154. }
  155. </script>
  156. <style lang="less">
  157. @import "./form-drawing/index";
  158. .empty-form--text{
  159. position: absolute;
  160. left: 0;
  161. right:0;
  162. top: 0;
  163. bottom: 0;
  164. display: flex;
  165. align-items: center;
  166. justify-content: center;
  167. color: #999;
  168. z-index: 1;
  169. }
  170. </style>