Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docs: ListGroup #402

Merged
merged 1 commit into from
May 22, 2024
Merged
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
60 changes: 60 additions & 0 deletions docs/widgets/keyboard/list_group/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from aiogram import F
from aiogram.filters.state import State, StatesGroup
from aiogram_dialog import (
Window,
Dialog,
)
from aiogram_dialog.widgets.kbd import (
Url,
ListGroup,
SwitchTo,
Back,
)
from aiogram_dialog.widgets.text import Const, Format


class SG(StatesGroup):
main = State()
result = State()


success = F["products"]
fail = ~success


async def actions(**kwargs):
products = [
{"id": 1, "name": "Ferrari", "category": "car",
"url": "https://www.ferrari.com/"},
{"id": 2, "name": "Detroit", "category": "game",
"url": "https://wikipedia.org/wiki/Detroit:_Become_Human"},
]
return {
"products": products,
}


dialog = Dialog(
Window(
Const("Click find products to show a list of available products:"),
SwitchTo(Const("Find products"), id="search", state=SG.result),
state=SG.main,
),
Window(
Const("Searching results:", when=success),
Const("Search did not return any results", when=fail),
ListGroup(
Url(
Format("{item[name]} ({item[category]})"),
Format("{item[url]}"),
id="url",
),
id="select_search",
item_id_getter=lambda item: item["id"],
items="products",
),
Back(Const("Back")),
state=SG.result,
getter=actions,
)
)
19 changes: 18 additions & 1 deletion docs/widgets/keyboard/list_group/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,27 @@
ListGroup
*************

``ListGroup`` is more complex way to render widgets for list of items. While ``Select`` generates simple buttons, with ``ListGroup`` you can create any set of keyboard widgets and repeat them.
**ListGroup** is more complex way to render widgets for list of items. While ``Select`` generates simple buttons, with ``ListGroup`` you can create any set of keyboard widgets and repeat them.

To identity ``item`` inside keyboard event you can check ``dialog_manager.item_id``. This is possible because ``SubManager`` is passed instead of ``DialogManager`` while corresponding widget is located inside ``ListGroup``.

To find widget for specific ``item_id`` in a ``ListGroup``, you can use the ``.find_for_item`` method from ``ManagedListGroup``.


Manager methods:

* **.find**: if manager already adapted for current ``ListGroup`` row, finds the widget within the current manager (``SubManager``).

* **.find_in_parent**: if you need to find widgets outside the row, finds the widget within the parent manager (``DialogManager``).

.. note::

If you nest a ``ListGroup`` in ``ListGroup`` your parent manager will be ``SubManager``

Code example:

.. literalinclude:: ./example.py
:language: python

.. autoclass:: aiogram_dialog.widgets.kbd.ListGroup
:special-members: __init__,
Expand Down