JGuang

多行输入

TextArea · Astryx

交互预览
npm install @astryxdesign/core@0.7.1 @astryxdesign/theme-neutral@0.7.1 @stylexjs/stylex@0.19.0

多行输入 · TextArea

输入多行内容,适合备注、描述和消息。

安装与前置条件

npm install @astryxdesign/core@0.7.1 @astryxdesign/theme-neutral@0.7.1 @stylexjs/stylex@0.19.0

需要 React 19+,按顺序导入基础 CSS 和主题,并设置 Theme Provider。参见开始使用。

示例与使用方式

  • 多行输入 · 综合展示 1:首先体验这个示例,再查看代码理解组件的组合方式。
  • 多行输入 · 交互示例 2:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 多行输入 · 多种状态 3:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 多行输入 · 输入校验 4:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 多行输入 · 包含图标 5:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。

API 与上游详细参考

优先使用 0.7.1 官方中文说明;上游尚未提供中文的条目保留英文。属性名、类型及默认值保持原生 API,不把示例状态名称当成组件属性。

Text Area

TextArea is a multi-line text input for collecting longer-form content like comments, descriptions, or messages. Use it when the expected input spans multiple lines. For shorter, single-line values, use TextInput.

属性类型默认值说明
refReact.Ref<HTMLTextAreaElement>转发至底层 <textarea> 元素的 ref。
label *string文本域的标签文本:始终渲染以确保无障碍性。
value *string文本域的当前值。
onChange(value: string, e: ChangeEvent<HTMLTextAreaElement>) => void文本域值变化时触发的回调。
changeAction(value: string, e: ChangeEvent<HTMLTextAreaElement>) => void | Promise<void>在 React transition 内于 onChange 之后触发的异步操作。通过 useOptimistic 启用乐观更新。
isLabelHiddenbooleanfalse视觉上隐藏标签,同时保持屏幕阅读器的无障碍性。
descriptionstring显示在标签和文本域之间的辅助文本。
isOptionalbooleanfalse在标签旁显示"可选"指示器。与 isRequired 互斥。
isRequiredbooleanfalse在标签旁显示"必填"指示器并设置 aria-required。与 isOptional 互斥。
isDisabledbooleanfalse禁用文本域,阻止交互。
isReadOnlybooleanfalse将文本域设为只读:值以完整不透明度显示并仍随表单提交,但无法编辑。与 isDisabled 不同,只读文本域不会变暗,并保留在 Tab 顺序中。同时设置时 isDisabled 优先。
disabledMessagestring说明文本域被禁用的原因。与 isDisabled 一起使用时,在悬停/键盘聚焦时显示工具提示,并通过 aria-disabled 保持文本域可聚焦(字段变为只读)。请使用此属性,而不是用 Tooltip 包裹已禁用的 TextArea。
isLoadingbooleanfalse使文本域进入加载状态,在输入框内显示旋转器。
placeholderstring文本域为空时显示的占位符文本。
rowsnumber3可见文本行数。
maxLengthnumber允许的最大字符数。设置后,在文本域下方显示字符计数器(当前/最大)。不原生强制限制:超出时计数器显示错误样式。
status{ type: 'warning' | 'error' | 'success'; message?: string }应用彩色边框和图标的状态指示器。可选消息显示在文本域下方的浮动框中。
statusVariant'attached' | 'detached' | 'tooltip''attached'状态消息相对于输入框的放置方式。attached 直接叠加在输入框下方(带边框处理);detached 作为独立元素浮于下方并留有间距;tooltip 隐藏消息框,并在状态图标上以提示气泡形式显示。
labelTooltipstring在标签末尾的信息图标中显示的工具提示文本。
startIconIconType在文本域包装器前端内部渲染的图标组件。
hasSpellCheckbooleantrue启用或禁用浏览器拼写检查。
hasAutoFocusbooleanfalse挂载时自动聚焦文本域。
size'sm' | 'md' | 'lg''md'文本域的尺寸,影响内部填充。高度由 rows 控制,而非 size。
onPaste(e: ClipboardEvent<HTMLTextAreaElement>) => void内容粘贴到文本域时触发的回调。
htmlNamestring文本域元素的 HTML name 属性,用于表单提交。
widthSizeValueWidth of the field (number = pixels, string used as-is, e.g. "100%"). Sizes the whole field (label, control, and status) so they stay aligned.
onFocus(e: FocusEvent<HTMLTextAreaElement>) => void文本域获得焦点时触发的回调。
onBlur(e: FocusEvent<HTMLTextAreaElement>) => void文本域失去焦点时触发的回调。
autoCompletestring原生 autocomplete 属性,原样转发给文本域。不影响受控的值。
containerXstyleStyleXStyles字段容器(标签、控件和状态整体)的 StyleX 样式。在 InputGroup 内(请改为设置 InputGroup 的样式)或 horizontal-labels FormLayout 中无效。不接受根级 xstyle、className 和 style。
containerClassNamestring字段容器的类名,作用目标同 containerXstyle。
containerStyleCSSProperties字段容器的内联样式,作用目标同 containerXstyle。
controlXstyleStyleXStyles可视控件框的 StyleX 样式:绘制边框和背景并包含图标与尾部控件的元素,而非原生输入元素。
controlClassNamestring可视控件框的类名。
controlStyleCSSProperties可视控件框的内联样式。

使用建议

  • Provide a visible label so users know what to enter. If the label must be hidden, set isLabelHidden with a descriptive label for screen readers.
  • Set maxLength with a character counter when there is a defined limit; it helps users stay within bounds before they submit.
  • Use the status prop to surface validation feedback inline: show success when input is valid, warning for soft limits, and error for hard failures.
  • Add a description or placeholder to clarify expected content, like "Describe the issue in detail," but never rely on placeholder alone as the only label.
  • 避免:Avoid using TextArea for short, single-line values like names or emails; use TextInput instead.
  • 避免:Don't rely solely on placeholder text to communicate the purpose of the field; placeholders disappear on focus and are not accessible labels.
  • 避免:Don't show a status message without also setting the status type; the colored border and icon are what draw the user's attention to the message.
  • 避免:Don't wrap a disabled TextArea in Tooltip to explain why it's disabled; disabled controls swallow the hover events the wrapper needs. Use the disabledMessage prop instead.

主题 API

{
  "targets": [
    {
      "className": "astryx-text-area",
      "visualProps": [
        "size",
        "status"
      ],
      "states": [
        "disabled",
        "readonly"
      ]
    },
    {
      "className": "astryx-text-area-control"
    },
    {
      "className": "astryx-text-area-counter"
    }
  ],
  "vars": [
    {
      "name": "--_textarea-inline-padding",
      "description": "文本域文本的行内内边距。外层容器保持无内边距(padding: 0),使原生缩放手柄位于真正的角落;该变量在内部 <textarea> 上承载内边距,起始图标、状态/加载指示器和字符计数器都与之对齐。",
      "default": "var(--spacing-2)",
      "private": true
    }
  ],
  "derived": [
    {
      "property": "paddingInline",
      "vars": [
        "--_textarea-inline-padding"
      ],
      "replaces": true
    }
  ]
}

组件结构

  • Label: Text identifying the multi-line field.
  • Description: Helper text between the label and the input.
  • Input container: Painted boundary containing the text area and its overlays.
  • Text area: Multi-line control that displays and edits the value.
  • Placeholder: Hint text shown inside the empty text area.
  • Start icon: Astryx Icon rendered at the start when startIcon is a semantic name or icon component.
  • Custom start content: Caller-provided ReactNode rendered at the start instead of an Astryx Icon.
  • Spinner: Loading indicator shown at the end of the input container.
  • Status icon: Error, warning, or success icon shown inside the input.
  • Character counter: Current and maximum character counts shown inside the input container.
  • Field status message: Attached or detached error, warning, or success message associated with the field.
  • Tooltip status message: Tooltip surface presenting the status message for the tooltip variant.

完整示例的额外依赖

部分示例使用独立图标包。示例清单中的每条安装命令包含该示例实际需要的依赖;若要运行本页全部示例,还需安装:

npm install @heroicons/react@2.2.0