Skip to content

Commit d14e5ba

Browse files
author
Peter Josling
committed
Support contextmenu event binding for table rows
Allows for right click as well as existing click + double click.
1 parent d471502 commit d14e5ba

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/components/table/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@
4141
}
4242
]
4343
},
44+
{
45+
"event": "row-contextmenu",
46+
"description": "Emitted when a row is right clicked.",
47+
"args": [
48+
{
49+
"arg": "item",
50+
"description": "Item data of the row being right clicked."
51+
},
52+
{
53+
"arg": "index",
54+
"description": "Index of the row being right clicked."
55+
},
56+
{
57+
"arg": "event",
58+
"description": "Native event object."
59+
}
60+
]
61+
},
4462
{
4563
"event": "row-hovered",
4664
"description": "Emitted when a row is hovered.",

src/components/table/table.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ export default {
249249
click: evt => {
250250
this.rowClicked(evt, item, rowIndex)
251251
},
252+
contextmenu: evt => {
253+
this.rowContextmenu(evt, item, rowIndex)
254+
},
252255
dblclick: evt => {
253256
this.rowDblClicked(evt, item, rowIndex)
254257
},
@@ -926,6 +929,13 @@ export default {
926929
}
927930
this.$emit('row-hovered', item, index, e)
928931
},
932+
rowContextmenu (e, item, index) {
933+
if (this.stopIfBusy(e)) {
934+
// If table is busy (via provider) then don't propagate
935+
return
936+
}
937+
this.$emit('row-contextmenu', item, index, e)
938+
},
929939
headClicked (e, field) {
930940
if (this.stopIfBusy(e)) {
931941
// If table is busy (via provider) then don't propagate

src/components/table/table.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,25 @@ describe('table', async () => {
404404
}
405405
})
406406

407+
it('each data row should emit a row-contextmenu event when right clicked', async () => {
408+
const { app: { $refs } } = window
409+
const vm = $refs.table_paginated
410+
411+
const tbody = [...vm.$el.children].find(el => el && el.tagName === 'TBODY')
412+
expect(tbody).toBeDefined()
413+
if (tbody) {
414+
const trs = [...tbody.children]
415+
expect(trs.length).toBe(vm.perPage)
416+
trs.forEach((tr, idx) => {
417+
const spy = jest.fn()
418+
vm.$on('row-contextmenu', spy)
419+
tr.dispatchEvent(new MouseEvent('contextmenu', {button: 2}))
420+
vm.$off('row-contextmenu', spy)
421+
expect(spy).toHaveBeenCalled()
422+
})
423+
}
424+
})
425+
407426
it('each header th should emit a head-clicked event when clicked', async () => {
408427
const { app: { $refs } } = window
409428
const vm = $refs.table_paginated

0 commit comments

Comments
 (0)