Skip to content

Commit

Permalink
Add support for opening/closing ContextMenu/GridContextMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Aug 6, 2024
1 parent b023668 commit 9129226
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,41 @@ internal fun DynaNodeGroup.contextMenuTestbatch() {
expect(1) { clicked }
}

test("setOpened(true) fires ContextMenuBase.OpenedChangeEvent") {
var called = false
lateinit var cm: ContextMenu
UI.getCurrent().div {
cm = contextMenu {
item("click me")
}
}
cm.addOpenedChangeListener { e ->
called = true
expect(true) { e.isOpened }
expect(false) { e.isFromClient }
}
cm.setOpened(true)
expect(true) { called }
}

test("setOpened(false) fires ContextMenuBase.OpenedChangeEvent") {
var called = false
lateinit var cm: ContextMenu
UI.getCurrent().div {
cm = contextMenu {
item("click me")
}
}
cm.setOpened(true)
cm.addOpenedChangeListener { e ->
called = true
expect(false) { e.isOpened }
expect(false) { e.isFromClient }
}
cm.setOpened(false)
expect(true) { called }
}

group("grid context menu") {
test("simple click") {
lateinit var clicked: String
Expand Down Expand Up @@ -308,5 +343,42 @@ internal fun DynaNodeGroup.contextMenuTestbatch() {
expect(true) { listenerCalled }
expect("foo") { clicked }
}

test("setOpened(true) fires GridContextMenuOpenedEvent") {
var called = false
lateinit var cm: GridContextMenu<String>
UI.getCurrent().grid<String> {
cm = gridContextMenu {
item("click me")
}
}
cm.addGridContextMenuOpenedListener() { e ->
called = true
expect(true) { e.isOpened }
expect(false) { e.isFromClient }
expect("foo") { e.item.orElse(null) }
}
cm.setOpened(true, "foo")
expect(true) { called }
}

test("setOpened(false) fires GridContextMenuOpenedEvent") {
var called = false
lateinit var cm: GridContextMenu<String>
UI.getCurrent().grid<String> {
cm = gridContextMenu {
item("click me")
}
}
cm.setOpened(true, "foo")
cm.addGridContextMenuOpenedListener { e ->
called = true
expect(false) { e.isOpened }
expect(false) { e.isFromClient }
expect("foo") { e.item.orElse(null) }
}
cm.setOpened(false, "foo")
expect(true) { called }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,18 @@ private fun MenuItemBase<*, *, *>.checkMenuItemEnabled(originalItem: MenuItemBas
}
}
}

/**
* Opens or closes the menu. Fires the [ContextMenuBase.OpenedChangeEvent].
*/
public fun ContextMenu.setOpened(opened: Boolean) {
element.setProperty("opened", opened)
}

/**
* Opens or closes the menu. Fires the [ContextMenuBase.OpenedChangeEvent].
*/
public fun <T> GridContextMenu<T>.setOpened(opened: Boolean, gridItem: T?) {
_setContextMenuTargetItemKey(gridItem)
element.setProperty("opened", opened)
}

0 comments on commit 9129226

Please sign in to comment.