` element.
## Examples
### Uncontrolled vs controlled
Render a `TextInput` in an uncontrolled fashion to let it keep track of its own value:
```tsx demo
import { TextInput } from "@tenstorrent/vesper/text-input";
export default function UncontrolledTextInput() {
return
;
}
```
If you need to control the value of the `TextInput`, you can do so via the `value` and `onChange` props:
```tsx demo
import { useState } from "react";
import { TextInput } from "@tenstorrent/vesper/text-input";
export default function ControlledTextInput() {
const [value, setValue] = useState("");
return (
setValue(e.target.value)}
/>
);
}
```
### Different sizes
The `TextInput` component can be rendered at `sm`, `md`, or `lg` size, defaulting to `md`. Size affects the padding and text styles of the input, as well as the size of the rendered icons.
```tsx demo
import { TextInput } from "@tenstorrent/vesper/text-input";
export default function TextInputSizes() {
return (
);
}
```
### Rendering icons
You can render icons to the left or the right of the input via the `iconLeft` and `iconRight` props:
```tsx demo
import { Globe, Search } from "@tenstorrent/vesper/icons";
import { TextInput } from "@tenstorrent/vesper/text-input";
export default function TextInputWithIcons() {
return (
}
iconRight={ }
/>
);
}
```
Rendered icons can be treated as buttons by passing the `iconLeftAction` and `iconRightAction` props. Each action takes a `handler` that is called when the icon is clicked, and an `ariaLabel` that gives the resulting button an accessible label:
```tsx demo
import { Search } from "@tenstorrent/vesper/icons";
import { TextInput } from "@tenstorrent/vesper/text-input";
export default function TextInputWithIconAction() {
return (
}
iconLeftAction={{
handler: () => console.log("Search logs"),
ariaLabel: "Search logs",
}}
/>
);
}
```
### Disabling the input
You can disable a `TextInput` by passing `disabled={true}` or just `disabled` as a prop:
```tsx demo
import { TextInput } from "@tenstorrent/vesper/text-input";
export default function DisabledTextInput() {
return (
);
}
```
### Variants
The `TextInput` component can be rendered in one of four variants: `default`, `warning`, `success`, or `error`. The variant determines the colour scheme of the input.
#### Default variant
A `TextInput` renders with the `default` variant when no `variant` prop is specified:
```tsx demo
import { TextInput } from "@tenstorrent/vesper/text-input";
export default function DefaultTextInput() {
return (
);
}
```
#### Warning variant
Use the `warning` variant to flag a value that needs attention, but that doesn't prevent the form from being submitted:
```tsx demo
import { TextInput } from "@tenstorrent/vesper/text-input";
export default function WarningTextInput() {
return (
);
}
```
#### Success variant
The `success` variant is best suited for confirming that a value has been validated as expected:
```tsx demo
import { useState } from "react";
import { TextInput } from "@tenstorrent/vesper/text-input";
export default function SuccessTextInput() {
const [workspace, setWorkspace] = useState("vesper");
return (
setWorkspace(e.target.value)}
/>
);
}
```
#### Error variant
The `error` variant highlights an invalid value that must be corrected before the form can be submitted:
```tsx demo
import { useState } from "react";
import { TextInput } from "@tenstorrent/vesper/text-input";
export default function ErrorTextInput() {
const [email, setEmail] = useState("you@example.com");
return (
setEmail(e.target.value)}
required
/>
);
}
```
### Usage in forms
`TextInput` renders a native ` `, so it works with regular form submission and native validation via props like `name`, `form`, `required`, `pattern`, `minLength`, and `maxLength`:
```tsx
import { Button } from "@tenstorrent/vesper/button";
import { TextInput } from "@tenstorrent/vesper/text-input";
export default function ContactForm() {
return (
);
}
```
### Accessing the underlying element
Use the `ref` prop when you need direct access to the underlying ` ` element, eg. to focus it or read its selection:
```tsx
import { useRef } from "react";
import { Search } from "@tenstorrent/vesper/icons";
import { TextInput } from "@tenstorrent/vesper/text-input";
export default function SearchField() {
const ref = useRef(null);
return (
}
iconLeftAction={{
handler: () => ref.current?.focus(),
ariaLabel: "Search logs",
}}
/>
);
}
```