Skip to content

Commit

Permalink
add default_expand_all parameter and warn when using the prop
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Sep 28, 2023
1 parent 3465dc1 commit 6ab9204
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
20 changes: 20 additions & 0 deletions nicegui/elements/tree.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -14,6 +17,7 @@ def __init__(self, nodes: List, *,
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
Expand All @@ -31,6 +35,7 @@ def __init__(self, nodes: List, *,
: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
Expand All @@ -42,6 +47,13 @@ def __init__(self, nodes: List, *,
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:
Expand All @@ -62,3 +74,11 @@ def handle_ticked(e: GenericEventArguments) -> None:
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 changes: 37 additions & 0 deletions tests/test_tree.py
Original file line number Diff line number Diff line change
@@ -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')

0 comments on commit 6ab9204

Please sign in to comment.