Skip to main content
Brand and assets
Build
Monitor and contribute
/
Component library
  • All components
  • Buttons & actions
  • Forms & inputs
    • Checkbox
    • ChoiceGroup
    • CurrencyInput
    • DateInput
    • FileInputZone
    • Filters
    • Form
    • MaskedTextInput
    • PasswordInput
    • Pill
    • Pill
    • Radio
    • SearchInput
    • SegmentedSlider
    • Select
    • Switch
    • TextArea
    • TextInput
  • Layout & containers
  • Overlays
  • Feedback & status
  • Data display
  • Typography
  • Accessibility
  • Contrib
  • Deprecated
  • Forms & inputs
  • TextInput

TextInput

Captures a single line of text: a name, an email, a reference. Inside a `Form`, use `Form.TextInput` to get validation.

Health
Component health
Source
View on Github
Design
View Figma
Storybook
View in Storybook
Playground
No Example
Accessibility

compliant

Platform
Universal
  • Design
  • Technical
  • Props
  • Figma

Example​

Where you live today

Use cases​

✓ When to use
  • Short free text: a name, an email, a city, a reference number.
  • Form fields: inside a <Form>, through Form.TextInput, to get Formik validation.
  • Inline edits: a single value edited in place, such as a nickname.
✗ When not to use
  • Several lines of text: comments or descriptions, use TextArea.
  • A fixed format: IBAN, social security number or phone number, use MaskedTextInput.
  • Secrets: passwords, use PasswordInput.
  • A known list of values: countries or plans, use Select.
  • Dates: use DateInput.

Alternatives​

TextArea
Choose when
The answer can span several lines, such as a comment or a description.
Why it wins
  • Grows with its content with autosize
  • Sets its height with numberOfLines
Tradeoffs
  • Takes more vertical space
  • Enter adds a line instead of submitting
Read docs
MaskedTextInput
Choose when
The value has a fixed format, such as an IBAN or a social security number.
Why it wins
  • Shows the formatted value while typing
  • onChange receives the raw value, without separators
Tradeoffs
  • Needs a mask per format
  • Not for free text
Read docs
PasswordInput
Choose when
The user types a password or another secret.
Why it wins
  • Hides the value, with a toggle to show it
  • Optional strength indicator with strengthCheck
Tradeoffs
  • Only for secrets
Read docs

States​

Default​

Where you live today

Filled​

Error​

Enter your first and last name

When one message covers several inputs, pass a boolean error and link the message with aria-describedby, so it is not shown by colour alone:

The end date must be after the start date

Disabled​

Read-only​

Required​

Responsive behavior​

The input takes the full width of its container on every platform. In apps using the condensed theme density, it renders in a condensed style:

Playground​

Loading...

Do's and don'ts​

✓ Do
  • Always give a visible label. When the label is rendered elsewhere, point to it with aria-labelledby. Use aria-label only when there is no visible label at all.
  • Explain the expected format in helpText, not in placeholder.
  • Set autoComplete on fields about the user, such as name, email or street-address.
  • Pass the error message to error, so it is announced with the input.
✗ Don't
  • Use placeholder as the label: it disappears as soon as the user types.
  • Use it for multi-line text, passwords or fixed formats: see the alternatives.
  • Show the error message in a separate Text next to the input.

Accessibility​

What you must provide:

  • A label for every input, see Do's and don'ts.
  • An error message as text in error. A boolean error alone only changes the colour.
  • autoComplete on fields about the user, so browsers and assistive tools can fill them.
  • Nothing essential in placeholder: it disappears when the user types.
  • Inside a group of fields, such as day, month and year, the group's name in groupLabel, so iOS and Android read it with each field.
WCAG criteria
  • 1.3.1 Info and Relationships: label, help text and error are linked to the input.
  • 1.3.5 Identify Input Purpose: autoComplete on fields about the user.
  • 1.4.1 Use of Color: an error is never shown by colour alone.
  • 1.4.3 Contrast (Minimum): text, placeholder and help text.
  • 2.4.7 Focus Visible: the focus ring.
  • 3.3.1 Error Identification and 3.3.3 Error Suggestion: say what is wrong and how to fix it.
  • 3.3.2 Labels or Instructions: a visible label, and the expected format in helpText.
  • 4.1.2 Name, Role, Value: the input exposes its name, required and invalid states.

Usage examples​

Uncontrolled​

Pass defaultValue and let the input keep its own state. Use onChange to read the value as the user types.

Controlled​

import { useState } from "react";
import { TextInput } from "murray";

