perf: modal and drawer api support chain calls (#5351)

* perf: modal and drawer api support chain calls

* fix: typo
This commit is contained in:
Netfan 2025-01-11 10:56:54 +08:00 committed by GitHub
parent b8bffd884c
commit 1a04a05b79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 57 additions and 55 deletions

View File

@ -133,13 +133,13 @@ const [Drawer, drawerApi] = useVbenDrawer({
| close-icon | 关闭按钮图标 | | close-icon | 关闭按钮图标 |
| extra | 额外内容(标题右侧) | | extra | 额外内容(标题右侧) |
### modalApi ### drawerApi
| 事件名 | 描述 | 类型 | | 方法 | 描述 | 类型 |
| --- | --- | --- | | --- | --- | --- |
| setState | 动态设置弹窗状态属性 | `setState(props) \| setState((prev)=>(props))` | | setState | 动态设置弹窗状态属性 | `(((prev: ModalState) => Partial<ModalState>)\| Partial<ModalState>)=>drawerApi` |
| open | 打开弹窗 | `()=>void` | | open | 打开弹窗 | `()=>void` |
| close | 关闭弹窗 | `()=>void` | | close | 关闭弹窗 | `()=>void` |
| setData | 设置共享数据 | `<T>(data:T)=>void` | | setData | 设置共享数据 | `<T>(data:T)=>drawerApi` |
| getData | 获取共享数据 | `<T>()=>T` | | getData | 获取共享数据 | `<T>()=>T` |
| useStore | 获取可响应式状态 | - | | useStore | 获取可响应式状态 | - |

View File

@ -143,11 +143,11 @@ const [Modal, modalApi] = useVbenModal({
### modalApi ### modalApi
| 事件名 | 描述 | 类型 | | 方法 | 描述 | 类型 |
| --- | --- | --- | | --- | --- | --- |
| setState | 动态设置弹窗状态属性 | `setState(props) \| setState((prev)=>(props))` | | setState | 动态设置弹窗状态属性 | `(((prev: ModalState) => Partial<ModalState>)\| Partial<ModalState>)=>modalApi` |
| open | 打开弹窗 | `()=>void` | | open | 打开弹窗 | `()=>void` |
| close | 关闭弹窗 | `()=>void` | | close | 关闭弹窗 | `()=>void` |
| setData | 设置共享数据 | `<T>(data:T)=>void` | | setData | 设置共享数据 | `<T>(data:T)=>modalApi` |
| getData | 获取共享数据 | `<T>()=>T` | | getData | 获取共享数据 | `<T>()=>T` |
| useStore | 获取可响应式状态 | - | | useStore | 获取可响应式状态 | - |

View File

@ -13,8 +13,7 @@ function open() {
} }
function handleUpdateTitle() { function handleUpdateTitle() {
drawerApi.setState({ title: '外部动态标题' }); drawerApi.setState({ title: '外部动态标题' }).open();
drawerApi.open();
} }
</script> </script>

View File

@ -9,11 +9,12 @@ const [Drawer, drawerApi] = useVbenDrawer({
}); });
function open() { function open() {
drawerApi.setData({ drawerApi
content: '外部传递的数据 content', .setData({
payload: '外部传递的数据 payload', content: '外部传递的数据 content',
}); payload: '外部传递的数据 payload',
drawerApi.open(); })
.open();
} }
</script> </script>

View File

@ -13,8 +13,7 @@ function openModal() {
} }
function handleUpdateTitle() { function handleUpdateTitle() {
modalApi.setState({ title: '外部动态标题' }); modalApi.setState({ title: '外部动态标题' }).open();
modalApi.open();
} }
</script> </script>

View File

@ -9,11 +9,12 @@ const [Modal, modalApi] = useVbenModal({
}); });
function openModal() { function openModal() {
modalApi.setData({ modalApi
content: '外部传递的数据 content', .setData({
payload: '外部传递的数据 payload', content: '外部传递的数据 content',
}); payload: '外部传递的数据 payload',
modalApi.open(); })
.open();
} }
</script> </script>

View File

@ -141,6 +141,7 @@ export class DrawerApi {
setData<T>(payload: T) { setData<T>(payload: T) {
this.sharedData.payload = payload; this.sharedData.payload = payload;
return this;
} }
setState( setState(
@ -153,5 +154,6 @@ export class DrawerApi {
} else { } else {
this.store.setState((prev) => ({ ...prev, ...stateOrFn })); this.store.setState((prev) => ({ ...prev, ...stateOrFn }));
} }
return this;
} }
} }

View File

@ -151,6 +151,7 @@ export class ModalApi {
setData<T>(payload: T) { setData<T>(payload: T) {
this.sharedData.payload = payload; this.sharedData.payload = payload;
return this;
} }
setState( setState(
@ -163,5 +164,6 @@ export class ModalApi {
} else { } else {
this.store.setState((prev) => ({ ...prev, ...stateOrFn })); this.store.setState((prev) => ({ ...prev, ...stateOrFn }));
} }
return this;
} }
} }

View File

@ -1,5 +1,5 @@
<script lang="ts" setup> <script lang="ts" setup>
import type { DrawerPlacement } from '@vben/common-ui'; import type { DrawerPlacement, DrawerState } from '@vben/common-ui';
import { Page, useVbenDrawer } from '@vben/common-ui'; import { Page, useVbenDrawer } from '@vben/common-ui';
@ -42,25 +42,21 @@ const [FormDrawer, formDrawerApi] = useVbenDrawer({
}); });
function openBaseDrawer(placement: DrawerPlacement = 'right') { function openBaseDrawer(placement: DrawerPlacement = 'right') {
baseDrawerApi.setState({ placement }); baseDrawerApi.setState({ placement }).open();
baseDrawerApi.open();
} }
function openInContentDrawer(placement: DrawerPlacement = 'right') { function openInContentDrawer(placement: DrawerPlacement = 'right') {
inContentDrawerApi.setState({ class: '', placement }); const state: Partial<DrawerState> = { class: '', placement };
if (placement === 'top') { if (placement === 'top') {
// 200200 // 200200
inContentDrawerApi.setState({ zIndex: 199 }); state.zIndex = 199;
} else {
inContentDrawerApi.setState({ zIndex: undefined });
} }
inContentDrawerApi.open(); inContentDrawerApi.setState(state).open();
} }
function openMaxContentDrawer() { function openMaxContentDrawer() {
// 便使Drawer // 便使Drawer
inContentDrawerApi.setState({ class: 'w-full', placement: 'right' }); inContentDrawerApi.setState({ class: 'w-full', placement: 'right' }).open();
inContentDrawerApi.open();
} }
function openAutoHeightDrawer() { function openAutoHeightDrawer() {
@ -72,24 +68,25 @@ function openDynamicDrawer() {
} }
function handleUpdateTitle() { function handleUpdateTitle() {
dynamicDrawerApi.setState({ title: '外部动态标题' }); dynamicDrawerApi.setState({ title: '外部动态标题' }).open();
dynamicDrawerApi.open();
} }
function openSharedDrawer() { function openSharedDrawer() {
sharedDrawerApi.setData({ sharedDrawerApi
content: '外部传递的数据 content', .setData({
payload: '外部传递的数据 payload', content: '外部传递的数据 content',
}); payload: '外部传递的数据 payload',
sharedDrawerApi.open(); })
.open();
} }
function openFormDrawer() { function openFormDrawer() {
formDrawerApi.setData({ formDrawerApi
// .setData({
values: { field1: 'abc', field2: '123' }, //
}); values: { field1: 'abc', field2: '123' },
formDrawerApi.open(); })
.open();
} }
</script> </script>

View File

@ -63,24 +63,25 @@ function openDynamicModal() {
} }
function openSharedModal() { function openSharedModal() {
sharedModalApi.setData({ sharedModalApi
content: '外部传递的数据 content', .setData({
payload: '外部传递的数据 payload', content: '外部传递的数据 content',
}); payload: '外部传递的数据 payload',
sharedModalApi.open(); })
.open();
} }
function handleUpdateTitle() { function handleUpdateTitle() {
dynamicModalApi.setState({ title: '外部动态标题' }); dynamicModalApi.setState({ title: '外部动态标题' }).open();
dynamicModalApi.open();
} }
function openFormModal() { function openFormModal() {
formModalApi.setData({ formModalApi
// .setData({
values: { field1: 'abc' }, //
}); values: { field1: 'abc' },
formModalApi.open(); })
.open();
} }
</script> </script>