Skip to content

Commit 5413518

Browse files
committed
Fix(directive): respond to updated directive value
1 parent fee48f8 commit 5413518

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

src/plugin/directive.dialog.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ import {CONFIRM_TYPES, DIRECTIVE_ATTRIBUTE_KEY} from './constants'
33

44

55
const DirectiveDialog = function (app) {
6+
this.shouldIgnoreClick = false
7+
this.bindingOptions = {
8+
9+
}
10+
611
Object.defineProperties(this, {
712
app: { get: () => app },
813
})
914
}
1015

11-
DirectiveDialog.prototype.shouldIgnoreClick = false
12-
1316
DirectiveDialog.prototype.getConfirmMessage = function (binding) {
1417
if (binding.value && binding.value.message) {
1518
return binding.value.message
@@ -70,15 +73,20 @@ DirectiveDialog.prototype.clickHandler = function (event, el, binding) {
7073

7174
DirectiveDialog.prototype.defineConfirm = function () {
7275
type BindFn = (el: unknown, binding: unknown) => void
73-
const DirectiveDefinition: {mounted: BindFn, unmounted: BindFn} = {
76+
type UpdateFn = (el: unknown, binding: unknown, vnode: unknown, prevVnode: unknown) => void
77+
const DirectiveDefinition: {mounted: BindFn, unmounted: BindFn, updated: UpdateFn} = {
7478
mounted: (el, binding) => {
7579
el[DIRECTIVE_ATTRIBUTE_KEY] = el[DIRECTIVE_ATTRIBUTE_KEY] || {}
76-
7780
el[DIRECTIVE_ATTRIBUTE_KEY].clickHandler = event => this.clickHandler(event, el, binding)
7881

7982
el.addEventListener('click', el[DIRECTIVE_ATTRIBUTE_KEY].clickHandler, true)
8083
},
81-
unmounted(el) {
84+
updated: (el, binding, vnode, prevVnode) => {
85+
el.removeEventListener('click', el[DIRECTIVE_ATTRIBUTE_KEY].clickHandler, true)
86+
el[DIRECTIVE_ATTRIBUTE_KEY].clickHandler = event => this.clickHandler(event, el, binding)
87+
el.addEventListener('click', el[DIRECTIVE_ATTRIBUTE_KEY].clickHandler, true)
88+
},
89+
unmounted: (el) => {
8290
el.removeEventListener('click', el[DIRECTIVE_ATTRIBUTE_KEY].clickHandler, true)
8391
delete el[DIRECTIVE_ATTRIBUTE_KEY]
8492
}

src/plugin/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ interface DialogPluginOptions extends Omit<DialogWindowOptionsInterface, 'id'>{}
1111

1212
const DialogPlugin = {
1313
install(app: App, options: DialogPluginOptions) {
14-
const DirectivesObj = new DirectiveDialog(app)
15-
app.directive('confirm', DirectivesObj.defineConfirm())
14+
const DirectivesInstance = new DirectiveDialog(app)
15+
app.directive('confirm', DirectivesInstance.defineConfirm())
1616

1717
const dialog = new PromiseDialog(app, options)
1818

src/views/IndexView.vue

+17-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
<hr style="margin: 35px 0;" />
1313
<div style="width: 100%;display: grid; grid-gap: 15px; grid-template-columns: repeat(auto-fill, 200px);justify-content: center">
1414
<button class="dg-btn" v-confirm="'Please confirm!'">Click Directive</button>
15-
<a href="https://example.com" v-confirm:soft="'Visit external link?'">Example website</a>
15+
<a href="https://example.com" v-confirm:soft="message.visitExternal">Example website</a>
1616
</div>
1717
</div>
1818
</template>
1919

2020
<script lang="ts">
21-
import {defineComponent} from "vue";
21+
import {defineComponent, nextTick, ref} from "vue";
2222
2323
export default defineComponent({
2424
methods: {
@@ -79,6 +79,21 @@ export default defineComponent({
7979
console.log('Prompt dismissed');
8080
});
8181
},
82+
},
83+
setup() {
84+
const message = ref({
85+
visitExternal: 'Visit external link?'
86+
})
87+
setTimeout(() => {
88+
message.value = {
89+
visitExternal: 'Visit this external link?'
90+
}
91+
nextTick(() => {
92+
console.log('#'.repeat(45), '[message.value.visitExternal]', message.value.visitExternal)
93+
})
94+
}, 5000)
95+
96+
return { message }
8297
}
8398
})
8499

0 commit comments

Comments
 (0)