Skip to content

Nested content_button can cancel event propagation #1119

Open
@jmfk

Description

@jmfk

Describe the bug
If you have a button in a button there is no way to cancel the event from propagating to container event handler resulting in an error.

Suggested solution
Make it possible to stop event propagation (e.g. event.stopPropagation())

Code To Reproduce

import mesop as me

projects = [
{"key": "hello", "text": "Hello", "tooltip": "This is a tooltip"},
{"key": "world", "text": "World", "tooltip": "This is another tooltip"},
]

@me.stateclass
class State:
select_message: str = "Nothing yet"
delete_message: str = "Nothing yet"

def on_reset(e: me.ClickEvent):
global projects
projects = [
{"key": "hello", "text": "Hello", "tooltip": "This is a tooltip"},
{"key": "world", "text": "World", "tooltip": "This is another tooltip"},
]

def on_select_project(e: me.ClickEvent):
state = me.state(State)
state.select_message = f"Selected: {str(e)}"

def on_delete_project(e: me.ClickEvent):
global projects
state = me.state(State)
state.delete_message = f"Deleted: {str(e)}"
projects = [p for p in projects if p["key"] != str(e.key)]

@me.page(
path="/",
title="Tooltip Bug",
)
def app():
state = me.state(State)
with me.box():
me.text(f"select_message: {state.select_message}")
me.text(f"delete_message: {state.delete_message}")
me.button(
on_click=on_reset,
label="Reset",
)

for project in projects:
    with me.box(
        style=me.Style(
            padding=me.Padding.symmetric(horizontal=10, vertical=8),
        )
    ):
        with me.content_button(
            on_click=on_select_project, key=project["key"], color="accent"
        ):
            with me.box(
                style=me.Style(
                    display="flex",
                    flex_direction="row",
                    justify_content="space-between",
                    align_items="center",
                    width="245px",
                    padding=me.Padding.symmetric(horizontal=6),
                )
            ):
                me.text(project["text"])
                with me.content_button(
                    on_click=on_delete_project,
                    type="icon",
                    key=project["key"],
                ):
                    me.icon(icon="delete_circle")

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions