Skip to main content

TextArea

The TextArea component can be used as a multi-line plain-text editing control to enter free-form text, such as a comment on a form.

Import

import { TextArea } from '@dynatrace/strato-components-preview/forms';

Use cases

This is the simplest version of the TextArea, which is uncontrolled and therefore handles its state internally. You can also set a specific initial value using the defaultValue prop.

CodeSandbox

Control the state

The TextArea can also be controlled, meaning that you can handle the state. To do so, you need to use the onChange prop to provide a handler that is called when the internal state of the TextArea changes. You also need to assign the value from the state to the TextArea by setting the value prop.

CodeSandbox

Disable resizing

The TextArea component is resizable by default. However, in case you need to disable resizing, you can set the resize prop to none. This doesn't change the amount of text you can enter, but only the visual appearance. The amount of text can be adjusted using the cols and rows.

CodeSandbox

Restrict resizing to one direction

The TextArea component can be resized in both directions by default. However, in case you need to resize in one direction only, you can set the resize prop to horizontal or vertical.

CodeSandbox

Validate

This example shows how you can validate the TextArea using the react-hook-form package, which handles error messages. For connecting the form with the TextArea and validating, you need to register the field with custom error messages and use the useForm hook from react-hook-form. Also, by using the controlState prop, you can override the error messages and connect the TextArea's error state and message to that of the form. This shows a hint in case of an error and applies proper styling to the component. The minLength and maxLength can be set on the TextArea as props, as well as when registering the field, to restrict the message length and show custom messages if it doesn't match.

CodeSandbox

Props

TextAreaProps

Signature:

export declare type TextAreaProps = <, (value: , event: <>) => | > & & & & & & { placeholder?: ; /** * This attribute indicates whether the textarea is resizable by the user, and, if so, in which direction. * @defaultValue */ resize?: | | | ; /** * The visible width of the text control, in average character widths. * If it is specified, it must be a positive integer. * @defaultValue 20 */ cols?: ; /** * The of visible text lines for the control. If it is specified, it must be a positive integer. * @defaultValue 2 */ rows?: ; /** * The minimum of characters required that the user should enter. * Doesn't stop the user from removing characters so that the entered goes past the minimum, * but it does make the value entered into the <textarea> invalid. * With a set minlength an empty <textarea> is still considered valid unless you also have the required attribute set. */ minLength?: ; /** * The maximum of characters that the user can enter. If this value isn't specified, * the user can enter an unlimited of characters. */ maxLength?: ; /** * This attribute indicates whether the value of the control can be automatically completed by the browser. * If no autocompletion is desired, it can be set to . */ autoComplete?: ; /** The name of the control - used when submitting the textarea in a form. */ width?: ; };

TextAreaBaseProps

NameTypeDefaultDescription
placeholder?
-
resize?
| | |
'both'This attribute indicates whether the textarea is resizable by the user, and, if so, in which direction.
cols?
20The visible width of the text control, in average character widths. If it is specified, it must be a positive integer.
rows?
2The number of visible text lines for the control. If it is specified, it must be a positive integer.
minLength?
-The minimum number of characters required that the user should enter. Doesn't stop the user from removing characters so that the number entered goes past the minimum, but it does make the value entered into the <textarea> invalid. With a set minlength an empty <textarea> is still considered valid unless you also have the required attribute set.
maxLength?
-The maximum number of characters that the user can enter. If this value isn't specified, the user can enter an unlimited number of characters.
autoComplete?
-This attribute indicates whether the value of the control can be automatically completed by the browser. If no autocompletion is desired, it can be set to "off".
width?
-The name of the control - used when submitting the textarea in a form.

TextAreaControlledProps

NameTypeDefaultDescription
value
-The current value of the textarea.
onChange
(value: , event: <>) =>
-Handler that is called when the value changes.

TextAreaUncontrolledProps

NameTypeDefaultDescription
defaultValue?
-The default value of the textarea.
Still have questions?
Find answers in the Dynatrace Community