Bläddra i källkod

add default_expand_all parameter and warn when using the prop

Falko Schindler 1 år sedan
förälder
incheckning
6ab9204ebd
2 ändrade filer med 57 tillägg och 0 borttagningar
  1. 20 0
      nicegui/elements/tree.py
  2. 37 0
      tests/test_tree.py

+ 20 - 0
nicegui/elements/tree.py

@@ -1,5 +1,8 @@
 from typing import Any, Callable, List, Literal, Optional
 
+from typing_extensions import Self
+
+from .. import globals  # pylint: disable=redefined-builtin
 from ..element import Element
 from ..events import GenericEventArguments, ValueChangeEventArguments, handle_event
 
@@ -14,6 +17,7 @@ class Tree(Element):
                  on_expand: Optional[Callable[..., Any]] = None,
                  on_tick: Optional[Callable[..., Any]] = None,
                  tick_strategy: Optional[Literal['leaf', 'leaf-filtered', 'strict']] = None,
+                 default_expand_all: bool = False,
                  ) -> None:
         """Tree
 
@@ -31,6 +35,7 @@ class Tree(Element):
         :param on_expand: callback which is invoked when the node expansion changes
         :param on_tick: callback which is invoked when a node is ticked or unticked
         :param tick_strategy: whether and how to use checkboxes ("leaf", "leaf-filtered" or "strict"; default: ``None``)
+        :param default_expand_all: whether to expand all nodes by default (default: ``False``)
         """
         super().__init__('q-tree')
         self._props['nodes'] = nodes
@@ -42,6 +47,13 @@ class Tree(Element):
         self._props['ticked'] = []
         if tick_strategy is not None:
             self._props['tick-strategy'] = tick_strategy
+        if default_expand_all:
+            # https://github.com/zauberzeug/nicegui/issues/1385
+            def expand_all(nodes: List) -> None:
+                for node in nodes:
+                    self._props['expanded'].append(node[node_key])
+                    expand_all(node.get(children_key, []))
+            expand_all(nodes)
 
         def update_prop(name: str, value: Any) -> None:
             if self._props[name] != value:
@@ -62,3 +74,11 @@ class Tree(Element):
             update_prop('ticked', e.args)
             handle_event(on_tick, ValueChangeEventArguments(sender=self, client=self.client, value=e.args))
         self.on('update:ticked', handle_ticked)
+
+    def props(self, add: Optional[str] = None, *, remove: Optional[str] = None) -> Self:
+        super().props(add, remove=remove)
+        if 'default-expand-all' in self._props:
+            del self._props['default-expand-all']
+            globals.log.warning('The prop "default_expand_all" is not supported by `ui.tree`.\n'
+                                'Use the parameter "default_expand_all" instead.')
+        return self

+ 37 - 0
tests/test_tree.py

@@ -0,0 +1,37 @@
+from nicegui import ui
+
+from .screen import Screen
+
+
+def test_tree(screen: Screen):
+    ui.tree([
+        {'id': 'numbers', 'children': [{'id': '1'}, {'id': '2'}]},
+        {'id': 'letters', 'children': [{'id': 'A'}, {'id': 'B'}]},
+    ], label_key='id')
+
+    screen.open('/')
+    screen.should_contain('numbers')
+    screen.should_contain('letters')
+    screen.should_not_contain('1')
+    screen.should_not_contain('2')
+    screen.should_not_contain('A')
+    screen.should_not_contain('B')
+
+    screen.find_by_class('q-icon').click()
+    screen.should_contain('1')
+    screen.should_contain('2')
+
+
+def test_default_expand_all(screen: Screen):
+    ui.tree([
+        {'id': 'numbers', 'children': [{'id': '1'}, {'id': '2'}]},
+        {'id': 'letters', 'children': [{'id': 'A'}, {'id': 'B'}]},
+    ], label_key='id', default_expand_all=True)
+
+    screen.open('/')
+    screen.should_contain('numbers')
+    screen.should_contain('letters')
+    screen.should_contain('1')
+    screen.should_contain('2')
+    screen.should_contain('A')
+    screen.should_contain('B')