TextAreas are multiline text inputs, useful for cases where users have a sizable amount of text to enter. They allow for all customizations that are available to text fields.
Value
Use the value or defaultValue prop to set the text value, and onChange to handle user input.
Your comment:
import {TextArea} from '@react-spectrum/s2/TextArea';
import {useState} from 'react';
function Example() {
let [name, setName] = useState('');
return (
<>
<TextArea
label="Comment"
placeholder="Share your thoughts!"
value={name}
onChange={setName} />
<p>Your comment: {name}</p>
</>
);
}
Prefix
Use the prefix prop to display an additional element before the input in the text area.
import {TextArea} from '@react-spectrum/s2/TextArea';
import MentionIcon from '@react-spectrum/s2/icons/Mention';
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
function Example() {
return (
<div className={style({display: 'flex', flexDirection: 'column', gap: 8})}>
<TextArea label="Irrational number" prefix="#" placeholder="3.1415926535897932384626433832795028841971" />
<TextArea label="Mention" prefix={<MentionIcon />} placeholder="username" />
</div>
);
}
Forms
Use the name prop to submit the text value to the server. Set the isRequired, minLength, or maxLength props to validate the value, or implement custom client or server-side validation. See the Forms guide to learn more.
API
| Name | Type | Default |
|---|---|---|
size | 'S'
| 'M'
| 'L'
| 'XL' | Default: 'M'
|
The size of the text field. | ||
prefix | ReactNode | Default: — |
The prefix to display in the text field. A non-interactive element that appears before the input. | ||
enterKeyHint | 'enter'
| 'done'
| 'go'
| 'next'
| 'previous'
| 'search'
| 'send' | Default: — |
An enumerated attribute that defines what action label or icon to preset for the enter key on virtual keyboards. See MDN. | ||
isDisabled | boolean | Default: — |
Whether the input is disabled. | ||
isReadOnly | boolean | Default: — |
Whether the input can be selected but not changed by the user. | ||
inputMode | 'none'
| 'text'
| 'tel'
| 'url'
| 'email'
| 'numeric'
| 'decimal'
| 'search' | Default: — |
Hints at the type of data that might be entered by the user while editing the element or its contents. See MDN. | ||
autoCorrect | string | Default: — |
An attribute that takes as its value a space-separated string that describes what, if any, type of autocomplete functionality the input should provide. See MDN. | ||
spellCheck | string | Default: — |
An enumerated attribute that defines whether the element may be checked for spelling errors. See MDN. | ||
styles | StylesProp | Default: — |
Spectrum-defined styles, returned by the | ||
placeholder | string | Default: — |
Temporary text that occupies the text input when it is empty. See MDN. | ||
value | string | Default: — |
The current value (controlled). | ||
defaultValue | string | Default: — |
The default value (uncontrolled). | ||
onChange | | Default: — |
Handler that is called when the value changes. | ||
label | ReactNode | Default: — |
The content to display as the label. | ||
labelPosition | LabelPosition | Default: 'top'
|
The label's overall position relative to the element it is labeling. | ||
labelAlign | Alignment | Default: 'start'
|
The label's horizontal alignment relative to the element it is labeling. | ||
contextualHelp | ReactNode | Default: — |
A ContextualHelp element to place next to the label. | ||
minLength | number | Default: — |
The minimum number of characters required by the input. See MDN. | ||
maxLength | number | Default: — |
The maximum number of characters supported by the input. See MDN. | ||
isRequired | boolean | Default: — |
Whether user input is required on the input before form submission. | ||
isInvalid | boolean | Default: — |
Whether the value is invalid. | ||
validate | | Default: — |
A function that returns an error message if a given value is invalid.
Validation errors are displayed to the user when the form is submitted
if | ||
validationBehavior | 'native' | 'aria' | Default: 'native'
|
Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA. | ||
necessityIndicator | NecessityIndicator | Default: 'icon'
|
Whether the required state should be shown as an icon or text. | ||
description | ReactNode | Default: — |
A description for the field. Provides a hint such as specific requirements for what to choose. | ||
errorMessage | ReactNode | | Default: — |
An error message for the field. | ||
onFocus | | Default: — |
Handler that is called when the element receives focus. | ||
onBlur | | Default: — |
Handler that is called when the element loses focus. | ||
onFocusChange | | Default: — |
Handler that is called when the element's focus status changes. | ||
onKeyDown | | Default: — |
Handler that is called when a key is pressed. | ||
onKeyUp | | Default: — |
Handler that is called when a key is released. | ||
onCopy | ClipboardEventHandler | Default: — |
Handler that is called when the user copies text. See MDN. | ||
onCut | ClipboardEventHandler | Default: — |
Handler that is called when the user cuts text. See MDN. | ||
onPaste | ClipboardEventHandler | Default: — |
Handler that is called when the user pastes text. See MDN. | ||
onCompositionStart | CompositionEventHandler | Default: — |
Handler that is called when a text composition system starts a new text composition session. See MDN. | ||
onCompositionEnd | CompositionEventHandler | Default: — |
Handler that is called when a text composition system completes or cancels the current text composition session. See MDN. | ||
onCompositionUpdate | CompositionEventHandler | Default: — |
Handler that is called when a new character is received in the current text composition session. See MDN. | ||
onSelect | ReactEventHandler | Default: — |
Handler that is called when text in the input is selected. See MDN. | ||
onBeforeInput | FormEventHandler | Default: — |
Handler that is called when the input value is about to be modified. See MDN. | ||
onInput | FormEventHandler | Default: — |
Handler that is called when the input value is modified. See MDN. | ||
name | string | Default: — |
The name of the input element, used when submitting an HTML form. See MDN. | ||
autoComplete | string | Default: — |
Describes the type of autocomplete functionality the input should provide if any. See MDN. | ||
form | string | Default: — |
The | ||
autoFocus | boolean | Default: — |
Whether the element should receive focus on render. | ||
id | string | Default: — |
The element's unique identifier. See MDN. | ||
excludeFromTabOrder | boolean | Default: — |
Whether to exclude the element from the sequential tab order. If true, the element will not be focusable via the keyboard by tabbing. This should be avoided except in rare scenarios where an alternative means of accessing the element or its functionality via the keyboard is available. | ||
aria-activedescendant | string | Default: — |
Identifies the currently active element when DOM focus is on a composite widget, textbox, group, or application. | ||
aria-autocomplete | 'none'
| 'inline'
| 'list'
| 'both' | Default: — |
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. | ||
aria-haspopup | boolean
| 'false'
| 'true'
| 'menu'
| 'listbox'
| 'tree'
| 'grid'
| 'dialog' | Default: — |
Indicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element. | ||
aria-controls | string | Default: — |
Identifies the element (or elements) whose contents or presence are controlled by the current element. | ||
aria-label | string | Default: — |
Defines a string value that labels the current element. | ||
aria-labelledby | string | Default: — |
Identifies the element (or elements) that labels the current element. | ||
aria-describedby | string | Default: — |
Identifies the element (or elements) that describes the object. | ||
aria-details | string | Default: — |
Identifies the element (or elements) that provide a detailed, extended description for the object. | ||
aria-errormessage | string | Default: — |
Identifies the element that provides an error message for the object. | ||
UNSAFE_className | UnsafeClassName | Default: — |
Sets the CSS className
for the element. Only use as a last resort. Use the | ||
UNSAFE_style | CSSProperties | Default: — |
Sets inline style for the
element. Only use as a last resort. Use the | ||
slot | string | null | Default: — |
A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit | ||