Skip to content

Commit c0c4cc1

Browse files
authored
feat(drawer): add service model (#27)
* feat(drawer): add service model * docs(drawer): add service model demo * fix(drawer): remove 'console.log()'
1 parent 58a24a1 commit c0c4cc1

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import type { App } from 'vue'
22
import Drawer from './src/drawer'
3+
import DrawerService from './src/drawer-service'
34

45
Drawer.install = function(app: App): void {
56
app.component(Drawer.name, Drawer)
67
}
78

8-
export { Drawer }
9+
export { Drawer, DrawerService }
910

1011
export default {
1112
title: 'Drawer 抽屉板',
1213
category: '反馈',
13-
status: '50%',
14+
status: '60%',
1415
install(app: App): void {
15-
1616
app.use(Drawer as any)
17+
app.config.globalProperties.$drawerService = DrawerService
1718
}
1819
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { createApp } from 'vue'
2+
import { DrawerProps } from './drawer-types'
3+
import Drawer from './drawer'
4+
5+
6+
function createDrawerApp(props: DrawerProps) {
7+
return createApp(Drawer, { ...props })
8+
}
9+
10+
export default class DrawerService {
11+
12+
static $body: HTMLElement | null = document.body
13+
static $div: HTMLDivElement | null = null
14+
static drawer = null;
15+
16+
static show(props: DrawerProps): void{
17+
this.$div = document.createElement('div')
18+
this.$body.appendChild(this.$div)
19+
this.drawer = createDrawerApp(props)
20+
this.drawer.mount(this.$div)
21+
}
22+
23+
static hide(): void {
24+
this.drawer?.unmount();
25+
this.drawer = null;
26+
if (this.$div) {
27+
this.$body.removeChild(this.$div)
28+
}
29+
this.$div = null
30+
}
31+
32+
33+
}

packages/devui-vue/devui/drawer/src/drawer.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import DrawerHeader from './components/drawer-header'
55
import DrawerContainer from './components/drawer-container'
66
import DrawerBody from './components/drawer-body'
77

8+
import DrawerService from './drawer-service';
9+
810
export default defineComponent({
911
name: 'DDrawer',
1012
props: drawerProps,
@@ -21,6 +23,7 @@ export default defineComponent({
2123
}
2224

2325
const closeDrawer = async () => {
26+
DrawerService.hide()
2427
const beforeHidden = props.beforeHidden;
2528
let result = (typeof beforeHidden === 'function' ? beforeHidden(): beforeHidden) ?? false;
2629
if (result instanceof Promise) {

packages/devui-vue/docs/components/drawer/index.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,35 @@ export default ({
127127

128128
:::
129129

130+
### 以服务的方式调用
131+
132+
:::demo
133+
134+
```vue
135+
<template>
136+
<d-button @click="open()">click me</d-button>
137+
</template>
138+
<script>
139+
import { defineComponent, ref, h } from 'vue'
140+
export default defineComponent({
141+
setup(props, ctx) {
142+
const results = ref(null);
143+
function open() {
144+
this.$drawerService.show({
145+
visible: true,
146+
isCover: false,
147+
});
148+
}
149+
return {
150+
open,
151+
}
152+
}
153+
})
154+
</script>
155+
```
156+
157+
:::
158+
130159
### 参数及API
131160

132161
| 参数 | 类型 | 默认 | 说明 | 跳转 Demo |

0 commit comments

Comments
 (0)