MUITextArea
MUITextArea is a multiline Material UI
TextField that wires up the
label, error state and helper text for you. It is a controlled component that
works with plain useState or any form library able to provide a value and
receive changes.
Unless you set rows, the field grows with its content. Use maxChars to cap
the length: typing stops at the limit and pasted text is cut to fit.
For single-line values, use MUITextField instead.
Usage
import MUITextArea, { MUITextAreaProps } from '@nish1896/mui-components/mui/text-area';Provide fieldName, value and onValueChange to render a fully functional
text area. Set rows for a fixed height.
const [bio, setBio] = useState<string | undefined>();
<MUITextArea
fieldName="bio"
value={bio}
onValueChange={({ newValue, event }) => setBio(newValue)}
rows={4}
/>onValueChange receives { newValue, event }: the typed string and the change
event of the underlying textarea.
Validation stays in your hands — derive errorMessage from your own state or
form library and the field renders in an error state:
<MUITextArea
fieldName="bio"
value={bio}
onValueChange={({ newValue }) => setBio(newValue)}
onBlur={() => setBioError(validateBio(bio))}
errorMessage={bioError}
/>Sizing
By default the field starts at one row and grows with its content. rows fixes
the height instead, and the content scrolls once it no longer fits. To keep the
auto-growing behavior within bounds, pass minRows and maxRows, which are
forwarded to the underlying TextField.
<MUITextArea
fieldName="notes"
value={notes}
onValueChange={({ newValue }) => setNotes(newValue)}
minRows={3}
maxRows={8}
/>Validating the length
maxChars only caps what the user can type. To enforce a minimum, or to explain
a rule, derive errorMessage from the value. It works well together with the
counter:
<MUITextArea
fieldName="review"
value={review}
onValueChange={({ newValue }) => setReview(newValue)}
maxChars={300}
showCharLimit
helperText="Tell us what you liked"
errorMessage={
review.trim().length < 20 ? 'Write at least 20 characters' : undefined
}
/>errorMessage also accepts a string[], renderError customizes how the
messages are shown, and hideErrorMessage keeps the error state without the
text. The label can be customized with label, showLabelAboveFormField,
hideLabel and formLabelProps, as described for
MUITextField.
Character Limit
Setting maxChars limits the number of characters. Typing past the limit
is ignored, and pasted text longer than the remaining room is truncated.
maxChars limits only what the user types or pastes. A value set from
code, such as an initial value or a state update, is shown as is, even when
it is longer than maxChars. The user can then only delete characters,
until the length drops below the limit. Truncate such values yourself
before passing them in.
maxChars uses the native maxLength attribute, which counts UTF-16 code
units, so an emoji counts as 2 characters.
<MUITextArea
fieldName="feedback"
value={feedback}
onValueChange={({ newValue }) => setFeedback(newValue)}
maxChars={50}
/>Add showCharLimit to render a current/max counter at the bottom-right inside
the field. It sits in its own strip below the text, so typed content never runs
under it:
<MUITextArea
fieldName="bio"
value={bio}
onValueChange={({ newValue }) => setBio(newValue)}
maxChars={150}
showCharLimit
/>Custom character limit text
Pass renderCharLimit to replace the default current/max text with your own
node. It receives the current count and maxChars, and shows the counter on its
own. showCharLimit isn't needed.
<MUITextArea
fieldName="bio"
value={bio}
onValueChange={({ newValue }) => setBio(newValue)}
maxChars={150}
renderCharLimit={(charCount, maxChars) => (
<Typography
variant="caption"
color={maxChars - charCount <= 10 ? 'error' : 'text.secondary'}
>
{maxChars - charCount} characters left
</Typography>
)}
/>API
MUITextAreaProps also accepts the remaining
TextFieldProps, except
multiline (always on), in addition to the props below.
Props marked with * are required.
| Name | Type | Description |
|---|---|---|
fieldName* | string | Name/path of the field. Used to derive the id, the default label, and the name attribute. This prop is required for all components. |
value | string | null | Current value of the field. This is a controlled component — value and onValueChange must be supplied together, typically backed by your own state or form library. undefined/null are treated as an empty string. |
onValueChange* | ({ newValue, event }) => void | Called on every input change with the next string value and the original change event. Call your state setter (or form library's setter) with newValue to update value. |
maxChars | number | Maximum number of characters the field accepts. Typing stops at the limit and pasted text is cut to fit, via the native maxLength attribute — so it also applies on mobile keyboards. Counts UTF-16 code units, so an emoji counts as 2. Limits only user input: a value set from code (e.g. an initial value) is shown in full even if longer than maxChars, and the user can then only delete characters. Truncate such values yourself. |
showCharLimit | boolean | When true, shows a current/max character counter at the bottom-right inside the field, in its own strip below the text so typed content never runs under it. Has no effect unless maxChars is set. Passing renderCharLimit also shows the counter, so this can be omitted then. Replaces any slotProps.input.endAdornment while shown. |
renderCharLimit | (charCount: number, maxChars: number) => ReactNode | Custom renderer for the character counter, replacing the default current/max text. Providing it shows the counter even without showCharLimit, as long as maxChars is set; the counter keeps its bottom-right strip inside the field. |
rows | number | Fixed number of visible text rows. When omitted, the field starts at one row and grows with its content. |
label | ReactNode | Label displayed for the field. Defaults to a human-readable label derived from fieldName, e.g. firstName becomes "First Name". |
showLabelAboveFormField | boolean | When true, renders the field label above the form field in the FormLabel component, instead of inside or beside it. Takes precedence over allLabelsAboveFields from the ConfigProvider, so false keeps the label inside this field even when the config places every label above its field. |
formLabelProps | FormLabelProps | FormLabelProps forwarded to the internal FormLabel. The id is managed by the component. Multiple fields can be configured using the ConfigProvider component. |
hideLabel | boolean | When true, hides the rendered field label while preserving accessible labeling where possible. |
required | boolean | Indicates that the field is mandatory by adding an asterisk symbol (*) to the form label and setting the relevant accessibility attributes. |
errorMessage | string | string[] | Validation error for the field — pass a single message string, or a string[] when the field can fail multiple rules at once (every message is shown together). A non-empty string or array puts the field in an error state; undefined/''/[] clear it.Normalize your form library's error shape to this at the call site (e.g. an RHF FieldError via its .message). Use renderError to customize how the message(s) are rendered. |
renderError | (errors: string[]) => ReactNode | Custom renderer for the resolved error message(s), called only when the field is in an error state. Always receives a string[] — use errors[0] for the common single-message case, or map over errors when a field fails several rules. By default a single message renders as text and multiple messages render on separate lines. |
hideErrorMessage | boolean | If true, hides the error message text while keeping the field in an error state. |
helperText | ReactNode | Content displayed in the FormHelperText component below the field when there is no visible validation error. |
formHelperTextProps | FormHelperTextProps | FormHelperTextProps forwarded to the internal FormHelperText. The id is managed by the component. Multiple fields can be configured using the ConfigProvider component. |
customIds | { field, label, helperText, error } | Overrides the default field, label, helper text, and error IDs used for accessibility. |
Playground
Auto-growing field with required & min length validation, and an in-field character counter (showCharLimit)
Fixed rows with maxChars, and a custom counter via renderCharLimit — typing stops at the limit and pasted text is cut to fit
Label above form-field, with custom ids
Form State - Values & Errors
Reference
Source Code
View the full implementation of this component on GitHub.