# Text Field

Text fields are single-line text inputs, ideal for cases where users have a small amount of text to enter.

## Example

```python
from deephaven import ui


my_text_field_basic = ui.text_field(
    label="Description", on_change=lambda value: print(f"Text changed to {value}")
)
```

## UI Recommendations

Recommendations for creating text fields:

1. Every text field should have a [label]() specified. Without one, the text field is ambiguous. In the rare case that context is sufficient, the label is unnecessary; you must still include an aria-label via the `aria_label` prop.
2. Text field labels and help text should follow sentence casing.
3. Use help text to provide instructions on input format, content, and requirements; the help text should not restate the same information as the label, or prompt a user to interact with the text field.
4. Dynamically switch between help text and error messages based on input, ensuring both convey essential input requirements.

Use [`text_area`](text_area.md) for cases where multiline input is required.

## Value

A text field’s value is empty by default, but the `default_value` prop can set an initial, uncontrolled value, or a controlled value can be set via the `value` prop.

```python
from deephaven import ui


@ui.component
def ui_text_field_value_examples():
    value, set_value = ui.use_state("Aardvark")
    return [
        ui.text_field(label="Favorite animal (Uncontrolled)", default_value="Aardvark"),
        ui.text_field(
            label="Favorite animal (controlled)", value=value, on_change=set_value
        ),
    ]


my_text_field_value_examples = ui_text_field_value_examples()
```

## HTML Forms

Text fields can support a `name` prop for integration with HTML forms, allowing for easy identification of a value on form submission.

```python
from deephaven import ui


my_text_field_name_example = ui.form(
    ui.text_field(label="Email", name="email", type="email")
)
```

## Labeling

To provide a visual label for the text field, use the `label` prop. To indicate that the text field is mandatory, use the `is_required` prop.

```python
from deephaven import ui


@ui.component
def ui_text_field_is_required_examples():
    return [
        ui.text_field(label="Address"),
        ui.text_field(label="Address", is_required=True),
    ]


my_text_field_is_required_example = ui_text_field_is_required_examples()
```

By setting `is_required` to True, the `necessity_indicator` is set to “icon” by default, but this can be changed. The `necessity_indicator` can also be used independently to indicate that the text field is optional.

When the `necessity_indicator` prop is set to “label”, a localized string will be generated for “(required)” or “(optional)” automatically.

```python
from deephaven import ui


@ui.component
def ui_text_field_necessity_indicator_examples():
    return [
        ui.text_field(label="Address", is_required=True, necessity_indicator="label"),
        ui.text_field(label="Address", necessity_indicator="label"),
    ]


my_text_field_necessity_indicator_examples = (
    ui_text_field_necessity_indicator_examples()
)
```

## Events

The `on_change` property is triggered whenever the value in the text field is edited.

```python
from deephaven import ui


@ui.component
def ui_text_field_on_change_example():
    value, set_value = ui.use_state("")
    return [
        ui.text_field(label="Your text", value=value, on_change=set_value),
        ui.text(f"Text has been changed to: {value}"),
    ]


my_text_field_on_change_example = ui_text_field_on_change_example()
```

## Input Types

The `type` prop changes the type of text field that is rendered to suit different input requirements.

```python
from deephaven import ui


@ui.component
def ui_text_field_input_types_examples():
    return ui.form(
        ui.text_field(label="Name", type="text", is_required=True),
        ui.text_field(label="Personal Website", type="url", is_required=True),
        ui.text_field(label="Phone", type="tel", is_required=True),
        ui.text_field(label="Email", type="email", is_required=True),
        ui.text_field(label="Password", type="password", is_required=True),
        ui.text_field(label="Search Bar", type="search"),
        validation_behavior="native",
    )


my_text_field_input_types_examples = ui_text_field_input_types_examples()
```

## Quiet State

The `is_quiet` prop makes text fields “quiet”. This can be useful when the text area and its corresponding styling should not distract users from surrounding content.

```python
from deephaven import ui


my_text_field_is_quiet_example = ui.text_field(label="Favorite animal", is_quiet=True)
```

## Disabled State

The `is_disabled` prop disables text fields to prevent user interaction. This is useful when the text field should be visible but not available for input.

```python
from deephaven import ui


my_text_field_is_disabled_example = ui.text_field(
    label="Favorite animal", is_disabled=True
)
```

## Read only

