Skip to content

Fix ts5 exports #12

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tabWidth": 2,
"useTabs": false
}
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"files.exclude": {},
"typescript.tsdk": "node_modules/typescript/lib"
}
47 changes: 20 additions & 27 deletions lib/components/DialogWrapper.vue
Original file line number Diff line number Diff line change
@@ -1,43 +1,36 @@
<template>
<transition v-bind="transitionAttrs">
<component :is="dialogRef.dialog" v-if="dialogRef && dialogRef.wrapper === name"
v-bind="dialogRef.props"
ref="dialogInstance"></component>
</transition>
<template v-for="(dialogRef, index) in dialogRefs" :key="index">
<transition v-bind="transitionAttrs">
<component
:is="dialogRef.dialog"
v-if="dialogRef && dialogRef.wrapper === name"
v-bind="dialogRef.props"
:ref="(ref: any) => (dialogRef.comp = ref)"
></component>
</transition>
</template>
</template>

<script lang="ts">

import { ComponentPublicInstance, defineComponent, ref, watch } from "vue";
import { dialogRef } from "../ts/lib";
import { defineComponent } from "vue";
import { dialogRefs } from "../ts/lib";

export default defineComponent({
name: 'DialogWrapper',
name: "DialogWrapper",
components: {},
props: {
name: {
type: String,
default: 'default'
default: "default",
},
transitionAttrs: Object
transitionAttrs: Object,
},
setup() {
const dialogInstance = ref<ComponentPublicInstance<typeof dialogRef.value.dialog>>();

watch(dialogInstance, () => {
if (dialogRef.value) {
dialogRef.value.comp = dialogInstance.value
}
})

return {
dialogRef,
dialogInstance
}
}
})
dialogRefs,
};
},
});
</script>

<style scoped lang="scss">

</style>
<style scoped lang="scss"></style>
11 changes: 3 additions & 8 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import DialogWrapper from "./components/DialogWrapper.vue"
import {openDialog, closeDialog, PromiseDialog} from "./ts/lib";
import DialogWrapper from "./components/DialogWrapper.vue";
import { openDialog, closeDialog, PromiseDialog } from "./ts/lib";

export {
DialogWrapper,
PromiseDialog,
openDialog,
closeDialog
}
export { DialogWrapper, PromiseDialog, openDialog, closeDialog };
92 changes: 51 additions & 41 deletions lib/ts/lib.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,57 @@
import {Component, shallowRef} from "vue";
import {DefineComponent} from "@vue/runtime-core";
import {
AllowedComponentProps,
Component,
VNodeProps,
shallowReactive,
} from "vue";

export interface DialogInstance {
comp?: any;
dialog: Component;
wrapper: string;
props: any;
resolve: (data: any) => void;
comp?: any;
dialog: Component;
wrapper: string;
props: unknown;
resolve: (data: unknown) => void;
}

export const dialogRef = shallowRef<DialogInstance>();
export const dialogRefs = shallowReactive<DialogInstance[]>([]);

/**
* Closes the currently opened dialog, resolving the promise with the return value of the dialog, or with the given
* Closes last opened dialog, resolving the promise with the return value of the dialog, or with the given
* data if any.
*/
export function closeDialog(data?: any) {
if (data === undefined) {
data = dialogRef.value.comp.returnValue();
}
dialogRef.value.resolve(data);
dialogRef.value = null;
export function closeDialog(data?: unknown) {
const lastDialog = dialogRefs.pop();
if (data === undefined) {
data = lastDialog.comp.returnValue();
}
lastDialog.resolve(data);
}

/**
* Extracts the type of props from a component definition.
*/
type PropsType<C extends DefineComponent<any, any, any>> = InstanceType<C>["$props"];
type PropsType<C extends Component> = C extends new (...args: any) => any
? Omit<
InstanceType<C>["$props"],
keyof VNodeProps | keyof AllowedComponentProps
>
: never;

/**
* Extracts the return type of the dialog from the setup function.
*/
type BindingReturnType<C extends DefineComponent<any, any, any>> = C extends DefineComponent<any, infer X, any> ?
(X extends { returnValue: () => infer Y } ? Y : never)
: never;

/**
* Extracts the return type of the dialog from the methods.
*/
type MethodReturnType<C extends DefineComponent<any, any, any, any, any>> = C extends DefineComponent<any, any, any, any, infer X> ?
(X extends { returnValue: () => infer Y } ? Y : never)
: never;
type BindingReturnType<C extends Component> = C extends new (
...args: any
) => any
? InstanceType<C> extends { returnValue: () => infer Y }
? Y
: never
: never;

/**
* Extracts the return type of the dialog either from the setup method or from the methods.
*/
type ReturnType<C extends DefineComponent<any, any, any, any, any>> = BindingReturnType<C> extends never ? MethodReturnType<C> : BindingReturnType<C>;
type ReturnType<C extends Component> = BindingReturnType<C>;

/**
* Opens a dialog.
Expand All @@ -54,21 +60,25 @@ type ReturnType<C extends DefineComponent<any, any, any, any, any>> = BindingRet
* @param wrapper The dialog wrapper you want the dialog to open into.
* @return A promise that resolves when the dialog is closed
*/
export function openDialog<C extends DefineComponent<any, any, any, any, any>>(dialog: C, props?: PropsType<C>, wrapper: string = 'default'): Promise<ReturnType<C>> {
return new Promise(resolve => {
dialogRef.value = {
dialog,
props,
wrapper,
resolve
}
export function openDialog<C extends Component>(
dialog: C,
props?: PropsType<C>,
wrapper: string = "default"
): Promise<ReturnType<C>> {
return new Promise((resolve) => {
dialogRefs.push({
dialog,
props,
wrapper,
resolve,
});
});
}

export const PromiseDialog = {
install: (app, options) => {
app.config.globalProperties.$close = (comp, alternateValue) => {
closeDialog(alternateValue);
}
}
}
install: (app) => {
app.config.globalProperties.$close = (comp, alternateValue) => {
closeDialog(alternateValue);
};
},
};
Loading