Textarea
<mo-textarea> | MOTextarea
Textareas allow users to input and edit text.
<mo-textarea></mo-textarea>
import { MOTextarea } from '@metsooutotec/modes-web-components/dist/react'; const App = () => <MOTextarea />;
This component works with standard <form>
elements. Please refer to the section on
form controls to learn more about form submission and
client-side validation.
Examples
Rows
Use the rows
attribute to change the number of text rows that get shown.
<mo-textarea rows="2"></mo-textarea>
import { MOTextarea } from '@metsooutotec/modes-web-components/dist/react'; const App = () => <MOTextarea rows={2} />;
Placeholders
Use the placeholder
attribute to add a placeholder.
<mo-textarea placeholder="Type something"></mo-textarea>
import { MOTextarea } from '@metsooutotec/modes-web-components/dist/react'; const App = () => <MOTextarea placeholder="Type something" />;
Filled textareas
Add the filled
attribute to draw a filled textarea.
<mo-textarea placeholder="Type something" filled></mo-textarea>
import { MOTextarea } from '@metsooutotec/modes-web-components/dist/react'; const App = () => <MOTextarea placeholder="Type something" filled />;
Disabled
Use the disabled
attribute to disable a textarea.
<mo-textarea placeholder="Textarea" disabled></mo-textarea>
import { MOTextarea } from '@metsooutotec/modes-web-components/dist/react'; const App = () => <MOTextarea placeholder="Textarea" disabled />;
Sizes
Use the size
attribute to change a textarea’s size.
<mo-textarea placeholder="Small" size="small"></mo-textarea> <br /> <mo-textarea placeholder="Medium" size="medium"></mo-textarea> <br /> <mo-textarea placeholder="Large" size="large"></mo-textarea>
import { MOTextarea } from '@metsooutotec/modes-web-components/dist/react'; const App = () => ( <> <MOTextarea placeholder="Small" size="small"></MOTextarea> <br /> <MOTextarea placeholder="Medium" size="medium"></MOTextarea> <br /> <MOTextarea placeholder="Large" size="large"></MOTextarea> </> );
Labels
Use the label
attribute to give the textarea an accessible label. For labels that contain HTML,
use the label
slot instead.
<mo-textarea label="Comments"></mo-textarea>
import { MOTextarea } from '@metsooutotec/modes-web-components/dist/react'; const App = () => <MOTextarea label="Comments" />;
Help text
Add descriptive help text to a textarea with the help-text
attribute. For help texts that
contain HTML, use the help-text
slot instead.
<mo-textarea label="Feedback" help-text="Please tell us what you think."> </mo-textarea>
import { MOTextarea } from '@metsooutotec/modes-web-components/dist/react'; const App = () => <MOTextarea label="Feedback" help-text="Please tell us what you think." />;
Prevent resizing
By default, textareas can be resized vertically by the user. To prevent resizing, set the
resize
attribute to none
.
<mo-textarea resize="none"></mo-textarea>
import { MOTextarea } from '@metsooutotec/modes-web-components/dist/react'; const App = () => <MOTextarea resize="none" />;
Expand with content
Textareas will automatically resize to expand to fit their content when resize
is set to
auto
.
<mo-textarea resize="auto"></mo-textarea>
import { MOTextarea } from '@metsooutotec/modes-web-components/dist/react'; const App = () => <MOTextarea resize="auto" />;
Importing
If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.
To import this component using a bundler:
import '@metsooutotec/modes-web-components/dist/components/textarea/textarea.js';
To import this component as a React component:
import MOTextarea from '@metsooutotec/modes-web-components/dist/react/textarea/';
To import this component using a script tag:
<script type="module" src="https://modes-web.metso.com/dist/components/cdn/components/textarea/textarea.js"></script>
Slots
Name | Description |
---|---|
label
|
The textarea’s label. Alternatively, you can use the label prop. |
help-text
|
Help text that describes how to use the input. |
Learn more about using slots.
Properties
Name | Description | Reflects | Type | Default |
---|---|---|---|---|
size
|
The textarea’s size. |
|
'small' | 'medium' | 'large'
|
'medium'
|
name
|
The textarea’s name attribute. |
string
|
- | |
value
|
The textarea’s value attribute. |
string
|
''
|
|
defaultValue
|
The textarea’s default value. |
string
|
''
|
|
filled
|
Draws a filled textarea. |
|
boolean
|
false
|
label
|
The textarea’s label. Alternatively, you can use the label slot. |
string
|
''
|
|
helpText
help-text
|
The textarea’s help text. Alternatively, you can use the help-text slot. |
string
|
''
|
|
placeholder
|
The textarea’s placeholder text. |
string
|
- | |
rows
|
The number of rows to display by default. |
number
|
4
|
|
resize
|
Controls how the textarea can be resized. |
'none' | 'vertical' | 'auto'
|
'vertical'
|
|
disabled
|
Disables the textarea. |
|
boolean
|
false
|
readonly
|
Makes the textarea readonly. |
|
boolean
|
false
|
minlength
|
The minimum length of input that will be considered valid. |
number
|
- | |
maxlength
|
The maximum length of input that will be considered valid. |
number
|
- | |
required
|
Makes the textarea a required field. |
|
boolean
|
false
|
invalid
|
This will be true when the control is in an invalid state. Validity is determined by props such as
type , required , minlength , and maxlength using
the browser’s constraint validation API.
|
|
boolean
|
false
|
autocapitalize
|
The textarea’s autocapitalize attribute. |
'off' | 'none' | 'on' | 'sentences' | 'words' | 'characters'
|
- | |
autocorrect
|
The textarea’s autocorrect attribute. |
string
|
- | |
autocomplete
|
The textarea’s autocomplete attribute. |
string
|
- | |
autofocus
|
The textarea’s autofocus attribute. |
boolean
|
- | |
spellcheck
|
Enables spell checking on the textarea. |
boolean
|
- | |
inputmode
|
The textarea’s inputmode attribute. |
'none' | 'text' | 'decimal' | 'numeric' | 'tel' | 'search' | 'email' | 'url'
|
- | |
validity
|
Gets the validity state object | - | - | |
validationMessage
|
Gets the validation message | - | - | |
updateComplete |
A read-only promise that resolves when the component has finished updating. |
Learn more about attributes and properties.
Events
Name | React Event | Description | Event Detail |
---|---|---|---|
mo-change |
onMoChange |
Emitted when an alteration to the control’s value is committed by the user. | - |
mo-input |
onMoInput |
Emitted when the control receives input and its value changes. | - |
mo-focus |
onMoFocus |
Emitted when the control gains focus. | - |
mo-blur |
onMoBlur |
Emitted when the control loses focus. | - |
Learn more about events.
Methods
Name | Description | Arguments |
---|---|---|
focus() |
Sets focus on the textarea. |
options: FocusOptions
|
blur() |
Removes focus from the textarea. | - |
select() |
Selects all the text in the textarea. | - |
scrollPosition() |
Gets or sets the textarea’s scroll position. |
position: { top?: number; left?: number }
|
setSelectionRange() |
Sets the start and end positions of the text selection (0-based). |
selectionStart: number, selectionEnd: number, selectionDirection: 'forward' | 'backward' | 'none'
|
setRangeText() |
Replaces a range of text with a new string. |
replacement: string, start: number, end: number, selectMode: 'select' | 'start' | 'end' |
'preserve'
|
checkValidity() |
Checks for validity but does not show a validation message. Returns true when valid and
false when invalid.
|
- |
getForm() |
Gets the associated form, if one exists. | - |
reportValidity() |
Checks for validity and shows the browser’s validation message if the control is invalid. | - |
setCustomValidity() |
Sets a custom validation message. Pass an empty string to restore validity. |
message: string
|
Learn more about methods.
Parts
Name | Description |
---|---|
base |
The component’s internal wrapper. |
form-control |
The form control that wraps the label, textarea, and help text. |
textarea |
The textarea control. |
help-text |
The textarea help text. |
label__base |
The textarea label. |
Learn more about customizing CSS parts.