Skip to content

Commit

Permalink
Close #1953 add promise support to dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
jtommy committed Jul 31, 2020
1 parent 44a5857 commit 9d45c6a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/components/dialog/Dialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ export default {
return
}
}
this.$emit('confirm', this.prompt)
this.onConfirm(this.prompt, this)
if (this.closeOnConfirm) this.close()
},
Expand Down
31 changes: 16 additions & 15 deletions src/components/dialog/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import Dialog from './Dialog'

import { VueInstance } from '../../utils/config'
import config, { VueInstance } from '../../utils/config'
import { merge } from '../../utils/helpers'
import { use, registerComponent, registerComponentProgrammatic } from '../../utils/plugins'

let localVueInstance

function open(defaultParam, params) {
let slot
if (Array.isArray(params.message)) {
slot = params.message
delete params.message
}
const propsData = merge(defaultParam, params)
function open(propsData) {
const vm = typeof window !== 'undefined' && window.Vue ? window.Vue : localVueInstance || VueInstance
const DialogComponent = vm.extend(Dialog)
const instance = new DialogComponent({
const component = new DialogComponent({
el: document.createElement('div'),
propsData
})
if (slot) {
instance.$slots.default = slot
if (!config.defaultProgrammaticPromise) {
return component
} else {
return new Promise((resolve) => {
component.$on('confirm', (event) => resolve({ result: event || true, dialog: component }))
component.$on('cancel', () => resolve({ result: false, dialog: component }))
})
}
return instance
}

const DialogProgrammatic = {
Expand All @@ -35,18 +33,21 @@ const DialogProgrammatic = {
const defaultParam = {
canCancel: false
}
return open(defaultParam, params)
const propsData = merge(defaultParam, params)
return open(propsData)
},
confirm(params) {
const defaultParam = {}
return open(defaultParam, params)
const propsData = merge(defaultParam, params)
return open(propsData)
},
prompt(params) {
const defaultParam = {
hasInput: true,
confirmText: 'Done'
}
return open(defaultParam, params)
const propsData = merge(defaultParam, params)
return open(propsData)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/modal/Modal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export default {
*/
cancel(method) {
if (this.cancelOptions.indexOf(method) < 0) return
this.$emit('cancel', arguments)
this.onCancel.apply(null, arguments)
this.close()
},
Expand Down
1 change: 1 addition & 0 deletions src/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ let config = {
defaultTabsAnimated: true,
defaultTabsType: null,
defaultStatusIcon: true,
defaultProgrammaticPromise: false,
defaultLinkTags: [
'a',
'button',
Expand Down

0 comments on commit 9d45c6a

Please sign in to comment.