---
title: TextInput
---

import LibraryInfo from '@theme/LibraryInfo'
import { ExternalCodeBlock, Feedback, TokensTable, PropsTable } from '../../src/components'
import { TextInput } from '@telus-uds/components-base'
import { TextInput as BaseDocs } from 'docs/@telus-uds/components-base'
import AvailableOnBoth from '../../partials/_platform-considerations-both.mdx'
import AdditionalVariants from '../../src/components/Variants/TextInput'

# TextInput

<LibraryInfo />

```jsx live
<TextInput
  label="Label"
  hint="A usually much longer hint below"
  hintPosition="below"
  tooltip="Tooltip content"
/>
```

## Introduction

A basic text input component. Use in forms or individually to receive user's input.

[Follow the appropriate instructions](importing-components) to add this component in to your app.

## Guidance

### Controlled component

If it is required that the state of the `TextInput` be controlled by the application or other external methods,
`value` and `onChange` props must be passed to the `TextInput`.

```jsx live
function ControlledInput() {
  const [value, setValue] = React.useState('')
  const [validation, feedback] = React.useMemo(() => {
    if (value && value.length > 6) {
      return ['error', 'The value must be shorter than 6 characters']
    } else if (value && value.length > 0) {
      return ['success', 'Yay! The value has less than 6 characters']
    }
    return [
      undefined,
      'This example shows an error if longer than 6 characters and success otherwise'
    ]
  }, [value])

  return (
    <TextInput
      label="Controlled"
      validation={validation}
      feedback={feedback}
      value={value}
      onChange={setValue}
    />
  )
}
```

If the `TextInput` value should not be changed by user input, a `readOnly` prop must be provided.

```jsx live
<TextInput label="Read only" readOnly value="won't change" />
```

### Uncontrolled component

If it is not necessary to control the `TextInput` state, you can create one without a value prop.
In this case its value can be accessed within the `onChange` callback.

### Getting feedback for entered values

Use the `feedback` prop to give the user feedback regarding their input (keep it as brief as possible).
You can affirm that the user's input was correct, or highlight errors that must be corrected by setting the
`validation` prop.

```jsx live
<TextInput label="Username" initialValue="guest12345" validation="success" />
```

```jsx live
<TextInput
  label="Email"
  initialValue="guest@telus.com"
  validation="error"
  feedback="That email is already associated with another account."
/>
```

This component does not perform input validations, as that is an application level concern. You will need to track the
value of your form fields and perform any required data validations either client side or server side, depending on
the context.

### Supplying a hint

Use a `hint` to succinctly clarify attributes of the expected input data, such as the expected format, or an indicator
that the field is optional. It is a more usable and accessible option than the HTML `placeholder` attribute.

```jsx live
<TextInput label="Transit number" hint="5 digits" />
```

- `hint` can be used beside the label (inline) or below
  - Label and inline hint text should not exceed 40 characters total, otherwise, use hint text below the label
- The `hint` should **not** be longer than 85 characters
- The `hint` should **not** wrap more than 3 lines on all viewport sizes
- Developers can use `hintPosition` to re-position the `hint` below the `label` if the text supplied to the inline
  `hint` wraps

```jsx live
<TextInput
  label="Quick view"
  hintPosition="below"
  hint="Enter the account number in the box below to go to the overview page for that account."
/>
```

### Using a tooltip

When a more in-depth explanation is needed to fill out a `TextInput` properly, additional instructions may be provided
to the `tooltip` prop.

```jsx live
<TextInput
  label="Customer ID"
  tooltip="You can find your customer ID in the account confirmation email."
/>
```

### Using maxCharacterAllowed

`maxCharacterAllowed` defines the maximum number of characters allowed in the `TextInput` field.
If the user exceeds this limit, an error message will be displayed using the feedback component. This ensures that users
receive clear guidance on the character limit, promoting a more intuitive form interaction.

```jsx live
function maxCharacterAllowedAndHintAndTooltip() {
  const [value, setValue] = React.useState('')

  return (
    <TextInput
      label="Label"
      hint="A usually much longer hint below"
      hintPosition="below"
      tooltip="Tooltip content"
      maxCharacterAllowed={10}
      value={value}
      onChange={(filteredText) => setValue(filteredText)}
    />
  )
}
```

:::info
In order to support more advanced usages all additional props will be passed down to the React Native's
`TextInput` component. See <a href="https://reactnative.dev/docs/textinput" target="_blank">React Native documentation</a> for
supported props and <a href="https://reactnative.dev/docs/textinput" target="_blank">React Native Web documentation</a> for
their implementation on the web.
:::

### Using a clear button

`TextInput` component has a built-in way to display a clear button showing up once the input value changes. Pressing that button
resets the input value to the initial state. The button gets rendered automatically whenever there is an `onClear` callback
provided. This callback will be called after the button gets pressed:

```jsx live
<TextInput
  label="Label"
  hint="A usually much longer hint below"
  hintPosition="below"
  tooltip="Tooltip content"
  onClear={() => console.log('Back to square one')}
/>
```

Note that while this feature is available in both uncontrolled and controlled mode, in the latter case it's the wrapper
component holding the state that will be responsible for listening to `onClear` and resetting the value whenever that
callback is called.

### Using with Card type

```jsx live
function ControlledInput() {
  const [value, setValue] = useState('')
  return (
    <>
      <TextInput
        label="Credit Card"
        type="card"
        value={value || ''}
        onChange={(filteredText) => setValue(filteredText)}
      />
    </>
  )
}
```

### Appearance

`TextInput` has no visual variants, but its appearance may change based on the `validation` and `inactive` props.

```jsx live
<TextInput label="Last name" validation="success" initialValue="Potter" />
```

```jsx live
<TextInput label="Last name" validation="error" />
```

```jsx live
<TextInput label="Inactive" inactive initialValue="can't touch this" />
```

### Accessibility

`TextInput` is accessible to screen readers on both native platforms and Web. It will be announced with the `label` prop
if it is provided, and optionally described with the `hint`, `tooltip` and `feedback`.

:::info
Due to React Native's implementation of `TextInput` it's not possible to access the current value of the input by
passing a ref. You can only access it within the `onChange` callback.
:::

### Platform considerations

<AvailableOnBoth />

:::info
Due to React Native's implementation of `TextInput` it's not possible to access the current value of the input by
passing a ref. You can only access it within the `onChange` callback.
:::

## Props

<PropsTable base={BaseDocs} />

## Tokens

<TokensTable base={BaseDocs} component="TextInput" />

## Variants

`TextInput` has no visual variants, but its appearance may change based on the `password` `validation`, `numeric` and `inactive` props.

```jsx live
<TextInput label="Last name" validation="success" initialValue="Potter" />
```

```jsx live
<TextInput label="Last name" validation="error" />
```

```jsx live
<TextInput label="Inactive" inactive initialValue="can't touch this" />
```

:::warning

The `password` and `numeric` variants have been deprecated. Please use `type='password'` and `type='number'` props instead.

:::

```jsx live
<TextInput label="Password" variant={{ password: true }} initialValue="Password" />
```

```jsx live
<TextInput label="Numeric" variant={{ numeric: true }} initialValue="12345" />
```

## Additional Variants

Please note that if you are building an application that is brand specific you can use these variants.

These brand specific variants are defined in the community themes and you'd need to install those relevant packages and use `mergeThemes` from `@telus-uds/system-constants` to merge the brand specific theme with the core theme.

<AdditionalVariants />

## Feedback

<Feedback component="TextInput" />
