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

fix(Drawer): export drawer component #1030

Merged
merged 2 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions src/drawer/demos/plugin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
</template>

<script lang="ts" setup>
import { inject } from 'vue';
import { DrawerPlugin } from 'tdesign-mobile-vue';
const $drawer: any = inject('$drawer');
const showDrawer = () => {
const instance = $drawer({
const instance = DrawerPlugin({
items: new Array(20).fill({ title: '菜单' }).map((item, index: number) => ({ title: `标题 ${index + 1}` })),
onItemClick(index: number) {
console.log(index);
Expand Down
58 changes: 9 additions & 49 deletions src/drawer/index.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,15 @@
import { createApp, App, h, ref, nextTick } from 'vue';
import vueDrawer from './drawer.vue';
import { WithInstallType } from '../shared';
import { TdDrawerProps } from './type';
import _Drawer from './drawer.vue';
import { withInstall, WithInstallType } from '../shared';

import './style';

type DrawerOptions = Omit<TdDrawerProps, 'attach'>;

const Drawer = (options: DrawerOptions) => {
const root = document.createElement('div');
document.body.appendChild(root);
const visible = ref(false);
const props = ref({});
const destroyOnClose = ref(false);

createApp(() =>
h(vueDrawer, { ...options, visible: visible.value, destroyOnClose: destroyOnClose.value, ...props.value }),
).mount(root);

const handler = {
destroy() {
destroyOnClose.value = true;
nextTick(() => {
visible.value = false;
document.body.removeChild(root);
});
},
hide() {
visible.value = false;
},
show() {
visible.value = true;
},
update(options: DrawerOptions) {
props.value = options;
},
};

return handler;
};

Drawer.install = (app: App, name = '') => {
app.component(name || vueDrawer.name, vueDrawer);

// 添加插件入口
app.config.globalProperties.$drawer = Drawer;
app.provide('$drawer', Drawer);
};

const _Drawer: WithInstallType<typeof Drawer> = Drawer;
import { TdDrawerProps } from './type';

export default _Drawer;
export type DividerProps = TdDrawerProps;

export * from './type';
export * from './plugin';
export { default as DrawerPlugin } from './plugin';

const Drawer: WithInstallType<typeof _Drawer> = withInstall(_Drawer);
export default Drawer;
48 changes: 48 additions & 0 deletions src/drawer/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { createApp, App, h, ref, nextTick } from 'vue';
import vueDrawer from './drawer.vue';
import { WithInstallType } from '../shared';
import { TdDrawerProps } from './type';

type DrawerOptions = Omit<TdDrawerProps, 'attach'>;

const Drawer = (options: DrawerOptions) => {
const root = document.createElement('div');
document.body.appendChild(root);
const visible = ref(false);
const props = ref({});
const destroyOnClose = ref(false);

createApp(() =>
h(vueDrawer, { ...options, visible: visible.value, destroyOnClose: destroyOnClose.value, ...props.value }),
).mount(root);

const handler = {
destroy() {
destroyOnClose.value = true;
nextTick(() => {
visible.value = false;
document.body.removeChild(root);
});
},
hide() {
visible.value = false;
},
show() {
visible.value = true;
},
update(options: DrawerOptions) {
props.value = options;
},
};

return handler;
};

Drawer.install = (app: App): void => {
// 添加插件入口
app.config.globalProperties.$drawer = Drawer;
};

const DrawerPlugin: WithInstallType<typeof Drawer> = Drawer;

export default DrawerPlugin;
2 changes: 1 addition & 1 deletion src/drawer/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export interface DrawerInstance {

export interface DrawerItem {
title: string;
icon: TNode;
icon?: TNode;
}

export type TriggerSource = 'overlay';
Expand Down
1 change: 1 addition & 0 deletions src/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { DialogPlugin } from './dialog';
export { MessagePlugin } from './message';
export { ToastPlugin } from './toast';
export { DrawerPlugin } from './drawer';
Loading