Skip to content

Commit

Permalink
ContextMenu: setOpened(true) calls dynamic content handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Aug 7, 2024
1 parent 00f0d3c commit 293e4d9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,28 @@ internal fun DynaNodeGroup.contextMenuTestbatch() {
expect(true) { called }
}

test("setOpened(true) calls dynamic handler") {
var called = 0
lateinit var cm: GridContextMenu<String>
UI.getCurrent().grid<String> {
cm = gridContextMenu {
item("click me")
setDynamicContentHandler { item ->
expect("foo") { item }
expect(0) { called }
called = 1
true
}
}
}
cm.addGridContextMenuOpenedListener { e ->
expect(1) { called }
called = 2
}
cm.setOpened(true, "foo")
expect(2) { called }
}

test("setOpened(false) fires GridContextMenuOpenedEvent") {
var called = false
lateinit var cm: GridContextMenu<String>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import kotlin.test.fail
*/
public fun HasMenuItems._clickItemMatching(searchSpec: SearchSpec<MenuItemBase<*, *, *>>) {
// fires ContextMenuOpenedListener to simulate menu opening
(this as Component).element.setProperty("opened", true)
(this as? ContextMenu)?.setOpened(true)

val parentMap: Map<MenuItemBase<*, *, *>, Component> = (this as Component).getParentMap()
val predicate = searchSpec.toPredicate()
Expand All @@ -28,7 +28,7 @@ public fun HasMenuItems._clickItemMatching(searchSpec: SearchSpec<MenuItemBase<*
(item as MenuItem)._click(parentMap)

// fires ContextMenuOpenedListener to simulate menu closing
(this as Component).element.setProperty("opened", false)
(this as? ContextMenu)?.setOpened(false)
}

/**
Expand Down Expand Up @@ -107,14 +107,6 @@ public fun <T> GridContextMenu<T>._clickItemMatching(searchSpec: SearchSpec<Menu
// fires ContextMenuOpenedListener to simulate menu opening
setOpened(true, gridItem)

// notify the context menu dynamic item generator
dynamicContentHandler?.also {
val openMenu: Boolean = it.test(gridItem)
if (!openMenu) {
fail("The dynamic content handler returned false signalling the menu should not open:\n${toPrettyTree()}")
}
}

val parentMap: Map<MenuItemBase<*, *, *>, Component> = getParentMap()
val predicate = searchSpec.toPredicate()
val item: MenuItemBase<*, *, *> = parentMap.keys.firstOrNull(predicate)
Expand Down Expand Up @@ -276,5 +268,14 @@ public fun <T> GridContextMenu<T>.setOpened(opened: Boolean, gridItem: T?, colum
require(id.isNotBlank()) { "Column $column must have an ID assigned in order to be identifiable in the event object" }
target.element.setProperty("_contextMenuTargetColumnId", id)
}
if (opened) {
// notify the context menu dynamic item generator
dynamicContentHandler?.also {
val openMenu: Boolean = it.test(gridItem)
if (!openMenu) {
fail("The dynamic content handler returned false signalling the menu should not open:\n${toPrettyTree()}")
}
}
}
element.setProperty("opened", opened)
}

0 comments on commit 293e4d9

Please sign in to comment.