|
| 1 | +[](https://dashboard.stryker-mutator.io/reports/github.com/Djaler/vue-promise-dialogs/master) |
| 2 | + |
| 3 | +# Vue Promise Dialogs |
| 4 | + |
| 5 | +> A tiny & modern library that allows you to work with dialogs as with asynchronous functions. |
| 6 | +
|
| 7 | +## Why? |
| 8 | + |
| 9 | +Because many dialogs work exactly as promises. |
| 10 | +They are opened (called) and then closed with some result (resolved) or canceled (rejected). |
| 11 | + |
| 12 | +## Install |
| 13 | + |
| 14 | +```sh |
| 15 | +npm install --save vue-promise-dialogs |
| 16 | +``` |
| 17 | + |
| 18 | +Or for a CDN version, you can use it on [unpkg.com](https://unpkg.com/vue-promise-dialogs) |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +Main requirements: |
| 23 | + |
| 24 | +1. You should mount exactly one `PromiseDialogsWrapper` somewhere in your application root. |
| 25 | +2. The dialog component should accept `params` props. |
| 26 | +3. The dialog component should emit `resolve` or `reject` events when the user makes a decision. |
| 27 | + |
| 28 | +```ts |
| 29 | +import { createPromiseDialog } from "vue-promise-dialogs" |
| 30 | + |
| 31 | +const BooleanDialog = Vue.extend({ |
| 32 | + template: ` |
| 33 | + <div class="dialog"> |
| 34 | + <p>{{ params.text }}</p> |
| 35 | + <button name="true" @click="$emit('resolve', true)">True</button> |
| 36 | + <button name="false" @click="$emit('resolve', false)">False</button> |
| 37 | + <button name="cancel" @click="$emit('reject', 'cancel')">Cancel</button> |
| 38 | + </div> |
| 39 | + `, |
| 40 | + props: { |
| 41 | + params: { |
| 42 | + type: Object, |
| 43 | + required: true, |
| 44 | + }, |
| 45 | + }, |
| 46 | +}); |
| 47 | + |
| 48 | +// First type argument is the type of params prop that will be passed to component |
| 49 | +// Second type argument is the type of value with which the promise will be fulfilled |
| 50 | +const openDialog = createPromiseDialog<{ text: string }, boolean>(BooleanDialog); |
| 51 | + |
| 52 | +// When you call this function, dialog will be mounted to `PromiseDialogsWrapper`. |
| 53 | +// When user press any button and resolve/reject event emitted, promise will be settled and dialog will be destroyed. |
| 54 | +const result: boolean = await openDialog({ text: 'Some text' }); |
| 55 | +``` |
| 56 | + |
| 57 | +### Vue Class Component Example |
| 58 | + |
| 59 | +If you're using Vue Class Component to define your dialog components, you can extend them from `BasePromiseDialog`. |
| 60 | +This will allow you to avoid defining type arguments in `createPromiseDialog`. |
| 61 | +`BasePromiseDialog` already defines `params` prop and methods `resolve`/`reject`, so you don't have to. |
| 62 | + |
| 63 | +```ts |
| 64 | +import { Component } from "vue-class-component" |
| 65 | +import { BasePromiseDialog, createPromiseDialog } from "vue-promise-dialogs" |
| 66 | + |
| 67 | +@Component({ |
| 68 | + template: ` |
| 69 | + <div class="dialog"> |
| 70 | + <p>{{ params.text }}</p> |
| 71 | + <button name="true" @click="onTrue">True</button> |
| 72 | + <button name="false" @click="onFalse">False</button> |
| 73 | + <button name="cancel" @click="onCancel">Cancel</button> |
| 74 | + </div> |
| 75 | + `, |
| 76 | +}) |
| 77 | +// You need to extend your component from BasePromiseDialog with correct type arguments |
| 78 | +class BooleanDialog extends BasePromiseDialog<{ text: string }, boolean> { |
| 79 | + onTrue() { |
| 80 | + this.resolve(true); |
| 81 | + } |
| 82 | + |
| 83 | + onFalse() { |
| 84 | + this.resolve(true); |
| 85 | + } |
| 86 | + |
| 87 | + onCancel() { |
| 88 | + this.reject('cancel'); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// You don't have to specify type arguments because they are inferenced from BooleanDialog |
| 93 | +const openDialog = createPromiseDialog(BooleanDialog); |
| 94 | + |
| 95 | +const result: boolean = await openDialog({ text: 'Some text' }); |
| 96 | +``` |
0 commit comments