TextInput

Capture string input from user
Import

Usage

Radius
xs
sm
md
lg
xl
Size
xs
sm
md
lg
xl
import { TextInput } from '@mantine/core';
function Demo() {
return (
<TextInput
placeholder="Your name"
label="Full name"
withAsterisk
/>
);
}

Controlled

import { useState } from 'react';
import { TextInput } from '@mantine/core';
function Demo() {
const [value, setValue] = useState('');
return <TextInput value={value} onChange={(event) => setValue(event.currentTarget.value)} />;
}

Invalid state and error

// Error as boolean – red border color
<TextInput error />
// Error as React node – red border color and message below input
<TextInput error="Invalid email" />

Disabled state

import { TextInput } from '@mantine/core';
function Demo() {
return <TextInput disabled />
}

With icon

Download more icon variants from https://tabler-icons.io/i/at
import { TextInput } from '@mantine/core';
import { IconAt } from '@tabler/icons';
function Demo() {
return <TextInput label="Your email" placeholder="Your email" icon={<IconAt size={14} />} />;
}

With right section

import { TextInput, Loader } from '@mantine/core';
function Demo() {
return <TextInput label="Your email" placeholder="Your email" rightSection={<Loader size="xs" />} />;
}

Get input ref

import { useRef } from 'react';
import { TextInput } from '@mantine/core';
function Demo() {
const ref = useRef<HTMLInputElement>(null);
return <TextInput ref={ref} />;
}

Accessibility

Provide aria-label in case you use component without label for screen reader support:

<TextInput /> // -> not ok, input is not labeled
<TextInput label="My input" /> // -> ok, input and label is connected
<TextInput aria-label="My input" /> // -> ok, label is not visible but will be announced by screen reader
Download more icon variants from https://tabler-icons.io/i/search

TextInput component props

NameTypeDescription
description
ReactNode
Input description, displayed after label
descriptionProps
Record<string, any>
Props spread to description element
disabled
boolean
Disabled input state
error
ReactNode
Displays error message after input
errorProps
Record<string, any>
Props spread to error element
icon
ReactNode
Adds icon on the left side of input
iconWidth
number
Width of icon section in px
inputContainer
(children: ReactNode) => ReactNode
Input container component, defaults to React.Fragment
inputWrapperOrder
("input" | "label" | "error" | "description")[]
Controls order of the Input.Wrapper elements
label
ReactNode
Input label, displayed before input
labelProps
Record<string, any>
Props spread to label element
radius
number | "xs" | "sm" | "md" | "lg" | "xl"
Input border-radius from theme or number to set border-radius in px
required
boolean
Sets required on input element
rightSection
ReactNode
Right section of input, similar to icon but on the right
rightSectionProps
Record<string, any>
Props spread to rightSection div element
rightSectionWidth
number
Width of right section, is used to calculate input padding-right
size
"xs" | "sm" | "md" | "lg" | "xl"
Input size
type
HTMLInputTypeAttribute
Input element type
variant
"unstyled" | "default" | "filled"
Defines input appearance, defaults to default in light color scheme and filled in dark
withAsterisk
boolean
Determines whether required asterisk should be rendered, overrides required prop, does not add required attribute to the input
wrapperProps
Record<string, any>
Props passed to root element (InputWrapper component)

TextInput component Styles API

NameStatic selectorDescription
wrapper.mantine-TextInput-wrapperRoot Input element
invalid.mantine-TextInput-invalidInvalid input modifier
disabled.mantine-TextInput-disabledInvalid disabled modifier
icon.mantine-TextInput-iconInput icon wrapper on the left side of the input, controlled by icon prop
withIcon.mantine-TextInput-withIconInput element styles when used with icon, controlled by icon prop
input.mantine-TextInput-inputMain input element
rightSection.mantine-TextInput-rightSectionInput right section, controlled by rightSection prop
root.mantine-TextInput-rootRoot element
label.mantine-TextInput-labelLabel element styles, defined by label prop
error.mantine-TextInput-errorError element styles, defined by error prop
description.mantine-TextInput-descriptionDescription element styles, defined by description prop
required.mantine-TextInput-requiredRequired asterisk element styles, defined by required prop