Text Area
Multiline inputs for text
TextArea is a controlled component. This means that the visible text will always match the contents of the value prop.
In this example, notice how value is stored within this.state. The onChange function will set the new value when the user enters or removes a character in the textarea.
Basic Text Area
function TextAreaExample() { const [value, setValue] = React.useState(''); return ( <div> <Label text="Business description"> <TextArea value={value} placeholder="Tell us about your business" onChange={v => setValue(v)} /> </Label> </div> ); }
Disabled Text Area
The isDisabled prop disables the TextArea visually and functionally.
function TextAreaExample() { const [value, setValue] = React.useState(''); return ( <TextArea isDisabled placeholder="Tell us about your business" onChange={v => setValue(v)} value={value} /> ); }
Text Area with an error
The hasError prop only changes the Text Area’s color. It should be used alongside an error message that helps users advance through the form.
function TextAreaExample() { const [value, setValue] = React.useState( 'We provide DJ services in Austin and have been in business for 25 years.', ); return ( <TextArea hasError placeholder="Tell us about your business" onChange={v => setValue(v)} value={value} /> ); }
Props
TextArea
valuerequiredThe current value of the textarea.
TypestringonChangerequiredThe function that is called when the textarea value changes.
It receives two arguments:
onChange(newValue, event).The consumer of this component should use that data to update the
valueprop passed in to this component.Type(newValue: string, event: React.ChangeEvent<HTMLTextAreaElement>) => voididAdds a HTML
idattribute to the textarea. This is used for linking the HTML with a Label.TypestringisDisabledVisually and functionally disable the textarea.
TypebooleanDefaultfalseisReadOnlyAdds
readonlyHTML attribute, allowing users to select (but not modify) the input.TypebooleanDefaultfalseisRequiredAdds the
requiredHTML attribute to the textarea.TypebooleanDefaultfalsehasErrorMakes the textarea border and text color red.
TypebooleanDefaultfalseplaceholderText that appears within the textarea when there is no
value.TypestringnameAdds
nameHTML attribute to element, indicating the property name associated with the selected value.TypestringmaxLengthThe maximum number of characters that a user can enter.
onChangewill not fire if a user enters a character that exceedsmaxLength.TypenumberonFocusFires when the textarea receives focus.
Type(event: React.FocusEvent<HTMLTextAreaElement>) => voidonBlurFires when the textarea loses focus.
Type(event: React.FocusEvent<HTMLTextAreaElement>) => voiddataTestA selector hook into the React component for use in automated testing environments. It is applied internally to the
<textarea />element.Typestring