The `is_read_only` prop makes text fields read-only to prevent user interaction. This is different than setting the `is_disabled` prop since the text field remains focusable, and the contents of the text field remain visible.

```python
from deephaven import ui


my_text_field_is_read_only_example = ui.text_field(
    label="Favorite animal", default_value="Panda", is_read_only=True
)
```

## Label position

By default, the position of a text field’s label is above the text field, but it can be changed to the side using the `label_position` prop.

While labels can be placed either on top or on the side of the text field, top labels are the default recommendation. Top labels work better with longer copy, localization, and responsive layouts. Side labels are more useful when vertical space is limited.

```python
from deephaven import ui


@ui.component
def ui_text_field_label_position_examples():
    return [
        ui.text_field(label="Sample Label"),
        ui.text_field(label="Sample Label", label_position="side", label_align="start"),
    ]


my_text_field_label_position_examples = ui_text_field_label_position_examples()
```

## Help text

A text field can have both a `description` and an `error_message`. The description remains visible at all times, except when the `validation_state` is set to “invalid” and an error message is present. Use the error message to offer specific guidance on how to correct the input.

```python
from deephaven import ui


@ui.component
def ui_text_field_help_text_examples():
    return [
        ui.text_field(
            label="Comment",
            default_value="Awesome!",
            validation_state="valid",
            description="Enter a comment.",
        ),
        ui.text_field(
            label="Comment",
            validation_state="invalid",
            error_message="Empty input is not allowed.",
        ),
    ]


my_text_field_help_text_examples = ui_text_field_help_text_examples()
```

## Contextual Help

Using the `contextual_help` prop, a `ui.contextual_help` can be placed next to the label to provide additional information about the text field.

```python
from deephaven import ui


my_text_field_contextual_help_example = ui.text_field(
    label="Favorite animal",
    contextual_help=ui.contextual_help(
        ui.heading("Information about animals"),
        ui.content(
            "Animals are classified into two main categories – the vertebrates and the invertebrates."
        ),
    ),
)
```

## Custom width

The `width` prop adjusts the width of a text field, and the `max_width` prop enforces a maximum width.

```python
from deephaven import ui


@ui.component
def ui_text_field_width_examples():
    return [
        ui.text_field(label="Favorite animal", width="size-3600"),
        ui.text_field(label="Favorite animal", width="size-3600", max_width="100%"),
    ]


my_text_field_width_examples = ui_text_field_width_examples()
```

## API Reference

TextFields are text inputs that allow users to input custom text entries with a keyboard. Various decorations can be displayed around the field to communicate the entry requirements.

**Returns:** `Element` The rendered text field element.

