diff --git a/docs/widgets/input/text_input/example.py b/docs/widgets/input/text_input/example.py index e69de29b..8181d8f2 100644 --- a/docs/widgets/input/text_input/example.py +++ b/docs/widgets/input/text_input/example.py @@ -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( + "You entered:\n\n" + "age: {{age}}\n" + "country: {{country}}\n" + ), + state=SG.result, + getter=getter, + parse_mode="html", + ), +) diff --git a/docs/widgets/input/text_input/index.rst b/docs/widgets/input/text_input/index.rst index 7711934c..c2c12cfb 100644 --- a/docs/widgets/input/text_input/index.rst +++ b/docs/widgets/input/text_input/index.rst @@ -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 `_ filters. + +Code example: + +.. literalinclude:: ./example.py + .. autoclass:: aiogram_dialog.widgets.input.text.OnSuccess :special-members: __call__