-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from SinglePoi/feat/cmd-msgbox
feat: command for messagebox
- Loading branch information
Showing
7 changed files
with
204 additions
and
9 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
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,128 @@ | ||
import { createVNode, render, type ComponentPublicInstance } from "vue"; | ||
import MessageBoxConstructor from "./MessageBox.vue"; | ||
import type { IMessageBoxProps } from "~/composables/messageBox/modal"; | ||
|
||
interface MessageBoxOptions { | ||
/** Text content of confirm button */ | ||
confirmBtnText: string; | ||
/** Text content of cancel button */ | ||
cancelBtnText: string; | ||
/** Custom element to append the message box to */ | ||
appendTo?: HTMLElement | string; | ||
} | ||
|
||
type Action = "confirm" | "cancel"; | ||
|
||
interface MessageBoxProps extends IMessageBoxProps { | ||
container: HTMLElement; | ||
} | ||
|
||
const messageInstance = new Map< | ||
ComponentPublicInstance<MessageBoxProps>, | ||
{ | ||
options: any; | ||
reject: (res: any) => void; | ||
resolve: (reson?: any) => void; | ||
} | ||
>(); | ||
|
||
const genContainer = () => { | ||
return document.createElement("div"); | ||
}; | ||
|
||
const getAppendToElement = (props: any): HTMLElement => { | ||
let appendTo: HTMLElement | null = document.body; | ||
if (props.appendTo) { | ||
if (typeof props.appendTo === "string") { | ||
appendTo = document.querySelector<HTMLElement>(props.appendTo); | ||
} | ||
if (props.appendTo instanceof Element) { | ||
appendTo = props.appendTo; | ||
} | ||
if (!(appendTo instanceof Element)) { | ||
appendTo = document.body; | ||
} | ||
} | ||
return appendTo; | ||
}; | ||
|
||
const teardown = ( | ||
vm: ComponentPublicInstance<MessageBoxProps>, | ||
container: HTMLElement | ||
) => { | ||
render(null, container); | ||
messageInstance.delete(vm); | ||
}; | ||
|
||
const initInstance = (props: any, container: HTMLElement) => { | ||
const vnode = createVNode(MessageBoxConstructor, props); | ||
render(vnode, container); | ||
getAppendToElement(props).appendChild(container.firstElementChild!); | ||
return vnode.component; | ||
}; | ||
|
||
const showMessage = (options: any) => { | ||
const container = genContainer(); | ||
|
||
options.onConfirm = () => { | ||
const currentMsg = messageInstance.get(vm)!; | ||
currentMsg.resolve("confirm"); | ||
|
||
teardown(vm, container); | ||
}; | ||
|
||
options.onCancel = () => { | ||
const currentMsg = messageInstance.get(vm)!; | ||
currentMsg.reject("cancel"); | ||
|
||
teardown(vm, container); | ||
}; | ||
|
||
const instance = initInstance(options, container)!; | ||
|
||
const vm = instance.proxy as ComponentPublicInstance<MessageBoxProps>; | ||
|
||
vm.container = container; | ||
|
||
for (const prop in options) { | ||
if (Object.hasOwn(options, prop) && !Object.hasOwn(vm.$props, prop)) { | ||
vm[prop as keyof ComponentPublicInstance] = options[prop]; | ||
} | ||
} | ||
|
||
return vm; | ||
}; | ||
|
||
function MessageBox( | ||
content: string = "Are you sure?", | ||
title: string = "Tips", | ||
options?: MessageBoxOptions | ||
): Promise<Action> { | ||
return new Promise((resolve, reject) => { | ||
const vm = showMessage( | ||
Object.assign( | ||
{ | ||
content, | ||
title, | ||
isShowModal: true, | ||
confirmBtnText: "Confirm", | ||
cancelBtnText: "Cancel", | ||
}, | ||
options | ||
) | ||
); | ||
messageInstance.set(vm, { | ||
options, | ||
resolve, | ||
reject, | ||
}); | ||
}); | ||
} | ||
|
||
MessageBox.close = () => { | ||
messageInstance.forEach((_, vm) => { | ||
teardown(vm, vm.container); | ||
}); | ||
}; | ||
|
||
export default MessageBox; |
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
65 changes: 65 additions & 0 deletions
65
apps/client/components/main/MessageBox/tests/message-box.spec.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,65 @@ | ||
import { afterEach, describe, expect, test } from "vitest"; | ||
import MessageBox from "../MessageBox"; | ||
import { nextTick } from "vue"; | ||
import { flushPromises } from "@vue/test-utils"; | ||
|
||
const selector = ".modal"; | ||
|
||
describe("MessageBox", () => { | ||
afterEach(async () => { | ||
MessageBox.close(); | ||
document.body.innerHTML = ""; | ||
await flushPromises(); | ||
}); | ||
|
||
test("create messageBox", async () => { | ||
MessageBox("这是一段内容", "消息"); | ||
let msgbox: any = document.querySelector(selector); | ||
|
||
expect(msgbox).toBeDefined(); | ||
expect(msgbox.querySelector(".font-bold").textContent).toEqual("消息"); | ||
expect(msgbox.querySelector(".py-4").textContent).toEqual("这是一段内容"); | ||
}); | ||
|
||
test("close messageBox", async () => { | ||
MessageBox("这是一段内容", "消息"); | ||
|
||
MessageBox.close(); | ||
let msgbox: any = document.querySelector(selector); | ||
expect(msgbox).toBe(null); | ||
}); | ||
|
||
describe("promise", () => { | ||
test("confirm", async () => { | ||
let msgAction = ""; | ||
MessageBox("此操作将永久删除该文件, 是否继续?", "提示").then((action) => { | ||
msgAction = action; | ||
}); | ||
await flushPromises(); | ||
const btn = document | ||
.querySelector(selector)! | ||
.querySelector(".confirm") as HTMLButtonElement; | ||
btn.click(); | ||
await flushPromises(); | ||
|
||
expect(msgAction).toEqual("confirm"); | ||
}); | ||
|
||
test("cancel", async () => { | ||
let msgAction = ""; | ||
MessageBox("此操作将永久删除该文件, 是否继续?", "提示").catch( | ||
(action) => { | ||
msgAction = action; | ||
} | ||
); | ||
await flushPromises(); | ||
const btn = document | ||
.querySelector(selector)! | ||
.querySelector(".cancel") as HTMLButtonElement; | ||
btn.click(); | ||
await flushPromises(); | ||
|
||
expect(msgAction).toEqual("cancel"); | ||
}); | ||
}); | ||
}); |
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