Textarea
is a form component that allows the user to enter a sizeable amount of multi-line plain text.
How to use Textarea
Textarea can be imported from @contentful/f36-components
or @contentful/f36-forms
.
This component is renders a form element, therefore it should be used within the context of a <Form>
.
Examples
With label
In order to create a correct textarea input the recommendation is to add a label to any kind of input. In order to do that, we provide you couple of additional components that you can use to compose your inputs with labels, help or validation messages.
Read more about FormControl
isRequired
By using the isRequired
prop, it’s possible to control the required state of the textarea
.
This prop will set disabled
attribute of the HTML tag to true
.
isDisabled
By using the isDisabled
prop, it’s possible to control the disabled state of the textarea
.
This prop will handle the disabled styles and set disabled
attribute of the HTML tag to true
.
isInvalid
By using the isInvalid
prop, it’s possible to control the error state of the textarea
.
This prop will handle the error styles.
Using ref
const textareaRef = React.createRef()<Textareaid="textarea"name="textarea"label="My textarea"placeholder="Placeholder"ref={textareaRef}/><Button onClick={() => textareaRef.current.focus()}>Focus Textarea with ref</Button>
The Textarea
is exposing the ref of the textarea
tag as a prop,
which means that you can pass a ref created with React.createRef()
or useRef
hook to it and it will work as expected.
Accessibility
- To ensure accessibility, provide a
name
prop - In case the input is required, use
isRequired
prop - If you are using this component in a form, provide validation to the input and use
isInvalid
to control the error state ofTextarea
. Also show meaningful error messages with theValidationMessage
component. - Provide instructions of how to fill the field with
HelpText
component