Skip to content
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
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,12 @@ let ok = await confirm("Are you sure ?");
### Closing dialogs

Your dialog must define a `returnValue` function. You may do so either in the setup function using Composition API or as
a method using Options API. To close the dialog, call `closeDialog()`. When you do so, the promise will resolve to the
a method using Options API. To close the dialog, call `resolveDialog()`. When you do so, the promise will resolve to the
result of the `returnValue` function. You may also resolve the promise to something else (for example null) by passing a
value to `closeDialog()`.
value to `resolveDialog()`.

Alertnatively, you can call `rejectDialog()`. This will throw a `DismissedDialog` error (you can also pass your own error as an argument),
meaning your promise won't be resolved, but rejected. As a result, the dialog will be closed as well.

### Typescript

Expand All @@ -178,8 +181,8 @@ for the centered div. It has one slot which is the content of the centered div.
The OkCancelBox.vue component is a Box that serves as base for all dialogs that include OK and CANCEL buttons. It has
two slots : `header` and `body`. Body is where the controls of the dialog reside. It has a `valid` prop. If `valid` is
false, the OK button is disabled. The whole thing is included into a `form` tag so that hitting enter when a control has
focus triggers a click on the OK button. When OK is clicked, `closeDialog` is called.
When CANCEL is clicked, `closeDialog` is called with a null return value.
focus triggers a click on the OK button. When OK is clicked, `resolveDialog` is called.
When CANCEL is clicked, `resolveDialog` is called with a null return value.

### ConfirmDialog.vue

Expand All @@ -189,4 +192,4 @@ resolves to true if the user clicks OK and to null if the use clicks CANCEL.
### TextDialog.vue

A TextBox is an OkCancelBox with a text field and a `returnValue` function that returns the content of the text field.
If the text field is empty, the valid prop is set to false on OkCancelBox.
If the text field is empty, the valid prop is set to false on OkCancelBox.
6 changes: 4 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import DialogWrapper from "./components/DialogWrapper.vue"
import {openDialog, closeDialog, PromiseDialog} from "./ts/lib";
import {openDialog, closeDialog, resolveDialog, rejectDialog, PromiseDialog} from "./ts/lib";

export {
DialogWrapper,
PromiseDialog,
openDialog,
closeDialog
closeDialog,
resolveDialog,
rejectDialog
}
31 changes: 28 additions & 3 deletions lib/ts/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,46 @@ export interface DialogInstance {
wrapper: string;
props: any;
resolve: (data: any) => void;
reject: (err: Error) => void;
}

export class DismissedDialog extends Error {}

export const dialogRef = shallowRef<DialogInstance>();

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

/**
* Closes the currently opened dialog, rejecting the promise with a DismissedDialog error, or with the given
* error if any.
*/
export function rejectDialog(err?: Error): void {
dialogRef.value.reject(err ?? new DismissedDialog('Dialog was dismissed.'));
dialogRef.value = null;
}

/**
* This is kept for backward compatility. Call either `resolveDialog()` or `rejectDialog()` instead.
* @deprecated
*/
export function closeDialog(data?: any): void {
if (data instanceof Error) {
rejectDialog(data);
} else {
resolveDialog(data);
}
}

/**
* Extracts the type of props from a component definition.
*/
Expand Down Expand Up @@ -55,12 +79,13 @@ type ReturnType<C extends DefineComponent<any, any, any, any, any>> = BindingRet
* @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 => {
return new Promise((resolve, reject) => {
dialogRef.value = {
dialog,
props,
wrapper,
resolve
resolve,
reject
}
});
}
Expand Down