# 文本域

## 使用方法

`TextArea` 用于输入多行纯文本，适合个人简介、反馈意见、评论等需要换行的内容。标签、说明与校验消息由内置的 `Field` 自动关联，通过 `v-model` 管理内容即可：

```vue
<script setup lang="ts">
import { TextArea } from '@astryx-vue/core/TextArea'
import { shallowRef } from 'vue'

const bio = shallowRef('')
</script>

<template>
  <TextArea v-model="bio" label="Bio" :rows="4" />
</template>
```

主题在应用入口引入一次：

```ts
import '@astryx-vue/themes/neutral.css'
```

### 结构

- **标签**: 标识这个多行字段的文字。
- **说明**: 标签和输入之间的辅助文字。
- **输入容器**: 装着文本域和盖在上面的那些部分的着色边界。
- **文本域**: 显示并编辑值的多行控件。
- **占位**: 空文本域里的提示文字。
- **起始图标**: startIcon 是语义名或图标组件时，在开头画出的 Astryx Icon。
- **自定义起始内容**: 使用方提供的插槽内容，画在开头，代替 Astryx Icon。
- **转圈**: 输入容器末尾的加载指示。
- **状态图标**: 输入框里的错误、警告或成功图标。
- **字数**: 输入容器里显示的当前字数和上限。
- **字段状态消息**: 跟字段关联的附着或分离的错误、警告或成功消息。
- **提示状态消息**: 提示变体里，用来呈现状态消息的 Tooltip 表面。

## 最佳实践

| 指引 | 实践 |
| --- | --- |
| 推荐 | 提供可见的标签，让用户知道该输入什么。若必须隐藏标签，设置 isLabelHidden，并为屏幕阅读器提供描述性标签。 |
| 推荐 | 设有字数上限时，设置 maxLength 并显示字数统计。这能帮助用户在提交前控制字数。 |
| 推荐 | 使用 status 属性显示行内校验反馈：输入有效时用 success，软限制用 warning，硬错误用 error。 |
| 推荐 | 添加说明或占位文字，说明预期内容，例如 “Describe the issue in detail”。避免只用占位文字作为唯一标签。 |
| 避免 | 用文本域输入名称或邮箱等单行短内容。应使用文本输入框。 |
| 避免 | 只依赖占位文字说明字段用途。占位文字在获得焦点后会消失，且不是可访问的标签。 |
| 避免 | 在未设置状态类型时显示状态消息。边框颜色和图标正是吸引用户注意消息的元素。 |
| 避免 | 用 Tooltip 包裹禁用的文本域来解释禁用原因。禁用的控件会吞掉包裹层需要的悬停事件。应改用 disabledMessage 属性。 |

## 示例

### 字数

一条状态更新，最多 280 个字符，并实时显示字数。

```vue
<script setup lang="ts">
import { TextArea } from '@astryx-vue/core/TextArea'
import { ref } from 'vue'

const value = ref('Excited to announce that our team just shipped the new dashboard! Check it out and let us know what you think.')
</script>

<template>
  <div style="width: 400px; max-width: 100%">
    <TextArea
      v-model="value"
      label="Status update"
      placeholder="What's on your mind?"
      :maxlength="280"
      :rows="3"
    />
  </div>
</template>
```

### 状态

必填、禁用和加载中的文本域。

```vue
<script setup lang="ts">
import { VStack } from '@astryx-vue/core/Stack'
import { TextArea } from '@astryx-vue/core/TextArea'
import { ref } from 'vue'

const requiredValue = ref('')
const disabledValue = ref('This field is disabled and cannot be edited.')
const loadingValue = ref('')
</script>

<template>
  <VStack :gap="4" width="100%" style="max-width: 400px">
    <TextArea v-model="requiredValue" label="Required field" placeholder="Describe the issue..." is-required />
    <TextArea v-model="disabledValue" label="Disabled field" is-disabled />
    <TextArea v-model="loadingValue" label="Loading field" placeholder="Generating summary..." is-loading />
  </VStack>
</template>
```

### 校验

错误、警告和成功说明，以及一条没有说明的错误。

