index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <!--
  2. * @Author: ypt
  3. * @Date: 2021/11/29
  4. * @Description: 渲染组件,无法使用Vben的组件
  5. -->
  6. <template>
  7. <Modal
  8. title="预览(标准Ant控件)"
  9. :visible="visible"
  10. @ok="handleGetData"
  11. @cancel="handleCancel"
  12. okText="获取数据"
  13. cancelText="关闭"
  14. style="top: 20px"
  15. :destroyOnClose="true"
  16. :width="900"
  17. >
  18. <VFormCreate
  19. :form-config="formConfig"
  20. v-model:fApi="fApi"
  21. v-model:formModel="formModel"
  22. @submit="onSubmit"
  23. >
  24. <template #slotName="{ formModel, field }">
  25. <a-input v-model:value="formModel[field]" placeholder="我是插槽传递的输入框" />
  26. </template>
  27. </VFormCreate>
  28. <JsonModal ref="jsonModal" />
  29. </Modal>
  30. </template>
  31. <script lang="ts">
  32. import { defineComponent, reactive, ref, toRaw, toRefs } from 'vue';
  33. import { IFormConfig } from '../../typings/v-form-component';
  34. import { IAnyObject } from '../../typings/base-type';
  35. import VFormCreate from '../VFormCreate/index.vue';
  36. import { formatRules } from '../../utils';
  37. import { IVFormMethods } from '../../hooks/useVFormMethods';
  38. import JsonModal from '../VFormDesign/components/JsonModal.vue';
  39. import { IToolbarMethods } from '../../typings/form-type';
  40. import { Modal } from 'ant-design-vue';
  41. export default defineComponent({
  42. name: 'VFormPreview',
  43. components: {
  44. JsonModal,
  45. VFormCreate,
  46. Modal,
  47. },
  48. setup() {
  49. const jsonModal = ref<IToolbarMethods | null>(null);
  50. const state = reactive<{
  51. formModel: IAnyObject;
  52. visible: boolean;
  53. formConfig: IFormConfig;
  54. fApi: IVFormMethods;
  55. }>({
  56. formModel: {},
  57. formConfig: {} as IFormConfig,
  58. visible: false,
  59. fApi: {} as IVFormMethods,
  60. });
  61. /**
  62. * 显示Json数据弹框
  63. * @param jsonData
  64. */
  65. const showModal = (jsonData: IFormConfig) => {
  66. // console.log('showModal-', jsonData);
  67. formatRules(jsonData.schemas);
  68. state.formConfig = jsonData;
  69. state.visible = true;
  70. };
  71. /**
  72. * 获取表单数据
  73. * @return {Promise<void>}
  74. */
  75. const handleCancel = () => {
  76. // console.log('handleCancel');
  77. state.visible = false;
  78. state.formModel = {};
  79. };
  80. const handleGetData = async () => {
  81. // console.log('handleGetData');
  82. console.log(toRaw(state.formModel));
  83. const _data = await state.fApi.submit?.();
  84. // console.log('handleGetData', 'end');
  85. jsonModal.value?.showModal?.(_data);
  86. // jsonModal.value?.showModal?.(toRaw(state.formModel));
  87. };
  88. const onSubmit = (_data: IAnyObject) => {
  89. // console.log('-> data', data);
  90. };
  91. const onCancel = () => {
  92. state.formModel = {};
  93. };
  94. return {
  95. handleGetData,
  96. handleCancel,
  97. ...toRefs(state),
  98. showModal,
  99. jsonModal,
  100. onSubmit,
  101. onCancel,
  102. };
  103. },
  104. });
  105. </script>