<ParamTable param={{"module_name": "deephaven.ui.", "name": "text_field", "parameters": [{"name": "icon", "type": "Element | None", "description": "An icon to display at the start of the input", "default": "None"}, {"name": "is_quiet", "type": "bool | None", "description": "Whether the input should be displayed with a quiet style", "default": "None"}, {"name": "is_disabled", "type": "bool | None", "description": "Whether the input should be disabled", "default": "None"}, {"name": "is_read_only", "type": "bool | None", "description": "Whether the input scan be selected but not changed by the user", "default": "None"}, {"name": "is_required", "type": "bool | None", "description": "Whether the input is required before form submission", "default": "None"}, {"name": "description", "type": "Any | None", "description": "A description for the field. Provides a hint such as specific requirements for what to choose.", "default": "None"}, {"name": "error_message", "type": "Any | None", "description": "An error message to display when the field is invalid", "default": "None"}, {"name": "auto_focus", "type": "bool | None", "description": "Whether the input should be focused on page load", "default": "None"}, {"name": "value", "type": "str | None", "description": "The current value of the input", "default": "None"}, {"name": "default_value", "type": "str | None", "description": "The default value of the input", "default": "None"}, {"name": "label", "type": "Any | None", "description": "The label for the input", "default": "None"}, {"name": "auto_complete", "type": "str | None", "description": "Describes the type of autocomplete functionality the input should provide", "default": "None"}, {"name": "max_length", "type": "int | None", "description": "The maximum number of characters the input can accept", "default": "None"}, {"name": "min_length", "type": "int | None", "description": "The minimum number of characters the input can accept", "default": "None"}, {"name": "pattern", "type": "str | None", "description": "A regular expression that the input's value must match", "default": "None"}, {"name": "type", "type": "Literal['text', 'search', 'url', 'tel', 'email', 'password'] | None", "description": "The type of input to display", "default": "None"}, {"name": "input_mode", "type": "Literal['none', 'text', 'decimal', 'numeric', 'tel', 'search', 'email', 'url'] | None", "description": "Hints at the tpye of data that might be entered by the user while editing the element or its contents", "default": "None"}, {"name": "name", "type": "str | None", "description": "The name of the input, used when submitting an HTML form", "default": "None"}, {"name": "validation_state", "type": "Literal['valid', 'invalid'] | None", "description": "Whether the input should display its \"valid\" or \"invalid\" state", "default": "None"}, {"name": "label_position", "type": "Literal['top', 'side']", "description": "The position of the label relative to the input", "default": "'top'"}, {"name": "label_align", "type": "Literal['start', 'end'] | None", "description": "The alignment of the label relative to the input", "default": "None"}, {"name": "necessity_indicator", "type": "Literal['icon', 'label'] | None", "description": "Whether the required state should be shown as an icon or text", "default": "None"}, {"name": "contextual_help", "type": "Any | None", "description": "A ContentualHelp element to place next to the label", "default": "None"}, {"name": "on_focus", "type": "Callable[[FocusEvent], None] | None", "description": "Function called when the button receives focus.", "default": "None"}, {"name": "on_blur", "type": "Callable[[FocusEvent], None] | None", "description": "Function called when the button loses focus.", "default": "None"}, {"name": "on_focus_change", "type": "Callable[[bool], None] | None", "description": "Function called when the focus state changes.", "default": "None"}, {"name": "on_key_down", "type": "Callable[[KeyboardEvent], None] | None", "description": "Function called when a key is pressed.", "default": "None"}, {"name": "on_key_up", "type": "Callable[[KeyboardEvent], None] | None", "description": "Function called when a key is released.", "default": "None"}, {"name": "on_change", "type": "Callable[[str], None] | None", "description": "Function called when the input value changes", "default": "None"}, {"name": "flex", "type": "str | float | bool | None", "description": "When used in a flex layout, specifies how the element will grow or shrink to fit the space available.", "default": "None"}, {"name": "flex_grow", "type": "float | None", "description": "When used in a flex layout, specifies how the element will grow to fit the space available.", "default": "None"}, {"name": "flex_shrink", "type": "float | None", "description": "When used in a flex layout, specifies how the element will shrink to fit the space available.", "default": "None"}, {"name": "flex_basis", "type": "str | float | None", "description": "When used in a flex layout, specifies the initial main size of the element.", "default": "None"}, {"name": "align_self", "type": "Literal['auto', 'normal', 'start', 'end', 'center', 'flex-start', 'flex-end', 'self-start', 'self-end', 'stretch'] | None", "description": "Overrides the alignItems property of a flex or grid container.", "default": "None"}, {"name": "justify_self", "type": "Literal['auto', 'normal', 'start', 'end', 'flex-start', 'flex-end', 'self-start', 'self-end', 'center', 'left', 'right', 'stretch'] | None", "description": "Species how the element is justified inside a flex or grid container.", "default": "None"}, {"name": "order", "type": "int | None", "description": "The layout order for the element within a flex or grid container.", "default": "None"}, {"name": "grid_area", "type": "str | None", "description": "When used in a grid layout specifies, specifies the named grid area that the element should be placed in within the grid.", "default": "None"}, {"name": "grid_row", "type": "str | None", "description": "When used in a grid layout, specifies the row the element should be placed in within the grid.", "default": "None"}, {"name": "grid_column", "type": "str | None", "description": "When used in a grid layout, specifies the column the element should be placed in within the grid.", "default": "None"}, {"name": "grid_row_start", "type": "str | None", "description": "When used in a grid layout, specifies the starting row to span within the grid.", "default": "None"}, {"name": "grid_row_end", "type": "str | None", "description": "When used in a grid layout, specifies the ending row to span within the grid.", "default": "None"}, {"name": "grid_column_start", "type": "str | None", "description": "When used in a grid layout, specifies the starting column to span within the grid.", "default": "None"}, {"name": "grid_column_end", "type": "str | None", "description": "When used in a grid layout, specifies the ending column to span within the grid.", "default": "None"}, {"name": "margin", "type": "str | float | None", "description": "The margin for all four sides of the element.", "default": "None"}, {"name": "margin_top", "type": "str | float | None", "description": "The margin for the top side of the element.", "default": "None"}, {"name": "margin_bottom", "type": "str | float | None", "description": "The margin for the bottom side of the element.", "default": "None"}, {"name": "margin_start", "type": "str | float | None", "description": "The margin for the logical start side of the element, depending on layout direction.", "default": "None"}, {"name": "margin_end", "type": "str | float | None", "description": "The margin for the logical end side of the element, depending on layout direction.", "default": "None"}, {"name": "margin_x", "type": "str | float | None", "description": "The margin for the left and right sides of the element.", "default": "None"}, {"name": "margin_y", "type": "str | float | None", "description": "The margin for the top and bottom sides of the element.", "default": "None"}, {"name": "width", "type": "str | float | None", "description": "The width of the element.", "default": "None"}, {"name": "min_width", "type": "str | float | None", "description": "The minimum width of the element.", "default": "None"}, {"name": "max_width", "type": "str | float | None", "description": "The maximum width of the element.", "default": "None"}, {"name": "height", "type": "str | float | None", "description": "The height of the element.", "default": "None"}, {"name": "min_height", "type": "str | float | None", "description": "The minimum height of the element.", "default": "None"}, {"name": "max_height", "type": "str | float | None", "description": "The maximum height of the element.", "default": "None"}, {"name": "position", "type": "Literal['static', 'relative', 'absolute', 'fixed', 'sticky'] | None", "description": "The position of the element.", "default": "None"}, {"name": "top", "type": "str | float | None", "description": "The distance from the top of the containing element.", "default": "None"}, {"name": "bottom", "type": "str | float | None", "description": "The distance from the bottom of the containing element.", "default": "None"}, {"name": "left", "type": "str | float | None", "description": "The distance from the left of the containing element.", "default": "None"}, {"name": "right", "type": "str | float | None", "description": "The distance from the right of the containing element.", "default": "None"}, {"name": "start", "type": "str | float | None", "description": "The distance from the start of the containing element, depending on layout direction.", "default": "None"}, {"name": "end", "type": "str | float | None", "description": "The distance from the end of the containing element, depending on layout direction.", "default": "None"}, {"name": "z_index", "type": "int | None", "description": "The stack order of the element.", "default": "None"}, {"name": "is_hidden", "type": "bool | None", "description": "Whether the element is hidden.", "default": "None"}, {"name": "id", "type": "str | None", "description": "The unique identifier of the element.", "default": "None"}, {"name": "exclude_from_tab_order", "type": "bool | None", "description": "Whether the element should be excluded from the tab order.", "default": "None"}, {"name": "aria_active_descendant", "type": "str | None", "description": "Identifies the currently active element when DOM focus is on a composite widget, textbox, group, or application.", "default": "None"}, {"name": "aria_auto_complete", "type": "Literal['true', 'false'] | bool | Literal['inline', 'list', 'both', 'none'] | None", "description": "Indicates whether inputting text could trigger display of one or more predictions of the user's intended value for an input and specifies how predictions would be presented if they are made.", "default": "None"}, {"name": "aria_haspopup", "type": "Literal['true', 'false'] | bool | Literal['menu', 'listbox', 'tree', 'grid', 'dialog'] | None", "description": "Indicates whether the element, or another grouping element it controls, is currently expanded or collapsed.", "default": "None"}, {"name": "aria_label", "type": "str | None", "description": "The label for the element.", "default": "None"}, {"name": "aria_labelledby", "type": "str | None", "description": "The id of the element that labels the current element.", "default": "None"}, {"name": "aria_describedby", "type": "str | None", "description": "The id of the element that describes the current element.", "default": "None"}, {"name": "aria_details", "type": "str | None", "description": "The id of the element that provides additional information about the current element.", "default": "None"}, {"name": "aria_errormessage", "type": "str | None", "description": "The id of the element that provides an error message for the current element.", "default": "None"}, {"name": "UNSAFE_class_name", "type": "str | None", "description": "A CSS class to apply to the element.", "default": "None"}, {"name": "UNSAFE_style", "type": "Dict[str, Any] | None", "description": "A CSS style to apply to the element.", "default": "None"}, {"name": "key", "type": "str | None", "description": "A unique identifier used by React to render elements in a list.", "default": "None"}]}} />
