输入多行文本、随输入调整字段大小,并把它连接到表单。

文本域是原生的多行文本框,用来输入消息、笔记和描述。它可以在字段和表单里使用,可以随文字增长,并报告与 Input 相同的变更原因。

<script setup lang="ts">
import { Field, FieldLabel } from "@ui-sdk/vue/field"
import { Textarea } from "@ui-sdk/vue/textarea"
</script>

<template>
  <Field name="message">
    <FieldLabel>Message</FieldLabel>
    <Textarea placeholder="Type your message here…" />
  </Field>
</template>
点击 Message 让文本域获得焦点,然后输入。按 Enter 开始新的一行。

安装

npm install @ui-sdk/vue @ui-sdk/core

用法

Textarea 渲染一个原生 <textarea>。直接传 rows、placeholder、maxlength 和其他原生属性。

<script setup lang="ts">
import { Textarea } from "@ui-sdk/vue/textarea"
</script>

<template>
  <Textarea aria-label="Message" placeholder="Type your message here…" />
</template>

字段

把文本域包进 Field,给它一个可见的标签和描述。文本域没有 name 时,字段也会提供自己的 name。

<script setup lang="ts">
import { Field, FieldDescription, FieldLabel } from "@ui-sdk/vue/field"
import { Textarea } from "@ui-sdk/vue/textarea"
</script>

<template>
  <Field name="feedback">
    <FieldLabel>Feedback</FieldLabel>
    <Textarea placeholder="What could we do better?" />
    <FieldDescription>Only the product team can see this.</FieldDescription>
  </Field>
</template>
点击 Feedback 让文本域获得焦点。屏幕阅读器会连同字段一起读出描述。

自动调整大小

设置 auto-resize,让文本域随文字增长和收缩。CSS min-height 和 max-height 设定它的范围;超过最大值后,文字在它内部滚动。

<script setup lang="ts">
import { Field, FieldDescription, FieldLabel } from "@ui-sdk/vue/field"
import { Textarea } from "@ui-sdk/vue/textarea"
</script>

<template>
  <Field name="notes">
    <FieldLabel>Notes</FieldLabel>
    <Textarea auto-resize placeholder="Add notes for the delivery driver…" />
    <FieldDescription>Your driver sees these notes.</FieldDescription>
  </Field>
</template>
在 Notes 里输入几行。文本域随每一行增长,直到达到最大高度,然后滚动。删掉这些行,它会缩回去。

支持 field-sizing: content 的浏览器会原生地调整它的大小。在其他情况下,组件会在值变化、表单重置或宽度变化时测量它。

受控

绑定 v-model:value,或者传 value 并处理 @value-change。这里的字符计数由那个值计算,原生 maxlength 限制文字长度。

<script setup lang="ts">
import { Field, FieldDescription, FieldLabel } from "@ui-sdk/vue/field"
import { Textarea } from "@ui-sdk/vue/textarea"
import { ref } from "vue"

const bio = ref(
  "Product designer in Lisbon. I write about type and interfaces."
)
</script>

<template>
  <Field name="bio">
    <FieldLabel>Bio</FieldLabel>
    <Textarea v-model:value="bio" :maxlength="160" />
    <FieldDescription>{{ bio.length }} / 160 characters</FieldDescription>
  </Field>
</template>
编辑 Bio,观察它下面的字符计数。文本域在 160 个字符时停止接收文字。

Textarea 与 Input 共用变更原因和取消行为。通用约定见状态与事件。

表单

文本域通过字段的 name 下的原生表单提交它的值。传入 createInputController() 创建的控制器来从外部更改它,这里用来在发送后清空它。原生 required 约束会阻止空消息。

控制台
<script setup lang="ts">
import { Field, FieldLabel } from "@ui-sdk/vue/field"
import { Textarea, createInputController } from "@ui-sdk/vue/textarea"
import { useLog } from "./log"

const log = useLog()
const message = createInputController()

function onSubmit(event: Event) {
  const data = new FormData(event.currentTarget as HTMLFormElement)
  log(`Sent “${data.get("message")}”`)
  message.clear()
}
</script>

<template>
  <form data-slot="stack" @submit.prevent="onSubmit">
    <Field name="message">
      <FieldLabel>Message</FieldLabel>
      <Textarea
        :controller="message"
        required
        placeholder="Write a reply to Hollis Grant…"
      />
    </Field>
    <div data-slot="row">
      <button type="submit" data-variant="solid">Send Message</button>
    </div>
  </form>
</template>
输入一条消息,然后按 Send Message。控制台会记录它,文本域会清空。在为空时按 Send Message 会被阻止。

控制器介绍了创建和共享控制器。

禁用

在文本域或它的字段上设置 disabled,阻止编辑,并把它移出 Tab 顺序和提交的值。

<script setup lang="ts">
import { Field, FieldLabel } from "@ui-sdk/vue/field"
import { Textarea } from "@ui-sdk/vue/textarea"
</script>

<template>
  <Field disabled>
    <FieldLabel>Reply</FieldLabel>
    <Textarea default-value="Replies are closed for this thread." />
  </Field>
</template>
试着编辑 Reply,或用 Tab 到达它。禁用的文本域保留它的值,但无法更改。

无效

对于应用自己判定的错误,在字段上设置 invalid。字段会显示错误,把它与文本域关联起来,并设置 aria-invalid。Field 介绍了原生约束和错误匹配。

