---
title: "Textarea"
description: "A multi-line text input that binds to FormField context."
url: https://weme-ui.github.io/slim/components/textarea.md
---
# Textarea

多行文本输入控件。可以单独用，也可以放进 `FormField`。有 `FormField` 时会合并 `id` / `aria-*` / `data-*`。支持自动增高（`autoSize`）、字数统计、加载态与 `v-model` modifiers。

## Installation

```bash
pnpm dlx @weme-ui/weme-ui add textarea
```

## Usage

```vue
<script lang="ts" setup>
import * as v from 'valibot'
import Button from '../../button/button.vue'
import FormField from '../../form-field/form-field.vue'
import Form from '../../form/form.vue'
import Textarea from '../textarea.vue'

const schema = v.object({
  bio: v.pipe(v.string('简介不能为空'), v.minLength(10, '至少写 10 个字')),
})

const defaultValues = {
  bio: '',
}

function onFormSubmit(values: { bio: string }) {
  // eslint-disable-next-line no-alert
  alert(JSON.stringify(values))
}
</script>

<template>
  <Form
    class="max-w-sm w-full"
    :default-values="defaultValues"
    :schema="schema"
    @form-submit="onFormSubmit"
  >
    <FormField v-slot="{ field }" name="bio" label="Bio" required>
      <Textarea
        :model-value="field.state.value"
        :max-length="100"
        placeholder="Tell us about yourself…"
        countable
        @blur="field.api.handleBlur"
        @update:model-value="field.api.handleChange"
      />
    </FormField>
    <Button type="submit"> Submit </Button>
  </Form>
</template>
```

## Props

| 属性             | 说明                                                                           | 类型                          | 默认值             |
| ---------------- | ------------------------------------------------------------------------------ | ----------------------------- | ------------------ |
| `as`             | 根节点渲染为的元素或组件（Reka UI `Primitive`）                                | `any`                         | `'div'`            |
| `modelValue`     | 受控值；可用 `v-model`                                                         | `string \| null \| undefined` | —                  |
| `defaultValue`   | 非受控初始值                                                                   | `string \| null \| undefined` | —                  |
| `variant`        | 外观变体                                                                       | `'soft' \| 'outline'`         | `'soft'`           |
| `size`           | 尺寸                                                                           | `'sm' \| 'md' \| 'lg'`        | `'md'`             |
| `placeholder`    | 占位文案                                                                       | `string`                      | —                  |
| `rows`           | 最小行数（自动增高的起始行数）                                                 | `number`                      | `3`                |
| `maxRows`        | 自动增高时的最大行数；未传则不限制                                             | `number`                      | —                  |
| `maxLength`      | 最大长度（`countable` 展示用）；超出时计数器使用 error 色                      | `number`                      | —                  |
| `autoSize`       | 是否按内容自动调整行数                                                         | `boolean`                     | —                  |
| `countable`      | 配合 `maxLength` 显示字数统计                                                  | `boolean`                     | —                  |
| `loadingIcon`    | 加载中图标（Iconify）；`loading` 为真时显示                                    | `IconifyIconProps['icon']`    | `'ri:loader-line'` |
| `unstyled`       | 移除样式变体类，仅保留结构                                                     | `boolean`                     | —                  |
| `id`             | 控件 `id`；未传时回退到 FormField Context                                      | `string`                      | —                  |
| `name`           | 控件 `name`；未传时回退到 FormField Context                                    | `string`                      | —                  |
| `loading`        | 是否加载中；会与 FormField Context 的 `loading` 合并                           | `boolean`                     | —                  |
| `required`       | 是否必填；会与 FormField Context 的 `required` 合并                            | `boolean`                     | —                  |
| `disabled`       | 是否禁用；会与 FormField Context 的 `disabled` 合并                            | `boolean`                     | —                  |
| `invalid`        | 是否无效；会与 FormField Context 的 `invalid` 合并                             | `boolean`                     | —                  |
| `modelModifiers` | `v-model` modifiers（`lazy` / `trim` / `nullable` / `optional`）               | `TextareaModelModifiers`      | —                  |
| `class`          | 根节点额外 class                                                               | `any`                         | —                  |
| `ui`             | 覆盖 slots 样式（`root` / `textarea` / `innerWrapper` / `counter` / `loader`） | `Partial<TextareaStyleSlots>` | —                  |

## Events

| 事件                | 说明                                                                                        | 参数                                 |
| ------------------- | ------------------------------------------------------------------------------------------- | ------------------------------------ |
| `update:modelValue` | 值变更时触发（含 `v-model`）；受 `lazy` / `trim` / `nullable` / `optional` modifiers 影响   | `value: string \| null \| undefined` |
| `change`            | 原生 `change` 时触发；参数为当前 textarea 字符串（非原生 `Event`）。`lazy` 时同时写入 model | `value: string \| null \| undefined` |
| `focus`             | 获得焦点                                                                                    | `event: FocusEvent`                  |
| `blur`              | 失去焦点                                                                                    | `event: FocusEvent`                  |

## Slots

| 插槽      | 说明                                                                                |
| --------- | ----------------------------------------------------------------------------------- |
| `counter` | 自定义字数统计；作用域参数 `{ count }`；未传时渲染 `count / maxLength`              |
| `loader`  | 自定义加载指示；作用域参数 `{ loading }`；未传且 `loading` 为真时渲染 `loadingIcon` |

## Accessibility

原生 `<textarea>` 控件。通过 `useFormFieldControl` 合并 FormField Context 的 `id`、`name`、`disabled`、`required`、`aria-labelledby`、`aria-describedby`、`aria-invalid` 与字段 `data-*`，并向 FormField 注册控件。根节点为 Reka UI `Primitive`，并设置 `data-slot="textarea"`。

## Examples

### Variants

```vue
<script lang="ts" setup>
import Textarea from '../textarea.vue'
</script>

<template>
  <div class="flex flex-col gap-3 max-w-sm w-full">
    <Textarea variant="soft" placeholder="Soft" />

    <Textarea variant="outline" placeholder="Outline" />
  </div>
</template>
```

### Sizes

```vue
<script lang="ts" setup>
import Textarea from '../textarea.vue'
</script>

<template>
  <div class="flex flex-col gap-3 max-w-sm w-full">
    <Textarea size="sm" placeholder="Small" />

    <Textarea size="md" placeholder="Medium" />

    <Textarea size="lg" placeholder="Large" />
  </div>
</template>
```

### Auto Size

```vue
<script lang="ts" setup>
import Textarea from '../textarea.vue'
</script>

<template>
  <div class="flex flex-col gap-3 max-w-sm w-full">
    <Textarea :auto-size="true" :rows="2" :max-rows="6" placeholder="Grows with content…" />

    <Textarea :auto-size="false" :rows="4" placeholder="Fixed height…" />
  </div>
</template>
```

### Loading

```vue
<script lang="ts" setup>
import Textarea from '../textarea.vue'
</script>

<template>
  <div class="max-w-sm w-full">
    <Textarea loading placeholder="Loading…" />
  </div>
</template>
```

### Disabled

```vue
<script lang="ts" setup>
import Textarea from '../textarea.vue'
</script>

<template>
  <div class="max-w-sm w-full">
    <Textarea disabled placeholder="Disabled" />
  </div>
</template>
```

### Invalid

```vue
<script lang="ts" setup>
import Textarea from '../textarea.vue'
</script>

<template>
  <div class="flex flex-col gap-3 max-w-sm w-full">
    <Textarea placeholder="Default" />
    <Textarea invalid placeholder="Invalid" />
  </div>
</template>
```

### Unstyled

```vue
<script lang="ts" setup>
import Textarea from '../textarea.vue'
</script>

<template>
  <div class="max-w-sm w-full">
    <Textarea
      unstyled
      placeholder="Fully custom…"
      class="text-sm py-1 outline-none border-b b-default w-full"
    />
  </div>
</template>
```