Reflex Logo

Intro

Gallery

Hosting

Components

New

Learn

Components

API Reference

Onboarding

Library

/

Forms

/

Input

The input component is an input field that users can type into.

rx.input()

Can set defaults for a placeholder for text to show in the input box before any text is input into it.

Can limit the max_length allowed as input into the input box.

rx.input(placeholder="Search here...", max_length=20)

The on_blur event handler is called when focus has left the input for example, it’s called when the user clicks outside of a focused text input.

Hello World!

class TextfieldBlur(rx.State):
    text: str = "Hello World!"


def blur_example():
    return rx.vstack(
        rx.heading(TextfieldBlur.text),
        rx.input(
            placeholder="Search here...",
            on_blur=TextfieldBlur.set_text,
        ),
    )

The on_change event handler is called when the value of input has changed.

Hello World!

class TextfieldControlled(rx.State):
    text: str = "Hello World!"


def controlled_example():
    return rx.vstack(
        rx.heading(TextfieldControlled.text),
        rx.input(
            placeholder="Search here...",
            value=TextfieldControlled.text,
            on_change=TextfieldControlled.set_text,
        ),
    )

Behind the scene, the input component is implemented using debounced input to avoid sending individual state updates per character to the backend while the user is still typing. This allows a state var to directly control the value prop from the backend without the user experiencing input lag. For advanced use cases, you can tune the debounce delay by setting the debounce_timeout when creating the Input component. You can find examples of how it is used in the DebouncedInput component.

The name prop is needed to submit with its owning form as part of a name/value pair.

When the required prop is True, it indicates that the user must input text before the owning form can be submitted.

The type is set here to password. The element is presented as a one-line plain text editor control in which the text is obscured so that it cannot be read. The type prop can take any value of email, file, password, text and several others. Learn more here .

Results

{}

class FormInputState(rx.State):
    form_data: dict = {}

    def handle_submit(self, form_data: dict):
        """Handle the form submit."""
        self.form_data = form_data


def form_input1():
    return rx.vstack(
        rx.form.root(
            rx.vstack(
                rx.input(
                    name="input",
                    default_value="search",
                    placeholder="Input text here...",
                    type="password",
                    required=True,
                ),
                rx.button("Submit", type="submit"),
                width="100%",
            ),
            on_submit=FormInputState.handle_submit,
            reset_on_submit=True,
            width="100%",
        ),
        rx.divider(width="100%"),
        rx.heading("Results"),
        rx.text(FormInputState.form_data.to_string()),
        width="100%",
    )

To learn more about how to use forms in the Form docs.

Set the value of the specified reference element, without needing to link it up to a State var. This is an alternate way to modify the value of the input.

rx.hstack(
    rx.input(id="input1"),
    rx.button("Erase", on_click=rx.set_value("input1", "")),
)

The Less I Know

Rock

Breathe Deeper

Rock

Let It Happen

Rock

Borderline

Pop

Lost In Yesterday

Rock

Is It True

Rock

def song(title, initials: str, genre: str):
    return rx.card(
        rx.flex(
            rx.flex(
                rx.avatar(fallback=initials),
                rx.flex(
                    rx.text(title, size="2", weight="bold"),
                    rx.text(
                        genre, size="1", color_scheme="gray"
                    ),
                    direction="column",
                    spacing="1",
                ),
                direction="row",
                align_items="left",
                spacing="1",
            ),
            rx.flex(
                rx.icon(tag="chevron_right"),
                align_items="center",
            ),
            justify="between",
        )
    )


def search():
    return rx.card(
        rx.flex(
            rx.input(
                placeholder="Search songs...",
            ),
            rx.flex(
                song("The Less I Know", "T", "Rock"),
                song("Breathe Deeper", "ZB", "Rock"),
                song("Let It Happen", "TF", "Rock"),
                song("Borderline", "ZB", "Pop"),
                song("Lost In Yesterday", "TO", "Rock"),
                song("Is It True", "TO", "Rock"),
                direction="column",
                spacing="1",
            ),
            direction="column",
            spacing="3",
        ),
        style={"maxWidth": 500},
    )

Captures user input with an optional slot for buttons and icons.

PropTypeDescriptionValues
sizeLiteral

Text field size "1" - "3"

variantLiteral

Variant of text field: "classic" | "surface" | "soft"

color_schemeLiteral

Override theme color for text field

radiusLiteral

Override theme radius for text field: "none" | "small" | "medium" | "large" | "full"

auto_completebool

Whether the input should have autocomplete enabled

default_valuestr

The value of the input when initially rendered.

disabledbool

Disables the input

max_lengthint

Specifies the maximum number of characters allowed in the input

min_lengthint

Specifies the minimum number of characters required in the input

namestr

Name of the input, used when sending form data

placeholderstr

Placeholder text in the input

read_onlybool

Indicates whether the input is read-only

requiredbool

Indicates that the input is required

typestr

Specifies the type of input

valueUnion

Value of the input

Event Triggers

TriggerDescription
on_focus

Function or event handler called when the element (or some element inside of it) receives focus. For example, it is called when the user clicks on a text input.

on_blur

Function or event handler called when focus has left the element (or left some element inside of it). For example, it is called when the user clicks outside of a focused text input.

on_change

The on_change event handler is called when the value or checked state of the component changes.

on_key_down

The on_key_down event handler is called when the user presses a key.

on_key_up

The on_key_up event handler is called when the user releases a key.

Captures user input with an optional slot for buttons and icons.

PropTypeDescriptionValues
sizeLiteral

Text field size "1" - "3"

variantLiteral

Variant of text field: "classic" | "surface" | "soft"

color_schemeLiteral

Override theme color for text field

radiusLiteral

Override theme radius for text field: "none" | "small" | "medium" | "large" | "full"

auto_completebool

Whether the input should have autocomplete enabled

default_valuestr

The value of the input when initially rendered.

disabledbool

Disables the input

max_lengthint

Specifies the maximum number of characters allowed in the input

min_lengthint

Specifies the minimum number of characters required in the input

namestr

Name of the input, used when sending form data

placeholderstr

Placeholder text in the input

read_onlybool

Indicates whether the input is read-only

requiredbool

Indicates that the input is required

typestr

Specifies the type of input

valueUnion

Value of the input

Event Triggers

TriggerDescription
on_focus

Function or event handler called when the element (or some element inside of it) receives focus. For example, it is called when the user clicks on a text input.

on_blur

Function or event handler called when focus has left the element (or left some element inside of it). For example, it is called when the user clicks outside of a focused text input.

on_change

The on_change event handler is called when the value or checked state of the component changes.

on_key_down

The on_key_down event handler is called when the user presses a key.

on_key_up

The on_key_up event handler is called when the user releases a key.

Contains icons or buttons associated with an Input.

PropTypeDescriptionValues
color_schemeLiteral

Override theme color for text field slot

← FormRadioGroup →

Did you find this useful?

HomeGalleryChangelogIntroductionHosting