<script setup lang="ts">
import { Field, FieldError, FieldLabel } from "@ui-sdk/vue/field"
import { Textarea } from "@ui-sdk/vue/textarea"
</script>

<template>
  <Field invalid>
    <FieldLabel>Review</FieldLabel>
    <Textarea default-value="Great." />
    <FieldError match>Write at least 20 characters.</FieldError>
  </Field>
</template>
聚焦 Review。文本域被标记为无效,下面的错误说明评审应该有多长。

提交快捷键

设置 submit-shortcut,用键盘提交文本域所在的表单。设为 "mod+enter" 时,Enter 仍然开始新的一行,Command 或 Control+Enter 提交。设为 "enter" 时,Enter 提交,Shift+Enter 开始新的一行。不设置时,Enter 总是开始新的一行,而输入法正在组合文字时,两者都不会提交。

<script setup lang="ts">
import { Textarea } from "@ui-sdk/vue/textarea"
</script>

<template>
  <form action="/messages" method="post">
    <Textarea
      name="message"
      aria-label="Message"
      required
      submit-shortcut="mod+enter"
    />
    <button type="submit">Send</button>
  </form>
</template>

这个快捷键像 Send 按钮一样提交,所以 required 和表单的 @submit 仍然生效。改为在文本域上监听 @submit 来自己处理这个快捷键:@submit="(value) => …" 会收到值,表单不会提交。它的第一个参数是值,不是事件,所以没有什么需要阻止,.prevent 这样的修饰符也不适用。

样式

直接给原生文本域设置样式,用 data-filled、data-disabled、data-readonly 和 data-required 表示它的状态。在字段内部,它还会收到字段的校验属性,比如 data-invalid。使用 auto-resize 时,设置 min-height 和 max-height 来限制它的增长。使用逻辑间距,让文字跟随周围的方向。

文本域的 data-slot 是 textarea,它的状态属性列在数据属性下。

数据属性

Textarea

[data-slot="textarea"]

属性含义
data-filledPresent while the field holds any text.
data-disabledPresent when the field is disabled, by its own prop or its Field.
data-readonlyPresent when the field is read-only.
data-requiredPresent when the field is required.

API 参考

Props 与下面相同,回调是事件,比如 @value-change。原生 textarea 属性会透传。

Textarea

value?string
The value, for a controlled field.
defaultValue?string
The value it starts with. A form reset returns here.
@value-change((value: string, details: ChangeDetails<InputValueChangeReason>) => void)
Called when the user changes the value: typing ("input-change"), a Clear part ("clear-press"), a form reset ("reset") or a controller ("imperative"). details.cancel() keeps the value as it was.
controller?object
Reads and writes the field from anywhere. Create one with createInputController().
debug?boolean
Logs every state change with its reason to the console, in development builds.
autoResize?boolean
Grows and shrinks with the text. Bound it with CSS min-height and max-height; past the maximum, it scrolls.
@submit((value: string, details: TextareaSubmitDetails) => void)
Runs on the submit shortcut, unless an input method is composing. Without it, the shortcut submits the form the textarea is in. With neither, the key is left alone, so Enter adds a line.
submitShortcut?"enter" | "mod+enter"
Which keys submit: "enter", with Shift+Enter for a new line, or "mod+enter", with Enter for a new line. Without it, Enter always starts a new line.
disabled?boolean
Disables the field.
readOnly?boolean
Makes the field read-only.
required?boolean
Makes the field required.

状态

filledboolean
Whether the field holds any text.
disabledboolean
Whether the field is disabled.
readOnlyboolean
Whether the field is read-only.
requiredboolean
Whether the field is required.

原因

input-changeEvent
Typing, pasting or dropping changed a text field's value; in a combobox it can also open or close it, or Enter keeps the typed text as the value.
clear-pressMouseEvent | KeyboardEvent
A Clear part was pressed, like on an input, a select or a combobox.
resetnone
A form reset.
imperativenone
A controller or api call.

控制器

用 createInputController() 创建一个,并作为 controller 传入。

getValue()() => string
The current value.
setValue(value, options?)(value: string, options?: ChangeOptions<InputValueChangeReason> | undefined) => boolean
Sets the value as if the user typed it, so input fires. Returns false when the change was canceled or nothing changed.
clear(options?)(options?: ChangeOptions<InputValueChangeReason> | undefined) => boolean
Empties the field, as if the user deleted the text, so input fires. Returns false when the change was canceled or nothing changed.
focus()() => void
Moves focus to the field.

无障碍

多行文本字段没有 WAI-ARIA 模式。文本域保持为原生 <textarea>,带有标准的文本编辑、选择、拼写检查和表单行为。给它一个可见的字段标签或 aria-label,并把描述和错误放在它们的字段部件里,这样它们会与它关联。

经过测试的行为

下面每一行都是一个自动化测试。

  • The label names the textarea and the description describes it.
Auto resize
  • Grows with its text and shrinks back.
  • Stops at its max height and scrolls.
  • Shrinks back when a reset empties it.
  • Follows text written from code.
Submit shortcut
  • Without a submit shortcut, Enter starts a new line and doesn't submit.
  • With enter, Enter submits the form and Shift+Enter starts a new line.
  • With mod+enter, Enter starts a new line and Cmd or Ctrl+Enter submits.
  • Enter while an input method composes doesn't submit.

运行于 React、Vue 和 web components。