Skip to content

Commit

Permalink
fix events on hidden and disabled elements (#3524)
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Aug 16, 2024
1 parent 7081253 commit cd9e547
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion nicegui/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def handle_event(self, msg: Dict) -> None:
"""Forward an event to the corresponding element."""
with self:
sender = self.elements.get(msg['id'])
if sender is not None:
if sender is not None and not sender.is_ignoring_events:
msg['args'] = [None if arg is None else json.loads(arg) for arg in msg.get('args', [])]
if len(msg['args']) == 1:
msg['args'] = msg['args'][0]
Expand Down
2 changes: 0 additions & 2 deletions nicegui/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,6 @@ def handle_event(handler: Optional[Callable[..., Any]], arguments: EventArgument

parent_slot: Union[Slot, nullcontext]
if isinstance(arguments, UiEventArguments):
if arguments.sender.is_ignoring_events:
return
parent_slot = arguments.sender.parent_slot or arguments.sender.client.layout.default_slot
else:
parent_slot = nullcontext()
Expand Down
15 changes: 11 additions & 4 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,26 @@ def test_throttling_variants(screen: Screen):

@pytest.mark.parametrize('attribute', ['disabled', 'hidden'])
def test_server_side_validation(screen: Screen, attribute: Literal['disabled', 'hidden']):
b = ui.button('Button', on_click=lambda: ui.label('Success'))
b = ui.button('Button', on_click=lambda: ui.label('Button clicked'))
n = ui.number('Number', on_change=lambda: ui.label('Number changed'))
if attribute == 'disabled':
b.disable()
n.disable()
else:
b.set_visibility(False)
ui.button('Hack', on_click=lambda: ui.run_javascript(f'''
n.set_visibility(False)
ui.button('Forbidden', on_click=lambda: ui.run_javascript(f'''
getElement({b.id}).$emit("click", {{"id": {b.id}, "listener_id": "{next(iter(b._event_listeners))}"}});
''')) # pylint: disable=protected-access
ui.button('Allowed', on_click=lambda: n.set_value(42))

screen.open('/')
screen.click('Hack')
screen.click('Forbidden')
screen.wait(0.5)
screen.should_not_contain('Success')
screen.should_not_contain('Button clicked') # triggering the click event through JavaScript does not work

screen.click('Allowed')
screen.should_contain('Number changed') # triggering the change event through Python works


def test_js_handler(screen: Screen) -> None:
Expand Down

0 comments on commit cd9e547

Please sign in to comment.