```vue
<script setup lang="ts">
import { VStack } from '@astryx-vue/core/Stack'
import { TextArea } from '@astryx-vue/core/TextArea'
import { ref } from 'vue'

const errorValue = ref('Fix the')
const warningValue = ref('Summarize the Q2 results')
const successValue = ref('Redesign the onboarding flow to reduce drop-off by 15% in Q3. Focus on simplifying the account creation step and adding a progress indicator.')
const errorNoMsgValue = ref('Invalid content')
</script>

<template>
  <VStack :gap="4" width="100%" style="max-width: 400px">
    <TextArea
      v-model="errorValue"
      label="Error message"
      :status="{ type: 'error', message: 'Description must be at least 20 characters.' }"
    />
    <TextArea
      v-model="warningValue"
      label="Warning message"
      :status="{ type: 'warning', message: 'Consider adding more detail for clarity.' }"
    />
    <TextArea
      v-model="successValue"
      label="Success message"
      :status="{ type: 'success', message: 'Looks good — clear and actionable.' }"
    />
    <TextArea
      v-model="errorNoMsgValue"
      label="Error without message"
      :status="{ type: 'error' }"
    />
  </VStack>
</template>
```

### 图标

会议记录，带铅笔图标和一段简短说明。

```vue
<script setup lang="ts">
import { TextArea } from '@astryx-vue/core/TextArea'
import { ref } from 'vue'

const value = ref('')
</script>

<template>
  <div style="width: 400px; max-width: 100%">
    <TextArea
      v-model="value"
      label="Meeting notes"
      description="Capture key decisions and action items."
      placeholder="What was discussed?"
    >
      <template #icon>
        <svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="1.5" aria-hidden="true">
          <path stroke-linecap="round" stroke-linejoin="round" d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L10.582 16.07a4.5 4.5 0 0 1-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 0 1 1.13-1.897l8.932-8.931Zm0 0L19.5 7.125" />
        </svg>
      </template>
    </TextArea>
  </div>
</template>
```

## 键盘操作

文本域沿用浏览器原生 `textarea` 的按键行为，组件只在个别按键上追加了处理，因此大部分操作和系统里的其它多行输入框一致：

- 按 Tab 把焦点移入或移出文本域。
- 按 Shift + Enter 在光标处插入一个换行。
- 按 Enter 时组件触发 `enter` 事件，你可以在回调里处理提交等逻辑。
- 按 Escape 关闭已经打开的禁用原因提示或状态提示。

如果「按 Enter 提交」这类行为对你有意义，直接在 `enter` 事件里实现即可。

## 使用建议

文本域不会限制用户输入什么，字数控制和内容校验都需要在你的应用里完成：

- 对字数有明确要求时，把 `minlength` 与 `maxlength` 写进 `description`，让用户在提交前就知道预期长度。
- `isRequired` 只负责显示必填提示并设置对应的 ARIA 状态，不会执行浏览器原生的必填校验。
- 输入内容是否符合业务规则，由应用层负责校验。

## 属性

