# TextField and TextArea

> Single and multi-line text inputs from @salmonui/inputs/text-field, on native input and textarea elements with labels, validation, adornments and a clear button.

Canonical: https://salmonui.dev/docs/inputs/text-field

`TextField` is a single-line text input and `TextArea` its multi-line
counterpart. Both render a real `<label>`, a native
`<input>` or `<textarea>` inside the shared input group, and the field's
description and error (see [Field](https://salmonui.dev/docs/inputs/field)).

```tsx
import { TextArea, TextField } from '@salmonui/inputs/text-field';
import '@salmonui/inputs/text-field/styles.css';

<TextField name="email" label="Email" type="email" clearable validation={{ required: true, email: true }} />
<TextArea name="notes" label="Notes" rows={3} autoResize resize="vertical" />
```

### Live demo: TextField basics

Live demo: https://salmonui.dev/demos/inputs/text-field-basics

A controlled clearable search field, start and end adornments, the three sizes and an auto-growing TextArea.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { TextArea, TextField } from '@salmonui/inputs/text-field';

// A controlled clearable search field, adornments, sizes and an auto-growing textarea.
export default function TextFieldBasicsDemo() {
  const [query, setQuery] = useState('salmon');
  return (
    <div style={{ display: 'grid', gap: 16, maxWidth: 360 }}>
      <TextField label="Search" type="search" clearable value={query} onValueChange={setQuery} description="Escape clears the field" />
      <output>Value: {JSON.stringify(query)}</output>
      <TextField label="Price" startAdornment="$" endAdornment="USD" inputMode="decimal" />
      <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, minmax(0, 1fr))', gap: 8, alignItems: 'end' }}>
        <TextField label="Small" size="sm" placeholder="sm" style={{ minInlineSize: 0 }} />
        <TextField label="Medium" placeholder="md" style={{ minInlineSize: 0 }} />
        <TextField label="Large" size="lg" placeholder="lg" style={{ minInlineSize: 0 }} />
      </div>
      <TextArea label="Notes" rows={2} autoResize resize="vertical" placeholder="Grows while you type" />
    </div>
  );
}
```

## Value

Pass `value` with `onValueChange` for a controlled field, or `defaultValue`
(or nothing) for an uncontrolled one that a form reset restores.
`value` / `defaultValue` / `onValueChange(value, details)` with
`details.reason` `'input'`, `'clear'` or `'reset'` and `details.source`.
Native `onChange` still fires on every edit. A controlled
value never moves the caret: React writes only the changed value.

## Native attributes

Every prop the component does not own goes to the native element: `type` (`'text' | 'password' | 'email' | 'tel' | 'url' |
'search'`; numbers use `NumberField`), `autoComplete` with the full token set,
`maxLength`, `rows`, `cols`, `inputMode`, `enterKeyHint`, `name`, `form`,
`required`, `pattern`, `spellCheck`, `aria-*` and `data-*`. `ref` is the
native element, so `ref.current.focus()`, `select()` and `setSelectionRange()`
are the native methods.

### Live demo: Native text attributes

Live demo: https://salmonui.dev/demos/inputs/text-field-native

TextField and TextArea pass native attributes through - autoComplete tokens, maxLength with a live counter, and a multi-line TextArea sized with rows and cols.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { TextArea, TextField } from '@salmonui/inputs/text-field';

const TAGLINE_MAX = 40;
const BIO_MAX = 160;

// Native input attributes pass straight through: autoComplete tokens,
// maxLength, and TextArea (the multiline TextBox) with rows, cols and maxLength.
export default function TextFieldNativeDemo() {
  const [tagline, setTagline] = useState('');
  const [bio, setBio] = useState('');
  return (
    <form style={{ display: 'grid', gap: 16, maxInlineSize: 420 }} onSubmit={(event) => event.preventDefault()} aria-label="Profile">
      <TextField label="Email" type="email" name="email" autoComplete="email" description='autoComplete="email": the browser offers saved addresses' />
      <TextField label="New password" type="password" name="new-password" autoComplete="new-password" description='autoComplete="new-password": password managers suggest a strong one' />
      <TextField label="Search history" type="search" autoComplete="off" description='autoComplete="off": no suggestions' />
      <TextField
        label="Tagline"
        maxLength={TAGLINE_MAX}
        value={tagline}
        onValueChange={setTagline}
        description={`${tagline.length} / ${TAGLINE_MAX} characters (native maxLength stops typing at the limit)`}
      />
      <TextArea
        label="Bio"
        rows={4}
        cols={40}
        maxLength={BIO_MAX}
        resize="none"
        style={{ justifySelf: 'start', maxInlineSize: '100%' }}
        value={bio}
        onValueChange={setBio}
        description={`TextArea: rows={4} cols={40}, ${bio.length} / ${BIO_MAX} characters`}
      />
    </form>
  );
}
```

## TextArea

- `resize`: `'vertical' | 'horizontal' | 'both' | 'none'` (default
  `'both'`).
- `autoResize` grows the textarea with its content. Browsers with
  `field-sizing: content` size it in CSS; elsewhere the component reads
  `scrollHeight` once per value change and writes the height once.
- Adornments lay themselves out; there are no flow or orientation options
  (see [Design notes](https://salmonui.dev/docs/inputs/text-field#design-notes)).

## Keyboard

| Key | Action |
| --- | --- |
| Typing, arrows, Home / End | Native text editing |
| Escape | Clears a `clearable` field with a value |
| Tab | Moves to the next field (the clear button is not a tab stop) |

Screen readers hear a native textbox named by the label, described by the
description and, while shown, the error.

## SSR

Server and client render the same markup: ids come from `useId`, and
`autoResize` measures only after mount (browsers with `field-sizing:
content` need no measuring at all). The field works before hydration as a
plain labelled input that submits with its form.

## Design notes

- there are no adornment flow or orientation options. Adornments
  are `ReactNode`s that lay themselves out, so two enums for their flow
  would add surface without capability; wrap stacked adornments in your own
  flex container.

Shared field behavior (static label, no `width` prop, no persistence, ...)
is listed on [Field](https://salmonui.dev/docs/inputs/field#design-notes).

## API

| Prop | Type | Default |
| --- | --- | --- |
| `value`, `defaultValue` | `string` | `''` |
| `onValueChange` | `(value, { source, reason }) => void` | |
| `label`, `description` | `ReactNode` | |
| `errorMessage` | `ReactNode \| ({ errors }) => ReactNode` | first error |
| `invalid` | `boolean` | |
| `validation` | rule object ([Form](https://salmonui.dev/docs/inputs/form)) | |
| `validateOn` | `'blur' \| 'change' \| 'submit'` | Form's, else `'blur'` |
| `clearable` | `boolean` | `false` |
| `startAdornment`, `endAdornment` | `ReactNode` | |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` |
| `localeText` | `{ clear, validation* }` | English |
| `classNames` | `{ group, input, label, description, error }` | |
| `type` (TextField) | text input types | `'text'` |
| `resize`, `autoResize` (TextArea) | see above | `'both'`, `false` |

`ref` is the native `<input>` (TextField) or `<textarea>` (TextArea); every
other native attribute is passed through. Full signatures:
[`@salmonui/inputs/text-field` API reference](https://salmonui.dev/docs/reference/inputs/text-field).
