index.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <script lang="ts" setup>
  2. import { onBeforeUnmount } from 'vue';
  3. import {
  4. alert,
  5. clearAllAlerts,
  6. confirm,
  7. Page,
  8. prompt,
  9. useVbenModal,
  10. } from '@vben/common-ui';
  11. import { Button, Card, Flex, message } from 'ant-design-vue';
  12. import DocButton from '../doc-button.vue';
  13. import AutoHeightDemo from './auto-height-demo.vue';
  14. import BaseDemo from './base-demo.vue';
  15. import BlurDemo from './blur-demo.vue';
  16. import DragDemo from './drag-demo.vue';
  17. import DynamicDemo from './dynamic-demo.vue';
  18. import FormModalDemo from './form-modal-demo.vue';
  19. import InContentModalDemo from './in-content-demo.vue';
  20. import NestedDemo from './nested-demo.vue';
  21. import SharedDataDemo from './shared-data-demo.vue';
  22. defineOptions({ name: 'ModalExample' });
  23. const [BaseModal, baseModalApi] = useVbenModal({
  24. // 连接抽离的组件
  25. connectedComponent: BaseDemo,
  26. });
  27. const [InContentModal, inContentModalApi] = useVbenModal({
  28. // 连接抽离的组件
  29. connectedComponent: InContentModalDemo,
  30. });
  31. const [AutoHeightModal, autoHeightModalApi] = useVbenModal({
  32. connectedComponent: AutoHeightDemo,
  33. });
  34. const [DragModal, dragModalApi] = useVbenModal({
  35. connectedComponent: DragDemo,
  36. });
  37. const [DynamicModal, dynamicModalApi] = useVbenModal({
  38. connectedComponent: DynamicDemo,
  39. });
  40. const [SharedDataModal, sharedModalApi] = useVbenModal({
  41. connectedComponent: SharedDataDemo,
  42. });
  43. const [FormModal, formModalApi] = useVbenModal({
  44. connectedComponent: FormModalDemo,
  45. });
  46. const [NestedModal, nestedModalApi] = useVbenModal({
  47. connectedComponent: NestedDemo,
  48. });
  49. const [BlurModal, blurModalApi] = useVbenModal({
  50. connectedComponent: BlurDemo,
  51. });
  52. function openBaseModal() {
  53. baseModalApi.open();
  54. }
  55. function openInContentModal() {
  56. inContentModalApi.open();
  57. }
  58. function openAutoHeightModal() {
  59. autoHeightModalApi.open();
  60. }
  61. function openDragModal() {
  62. dragModalApi.open();
  63. }
  64. function openDynamicModal() {
  65. dynamicModalApi.open();
  66. }
  67. function openSharedModal() {
  68. sharedModalApi
  69. .setData({
  70. content: '外部传递的数据 content',
  71. payload: '外部传递的数据 payload',
  72. })
  73. .open();
  74. }
  75. function openNestedModal() {
  76. nestedModalApi.open();
  77. }
  78. function openBlurModal() {
  79. blurModalApi.open();
  80. }
  81. function handleUpdateTitle() {
  82. dynamicModalApi.setState({ title: '外部动态标题' }).open();
  83. }
  84. function openFormModal() {
  85. formModalApi
  86. .setData({
  87. // 表单值
  88. values: { field1: 'abc', field2: '123' },
  89. })
  90. .open();
  91. }
  92. function openAlert() {
  93. alert({
  94. content: '这是一个弹窗',
  95. icon: 'success',
  96. }).then(() => {
  97. message.info('用户关闭了弹窗');
  98. });
  99. }
  100. onBeforeUnmount(() => {
  101. // 清除所有弹窗
  102. clearAllAlerts();
  103. });
  104. function openConfirm() {
  105. confirm({
  106. beforeClose({ isConfirm }) {
  107. if (!isConfirm) return;
  108. // 这里可以做一些异步操作
  109. return new Promise((resolve) => {
  110. setTimeout(() => {
  111. resolve(true);
  112. }, 1000);
  113. });
  114. },
  115. centered: false,
  116. content: '这是一个确认弹窗',
  117. icon: 'question',
  118. })
  119. .then(() => {
  120. message.success('用户确认了操作');
  121. })
  122. .catch(() => {
  123. message.error('用户取消了操作');
  124. });
  125. }
  126. async function openPrompt() {
  127. prompt<string>({
  128. async beforeClose({ isConfirm, value }) {
  129. if (isConfirm && value === '芝士') {
  130. message.error('不能吃芝士');
  131. return false;
  132. }
  133. },
  134. componentProps: { placeholder: '不能吃芝士...' },
  135. content: '中午吃了什么?',
  136. icon: 'question',
  137. overlayBlur: 3,
  138. })
  139. .then((res) => {
  140. message.success(`用户输入了:${res}`);
  141. })
  142. .catch(() => {
  143. message.error('用户取消了输入');
  144. });
  145. }
  146. </script>
  147. <template>
  148. <Page
  149. auto-content-height
  150. description="弹窗组件常用于在不离开当前页面的情况下,显示额外的信息、表单或操作提示,更多api请查看组件文档。"
  151. title="弹窗组件示例"
  152. >
  153. <template #extra>
  154. <DocButton path="/components/common-ui/vben-modal" />
  155. </template>
  156. <BaseModal />
  157. <InContentModal />
  158. <AutoHeightModal />
  159. <DragModal />
  160. <DynamicModal />
  161. <SharedDataModal />
  162. <FormModal />
  163. <NestedModal />
  164. <BlurModal />
  165. <Flex wrap="wrap" class="w-full" gap="10">
  166. <Card class="w-[300px]" title="基本使用">
  167. <p>一个基础的弹窗示例</p>
  168. <template #actions>
  169. <Button type="primary" @click="openBaseModal">打开弹窗</Button>
  170. </template>
  171. </Card>
  172. <Card class="w-[300px]" title="指定容器">
  173. <p>在内容区域打开弹窗的示例</p>
  174. <template #actions>
  175. <Button type="primary" @click="openInContentModal">打开弹窗</Button>
  176. </template>
  177. </Card>
  178. <Card class="w-[300px]" title="内容高度自适应">
  179. <p>可根据内容并自动调整高度</p>
  180. <template #actions>
  181. <Button type="primary" @click="openAutoHeightModal">
  182. 打开弹窗
  183. </Button>
  184. </template>
  185. </Card>
  186. <Card class="w-[300px]" title="可拖拽示例">
  187. <p>配置 draggable 可开启拖拽功能</p>
  188. <template #actions>
  189. <Button type="primary" @click="openDragModal"> 打开弹窗 </Button>
  190. </template>
  191. </Card>
  192. <Card class="w-[300px]" title="动态配置示例">
  193. <p>通过 setState 动态调整弹窗数据</p>
  194. <template #extra>
  195. <Button type="link" @click="openDynamicModal">打开弹窗</Button>
  196. </template>
  197. <template #actions>
  198. <Button type="primary" @click="handleUpdateTitle">
  199. 外部修改标题并打开
  200. </Button>
  201. </template>
  202. </Card>
  203. <Card class="w-[300px]" title="内外数据共享示例">
  204. <p>通过共享 sharedData 来进行数据交互</p>
  205. <template #actions>
  206. <Button type="primary" @click="openSharedModal">
  207. 打开弹窗并传递数据
  208. </Button>
  209. </template>
  210. </Card>
  211. <Card class="w-[300px]" title="表单弹窗示例">
  212. <p>弹窗与表单结合</p>
  213. <template #actions>
  214. <Button type="primary" @click="openFormModal"> 打开表单弹窗 </Button>
  215. </template>
  216. </Card>
  217. <Card class="w-[300px]" title="嵌套弹窗示例">
  218. <p>在已经打开的弹窗中再次打开弹窗</p>
  219. <template #actions>
  220. <Button type="primary" @click="openNestedModal">打开嵌套弹窗</Button>
  221. </template>
  222. </Card>
  223. <Card class="w-[300px]" title="遮罩模糊示例">
  224. <p>遮罩层应用类似毛玻璃的模糊效果</p>
  225. <template #actions>
  226. <Button type="primary" @click="openBlurModal">打开弹窗</Button>
  227. </template>
  228. </Card>
  229. <Card class="w-[300px]" title="轻量提示弹窗">
  230. <template #extra>
  231. <DocButton path="/components/common-ui/vben-alert" />
  232. </template>
  233. <p>通过快捷方法创建动态提示弹窗,适合一些轻量的提示和确认、输入等</p>
  234. <template #actions>
  235. <Button type="primary" @click="openAlert">Alert</Button>
  236. <Button type="primary" @click="openConfirm">Confirm</Button>
  237. <Button type="primary" @click="openPrompt">Prompt</Button>
  238. </template>
  239. </Card>
  240. </Flex>
  241. </Page>
  242. </template>