Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(drawer): add service model #27

Merged
merged 4 commits into from
Dec 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/devui-vue/devui/drawer/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import type { App } from 'vue'
import Drawer from './src/drawer'
import DrawerService from './src/drawer-service'

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

export { Drawer }
export { Drawer, DrawerService }

export default {
title: 'Drawer 抽屉板',
category: '反馈',
status: '50%',
status: '60%',
install(app: App): void {

app.use(Drawer as any)
app.config.globalProperties.$drawerService = DrawerService
}
}
34 changes: 34 additions & 0 deletions packages/devui-vue/devui/drawer/src/drawer-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { createApp } from 'vue'
import { DrawerProps } from './drawer-types'
import Drawer from './drawer'


function createDrawerApp(props: DrawerProps) {
return createApp(Drawer, { ...props })
}

export default class DrawerService {

static $body: HTMLElement | null = document.body
static $div: HTMLDivElement | null = null
static drawer = null;

static show(props: DrawerProps): void{
this.$div = document.createElement('div')
this.$body.appendChild(this.$div)
this.drawer = createDrawerApp(props)
this.drawer.mount(this.$div)
}

static hide(): void {
console.log(this.drawer);
this.drawer?.unmount();
this.drawer = null;
if (this.$div) {
this.$body.removeChild(this.$div)
}
this.$div = null
}


}
3 changes: 3 additions & 0 deletions packages/devui-vue/devui/drawer/src/drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import DrawerHeader from './components/drawer-header'
import DrawerContainer from './components/drawer-container'
import DrawerBody from './components/drawer-body'

import DrawerService from './drawer-service';

export default defineComponent({
name: 'DDrawer',
props: drawerProps,
Expand All @@ -21,6 +23,7 @@ export default defineComponent({
}

const closeDrawer = async () => {
DrawerService.hide()
Copy link
Member

@kagol kagol Dec 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么要加这一行呢?这会不会无意中关闭掉用户不想关闭的drawer?
@lnzhangsong

const beforeHidden = props.beforeHidden;
let result = (typeof beforeHidden === 'function' ? beforeHidden(): beforeHidden) ?? false;
if (result instanceof Promise) {
Expand Down
29 changes: 29 additions & 0 deletions packages/devui-vue/docs/components/drawer/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,35 @@ export default ({

:::

### 以服务的方式调用

:::demo

```vue
<template>
<d-button @click="open()">click me</d-button>
</template>
<script>
import { defineComponent, ref, h } from 'vue'
export default defineComponent({
setup(props, ctx) {
const results = ref(null);
function open() {
this.$drawerService.show({
visible: true,
isCover: false,
});
}
return {
open,
}
}
})
</script>
```

:::

### 参数及API

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