|
6 | 6 |
|
7 | 7 | <script setup lang="ts"> |
8 | 8 | import { ref } from 'vue' |
9 | | - import { Dialog, AlertDialog, Button, InfoIcon } from "../../src" |
| 9 | + import { Dialog, AlertDialog, Button, InfoIcon, Loading, Message } from "../../src" |
10 | 10 |
|
11 | 11 | const show = ref(false) |
12 | 12 | const show1 = ref(false) |
|
15 | 15 |
|
16 | 16 | function handleBeforeClose(type: 'cancel' | 'close' | 'ok', done) { |
17 | 17 | if (type === 'ok') { |
18 | | - console.log('ok') |
| 18 | + const loading = Loading.open({ |
| 19 | + to: '.edit-dialog', |
| 20 | + bar: true |
| 21 | + }) |
19 | 22 | // 模拟数据提交 |
20 | 23 | setTimeout(() => { |
| 24 | + loading.close() |
| 25 | + Message.success("提交成功") |
21 | 26 | done() |
22 | | - }, 1000); |
| 27 | + }, 2000); |
23 | 28 | } else { |
24 | 29 | done() |
25 | 30 | } |
|
28 | 33 |
|
29 | 34 | ### 基础用法 |
30 | 35 |
|
31 | | -需要设置 `model-value / v-model` 属性,它接收 `Boolean`,当为 `true` 时显示 `Dialog`。`title` 属性用于定义标题,它是可选的,默认值为空。`before-close` 关闭前的回调,会暂停 `Dialog` 的关闭. 返回 `true` 则关闭,返回 `false` 则不关闭。 |
| 36 | +需要设置 `model-value / v-model` 属性,它接收 `Boolean`,当为 `true` 时显示 `Dialog`。`title` 属性用于定义标题,它是可选的,默认值为空。 |
32 | 37 |
|
33 | 38 | <ClientOnly> |
34 | 39 | <CodePreview> |
|
37 | 42 | import { ref } from 'vue'; |
38 | 43 | //- |
39 | 44 | const show = ref(false); |
40 | | - //- |
41 | | - /** |
42 | | - * @param type 事件类型, cancel - 底部取消按钮触发, close - 关闭, ok - 底部确认按钮触发 |
43 | | - * @param done 只有调用 done 参数方法的时候才是真正关闭对话框的时候. |
44 | | - */ |
45 | | - function handleBeforeClose(type: 'cancel' | 'close' | 'ok', done) { |
46 | | - if (type === 'ok') { |
47 | | - // 模拟数据提交 |
48 | | - setTimeout(() => { |
49 | | - done(); |
50 | | - }, 1000); |
51 | | - } else { |
52 | | - done(); |
53 | | - } |
54 | | - } |
55 | 45 | </script> |
56 | 46 | <template> |
57 | 47 | <nt-button type="primary" @click="show = true">显示 Dialog</nt-button> |
58 | 48 | <!----> |
59 | | - <nt-dialog v-model="show" title="Title" :before-close="handleBeforeClose"> |
| 49 | + <nt-dialog v-model="show" title="Title"> |
60 | 50 | 这是内容 |
61 | 51 | </nt-dialog> |
62 | 52 | </template> |
63 | 53 | </textarea> |
64 | 54 | <template #preview> |
65 | 55 | <Button type="primary" @click="show = true">显示 Dialog</Button> |
66 | 56 | <!----> |
67 | | - <Dialog v-model="show" title="Title" :before-close="handleBeforeClose"> |
| 57 | + <Dialog v-model="show" title="Title"> |
68 | 58 | 这是内容 |
69 | 59 | </Dialog> |
70 | 60 | </template> |
|
109 | 99 | </CodePreview> |
110 | 100 | </ClientOnly> |
111 | 101 |
|
112 | | -### 自定义内容 |
| 102 | +### 异步关闭 |
113 | 103 |
|
114 | | -对话框的内容除了可以是简单的文本外,也可以是复杂的表单或者表格等。 |
| 104 | +有时候点击对话框完毕后,我们需要提交数据,提交成功则关闭对话框,提交失败则给出提示。 |
| 105 | + |
| 106 | +`before-close` 对话框关闭前的回调,通过调用回调的 `done()` 函数来关闭对话框;通过 `main-class` 给对话框主体添加样式;搭配 [Loading](/components/loading) 就能实现数据提交带上进度。 |
115 | 107 |
|
116 | 108 | <ClientOnly> |
117 | 109 | <CodePreview> |
118 | 110 | <textarea lang="vue"> |
119 | 111 | <script setup lang="ts"> |
| 112 | + import { ref } from 'vue'; |
| 113 | + //- |
| 114 | + const show = ref(false); |
| 115 | + //- |
| 116 | + function handleBeforeClose(type: 'cancel' | 'close' | 'ok', done) { |
| 117 | + if (type === 'ok') { |
| 118 | + const loading = NtLoading.open({ |
| 119 | + to: '.edit-dialog', |
| 120 | + bar: true |
| 121 | + }) |
| 122 | + // 模拟数据提交 |
| 123 | + setTimeout(() => { |
| 124 | + loading.close() |
| 125 | + NtMessage.success("提交成功") |
| 126 | + done() |
| 127 | + }, 2000); |
| 128 | + } else { |
| 129 | + done() |
| 130 | + } |
| 131 | + } |
120 | 132 | </script> |
121 | 133 | <template> |
122 | | - <hr /> |
| 134 | + <nt-button type="primary" @click="show3 = true">显示 Dialog</nt-button> |
| 135 | + <!----> |
| 136 | + <nt-dialog v-model="show3" title="Title" main-class="edit-dialog" :before-close="handleBeforeClose"> |
| 137 | + 这是内容 |
| 138 | + </nt-dialog> |
123 | 139 | </template> |
124 | 140 | </textarea> |
125 | 141 | <template #preview> |
126 | | - <Button type="primary" @click="show3 = true">显示表单弹窗</Button> |
127 | | - <Button type="primary" @click="show4 = true">显示表格弹窗</Button> |
| 142 | + <Button type="primary" @click="show3 = true">显示 Dialog</Button> |
128 | 143 | <!----> |
129 | | - <Dialog v-model="show3" width="300px"> |
130 | | - <template #header> |
131 | | - <InfoIcon /> |
132 | | - <span>提示</span> |
133 | | - </template> |
134 | | - 提示内容 |
| 144 | + <Dialog v-model="show3" title="Title" main-class="edit-dialog" :before-close="handleBeforeClose"> |
| 145 | + 这是内容 |
135 | 146 | </Dialog> |
136 | 147 | </template> |
137 | 148 | </CodePreview> |
|
143 | 154 |
|
144 | 155 | <!-- prettier-ignore --> |
145 | 156 | | 参数 | 说明 | 类型 | 默认值 | |
146 | | -| ---- | ---- | ---- | ---- | |
147 | | -| x | x | x | x | |
| 157 | +| --- | --- | --- | --- | |
| 158 | +| `mask` | 是否需要遮罩层 | `Boolean` | `true` | |
| 159 | +| `mask-closable` | 是否可以通过点击遮罩关闭对话框 | `Boolean` | `true` | |
| 160 | +| `model-value / v-model` | `是否显示 Dialog` | `Boolean` | - | |
| 161 | +| `title` | 标题, 也可通过具名 `slot-header` 传入 | `String` | - | |
| 162 | +| `show-close` | 右上角关闭按钮显示, `1`-显示在框内,`2`-显示在框角, `0`-不显示 | `0/1/2` | `1` | |
| 163 | +| `show-cancel` | 是否显示底部取消按钮 | `Boolean` | `true` | |
| 164 | +| `show-ok` | 是否显示底部确定按钮 | `Boolean` | `true` | |
| 165 | +| `main-class` | 主体样式类名 | `String` | - | |
| 166 | +| `width` | 宽度 | `String` | `30%` | |
| 167 | +| `before-close` | 关闭前的回调,会暂停关闭对话框, 通过调用回调函数的 `done` 关闭对话框; `cancel`-点击取消按钮触发, `close`-关闭时触发[右上角关闭按钮、遮罩], `ok`-点击确定按钮触发 | `(type: 'cancel' \| 'close' \| 'ok', done: () => void) => void` | - | |
0 commit comments