fixed dialog classmethod
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Self
|
||||
from typing import Self, Any
|
||||
from nicegui import ui
|
||||
|
||||
|
||||
@@ -21,6 +21,39 @@ class AsyncColumn(ui.column, ABC):
|
||||
await instance.build(*args, **kwargs)
|
||||
return instance
|
||||
|
||||
@classmethod
|
||||
async def as_dialog(cls, *args, **kwargs) -> Any:
|
||||
"""Create element inside a dialog and await its result
|
||||
|
||||
Works like create() but wraps the element in a dialog with a card that opens automatically
|
||||
and returns the dialog result when submitted.
|
||||
|
||||
The dialog can be submitted using dialog.submit(result) from within the build() method.
|
||||
Access the dialog via self._dialog in your build() implementation.
|
||||
|
||||
Returns the value passed to dialog.submit() or None if dismissed.
|
||||
"""
|
||||
# Create the dialog
|
||||
dialog = ui.dialog()
|
||||
dialog.open()
|
||||
|
||||
# Build the element inside the dialog with a card
|
||||
with dialog, ui.card():
|
||||
instance = cls()
|
||||
|
||||
# Store dialog reference for potential use in build()
|
||||
instance._dialog = dialog # pyright: ignore[reportAttributeAccessIssue]
|
||||
|
||||
await instance.build(*args, **kwargs)
|
||||
|
||||
# Await the dialog result
|
||||
result = await dialog
|
||||
|
||||
# Clean up the dialog after it's closed
|
||||
dialog.clear()
|
||||
|
||||
return result
|
||||
|
||||
|
||||
class AsyncRow(ui.row, ABC):
|
||||
"""Async row that inherits from ui.row for perfect typing"""
|
||||
@@ -40,6 +73,21 @@ class AsyncRow(ui.row, ABC):
|
||||
await instance.build(*args, **kwargs)
|
||||
return instance
|
||||
|
||||
@classmethod
|
||||
async def as_dialog(cls, *args, **kwargs) -> Any:
|
||||
"""Create element inside a dialog and await its result"""
|
||||
dialog = ui.dialog()
|
||||
dialog.open()
|
||||
|
||||
with dialog, ui.card():
|
||||
instance = cls()
|
||||
instance._dialog = dialog # pyright: ignore[reportAttributeAccessIssue]
|
||||
await instance.build(*args, **kwargs)
|
||||
|
||||
result = await dialog
|
||||
dialog.clear()
|
||||
return result
|
||||
|
||||
|
||||
class AsyncCard(ui.card, ABC):
|
||||
"""Async card that inherits from ui.card for perfect typing"""
|
||||
@@ -59,6 +107,21 @@ class AsyncCard(ui.card, ABC):
|
||||
await instance.build(*args, **kwargs)
|
||||
return instance
|
||||
|
||||
@classmethod
|
||||
async def as_dialog(cls, *args, **kwargs) -> Any:
|
||||
"""Create element inside a dialog and await its result"""
|
||||
dialog = ui.dialog()
|
||||
dialog.open()
|
||||
|
||||
with dialog, ui.card():
|
||||
instance = cls()
|
||||
instance._dialog = dialog # pyright: ignore[reportAttributeAccessIssue]
|
||||
await instance.build(*args, **kwargs)
|
||||
|
||||
result = await dialog
|
||||
dialog.clear()
|
||||
return result
|
||||
|
||||
|
||||
class AsyncScrollArea(ui.scroll_area, ABC):
|
||||
"""Async ScrollArea that inherits from ui.scrol_area for perfect typing"""
|
||||
@@ -77,3 +140,18 @@ class AsyncScrollArea(ui.scroll_area, ABC):
|
||||
instance = cls()
|
||||
await instance.build(*args, **kwargs)
|
||||
return instance
|
||||
|
||||
@classmethod
|
||||
async def as_dialog(cls, *args, **kwargs) -> Any:
|
||||
"""Create element inside a dialog and await its result"""
|
||||
dialog = ui.dialog()
|
||||
dialog.open()
|
||||
|
||||
with dialog, ui.card():
|
||||
instance = cls()
|
||||
instance._dialog = dialog # pyright: ignore[reportAttributeAccessIssue]
|
||||
await instance.build(*args, **kwargs)
|
||||
|
||||
result = await dialog
|
||||
dialog.clear()
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user