TextInput
Captures a single line of text: a name, an email, a reference. Inside a `Form`, use `Form.TextInput` to get validation.
compliant
- Design
- Technical
- Props
- Figma
Example
Where you live today
Use cases
- Short free text: a name, an email, a city, a reference number.
- Form fields: inside a
<Form>, throughForm.TextInput, to get Formik validation. - Inline edits: a single value edited in place, such as a nickname.
- 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
- Grows with its content with
autosize - Sets its height with
numberOfLines
- Takes more vertical space
- Enter adds a line instead of submitting
- Shows the formatted value while typing
onChangereceives the raw value, without separators
- Needs a
maskper format - Not for free text
- Hides the value, with a toggle to show it
- Optional strength indicator with
strengthCheck
- Only for secrets
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
Do's and don'ts
- Always give a visible
label. When the label is rendered elsewhere, point to it witharia-labelledby. Usearia-labelonly when there is no visible label at all. - Explain the expected format in
helpText, not inplaceholder. - Set
autoCompleteon fields about the user, such asname,emailorstreet-address. - Pass the error message to
error, so it is announced with the input.
- Use
placeholderas 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
Textnext 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 booleanerroralone only changes the colour. autoCompleteon 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:
autoCompleteon 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:
murray and murray/mobile.TextInput props
| Name | Type | Default Value | Description | Usage count |
|---|---|---|---|---|
| aria-describedby | string | Id of an element outside the input that also describes it, such as an error shared by several inputs | ||
| aria-label | string | Accessibility label, only necessary when not associated with a label | ||
| aria-labelledby | string | |||
| aria-required | boolean | Overrides `aria-required` set by `required`, to mark the input as mandatory without the asterisk | ||
| autoFocus | boolean | If true, focuses the input on componentDidMount.The default value is false. | ||
| cursor | Cursor | Enforce a cursor type | ||
| defaultValue | string | Uncontrolled default value. | ||
| disabled | boolean | Disable input | ||
| enterKeyHint | "search""done""go""next""send" | |||
| error | ReactNode | Contents of `InputError` component, if not set, error is not rendered. | ||
| focused | boolean | Show focus styles even when the input isn't focused, e.g. while a readonly input's picker is open | ||
| groupLabel | ReactNode | Name 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`. | ||
| helpText | ReactNode | Contents of `InputHelp` component, if not set, description is not rendered. | ||
| id | string | |||
| label | ReactNode | |||
| max | stringnumber | |||
| maxLength | number | Maximum string length that the user can enter in UTF-16 code units | ||
| min | stringnumber | |||
| minLength | number | Maximum string length that the user can enter in UTF-16 code units | ||
| name | string | Html input name | ||
| onChange | ((value: string) => void) | Trigger when input value is changing | ||
| onPress | ((event: GestureResponderEvent) => void) | |||
| placeholder | string | |||
| prefix | ReactNode | Content 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 | ||
| readonly | boolean | Make 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 | ||
| required | boolean | Adds 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 | ||
| step | stringnumber | |||
| suffix | ReactNode | Content 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 | ||
| sx | PositionProps | Positioning properties | ||
| title | string | Html input title | ||
| type | HTMLInputTypeAttribute | HTML input type | ||
| value | string | Control the input. |
Undocumented props
| Name | Type | Default Value | Description | Usage count |
|---|---|---|---|---|
| autoCapitalize | any | |||
| autoComplete | any | |||
| autoCorrect | any | |||
| clearButtonMode | any | |||
| inputMode | any | |||
| onBlur | any | |||
| onFocus | any | |||
| onKeyPress | any | |||
| onSubmitEditing | any | |||
| ref | any | |||
| returnKeyType | any | |||
| style | any | |||
| testID | any | |||
| textContentType | any |
Style API
Use sx for one-off adjustments. It accepts these style keys:
Each can be overridden per breakpoint with: