Skip to content

Commit 3984992

Browse files
author
Tenny
committed
docs(Dialog): 完善对话框文档
Signed-off-by: Tenny <joel.shu@qq.com>
1 parent 56f0cf7 commit 3984992

File tree

4 files changed

+68
-38
lines changed

4 files changed

+68
-38
lines changed

docs/components/dialog.md

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<script setup lang="ts">
88
import { ref } from 'vue'
9-
import { Dialog, AlertDialog, Button, InfoIcon } from "../../src"
9+
import { Dialog, AlertDialog, Button, InfoIcon, Loading, Message } from "../../src"
1010

1111
const show = ref(false)
1212
const show1 = ref(false)
@@ -15,11 +15,16 @@
1515

1616
function handleBeforeClose(type: 'cancel' | 'close' | 'ok', done) {
1717
if (type === 'ok') {
18-
console.log('ok')
18+
const loading = Loading.open({
19+
to: '.edit-dialog',
20+
bar: true
21+
})
1922
// 模拟数据提交
2023
setTimeout(() => {
24+
loading.close()
25+
Message.success("提交成功")
2126
done()
22-
}, 1000);
27+
}, 2000);
2328
} else {
2429
done()
2530
}
@@ -28,7 +33,7 @@
2833

2934
### 基础用法
3035

31-
需要设置 `model-value / v-model` 属性,它接收 `Boolean`,当为 `true` 时显示 `Dialog``title` 属性用于定义标题,它是可选的,默认值为空。`before-close` 关闭前的回调,会暂停 `Dialog` 的关闭. 返回 `true` 则关闭,返回 `false` 则不关闭。
36+
需要设置 `model-value / v-model` 属性,它接收 `Boolean`,当为 `true` 时显示 `Dialog``title` 属性用于定义标题,它是可选的,默认值为空。
3237

3338
<ClientOnly>
3439
<CodePreview>
@@ -37,34 +42,19 @@
3742
import { ref } from 'vue';
3843
//-
3944
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-
}
5545
</script>
5646
<template>
5747
<nt-button type="primary" @click="show = true">显示 Dialog</nt-button>
5848
<!---->
59-
<nt-dialog v-model="show" title="Title" :before-close="handleBeforeClose">
49+
<nt-dialog v-model="show" title="Title">
6050
这是内容
6151
</nt-dialog>
6252
</template>
6353
</textarea>
6454
<template #preview>
6555
<Button type="primary" @click="show = true">显示 Dialog</Button>
6656
<!---->
67-
<Dialog v-model="show" title="Title" :before-close="handleBeforeClose">
57+
<Dialog v-model="show" title="Title">
6858
这是内容
6959
</Dialog>
7060
</template>
@@ -109,29 +99,50 @@
10999
</CodePreview>
110100
</ClientOnly>
111101

112-
### 自定义内容
102+
### 异步关闭
113103

114-
对话框的内容除了可以是简单的文本外,也可以是复杂的表单或者表格等。
104+
有时候点击对话框完毕后,我们需要提交数据,提交成功则关闭对话框,提交失败则给出提示。
105+
106+
`before-close` 对话框关闭前的回调,通过调用回调的 `done()` 函数来关闭对话框;通过 `main-class` 给对话框主体添加样式;搭配 [Loading](/components/loading) 就能实现数据提交带上进度。
115107

116108
<ClientOnly>
117109
<CodePreview>
118110
<textarea lang="vue">
119111
<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+
}
120132
</script>
121133
<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>
123139
</template>
124140
</textarea>
125141
<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>
128143
<!---->
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+
这是内容
135146
</Dialog>
136147
</template>
137148
</CodePreview>
@@ -143,5 +154,14 @@
143154

144155
<!-- prettier-ignore -->
145156
| 参数 | 说明 | 类型 | 默认值 |
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` | - |

scripts/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ async function createComponentTemplate(name) {
7474
`### ${name} Props\r\n`,
7575
'<!-- prettier-ignore -->',
7676
'| 参数 | 说明 | 类型 | 默认值 |',
77-
'| ---- | ---- | ---- | ---- |',
77+
'| --- | --- | --- | --- |',
7878
'| x | x | x | x |',
7979
];
8080
await write(

src/components/dialog/Dialog.vue

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ export default defineComponent({
4242
type: Boolean,
4343
default: true,
4444
},
45+
/** 主体样式 */
46+
mainClass: {
47+
type: String,
48+
default: '',
49+
},
4550
/** 内容区域样式 */
4651
containerClass: {
4752
type: String,
@@ -52,7 +57,7 @@ export default defineComponent({
5257
type: Boolean,
5358
default: false,
5459
},
55-
/** 关闭前的回调, 会暂停 Dialog 的关闭. 回调返回 true 表明关闭对话框. */
60+
/** 关闭前的回调, 会暂停 Dialog 的关闭. */
5661
beforeClose: {
5762
type: Function as PropType<
5863
(type: 'cancel' | 'ok' | 'close', done: () => void) => void
@@ -130,7 +135,9 @@ export default defineComponent({
130135
const $contents: VNode[] = [
131136
h(
132137
'header',
133-
{ class: 'nt-dialog-header' },
138+
{
139+
class: 'nt-dialog-header',
140+
},
134141
slots.header != null ? slots.header() : props.title,
135142
),
136143
];
@@ -196,7 +203,7 @@ export default defineComponent({
196203
return h(
197204
'div',
198205
{
199-
class: 'nt-dialog-main',
206+
class: ['nt-dialog-main', props.mainClass],
200207
style: {
201208
width: props.width,
202209
},

src/directives/loading.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ class LoadingInstance {
169169
if (toElem == null) {
170170
toElem = document.body;
171171
}
172+
if (toElem.tagName !== 'BODY') {
173+
this.option.fullscreen = false;
174+
}
172175
this.el = toElem;
173176
addLoading(this.el, this.option, this.option.bar ? 'bar' : '');
174177
}

0 commit comments

Comments
 (0)