Ben Lin
2024-06-18 ebbd788fbb2c0b45d4473798efc57eec8ba74a25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!--
 * @Description: 渲染代码
-->
<template>
  <Modal
    title="代码"
    :footer="null"
    :open="visible"
    @cancel="visible = false"
    wrapClassName="v-code-modal"
    style="top: 20px"
    width="850px"
    :destroyOnClose="true"
  >
    <PreviewCode :editorJson="editorVueJson" fileFormat="vue" />
  </Modal>
</template>
<script lang="ts">
  import { computed, defineComponent, reactive, toRefs } from 'vue';
  import { formatRules, removeAttrs } from '../../../utils';
  import PreviewCode from './PreviewCode.vue';
  import { IFormConfig } from '../../../typings/v-form-component';
  import { Modal } from 'ant-design-vue';
 
  const codeVueFront = `<template>
  <div>
    <v-form-create
      :formConfig="formConfig"
      :formData="formData"
      v-model="fApi"
    />
    <a-button @click="submit">提交</a-button>
  </div>
</template>
<script>
 
export default {
  name: 'Demo',
  data () {
    return {
      fApi:{},
      formData:{},
      formConfig: `;
  /* eslint-disable */
  let codeVueLast = `
    }
  },
  methods: {
    async submit() {
      const data = await this.fApi.submit()
      console.log(data)
     }
  }
}
<\/script>`;
  //
  export default defineComponent({
    name: 'CodeModal',
    components: { PreviewCode, Modal },
    setup() {
      const state = reactive({
        visible: false,
        jsonData: {} as IFormConfig,
      });
 
      const showModal = (formConfig: IFormConfig) => {
        formConfig.schemas && formatRules(formConfig.schemas);
        state.visible = true;
        state.jsonData = formConfig as any;
      };
 
      const editorVueJson = computed(() => {
        return (
          codeVueFront +
          JSON.stringify(removeAttrs(state.jsonData as any), null, '\t') +
          codeVueLast
        );
      });
 
      return { ...toRefs(state), editorVueJson, showModal };
    },
  });
</script>