# Text field API

> Explore the API of Cimpress UI's text field component.

Source: https://ui.cimpress.io/components/text-field/api/

## Import

```tsx
import { TextField } from '@cimpress-ui/react';
```

## Controlled/uncontrolled usage

```tsx
import { TextField, Stack } from '@cimpress-ui/react';
import { useState } from 'react';

export default function Demo() {
  const [name, setName] = useState('Jane Doe');

  return (
    <Stack gap={32}>
      <TextField label="Name (controlled)" value={name} onChange={setName} />
      <TextField label="Name (uncontrolled)" defaultValue="Jane Doe" />
    </Stack>
  );
}
```

## Imperative API

```tsx
import { Button, Stack, TextField, type TextFieldApi } from '@cimpress-ui/react';
import { useRef } from 'react';

export default function Demo() {
  const apiRef = useRef<TextFieldApi>(null);

  return (
    <Stack gap={16}>
      <TextField label="Name" defaultValue="Jane Doe" apiRef={apiRef} />

      <Stack gap={16} direction={{ md: 'horizontal' }}>
        <Button onPress={() => apiRef.current?.focus()}>Focus</Button>
        <Button onPress={() => apiRef.current?.select()}>Select contents</Button>
      </Stack>
    </Stack>
  );
}
```

## Accessibility notes

`TextField` requires a textual label to remain accessible to assistive technologies. See our [accessibility guide](/dev-guides/accessibility#labelling) for more details.

## Forms

Text field can integrate with native HTML forms. See our [forms guide](/dev-guides/forms) to learn how to work with forms.

## Affixes

`TextField` supports affixes displayed as part of the input. Affixes can be used for decorative icons or short pieces of text.

Note that affixes are hidden from assistive technologies. It's up to you to make sure that the input's purpose is adequately conveyed to screen readers. Note the different values of `label` and `aria-label` in the example below:

```tsx
import { Stack, TextField } from '@cimpress-ui/react';
import { IconEmail } from '@cimpress-ui/react/icons';

export default function Demo() {
  return (
    <Stack gap={16}>
      <TextField label="Phone number" aria-label="A Dutch phone number, without a country code" prefix="+31" />
      <TextField
        label="Email"
        aria-label="Local part of an email in the cimpress.com domain"
        prefix={<IconEmail />}
        suffix="@cimpress.com"
      />
    </Stack>
  );
}
```

## API reference

### TextField

Allows users to enter a single line of text with a keyboard.

See [text field usage guidelines](https://ui.cimpress.io/components/text-field/).

- `ref`: `Ref<HTMLDivElement>`

  The `ref` type for this component.

**Props: `TextFieldProps`**

- `isClearable`: `boolean`

  Whether to render a button that can be used to clear the text field.

  Defaults to `false`.

- `id`: `string`

  The element's unique identifier. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id).

- `data-cim-style-root`: `boolean`

  Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.

- `UNSAFE_className`: `string`

  Sets the CSS className for the element. Only use as a **last resort**. Use style props instead.

  See [styling guide](https://ui.cimpress.io/dev-guides/styling/).

- `UNSAFE_style`: `CSSProperties`

  Sets the CSS style for the element. Only use as a **last resort**. Use style props instead.

  See [styling guide](https://ui.cimpress.io/dev-guides/styling/).

- `label`: `string`

  The content to display as the label.

- `aria-label`: `string`

  Defines a string value that labels the current element.

- `aria-labelledby`: `string`

  Identifies the element (or elements) that labels the current element.

- `aria-describedby`: `string`

  Identifies the element (or elements) that describes the object.

- `aria-details`: `string`

  Identifies the element (or elements) that provide a detailed, extended description for the object.

- `form`: `string`

  The `<form>` element to associate the input with.
  The value of this attribute must be the id of a `<form>` in the same document.
  See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#form).

- `name`: `string`

  The name of the input element, used when submitting an HTML form. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#name).

- `description`: `string`

  A description for the field. Provides a hint such as specific requirements for what to choose.

- `error`: `FieldError`

  An error message for the field.

- `validate`: `(value: string) => string | true | string[] | undefined`

  A function that returns an error message (or `true`) if a given value is invalid.
  Validation errors are displayed to the user when the form is submitted.
  For real-time validation, use the `error` prop instead.

- `placeholder`: `string`

  The placeholder text displayed in the input field.

- `prefix`: `ReactNode`

  The content displayed at the start of the component. Can be either an icon or a piece of text.

- `suffix`: `ReactNode`

  The content displayed at the end of the component. Can be either an icon or a piece of text.

- `apiRef`: `RefObject<TextFieldApi | null>`

  A React ref that allows access to the imperative API of this component.

- `autoComplete`: `string`

  Describes the type of autocomplete functionality the input should provide if any. See
  [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefautocomplete).

- `maxLength`: `number`

  The maximum number of characters supported by the input. See
  [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefmaxlength).

- `minLength`: `number`

  The minimum number of characters required by the input. See
  [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefminlength).

- `pattern`: `string`

  Regex pattern that the value of the input must match to be valid. See
  [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefpattern).

- `type`: `'search' | (string & ({  })) | 'text' | 'tel' | 'url' | 'email' | 'password'`

  The type of input to render. See
  [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdeftype).

  Defaults to `'text'`.

- `inputMode`: `'search' | 'none' | 'text' | 'tel' | 'url' | 'email' | 'numeric' | 'decimal'`

  Hints at the type of data that might be entered by the user while editing the element or its
  contents. See
  [MDN](https://html.spec.whatwg.org/multipage/interaction.html#input-modalities:-the-inputmode-attribute).

- `onKeyDown`: `(e: KeyboardEvent) => void`

  Handler that is called when a key is pressed.

- `onFocus`: `(e: FocusEvent<HTMLInputElement>) => void`

  Handler that is called when the element receives focus.

- `onBlur`: `(e: FocusEvent<HTMLInputElement>) => void`

  Handler that is called when the element loses focus.

- `autoFocus`: `boolean`

  Whether the element should receive focus on render.

- `isRequired`: `boolean`

  Whether user input is required on the input before form submission.

- `isDisabled`: `boolean`

  Whether the input is disabled.

- `isReadOnly`: `boolean`

  Whether the input can be selected but not changed by the user.

- `value`: `string`

  The current value (controlled).

- `defaultValue`: `string`

  The default value (uncontrolled).

- `onChange`: `(value: string) => void`

  Handler that is called when the value changes.

- `isInvalid`: `boolean`

  Whether the value is invalid.

**Props: `StyleProps`**

- `margin`: `Responsive<Spacing | "auto">`

  The amount of margin applied to all edges of this component.

- `marginX`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the left and right edges of this component. Takes priority over `margin`.

- `marginY`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the top and bottom edges of this component. Takes priority over `margin`.

- `marginTop`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the top edge of this component. Takes priority over `marginY` and `margin`.

- `marginRight`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the right edge of this component. Takes priority over `marginX` and `margin`.

- `marginBottom`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the bottom edge of this component. Takes priority over `marginY` and `margin`.

- `marginLeft`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the left edge of this component. Takes priority over `marginX` and `margin`.
