# Textarea

## Introduction

The Textarea component provides a multi-line text input field for collecting longer-form user input such as comments, descriptions, and notes. It is built on top of Joy UI's Textarea and automatically adjusts its height between `minRows` (default 2) and `maxRows` (default 4) as the user types.

Textarea integrates with the form system by wrapping itself in a FormControl, supporting built-in `label`, `helperText`, and `error` props. It also supports decorators, multiple visual variants, sizes, and color themes to fit a wide range of admin UI use cases.

> **Use built-in form props**
>
> This component natively supports form elements such as `label` and `helperText` props.
> When building forms, use these built-in props instead of manually composing labels and helper text with Typography.

## Playground

Interact with the Textarea component using the controls below.

```tsx
<Textarea name="Textarea" />
```

| Field          | Description | Default    |
| -------------- | ----------- | ---------- |
| name           | —           | "Textarea" |
| variant        | —           | —          |
| color          | —           | —          |
| size           | —           | —          |
| label          | —           | —          |
| startDecorator | —           | —          |
| endDecorator   | —           | —          |
| required       | —           | —          |
| disabled       | —           | —          |
| error          | —           | —          |
| onChange       | —           | —          |

## Usage

```tsx
import { Textarea } from '@ceed/cds';

function MyComponent() {
  return (
    <Textarea
      name="description"
      label="Description"
      placeholder="Enter a description..."
      helperText="Provide a brief summary."
    />
  );
}
```

## Variants

Textarea supports four visual variants: `solid`, `soft`, `outlined`, and `plain`. The default variant is `outlined`.

```tsx
<>
    <Textarea name="Solid" placeholder="Type in here…" variant="solid" />
    <Textarea name="Soft" placeholder="Type in here…" variant="soft" />
    <Textarea name="Outlined" placeholder="Type in here…" variant="outlined" />
    <Textarea name="Plain" placeholder="Type in here…" variant="plain" />
  </>
```

```tsx
<Textarea variant="solid" placeholder="Solid" />
<Textarea variant="soft" placeholder="Soft" />
<Textarea variant="outlined" placeholder="Outlined" />
<Textarea variant="plain" placeholder="Plain" />
```

## Sizes

Textarea comes in three sizes: `sm`, `md`, and `lg`. The default size is `md`.

```tsx
<>
    <Textarea size="sm" name="Size" placeholder="Small" />
    <Textarea size="md" name="Size" placeholder="Medium" />
    <Textarea size="lg" name="Size" placeholder="Large" />
  </>
```

```tsx
<Textarea size="sm" placeholder="Small" />
<Textarea size="md" placeholder="Medium" />
<Textarea size="lg" placeholder="Large" />
```

## Colors

Textarea supports semantic colors via the `color` prop: `primary`, `neutral`, `danger`, and `warning`.

```tsx
<>
    <Textarea name="Primary" placeholder="Type in here…" variant="outlined" color="primary" />
    <Textarea name="Neutral" placeholder="Type in here…" variant="outlined" color="neutral" />
    <Textarea name="Danger" placeholder="Type in here…" variant="outlined" color="danger" />
    <Textarea name="Warning" placeholder="Type in here…" variant="outlined" color="warning" />
  </>
```

```tsx
<Textarea color="primary" placeholder="Primary" />
<Textarea color="neutral" placeholder="Neutral" />
<Textarea color="danger" placeholder="Danger" />
<Textarea color="warning" placeholder="Warning" />
```

## Required with Label and Helper Text

Use the `required`, `label`, and `helperText` props together to build complete form fields. The `required` prop adds an asterisk indicator to the label.

```tsx
<Textarea
  name="Textarea"
  placeholder="Type in here..."
  helperText="I'm helper text"
  label="Label"
  required
/>
```

```tsx
<Textarea label="Label" helperText="I'm helper text" placeholder="Type in here..." required />
```

## Common Use Cases

### Feedback Form

```tsx
import { Textarea, Button, Stack } from '@ceed/cds';

function FeedbackForm() {
  const [value, setValue] = React.useState('');

  return (
    <Stack spacing={2}>
      <Textarea
        name="feedback"
        label="Feedback"
        placeholder="Tell us what you think..."
        helperText="Your feedback helps us improve the service."
        minRows={3}
        maxRows={6}
        value={value}
        onChange={(e) => setValue(e.target.value)}
        required
      />
      <Button disabled={!value.trim()}>Submit</Button>
    </Stack>
  );
}
```

### Error State with Validation