| 属性 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `v-model` | `string` | — | 必需。文本域的内容，通过 v-model 双向绑定。 |
| `label` | `string` | — | 必需。字段的可见标签。 |
| `id` | `string` | — | 关联 ID 的前缀，省略时自动生成。 |
| `size` | `'sm' \| 'md' \| 'lg'` | md | 文本域的高度尺寸。 |
| `rows` | `number` | — | 原生 textarea 的 rows 属性。 |
| `cols` | `number` | — | 原生 textarea 的 cols 属性。 |
| `minlength` | `number \| string` | — | 原生 textarea 的 minlength 属性。 |
| `maxlength` | `number \| string` | — | 可见字符上限，一个 emoji 算一个。可以继续输入，超出后字数变红，接近上限和超出时读屏会播报。不会写进原生 maxlength。 |
| `wrap` | `'soft' \| 'hard'` | soft | 原生 textarea 的 wrap 属性。 |
| `resize` | `'none' \| 'both' \| 'horizontal' \| 'vertical'` | vertical | 原生 resize 样式，控制可拖拽调整的方向。默认 vertical，只允许垂直拖动。 |
| `autoComplete` | `string` | — | 原生 textarea 的 autocomplete，例如 `off`、`email`。原样透传，不影响受控值。 |
| `hasSpellCheck` | `boolean` | true | 是否启用浏览器拼写检查。 |
| `placeholder` | `string` | — | 输入为空时显示的占位文本，不能替代 label。 |
| `description` | `string` | — | 显示在标签下方的补充说明。 |
| `isLabelHidden` | `boolean` | false | 在视觉上隐藏标签，仍保留无障碍名称。 |
| `isDisabled` | `boolean` | false | 禁用文本域，内容不可修改，值也不会随表单提交。 |
| `disabledMessage` | `string` | — | 说明禁用原因。设置后，被禁用的文本域仍可聚焦，以便用户查看原因。 |
| `isReadOnly` | `boolean` | false | 内容只读、不可修改，但值仍会随表单提交。 |
| `isRequired` | `boolean` | false | 显示必填提示，但不会执行浏览器原生的必填校验。 |
| `isOptional` | `boolean` | false | 显示可选提示。与 isRequired 同时设置时优先。 |
| `requiredLabel` | `string` | Required | 必填提示的显示文字，用于本地化。 |
| `optionalLabel` | `string` | Optional | 可选提示的显示文字，用于本地化。 |
| `width` | `number \| string` | — | 整个字段的宽度。数字按像素处理，CSS 字符串按原样使用。 |
| `htmlName` | `string` | — | 原生 textarea 的 name 属性，优先于通过原生属性传入的 name。 |
| `form` | `string` | — | 原生 form 属性，通过 ID 关联所属表单。 |
| `status` | `FieldStatusInput` | — | 设置字段的错误、警告或成功校验状态，可附带消息。 |
| `statusVariant` | `'attached' \| 'detached' \| 'tooltip'` | detached | 状态消息的展示方式。文本域默认为 detached，消息显示在控件下方。 |
| `changeAction` | `(value: string, event) => void \| Promise<void>` | — | 内容变化后执行的动作。返回 Promise 时，组件会显示加载状态。 |
| `isLoading` | `boolean` | false | 控制加载指示器的显示。 |
| `hasClear` | `boolean` | false | 在有内容且可编辑时显示清空按钮，点击后清空内容。 |
| `hasAutoFocus` | `boolean` | false | 组件挂载后自动聚焦文本域。 |
| `clearLabel` | `string` | — | 清空按钮的读屏名称，默认带字段名。悬停或键盘聚焦时，提示只说「清除」。 |
| `containerClass` | `string \| object \| array` | — | 加到字段外壳的 class。在 InputGroup 和横向标签的 FormLayout 里没有外壳，此时不生效，开发环境会警告。 |
| `containerStyle` | `StyleValue` | — | 加到字段外壳的行内样式。 |
| `controlClass` | `string \| object \| array` | — | 加到文本域本体的 class。直接写在组件上的 class 也落在本体，不会警告。 |
| `controlStyle` | `StyleValue` | — | 加到文本域本体的行内样式。直接写在组件上的 style 也落在本体。 |
| `loadingLabel` | `string` | Loading | 加载指示器的无障碍标签。 |
| `labelIcon` | `Component` | — | 显示在标签前的装饰图标。 |
| `labelTooltip` | `string` | — | 在标签旁显示信息提示，悬停、键盘聚焦或点击都可以查看。 |

## 事件

- `update:modelValue(value)`：更新文本内容。
- `change(value, event)`：内容变化时触发。
- `clear(event)`：点击清空按钮时触发。
- `enter(event)`：按 Enter 时触发。Shift + Enter 不触发。
- `keydown(event)`：按键时触发。
- `actionError(error, value, event)`：changeAction 抛出异常时触发。

内容变化时，`update:modelValue` 与 `change` 在 `changeAction` 之前触发。
取消 `change` 事件会阻止 `changeAction` 执行。

## 原生属性

- `class`、`style` 和其他属性应用于字段外层。
- `aria-labelledby` 与 `aria-describedby` 合并到原生 textarea。
- `focus`、`blur`、`paste` 监听器落在原生 textarea 上。
- 组件管理 textarea 的 `disabled`、`readonly`、`spellcheck` 与 `aria-invalid`。
- `icon` 插槽在文本域左侧显示装饰图标。
- `end` 插槽在文本域右侧显示附加内容。
- `labelIcon` 插槽替换标签前的图标。

## 主题

```css
.my-text-area {
  --astryx-text-area-min-height-md: 120px;
}
```

- `--astryx-text-area-min-height-sm`：sm 尺寸最小高度。
- `--astryx-text-area-min-height-md`：md 尺寸最小高度。
- `--astryx-text-area-min-height-lg`：lg 尺寸最小高度。

其他颜色与边框变量与 TextInput 共享。Neutral 自动适配明暗模式。
