1
0

menu_item.py 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. from typing import Callable, Optional
  2. import justpy as jp
  3. from ..events import ClickEventArguments, handle_event
  4. from .element import Element
  5. class MenuItem(Element):
  6. def __init__(self, text: str = '', on_click: Optional[Callable] = None, *, auto_close: bool = True):
  7. """Menu Item Element
  8. A menu item to be added to a menu.
  9. :param text: label of the menu item
  10. :param on_click: callback to be executed when selecting the menu item
  11. :param auto_close: whether the menu should be closed after a click event (default: `True`)
  12. """
  13. view = jp.QItem(text=text, clickable=True, temp=False)
  14. def handle_click(view, event) -> Optional[bool]:
  15. socket = event.get('websocket')
  16. result = handle_event(on_click, ClickEventArguments(sender=self, socket=socket), update=self.parent_view)
  17. if auto_close:
  18. assert isinstance(self.parent_view, jp.QMenu)
  19. self.parent_view.value = False
  20. return result
  21. view.on('click', handle_click)
  22. super().__init__(view)