Textarea
输入多行文本、随输入调整字段大小,并把它连接到表单。
文本域是原生的多行文本框,用来输入消息、笔记和描述。它可以在字段和表单里使用,可以随文字增长,并报告与 Input 相同的变更原因。
import "@ui-sdk/web/field"
import "@ui-sdk/web/textarea"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-field name="message">
<ui-field-label>Message</ui-field-label>
<ui-textarea>
<textarea placeholder="Type your message here…"></textarea>
</ui-textarea>
</ui-field>
`
}
export { mount }安装
npm install @ui-sdk/web @ui-sdk/core导入组件会定义它的元素。如果用字段,也导入字段。在服务器上,导入什么也不做。
import "@ui-sdk/web/textarea"
import "@ui-sdk/web/field"用法
ui-textarea 包住你自己的 <textarea>。把 rows、placeholder、maxlength 和其他原生属性放在 <textarea> 上。
<script type="module">
import "@ui-sdk/web/textarea"
</script>
<ui-textarea>
<textarea
aria-label="Message"
placeholder="Type your message here…"
></textarea>
</ui-textarea>字段
把文本域包进 Field,给它一个可见的标签和描述。文本域没有 name 时,字段也会提供自己的 name。
import "@ui-sdk/web/field"
import "@ui-sdk/web/textarea"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-field name="feedback">
<ui-field-label>Feedback</ui-field-label>
<ui-textarea>
<textarea placeholder="What could we do better?"></textarea>
</ui-textarea>
<ui-field-description>Only the product team can see this.</ui-field-description>
</ui-field>
`
}
export { mount }自动调整大小
设置 auto-resize,让文本域随文字增长和收缩。CSS min-height 和 max-height 设定它的范围;超过最大值后,文字在它内部滚动。
import "@ui-sdk/web/field"
import "@ui-sdk/web/textarea"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-field name="notes">
<ui-field-label>Notes</ui-field-label>
<ui-textarea auto-resize>
<textarea placeholder="Add notes for the delivery driver…"></textarea>
</ui-textarea>
<ui-field-description>Your driver sees these notes.</ui-field-description>
</ui-field>
`
}
export { mount }支持 field-sizing: content 的浏览器会原生地调整它的大小。在其他情况下,组件会在值变化、表单重置或宽度变化时测量它。
受控
监听 valuechange 读取值,设置元素的 value property 来从你的应用里更改它。这里的字符计数由那个值计算,原生 maxlength 限制文字长度。
import "@ui-sdk/web/field"
import "@ui-sdk/web/textarea"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-field name="bio">
<ui-field-label>Bio</ui-field-label>
<ui-textarea>
<textarea maxlength="160">Product designer in Lisbon. I write about type and interfaces.</textarea>
</ui-textarea>
<ui-field-description>62 / 160 characters</ui-field-description>
</ui-field>
`
const count = root.querySelector("ui-field-description")!
root
.querySelector("ui-textarea")!
.addEventListener("valuechange", (event) => {
count.textContent = `${event.detail.value.length} / 160 characters`
})
}
export { mount }Textarea 与 Input 共用变更原因和取消行为。通用约定见状态与事件。
表单
文本域通过字段的 name 下的原生表单提交它的值。用元素的 api 从外部更改它,这里用来在发送后清空它。原生 required 约束会阻止空消息。
import "@ui-sdk/web/field"
import "@ui-sdk/web/textarea"
import { log } from "./log"
function mount(root: HTMLElement) {
root.innerHTML = `
<form data-slot="stack">
<ui-field name="message">
<ui-field-label>Message</ui-field-label>
<ui-textarea>
<textarea required placeholder="Write a reply to Hollis Grant…"></textarea>
</ui-textarea>
</ui-field>
<div data-slot="row">
<button type="submit" data-variant="solid">Send Message</button>
</div>
</form>
`
const form = root.querySelector("form")!
const message = root.querySelector("ui-textarea")!
form.addEventListener("submit", (event) => {
event.preventDefault()
log(root, `Sent “${new FormData(form).get("message")}”`)
message.api.clear()
})
}
export { mount }控制器介绍了创建和共享控制器。
禁用
在原生 textarea 或它的字段上设置 disabled,阻止编辑,并把它移出 Tab 顺序和提交的值。
import "@ui-sdk/web/field"
import "@ui-sdk/web/textarea"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-field disabled>
<ui-field-label>Reply</ui-field-label>
<ui-textarea>
<textarea>Replies are closed for this thread.</textarea>
</ui-textarea>
</ui-field>
`
}
export { mount }无效
对于应用自己判定的错误,在字段上设置 invalid。字段会显示错误,把它与文本域关联起来,并设置 aria-invalid。Field 介绍了原生约束和错误匹配。
import "@ui-sdk/web/field"
import "@ui-sdk/web/textarea"
function mount(root: HTMLElement) {
root.innerHTML = `
<ui-field invalid>
<ui-field-label>Review</ui-field-label>
<ui-textarea>
<textarea>Great.</textarea>
</ui-textarea>
<ui-field-error match>Write at least 20 characters.</ui-field-error>
</ui-field>
`
}
export { mount }提交快捷键
设置 submit-shortcut,用键盘提交文本域所在的表单。设为 "mod+enter" 时,Enter 仍然开始新的一行,Command 或 Control+Enter 提交。设为 "enter" 时,Enter 提交,Shift+Enter 开始新的一行。不设置时,Enter 总是开始新的一行,而输入法正在组合文字时,两者都不会提交。
<script type="module">
import "@ui-sdk/web/textarea"
</script>
<form action="/messages" method="post">
<ui-textarea submit-shortcut="mod+enter">
<textarea name="message" aria-label="Message" required></textarea>
</ui-textarea>
<button type="submit">Send</button>
</form>这个快捷键像 Send 按钮一样提交表单,所以 required 和表单的 submit 事件仍然生效。
样式
直接给原生文本域设置样式,用 data-filled、data-disabled、data-readonly 和 data-required 表示它的状态。在字段内部,它还会收到字段的校验属性,比如 data-invalid。使用 auto-resize 时,设置 min-height 和 max-height 来限制它的增长。使用逻辑间距,让文字跟随周围的方向。
文本域的 data-slot 是 textarea,它的状态属性列在数据属性下。
数据属性
<ui-textarea>
[data-slot="textarea"]
| 属性 | 含义 |
|---|---|
data-filled | Present while the field holds any text. |
data-disabled | Present when the field is disabled, by its own prop or its Field. |
data-readonly | Present when the field is read-only. |
data-required | Present when the field is required. |
API 参考
Props 是 kebab 写法的属性,比如 auto-resize 和 default-value。disabled、readonly、required 和其他原生属性从 <textarea> 上读取。回调是 DOM 事件:valuechange 在 ui-textarea 上触发,event.detail 里带有 { value, reason, event, source }。
Textarea
value?string- The value, for a controlled field.
defaultValue?string- The value it starts with. A form reset returns here.
valuechange event- 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-heightandmax-height; past the maximum, it scrolls. submit event- 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
apicall.
控制器
元素的 api property 包含这些方法,它的 value property 读写文字。写入会像输入一样触发 valuechange。
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
inputfires. 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
inputfires. 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.
- 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.
- 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。