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

Add docs for TextInput widget #397

Merged
merged 1 commit into from
May 3, 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
63 changes: 63 additions & 0 deletions docs/widgets/input/text_input/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from typing import Any

from aiogram.fsm.state import State, StatesGroup
from aiogram.types import Message
from aiogram_dialog import (
Dialog,
DialogManager,
Window,
)
from aiogram_dialog.widgets.input import TextInput
from aiogram_dialog.widgets.kbd import Next
from aiogram_dialog.widgets.text import Const, Jinja


class SG(StatesGroup):
age = State()
country = State()
result = State()


async def error(
message: Message,
dialog_: Any,
manager: DialogManager,
error_: ValueError
):
await message.answer("Age must be a number!")


async def getter(dialog_manager: DialogManager, **kwargs):
return {
"age": dialog_manager.find("age").get_value(),
"country": dialog_manager.find("country").get_value(),
}


dialog = Dialog(
Window(
Const("Enter your country:"),
TextInput(id="country", on_success=Next()),
state=SG.country,
),
Window(
Const("Enter your age:"),
TextInput(
id="age",
on_error=error,
on_success=Next(),
type_factory=int,
),
state=SG.age,
),
Window(
Jinja(
"<b>You entered</b>:\n\n"
"<b>age</b>: {{age}}\n"
"<b>country</b>: {{country}}\n"
),
state=SG.result,
getter=getter,
parse_mode="html",
),
)
13 changes: 13 additions & 0 deletions docs/widgets/input/text_input/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
TextInput
*************

The **TextInput** widget automatically saving and validating text for later processing in the dialog.

Parameters:

* ``type_factory``: allows for input validation and automatic conversion to the specified type.
* ``on_success``: for handling successful input.
* ``on_error``: for error handling, will work if ``type_factory`` throws ValueError.
* ``filter``: support `aiogram <https://docs.aiogram.dev/en/latest/dispatcher/filters/index.html>`_ filters.

Code example:

.. literalinclude:: ./example.py

.. autoclass:: aiogram_dialog.widgets.input.text.OnSuccess
:special-members: __call__

Expand Down