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. The component uses a
fallback FormFieldMessages context if it wasn't wrapped in a FormField.
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.
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.
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.
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.
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.