Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nicegui/elements/button_dropdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self,
def on_click(self, callback: Handler[ClickEventArguments]) -> Self:
"""Add a callback to be invoked when the dropdown button is clicked.

**Added in version 2.22.0**
*Added in version 2.22.0*
"""
self.on('click', lambda _: handle_event(callback, ClickEventArguments(sender=self, client=self.client)), [])
return self
Expand Down
2 changes: 1 addition & 1 deletion nicegui/elements/fab.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(self, icon: str, *,
An action that can be added to a floating action button (FAB).
This element is based on Quasar's `QFabAction <https://quasar.dev/vue-components/floating-action-button#qfabaction-api>`_ component.

**Added in version 2.22.0**
*Added in version 2.22.0*

:param icon: icon to be displayed on the action button
:param label: optional label for the action button
Expand Down
1 change: 1 addition & 0 deletions nicegui/elements/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default {
if (evt.repeat && !this.repeating) return;

this.$emit("key", {
event: evt,
action: event,
altKey: evt.altKey,
ctrlKey: evt.ctrlKey,
Expand Down
19 changes: 19 additions & 0 deletions website/documentation/content/keyboard_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,23 @@ def handle_key(e: KeyEventArguments):
ui.checkbox('Track key events').bind_value_to(keyboard, 'active')


@doc.demo('Prevent default and stop propagation', '''
You can use the `js_handler` parameter of the `on` method to control how an event is handled on the client side.
To prevent the default behavior, you can call the `preventDefault` method of the `event` object.
To stop the propagation of the event, you could also call the `stopPropagation` method of the `event` object.

*Added in version 3.1.0*
''')
def prevent_default() -> None:
ui.label('Select via Ctrl-A or Cmd-A is disabled')

ui.keyboard() \
.on('key', lambda: ui.notify('Select all prevented.'), js_handler='''(e) => {
if (e.key === 'a' && (e.ctrlKey || e.metaKey) && e.action === 'keydown') {
emit(e);
e.event.preventDefault();
}
}''')


doc.reference(ui.keyboard)
16 changes: 7 additions & 9 deletions website/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,18 @@ def __init__(self) -> None:
.props('padding="2px 8px" outline size=sm color=grey-5').classes('shadow')
ui.separator()
self.results = ui.element('q-list').classes('w-full').props('separator')
ui.keyboard(self.handle_keypress)
ui.keyboard().on('key', self.dialog.open, js_handler='''(e) => {
if (e.action !== 'keydown') return;
if (e.key === '/' || (e.key === 'k' && (e.ctrlKey || e.metaKey))) {
emit(e);
e.event.preventDefault();
}
}''')

def create_button(self) -> ui.button:
return ui.button(on_click=self.dialog.open, icon='search').props('flat color=white') \
.tooltip('Press Ctrl+K or / to search the documentation')

def handle_keypress(self, e: events.KeyEventArguments) -> None:
if not e.action.keydown:
return
if e.key == '/':
self.dialog.open()
if e.key == 'k' and (e.modifiers.ctrl or e.modifiers.meta):
self.dialog.open()

def handle_input(self, e: events.ValueChangeEventArguments) -> None:
async def handle_input() -> None:
with self.results:
Expand Down