Skip to content

Commit

Permalink
make sure to call _on_delete() for each deleted element
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Aug 29, 2023
1 parent 17e6e13 commit 51a8c54
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions nicegui/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def remove_elements(self, elements: Iterable[Element]) -> None:
for element_id in element_ids:
del self.elements[element_id]
for element in elements:
element._on_delete() # pylint: disable=protected-access
element._deleted = True # pylint: disable=protected-access
outbox.enqueue_delete(element)

Expand Down
29 changes: 29 additions & 0 deletions tests/test_element_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,32 @@ def test_delete_element(screen: Screen):
assert b.id not in row.client.elements
assert len(row.default_slot.children) == 2
assert len(binding.active_links) == 2


def test_on_delete(screen: Screen):
deleted_labels = []

class CustomLabel(ui.label):

def __init__(self, text: str) -> None:
super().__init__(text)

def _on_delete(self) -> None:
deleted_labels.append(self.text)
super()._on_delete()

with ui.row() as row:
CustomLabel('Label A')
b = CustomLabel('Label B')
CustomLabel('Label C')

ui.button('Delete C', on_click=lambda: row.remove(2))
ui.button('Delete B', on_click=lambda: row.remove(b))
ui.button('Clear row', on_click=row.clear)

screen.open('/')
screen.click('Delete C')
screen.click('Delete B')
screen.click('Clear row')
screen.wait(0.5)
assert deleted_labels == ['Label C', 'Label B', 'Label A']

0 comments on commit 51a8c54

Please sign in to comment.