```tsx
import { Textarea } from '@ceed/cds';

function ValidatedTextarea() {
  const [value, setValue] = React.useState('');
  const isError = value.length > 0 && value.length < 10;

  return (
    <Textarea
      name="notes"
      label="Notes"
      placeholder="Write at least 10 characters..."
      helperText={isError ? 'Must be at least 10 characters.' : 'Optional notes for this record.'}
      error={isError}
      value={value}
      onChange={(e) => setValue(e.target.value)}
    />
  );
}
```

### Disabled Read-Only Display

```tsx
import { Textarea } from '@ceed/cds';

function ReadOnlyNotes() {
  return (
    <Textarea
      name="archived-notes"
      label="Archived Notes"
      value="This record has been archived and cannot be edited."
      disabled
    />
  );
}
```

## Best Practices

1. **Use built-in form props instead of manual composition.**

```tsx
// ✅ Recommended
<Textarea label="Description" helperText="Keep it brief." />

// ❌ Avoid
<FormControl>
  <Typography level="title-sm" component="label">Description</Typography>
  <Textarea />
  <Typography level="body-xs" color="neutral">Keep it brief.</Typography>
</FormControl>
```

2. **Set appropriate `minRows` and `maxRows` for the expected content length.**

```tsx
// ✅ Short comment field
<Textarea minRows={2} maxRows={4} placeholder="Add a comment..." />

// ✅ Long description field
<Textarea minRows={4} maxRows={10} placeholder="Write a detailed description..." />
```

3. **Always provide a `name` prop for form submission.**

```tsx
// ✅ Named textarea for form data
<Textarea name="description" label="Description" />

// ❌ Missing name — value won't be included in form data
<Textarea label="Description" />
```

4. **Use `error` and `helperText` together for validation feedback.**

```tsx
// ✅ Clear error feedback
<Textarea
  error={hasError}
  helperText={hasError ? 'This field is required.' : 'Optional description.'}
/>

// ❌ Error state without explanation
<Textarea error />
```

5. **Use a descriptive `placeholder` to guide user input.**

```tsx
// ✅ Descriptive placeholder
<Textarea placeholder="Describe the issue in detail..." />

// ❌ Vague placeholder
<Textarea placeholder="Type here" />
```

## Props and Customization

### Key Props

| Prop           | Type                                                           | Default      | Description                                       |
| -------------- | -------------------------------------------------------------- | ------------ | ------------------------------------------------- |
| `label`        | `string`                                                       | -            | Form label displayed above the textarea           |
| `helperText`   | `ReactNode`                                                    | -            | Helper text displayed below the textarea          |
| `error`        | `boolean`                                                      | `false`      | Applies danger color to indicate validation error |
| `minRows`      | `number`                                                       | `2`          | Minimum number of visible text rows               |
| `maxRows`      | `number`                                                       | `4`          | Maximum number of rows before scrolling           |
| `value`        | `string`                                                       | -            | Textarea value for controlled mode                |
| `defaultValue` | `string`                                                       | -            | Initial value for uncontrolled mode               |
| `onChange`     | `(event: ChangeEvent<HTMLTextAreaElement>) => void`            | -            | Callback when the value changes                   |
| `placeholder`  | `string`                                                       | -            | Placeholder text when the textarea is empty       |
| `disabled`     | `boolean`                                                      | `false`      | Disables the textarea                             |
| `required`     | `boolean`                                                      | `false`      | Marks the field as required                       |
| `size`         | `'sm' \| 'md' \| 'lg'`                                         | `'md'`       | Textarea size                                     |
| `variant`      | `'outlined' \| 'solid' \| 'soft' \| 'plain'`                   | `'outlined'` | Visual style variant                              |
| `color`        | `'primary' \| 'neutral' \| 'danger' \| 'success' \| 'warning'` | `'neutral'`  | Color scheme                                      |
| `sx`           | `SxProps`                                                      | -            | Custom styles using the MUI system                |

> **Note**: Textarea also accepts all Joy UI Textarea props and Framer Motion props.

## Accessibility

- The component automatically associates the `label` with the textarea element through FormControl, ensuring screen readers announce the label when the field receives focus.
- When `required` is set, the field is marked with `aria-required` and a visual asterisk indicator is added to the label.
- When `error` is set, the `helperText` is associated with the textarea via `aria-describedby`, allowing screen readers to announce the error message.
- Use a clear and descriptive `label` for every Textarea in a form. Avoid relying solely on `placeholder` text, as it disappears once the user starts typing and is not reliably announced by all screen readers.
