This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: api of dialog component (#646)
#### What type of PR is this? /kind api-change /kind improvement /milestone 2.0 #### What this PR does / why we need it: 重构 Dialog 组件使用 API 的调用方式,改为与 Toast 组件一致。#644 同样的,使用此方式调用 Dialog 组件不限制在 Vue 组件。 #### Special notes for your reviewer: /cc @halo-dev/sig-halo-console 需要测试后台各个操作的会话框是否正常。 #### Does this PR introduce a user-facing change? ```release-note 重构 Dialog 组件使用 API 的调用方式。 ```
- Loading branch information
Showing
26 changed files
with
153 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 0 additions & 27 deletions
27
packages/components/src/components/dialog/DialogProvider.vue
This file was deleted.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
packages/components/src/components/dialog/dialog-manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import DialogComponent from "./Dialog.vue"; | ||
import { createVNode, render, type Component } from "vue"; | ||
import type { DialogProps } from "./interface"; | ||
|
||
export type DialogApiProps = Omit<DialogProps, "type" | "visible">; | ||
|
||
export type DialogApi = (props?: DialogApiProps) => void; | ||
|
||
export interface DialogEntry { | ||
(props: DialogProps): void; | ||
info: DialogApi; | ||
success: DialogApi; | ||
error: DialogApi; | ||
warning: DialogApi; | ||
} | ||
|
||
const defaultProps: DialogProps = { | ||
title: "", | ||
visible: false, | ||
}; | ||
|
||
const dialog: DialogEntry = (userProps: DialogProps) => { | ||
const props = { | ||
...defaultProps, | ||
...userProps, | ||
}; | ||
|
||
let container = document.body.querySelector(".dialog-container"); | ||
if (!container) { | ||
container = document.createElement("div"); | ||
container.className = "dialog-container"; | ||
document.body.appendChild(container); | ||
} | ||
|
||
const { vnode, container: hostContainer } = createVNodeComponent( | ||
DialogComponent, | ||
props | ||
); | ||
|
||
if (hostContainer.firstElementChild) { | ||
container.appendChild(hostContainer.firstElementChild); | ||
} | ||
|
||
if (vnode.component?.props) { | ||
vnode.component.props.visible = true; | ||
} | ||
|
||
if (vnode?.props) { | ||
// close emit | ||
|
||
vnode.props.onClose = () => { | ||
container?.remove(); | ||
render(null, hostContainer); | ||
}; | ||
} | ||
}; | ||
|
||
function createVNodeComponent( | ||
component: Component, | ||
props: Record<string, unknown> | ||
) { | ||
const vnode = createVNode(component, props); | ||
const container = document.createElement("div"); | ||
render(vnode, container); | ||
return { vnode, container }; | ||
} | ||
|
||
dialog.success = (props?: DialogApiProps) => | ||
dialog({ ...props, type: "success" }); | ||
dialog.info = (props?: DialogApiProps) => dialog({ ...props, type: "info" }); | ||
dialog.warning = (props?: DialogApiProps) => | ||
dialog({ ...props, type: "warning" }); | ||
dialog.error = (props?: DialogApiProps) => dialog({ ...props, type: "error" }); | ||
|
||
export { dialog as Dialog }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export { default as VDialog } from "./Dialog.vue"; | ||
export { default as VDialogProvider } from "./DialogProvider.vue"; | ||
export * from "./use-dialog"; | ||
export { Dialog } from "./dialog-manager"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.