diff --git a/nicegui/elements/popup_edit.py b/nicegui/elements/popup_edit.py new file mode 100644 index 000000000..81f53a634 --- /dev/null +++ b/nicegui/elements/popup_edit.py @@ -0,0 +1,50 @@ +from typing import Optional + +from ..elements.mixins.disableable_element import DisableableElement +from ..events import Handler, UiEventArguments, handle_event + + +class PopupEdit(DisableableElement): + def __init__(self, *, + on_show: Optional[Handler[UiEventArguments]] = None, + on_hide: Optional[Handler[UiEventArguments]] = None,) -> None: + """Popup Edit + + This element is based on Quasar's `QPopupEdit `_ component. + It provides a popup editor that can be used to edit text or other content in a popup dialog. + + NOTE: We only use the popup edit as a container for other elements. + + - The JavaScript value of the popup edit will not be used. + - Rather, value handling is done by the child elements using `NiceGUI's data binding `_. + + :param on_show: callback to execute when the popup editor is shown + :param on_hide: callback to execute when the popup editor is hidden + """ + super().__init__(tag='q-popup-edit') + if on_show: + self.on_show(on_show) + if on_hide: + self.on_hide(on_hide) + + def show(self) -> None: + """Open the popup editor.""" + self.run_method('show') + + def hide(self) -> None: + """Close the popup editor.""" + self.run_method('hide') + + def on_show(self, callback: Handler[UiEventArguments]) -> None: + """Register a handler to be called when the popup editor is shown. + + :param handler: callback to execute when the popup editor is shown + """ + self.on('show', lambda: handle_event(callback, UiEventArguments(sender=self, client=self.client)), args=[]) + + def on_hide(self, callback: Handler[UiEventArguments]) -> None: + """Register a handler to be called when the popup editor is hidden. + + :param handler: callback to execute when the popup editor is hidden + """ + self.on('hide', lambda: handle_event(callback, UiEventArguments(sender=self, client=self.client)), args=[]) diff --git a/nicegui/ui.py b/nicegui/ui.py index fb8ea671b..3d8aaf89f 100644 --- a/nicegui/ui.py +++ b/nicegui/ui.py @@ -84,6 +84,7 @@ 'page_title', 'pagination', 'plotly', + 'popup_edit', 'pyplot', 'query', 'radio', @@ -206,6 +207,7 @@ from .elements.page_sticky import PageSticky as page_sticky from .elements.pagination import Pagination as pagination from .elements.plotly import Plotly as plotly +from .elements.popup_edit import PopupEdit as popup_edit from .elements.progress import CircularProgress as circular_progress from .elements.progress import LinearProgress as linear_progress from .elements.pyplot import Matplotlib as matplotlib diff --git a/website/documentation/content/popup_edit_documentation.py b/website/documentation/content/popup_edit_documentation.py new file mode 100644 index 000000000..1e73d562f --- /dev/null +++ b/website/documentation/content/popup_edit_documentation.py @@ -0,0 +1,18 @@ +from nicegui import app, ui + +from . import doc + + +@doc.demo(ui.popup_edit) +def main_demo() -> None: + # from nicegui import app, ui + app.storage.client['name'] = 'NiceGUI User' + + with ui.label().bind_text_from(app.storage.client, 'name'): + with ui.popup_edit(on_show=lambda _: ui.notify('Shown!'), + on_hide=lambda _: ui.notify('Hidden!')) as popup: + ui.input().bind_value(app.storage.client, 'name') + ui.button('Force show popup edit', on_click=popup.show) + + +doc.reference(ui.popup_edit) diff --git a/website/documentation/content/section_controls.py b/website/documentation/content/section_controls.py index 96ed27ec1..9ad7cccfa 100644 --- a/website/documentation/content/section_controls.py +++ b/website/documentation/content/section_controls.py @@ -2,6 +2,7 @@ badge_documentation, button_documentation, button_dropdown_documentation, + popup_edit_documentation, button_group_documentation, checkbox_documentation, codemirror_documentation, @@ -51,6 +52,7 @@ doc.intro(joystick_documentation) doc.intro(input_documentation) doc.intro(textarea_documentation) +doc.intro(popup_edit_documentation) doc.intro(codemirror_documentation) doc.intro(xterm_documentation) doc.intro(number_documentation)