Skip to content

Commit 624e38a

Browse files
committed
feat: add preset alert, confirm, prompt components that can be simple called
1 parent 0e3abc2 commit 624e38a

File tree

23 files changed

+943
-4
lines changed

23 files changed

+943
-4
lines changed

docs/.vitepress/config/zh.mts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ function sidebarComponents(): DefaultTheme.SidebarItem[] {
168168
link: 'common-ui/vben-api-component',
169169
text: 'ApiComponent Api组件包装器',
170170
},
171+
{
172+
link: 'common-ui/vben-alert',
173+
text: 'Alert 轻量提示框',
174+
},
171175
{
172176
link: 'common-ui/vben-modal',
173177
text: 'Modal 模态框',
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
outline: deep
3+
---
4+
5+
# Vben Alert 轻量提示框
6+
7+
框架提供的一些用于轻量提示的弹窗,仅使用js代码即可快速动态创建提示而不需要在template写任何代码。
8+
9+
::: info 应用场景
10+
11+
Alert提供的功能与Modal类似,但只适用于简单应用场景。例如临时性、动态地弹出模态确认框、输入框等。如果对弹窗有更复杂的需求,请使用VbenModal
12+
13+
:::
14+
15+
::: tip README
16+
17+
下方示例代码中的,存在一些主题色未适配、样式缺失的问题,这些问题只在文档内会出现,实际使用并不会有这些问题,可忽略,不必纠结。
18+
19+
:::
20+
21+
## 基础用法
22+
23+
使用 `alert` 创建只有一个确认按钮的提示框。
24+
25+
<DemoPreview dir="demos/vben-alert/alert" />
26+
27+
使用 `confirm` 创建有确认和取消按钮的提示框。
28+
29+
<DemoPreview dir="demos/vben-alert/confirm" />
30+
31+
使用 `prompt` 创建有确认和取消按钮、接受用户输入的提示框。
32+
33+
<DemoPreview dir="demos/vben-alert/prompt" />
34+
35+
## 类型说明
36+
37+
```ts
38+
/** 预置的图标类型 */
39+
export type IconType = 'error' | 'info' | 'question' | 'success' | 'warning';
40+
41+
export type AlertProps = {
42+
/** 关闭前的回调,如果返回false,则终止关闭 */
43+
beforeClose?: () => boolean | Promise<boolean | undefined> | undefined;
44+
/** 边框 */
45+
bordered?: boolean;
46+
/** 取消按钮的标题 */
47+
cancelText?: string;
48+
/** 是否居中显示 */
49+
centered?: boolean;
50+
/** 确认按钮的标题 */
51+
confirmText?: string;
52+
/** 弹窗容器的额外样式 */
53+
containerClass?: string;
54+
/** 弹窗提示内容 */
55+
content: Component | string;
56+
/** 弹窗内容的额外样式 */
57+
contentClass?: string;
58+
/** 弹窗的图标(在标题的前面) */
59+
icon?: Component | IconType;
60+
/** 是否显示取消按钮 */
61+
showCancel?: boolean;
62+
/** 弹窗标题 */
63+
title?: string;
64+
};
65+
66+
/**
67+
* 函数签名
68+
* alert和confirm的函数签名相同。
69+
* confirm默认会显示取消按钮,而alert默认只有一个按钮
70+
* */
71+
export function alert(options: AlertProps): Promise<void>;
72+
export function alert(
73+
message: string,
74+
options?: Partial<AlertProps>,
75+
): Promise<void>;
76+
export function alert(
77+
message: string,
78+
title?: string,
79+
options?: Partial<AlertProps>,
80+
): Promise<void>;
81+
82+
/**
83+
* 弹出输入框的函数签名。
84+
* 参数beforeClose会传入用户当前输入的值
85+
* component指定接受用户输入的组件,默认为Input
86+
* componentProps 为输入组件设置的属性数据
87+
* defaultValue 默认的值
88+
* modelPropName 输入组件的值属性名称。默认为modelValue
89+
*/
90+
export async function prompt<T = any>(
91+
options: Omit<AlertProps, 'beforeClose'> & {
92+
beforeClose?: (
93+
val: T,
94+
) => boolean | Promise<boolean | undefined> | undefined;
95+
component?: Component;
96+
componentProps?: Recordable<any>;
97+
defaultValue?: T;
98+
modelPropName?: string;
99+
},
100+
): Promise<T | undefined>;
101+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<script lang="ts" setup>
2+
import { h } from 'vue';
3+
4+
import { alert, VbenButton } from '@vben/common-ui';
5+
6+
import { Empty } from 'ant-design-vue';
7+
8+
function showAlert() {
9+
alert('This is an alert message');
10+
}
11+
12+
function showIconAlert() {
13+
alert({
14+
content: 'This is an alert message with icon',
15+
icon: 'success',
16+
});
17+
}
18+
19+
function showCustomAlert() {
20+
alert({
21+
content: h(Empty, { description: '什么都没有' }),
22+
});
23+
}
24+
</script>
25+
<template>
26+
<div class="flex gap-4">
27+
<VbenButton @click="showAlert">Alert</VbenButton>
28+
<VbenButton @click="showIconAlert">Alert With Icon</VbenButton>
29+
<VbenButton @click="showCustomAlert">Alert With Custom Content</VbenButton>
30+
</div>
31+
</template>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<script lang="ts" setup>
2+
import { alert, confirm, VbenButton } from '@vben/common-ui';
3+
4+
function showConfirm() {
5+
confirm('This is an alert message')
6+
.then(() => {
7+
alert('Confirmed');
8+
})
9+
.catch(() => {
10+
alert('Canceled');
11+
});
12+
}
13+
14+
function showIconConfirm() {
15+
confirm({
16+
content: 'This is an alert message with icon',
17+
icon: 'success',
18+
});
19+
}
20+
21+
function showAsyncConfirm() {
22+
confirm({
23+
beforeClose() {
24+
return new Promise((resolve) => setTimeout(resolve, 2000));
25+
},
26+
content: 'This is an alert message with async confirm',
27+
icon: 'success',
28+
}).then(() => {
29+
alert('Confirmed');
30+
});
31+
}
32+
</script>
33+
<template>
34+
<div class="flex gap-4">
35+
<VbenButton @click="showConfirm">Confirm</VbenButton>
36+
<VbenButton @click="showIconConfirm">Confirm With Icon</VbenButton>
37+
<VbenButton @click="showAsyncConfirm">Async Confirm</VbenButton>
38+
</div>
39+
</template>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<script lang="ts" setup>
2+
import { alert, prompt, VbenButton } from '@vben/common-ui';
3+
4+
import { VbenSelect } from '@vben-core/shadcn-ui';
5+
6+
function showPrompt() {
7+
prompt({
8+
content: '请输入一些东西',
9+
})
10+
.then((val) => {
11+
alert(`已收到你的输入:${val}`);
12+
})
13+
.catch(() => {
14+
alert('Canceled');
15+
});
16+
}
17+
18+
function showSelectPrompt() {
19+
prompt({
20+
component: VbenSelect,
21+
componentProps: {
22+
options: [
23+
{ label: 'Option 1', value: 'option1' },
24+
{ label: 'Option 2', value: 'option2' },
25+
{ label: 'Option 3', value: 'option3' },
26+
],
27+
placeholder: '请选择',
28+
},
29+
content: 'This is an alert message with icon',
30+
icon: 'question',
31+
}).then((val) => {
32+
alert(`你选择的是${val}`);
33+
});
34+
}
35+
</script>
36+
<template>
37+
<div class="flex gap-4">
38+
<VbenButton @click="showPrompt">Prompt</VbenButton>
39+
<VbenButton @click="showSelectPrompt">Confirm With Select</VbenButton>
40+
</div>
41+
</template>

packages/@core/base/icons/src/lucide.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ export {
1515
ChevronsLeft,
1616
ChevronsRight,
1717
Circle,
18+
CircleAlert,
1819
CircleCheckBig,
1920
CircleHelp,
21+
CircleX,
2022
Copy,
2123
CornerDownLeft,
2224
Ellipsis,

packages/@core/composables/src/use-simple-locale/messages.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const messages: Record<Locale, Record<string, string>> = {
66
collapse: 'Collapse',
77
confirm: 'Confirm',
88
expand: 'Expand',
9+
prompt: 'Prompt',
910
reset: 'Reset',
1011
submit: 'Submit',
1112
},
@@ -14,6 +15,7 @@ export const messages: Record<Locale, Record<string, string>> = {
1415
collapse: '收起',
1516
confirm: '确认',
1617
expand: '展开',
18+
prompt: '提示',
1719
reset: '重置',
1820
submit: '提交',
1921
},

0 commit comments

Comments
 (0)