浏览代码

#514 minor cleanup

Falko Schindler 2 年之前
父节点
当前提交
318ecb278a
共有 4 个文件被更改,包括 14 次插入9 次删除
  1. 1 1
      examples/slots/main.py
  2. 5 2
      nicegui/element.py
  3. 6 4
      nicegui/slot.py
  4. 2 2
      nicegui/templates/index.html

+ 1 - 1
examples/slots/main.py

@@ -5,7 +5,7 @@ with ui.tree([
     {'id': 'numbers', 'icon': 'tag', 'children': [{'id': '1'}, {'id': '2'}]},
     {'id': 'letters', 'icon': 'text_fields', 'children': [{'id': 'A'}, {'id': 'B'}]},
 ], label_key='id', on_select=lambda e: ui.notify(e.value)) as tree:
-    tree.add_slot('default-header', '''
+    tree.add_slot('default-header', r'''
         <div class="row items-center">
             <q-icon :name="props.node.icon || 'share'" color="orange" size="28px" class="q-mr-sm" />
             <div class="text-weight-bold text-primary">{{ props.node.id }}</div>

+ 5 - 2
nicegui/element.py

@@ -58,7 +58,7 @@ class Element(ABC, Visibility):
         """Add a slot to the element.
 
         :param name: name of the slot
-        :param template: vueJS template of the slot
+        :param template: Vue template of the slot
         :return: the slot
         """
         self.slots[name] = Slot(self, name, template)
@@ -91,7 +91,10 @@ class Element(ABC, Visibility):
         return events
 
     def _collect_slot_dict(self) -> Dict[str, List[int]]:
-        return {name: {'template': slot.template, 'ids': [child.id for child in slot.children]} for name, slot in self.slots.items()}
+        return {
+            name: {'template': slot.template, 'ids': [child.id for child in slot.children]}
+            for name, slot in self.slots.items()
+        }
 
     def _to_dict(self, *keys: str) -> Dict:
         if not keys:

+ 6 - 4
nicegui/slot.py

@@ -1,4 +1,6 @@
-from typing import Optional, TYPE_CHECKING, List
+from typing import TYPE_CHECKING, List, Optional
+
+from typing_extensions import Self
 
 from . import globals
 
@@ -11,13 +13,13 @@ class Slot:
     def __init__(self, parent: 'Element', name: str, template: Optional[str] = None) -> None:
         self.name = name
         self.parent = parent
+        self.template = template
         self.children: List['Element'] = []
-        self.template: str = template
 
-    def __enter__(self):
+    def __enter__(self) -> Self:
         globals.get_slot_stack().append(self)
         return self
 
-    def __exit__(self, *_):
+    def __exit__(self, *_) -> None:
         globals.get_slot_stack().pop()
         globals.prune_slot_stack()

+ 2 - 2
nicegui/templates/index.html

@@ -75,11 +75,11 @@
             const rendered = [];
             if (data.template) {
               rendered.push(Vue.h({
-                props: { props: { type: Object, default: {}} },
+                props: { props: { type: Object, default: {} } },
                 template: data.template,
               }, {
                 props: props
-              }))
+              }));
             }
             const ids = ('ids' in data) ? data.ids : data;
             const children = ids.map(id => renderRecursively(elements, id));