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: inherit global components and provides from root #143

Merged
merged 3 commits into from
Aug 10, 2021
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
18 changes: 16 additions & 2 deletions src/qComponents/QMessageBox/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import { createMessageBox as useMessageBox } from './src';
import { getCurrentInstance } from 'vue';

import { createMessageBox } from './src';
import { QMessageBox, QMessageBoxHookOptions } from './src/types';

const useMessageBox = (options?: QMessageBoxHookOptions): QMessageBox => {
const parentInstance = getCurrentInstance();

const messageBox = createMessageBox({
parentInstance,
...options
});

return messageBox;
};

export { useMessageBox };
export { QMessageBoxContent } from './src/QMessageBoxContent';
Expand All @@ -8,4 +22,4 @@ export type {
QMessageBoxContentInstance
} from './src/QMessageBoxContent';
export { QMessageBoxAction } from './src/constants';
export type { QMessageBoxOptions } from './src/types';
export type { QMessageBox, QMessageBoxOptions } from './src/types';
109 changes: 68 additions & 41 deletions src/qComponents/QMessageBox/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,85 @@ import { QMessageBoxContainer } from './QMessageBoxContainer';
import type { QMessageBoxContainerInstance } from './QMessageBoxContainer';
import { QMessageBoxAction } from './constants';
import type {
QMessageBoxHookOptions,
QMessageBoxContent,
QMessageBoxOptions,
QMessageBoxEvent,
MessageBoxPromise
MessageBoxPromise,
QMessageBox
} from './types';

export const createMessageBox = (
content: QMessageBoxContent,
options?: QMessageBoxOptions
): Promise<QMessageBoxEvent> => {
let messageBoxPromise: MessageBoxPromise;
let app: Optional<App<Element>>;
config?: QMessageBoxHookOptions
): QMessageBox => {
const messageBox = (
content: QMessageBoxContent,
options?: QMessageBoxOptions
): Promise<QMessageBoxEvent> => {
let messageBoxPromise: MessageBoxPromise;
let app: Optional<App<Element>>;

const handleDone = ({ action, payload }: QMessageBoxEvent): void => {
if (action === QMessageBoxAction.confirm) {
messageBoxPromise.resolve({ action, payload });
} else if (
action === QMessageBoxAction.cancel ||
action === QMessageBoxAction.close
) {
messageBoxPromise.reject({ action, payload });
}
};
const handleDone = ({ action, payload }: QMessageBoxEvent): void => {
if (action === QMessageBoxAction.confirm) {
messageBoxPromise.resolve({ action, payload });
} else if (
action === QMessageBoxAction.cancel ||
action === QMessageBoxAction.close
) {
messageBoxPromise.reject({ action, payload });
}
};

const handleRemove = (): void => {
if (!app) return;
const handleRemove = (): void => {
if (!app) return;

app.unmount();
options?.onUnmounted?.(app);
};
app.unmount();
options?.onUnmounted?.(app);
};

nextTick(() => {
app = createApp(QMessageBoxContainer, {
...(options ?? {}),
content,
onDone: handleDone,
onRemove: handleRemove
});
nextTick(() => {
app = createApp(QMessageBoxContainer, {
...(options ?? {}),
content,
onDone: handleDone,
onRemove: handleRemove
});

options?.onBeforeMount?.(app);
const parentAppContext = config?.parentInstance?.appContext;

const container = app.mount(document.createElement('div'));
options?.onMounted?.(
app,
container as NonNullable<UnwrappedInstance<QMessageBoxContainerInstance>>
);
});
// Register a global components from main app instance
const components = parentAppContext?.components ?? {};
Object.entries(components).forEach(([key, value]) => {
app?.component(key, value);
});

return new Promise((resolve, reject) => {
messageBoxPromise = {
resolve,
reject
};
});
// Reprovide a global provides from main app instance
const provides = parentAppContext?.provides ?? {};
const providerKeys = Object.getOwnPropertySymbols(provides);
providerKeys.forEach(key => {
// TS does not allow use 'symbol' as index type, so we pretend like we don't
const value = provides[key as unknown as string];
if (value) app?.provide(key, value);
});

options?.onBeforeMount?.(app);

const container = app.mount(document.createElement('div'));
options?.onMounted?.(
app,
container as NonNullable<
UnwrappedInstance<QMessageBoxContainerInstance>
>
);
});

return new Promise((resolve, reject) => {
messageBoxPromise = {
resolve,
reject
};
});
};

return messageBox;
};
14 changes: 13 additions & 1 deletion src/qComponents/QMessageBox/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { App } from 'vue';
import type { App, ComponentInternalInstance } from 'vue';

import type { Nullable, UnwrappedInstance } from '#/helpers';

Expand All @@ -16,7 +16,12 @@ type Styles = Record<string, string | number>;
export type QMessageBoxOptionWrapClass = Nullable<string | Classes | Classes[]>;
export type QMessageBoxOptionWrapStyle = Nullable<string | Styles | Styles[]>;

export interface QMessageBoxHookOptions {
parentInstance?: Nullable<ComponentInternalInstance>;
}

export interface QMessageBoxOptions {
parentInstance?: Nullable<ComponentInternalInstance>;
closeOnClickShadow?: Nullable<boolean>;
distinguishCancelAndClose?: Nullable<boolean>;
wrapClass?: QMessageBoxOptionWrapClass;
Expand All @@ -38,3 +43,10 @@ export interface MessageBoxPromise {
resolve: (event: QMessageBoxEvent) => void;
reject: (event: QMessageBoxEvent) => void;
}

export interface QMessageBox {
(
content: QMessageBoxContent,
options?: QMessageBoxOptions
): Promise<QMessageBoxEvent>;
}
4 changes: 3 additions & 1 deletion stories/plugins/QMessageBox/Component.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ const storyMetadata: Meta = {
const QMessageBoxComponentStory: Story<never> = () =>
defineComponent({
setup() {
const messageBox = useMessageBox();

const handleClick = async (): Promise<void> => {
try {
const result = await useMessageBox(
const result = await messageBox(
defineAsyncComponent(() => import('./MessageBoxFormTest.vue')),
{ distinguishCancelAndClose: true }
);
Expand Down
4 changes: 3 additions & 1 deletion stories/plugins/QMessageBox/Default.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const storyMetadata: Meta = {
const QMessageBoxStory: Story<QMessageBoxContentProps> = args =>
defineComponent({
setup() {
const messageBox = useMessageBox();

const beforeClose: QMessageBoxContentPropBeforeClose = async action => {
if (action !== 'confirm') return true;

Expand All @@ -34,7 +36,7 @@ const QMessageBoxStory: Story<QMessageBoxContentProps> = args =>

const handleClick = async (): Promise<void> => {
try {
const result = await useMessageBox(
const result = await messageBox(
{
title: args.title,
message: args.message,
Expand Down
4 changes: 3 additions & 1 deletion stories/plugins/QMessageBox/Extended.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ const storyMetadata: Meta = {
const QMessageBoxExtendedStory: Story<never> = () =>
defineComponent({
setup() {
const messageBox = useMessageBox();

const handleClick = async (): Promise<void> => {
try {
const result = await useMessageBox(
const result = await messageBox(
{
component: defineAsyncComponent(
() => import('./MessageBoxFormTest.vue')
Expand Down