Skip to content

Commit 7f73571

Browse files
committed
bug fixed #457
1 parent 7ab29cb commit 7f73571

File tree

3 files changed

+86
-20
lines changed

3 files changed

+86
-20
lines changed

packages/ve-contextmenu/src/index.jsx

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,13 @@ export default {
156156

157157
watch: {
158158
options: {
159-
handler: function () {
160-
this.rootContextmenuId = this.getRandomIdWithPrefix();
161-
this.createInternalOptions();
162-
this.createPanelOptions({ options: this.internalOptions });
159+
handler: function (val) {
160+
if (Array.isArray(val) && val.length > 0) {
161+
this.rootContextmenuId = this.getRandomIdWithPrefix();
162+
this.createInternalOptions();
163+
this.createPanelOptions({ options: this.internalOptions });
164+
this.addRootContextmenuPanelToBody();
165+
}
163166
},
164167
immediate: true,
165168
},
@@ -327,14 +330,16 @@ export default {
327330
event.preventDefault();
328331
const { rootContextmenuId } = this;
329332

330-
// refresh contextmenu
331-
this.resetContextmenu();
332-
this.showContextmenuPanel({
333-
event,
334-
contextmenuId: rootContextmenuId,
335-
isRootContextmenu: true,
336-
});
337-
this.isPanelsEmptyed = false;
333+
if (rootContextmenuId) {
334+
// refresh contextmenu
335+
this.resetContextmenu();
336+
this.showContextmenuPanel({
337+
event,
338+
contextmenuId: rootContextmenuId,
339+
isRootContextmenu: true,
340+
});
341+
this.isPanelsEmptyed = false;
342+
}
338343
},
339344

340345
// show contextmenu panel
@@ -507,9 +512,11 @@ export default {
507512

508513
// add root contextmenu panel to body
509514
addRootContextmenuPanelToBody() {
510-
this.addContextmenuPanelToBody({
511-
contextmenuId: this.rootContextmenuId,
512-
});
515+
if (this.rootContextmenuId) {
516+
this.addContextmenuPanelToBody({
517+
contextmenuId: this.rootContextmenuId,
518+
});
519+
}
513520
},
514521

515522
// register contextmenu event

packages/ve-table/src/index.jsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,11 +1948,9 @@ export default {
19481948
this.parentRendered = true;
19491949

19501950
// set contextmenu event target
1951-
if (this.hasContextmenu) {
1952-
this.contextmenuEventTarget = this.$el.querySelector(
1953-
`.${clsName("body")}`,
1954-
);
1955-
}
1951+
this.contextmenuEventTarget = this.$el.querySelector(
1952+
`.${clsName("body")}`,
1953+
);
19561954

19571955
// create hook instance
19581956
this.hooks = new Hooks();

tests/unit/specs/ve-contextmenu.spec.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,67 @@ describe("veContextmenu", () => {
163163
wrapper.destroy();
164164
});
165165

166+
it("contextmenu async contextmenu option", async () => {
167+
const wrapper = mount(
168+
{
169+
render() {
170+
return (
171+
<div>
172+
<ve-contextmenu
173+
eventTarget={document.querySelector(
174+
"#contextmenu-target",
175+
)}
176+
options={this.options}
177+
/>
178+
</div>
179+
);
180+
},
181+
data() {
182+
return {
183+
options: [],
184+
};
185+
},
186+
},
187+
// need attach to documnet
188+
{ attachTo: document.body },
189+
);
190+
191+
const contextmenuTargetEl = document.querySelector(
192+
"#contextmenu-target",
193+
);
194+
195+
//trigger element hover
196+
const event = new MouseEvent("contextmenu", {
197+
view: window, // window
198+
bubbles: true,
199+
cancelable: true,
200+
});
201+
202+
contextmenuTargetEl.dispatchEvent(event);
203+
204+
await later();
205+
206+
const contextmenuPoppers = document.querySelectorAll(
207+
".ve-contextmenu-popper",
208+
);
209+
expect(contextmenuPoppers.length).toBe(0);
210+
211+
wrapper.vm.options = OPTIONS;
212+
213+
await later();
214+
215+
contextmenuTargetEl.dispatchEvent(event);
216+
217+
await later();
218+
219+
const contextmenuPoppers2 = document.querySelectorAll(
220+
".ve-contextmenu-popper",
221+
);
222+
expect(contextmenuPoppers2.length).toBe(1);
223+
224+
wrapper.destroy();
225+
});
226+
166227
it("contextmenu eventTarget", async () => {
167228
const wrapper = mount(
168229
{

0 commit comments

Comments
 (0)