function Demo() {
const [value, setValue] = useState("");
return <TextInput label="Full name" value={value} onChange={(newValue) => setValue(newValue)} />;
}

Prefix and suffix​

Use prefix and suffix to add icons or other elements on the left and right side of the input.

@alan.eu

Only the part before @alan.eu

Usage in <Form>​

On web, Form.TextInput connects the input to Formik:

Any name starting with "Alan".

tip
This component is universal, its usage is the same in murray and murray/mobile.

TextInput props

NameTypeDefault ValueDescriptionUsage count
aria-describedbystringId of an element outside the input that also describes it, such as an error shared by several inputs
aria-labelstringAccessibility label, only necessary when not associated with a label
aria-labelledbystring
aria-requiredbooleanOverrides `aria-required` set by `required`, to mark the input as mandatory without the asterisk
autoFocusbooleanIf true, focuses the input on componentDidMount.The default value is false.
cursorCursorEnforce a cursor type
defaultValuestringUncontrolled default value.
disabledbooleanDisable input
enterKeyHint"search""done""go""next""send"
errorReactNodeContents of `InputError` component, if not set, error is not rendered.
focusedbooleanShow focus styles even when the input isn't focused, e.g. while a readonly input's picker is open
groupLabelReactNodeName of the group the input belongs to, such as the date its day field is part of. VoiceOver and TalkBack don't announce it when focus lands on the input, so on native it ends the input's accessible name, after `label`. Ignored without a `label`.
helpTextReactNodeContents of `InputHelp` component, if not set, description is not rendered.
idstring
labelReactNode
maxstringnumber
maxLengthnumberMaximum string length that the user can enter in UTF-16 code units
minstringnumber
minLengthnumberMaximum string length that the user can enter in UTF-16 code units
namestringHtml input name
onChange((value: string) => void)Trigger when input value is changing
onPress((event: GestureResponderEvent) => void)
placeholderstring
prefixReactNodeContent section rendered on the left side of the input
prefixPointerEvents"none""auto""box-none""box-only"Sets pointer-events styles on the `prefix` element, `none` by default
readonlybooleanMake input readonly
render
1 possibilities((elements: { input: ReactNode; prefix?: ReactNode; suffix?: ReactNode; }, pseudoState: PseudoState) => ReactNode)
Fully control how input, prefix and suffix are rendererd
renderPlaceholder(() => ReactNode)To render a custom placeholder view when the field is empty
requiredbooleanAdds required attribute to the input and a red asterisk on the right side of label, `false` by default Determines whether the input should have `aria-required` attribute
stepstringnumber
suffixReactNodeContent section rendered on the right side of the input
suffixPointerEvents"none""auto""box-none""box-only"Sets pointer-events styles on the `suffix` element, `none` by default
sxPositionPropsPositioning properties
titlestringHtml input title
typeHTMLInputTypeAttributeHTML input type
valuestringControl the input.

Undocumented props

NameTypeDefault ValueDescriptionUsage count
autoCapitalizeany
autoCompleteany
autoCorrectany
clearButtonModeany
inputModeany
onBlurany
onFocusany
onKeyPressany
onSubmitEditingany
refany
returnKeyTypeany
styleany
testIDany
textContentTypeany

Style API​

Use sx for one-off adjustments. It accepts these style keys:

alignSelfanimationaspectRatiobottomcursordisplayendflexflexBasisflexGrowflexShrinkheightinsetinsetInlineinsetInlineEndinsetInlineStartleftmarginmarginBottommarginHorizontalmarginInlinemarginInlineEndmarginInlineStartmarginLeftmarginRightmarginTopmarginVerticalmatrixmaxHeightmaxWidthminHeightminWidthopacityperspectivepositionrightrotaterotateXrotateYrotateZscalescaleXscaleYskewXskewYstarttoptransformtransformOriginwidthxyzIndex

Each can be overridden per breakpoint with:

$lg$ltLg$ltMd$ltSm$ltVs$ltXl$ltXxl$md$sm$vs$xl$xs$xxl
Edit this page
Previous
TextArea
Next
Anchors
  • Design
    • Example
    • Use cases
      • Alternatives
    • States
      • Default
      • Filled
      • Error
      • Disabled
      • Read-only
      • Required
    • Responsive behavior
  • Technical
    • Playground
    • Do's and don'ts
    • Accessibility
    • Usage examples
      • Uncontrolled
      • Controlled
      • Prefix and suffix
      • Usage in <Form>

Where you live today