Christoph Trappe 3 лет назад
Родитель
Сommit
78ebfb7c19
3 измененных файлов с 33 добавлено и 4 удалено
  1. 4 4
      main.py
  2. 28 0
      nicegui/elements/menu_item.py
  3. 1 0
      nicegui/ui.py

+ 4 - 4
main.py

@@ -311,11 +311,11 @@ with example(ui.dialog):
 
 with example(ui.menu):
 
+    choice = ui.label('Try the menu.')
     with ui.menu() as menu:
-        with ui.card():
-            ui.label('Menu item 1')
-            ui.label('Menu item 2')
-            ui.button('Close', on_click=menu.close).props('icon=close text-color=black color=white flat')
+        ui.menu_item('Menu item 1', lambda: choice.set_text('Selected item 1.'))
+        ui.menu_item('Menu item 2', lambda: choice.set_text('Selected item 2.'))
+        ui.menu_item('Close', on_click=menu.close)
 
     ui.button('Open menu', on_click=menu.open).props('color=secondary')
 

+ 28 - 0
nicegui/elements/menu_item.py

@@ -0,0 +1,28 @@
+from typing import Callable
+
+import justpy as jp
+
+from .element import Element
+from ..utils import handle_exceptions, provide_arguments
+
+
+class MenuItem(Element):
+
+    def __init__(self,
+                 text: str = '',
+                 on_click: Callable = None
+                 ):
+        """Menu Item Element
+
+        A menu item to be added to a menu.
+
+        :param text: label of the menu item
+        :param on_click: function to be executed when selecting the menu item
+        """
+
+        view = jp.QItem(text=text, clickable=True)
+
+        if on_click is not None:
+            view.on('click', handle_exceptions(provide_arguments(on_click)))
+
+        super().__init__(view)

+ 1 - 0
nicegui/ui.py

@@ -16,6 +16,7 @@ class Ui:
     from .elements.log import Log as log
     from .elements.markdown import Markdown as markdown
     from .elements.menu import Menu as menu
+    from .elements.menu_item import MenuItem as menu_item
     from .elements.notify import Notify as notify
     from .elements.number import Number as number
     from .elements.page import Page as page