index.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <PageWrapper title="modal组件使用示例">
  3. <Alert
  4. message="使用 useModal 进行弹窗操作,默认可以拖动,可以通过 draggable
  5. 参数进行控制是否可以拖动/全屏,并演示了在Modal内动态加载内容并自动调整高度"
  6. show-icon
  7. />
  8. <a-button type="primary" class="my-4" @click="openModalLoading">
  9. 打开弹窗,加载动态数据并自动调整高度(默认可以拖动/全屏)
  10. </a-button>
  11. <Alert message="内外同时同时显示隐藏" show-icon />
  12. <a-button type="primary" class="my-4" @click="openModal2"> 打开弹窗 </a-button>
  13. <Alert message="自适应高度" show-icon />
  14. <a-button type="primary" class="my-4" @click="openModal3"> 打开弹窗 </a-button>
  15. <Alert message="内外数据交互" show-icon />
  16. <a-button type="primary" class="my-4" @click="send"> 打开弹窗并传递数据 </a-button>
  17. <Alert message="使用动态组件的方式在页面内使用多个弹窗" show-icon />
  18. <Space>
  19. <a-button type="primary" class="my-4" @click="openTargetModal(1)"> 打开弹窗1 </a-button>
  20. <a-button type="primary" class="my-4" @click="openTargetModal(2)"> 打开弹窗2 </a-button>
  21. <a-button type="primary" class="my-4" @click="openTargetModal(3)"> 打开弹窗3 </a-button>
  22. <a-button type="primary" class="my-4" @click="openTargetModal(4)"> 打开弹窗4 </a-button>
  23. </Space>
  24. <Alert
  25. message="使用函数方式创建Prompt,适合较为简单的表单内容,如果需要弹出较为复杂的内容,请使用 Modal."
  26. show-icon
  27. />
  28. <a-button type="primary" class="my-4" @click="handleCreatePrompt"> Prompt </a-button>
  29. <component
  30. v-if="currentModal"
  31. :is="currentModal"
  32. v-model:open="modalOpen"
  33. :userData="userData"
  34. />
  35. <Modal1 @register="register1" :minHeight="100" />
  36. <Modal2 @register="register2" />
  37. <Modal3 @register="register3" />
  38. <Modal4 @register="register4" />
  39. </PageWrapper>
  40. </template>
  41. <script lang="ts" setup>
  42. import { shallowRef, ComponentOptions, ref, nextTick } from 'vue';
  43. import { Alert, Space, message } from 'ant-design-vue';
  44. import { useModal } from '@/components/Modal';
  45. import Modal1 from './Modal1.vue';
  46. import Modal2 from './Modal2.vue';
  47. import Modal3 from './Modal3.vue';
  48. import Modal4 from './Modal4.vue';
  49. import { PageWrapper } from '@/components/Page';
  50. import { type Nullable } from '@vben/types';
  51. import { createPrompt } from '@/components/Prompt';
  52. const currentModal = shallowRef<Nullable<ComponentOptions>>(null);
  53. const [register1, { openModal: openModal1 }] = useModal();
  54. const [register2, { openModal: openModal2 }] = useModal();
  55. const [register3, { openModal: openModal3 }] = useModal();
  56. const [register4, { openModal: openModal4 }] = useModal();
  57. const modalOpen = ref<Boolean>(false);
  58. const userData = ref<any>(null);
  59. function send() {
  60. openModal4(true, {
  61. data: 'content',
  62. info: 'Info',
  63. });
  64. }
  65. function openModalLoading() {
  66. openModal1(true);
  67. // setModalProps({ loading: true });
  68. // setTimeout(() => {
  69. // setModalProps({ loading: false });
  70. // }, 2000);
  71. }
  72. function openTargetModal(index: number) {
  73. switch (index) {
  74. case 1:
  75. currentModal.value = Modal1 as ComponentOptions;
  76. break;
  77. case 2:
  78. currentModal.value = Modal2 as ComponentOptions;
  79. break;
  80. case 3:
  81. currentModal.value = Modal3 as ComponentOptions;
  82. break;
  83. default:
  84. currentModal.value = Modal4 as ComponentOptions;
  85. break;
  86. }
  87. nextTick(() => {
  88. // `useModal` not working with dynamic component
  89. // passing data through `userData` prop
  90. userData.value = { data: Math.random(), info: 'Info222' };
  91. // open the target modal
  92. modalOpen.value = true;
  93. });
  94. }
  95. function handleCreatePrompt() {
  96. createPrompt({
  97. title: '请输入邮箱',
  98. required: true,
  99. label: '邮箱',
  100. defaultValue: '默认邮箱',
  101. onOK: async (email: string) => {
  102. message.success('填写的邮箱地址为' + email);
  103. },
  104. inputType: 'Input',
  105. });
  106. }
  107. </script>