JGuang

文件输入

FileInput · Astryx

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

文件输入 · FileInput

通过文件选择控件读取用户主动选择的本地文件;预览不向服务器上传。

安装与前置条件

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:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。

API 与上游详细参考

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

File Input

FileInput provides file upload with optional drag-and-drop support. Use it for single or multiple file selection with built-in validation for file type, size, and count. Pair with validation status for upload feedback.

属性类型默认值说明
label *string文件输入框的无障碍标签。
value *File | File[] | null当前选中的文件。受控组件。
onChange *(files: File | File[] | null) => void文件选择或移除时触发的回调。
changeAction(files: File | File[] | null) => Promise<void>Async change action (React 19 transitions pattern). Use for immediate upload on file selection.
acceptstring接受的文件类型。使用 HTML accept 属性格式。
isMultiplebooleanfalse是否可以选择多个文件。
maxSizenumber最大文件大小(字节)。超过此限制的文件将被拒绝。
maxFilesnumber最大文件数(仅在 isMultiple 为 true 时适用)。
isLabelHiddenbooleanfalse视觉上隐藏标签,保持屏幕阅读器可访问。
descriptionstring显示在标签和输入框之间的描述文本。
isOptionalbooleanfalseDisplays an "Optional" indicator next to the label. Mutually exclusive with isRequired.
isRequiredbooleanfalseDisplays a "Required" indicator next to the label and adds a screen-reader-only "Required" description to the trigger (assistive tech does not reliably announce aria-required on the trigger). Mutually exclusive with isOptional.
isDisabledbooleanfalse禁用输入框,阻止交互并使元素变暗。
disabledMessagestring说明输入框被禁用的原因。与 isDisabled 一起使用时,在悬停/键盘聚焦时显示工具提示,并通过 aria-disabled 保持触发器可聚焦(打开文件选择器仍被阻止)。请使用此属性,而不是用 Tooltip 包裹已禁用的 FileInput。
isLoadingbooleanfalse加载状态,显示旋转器并设置 aria-busy。
placeholderstring"Choose file" or "Choose files"未选择文件时显示的占位符文本。
mode'input' | 'dropzone''input'视觉模式。'input' 为紧凑内联样式;'dropzone' 为支持拖放的较大区域。
status{type: 'error' | 'warning' | 'success', message?: string}验证状态。
statusVariant'attached' | 'detached' | 'tooltip''attached'状态消息相对于输入框的放置方式。attached 直接叠加在输入框下方(带边框处理);detached 作为独立元素浮于下方并留有间距;tooltip 隐藏消息框,并在状态图标上以提示气泡形式显示。
labelTooltipstringTooltip text displayed in an info icon at the end of the label.
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.
containerXstyleStyleXStyles字段容器(标签、控件和状态整体)的 StyleX 样式。在 InputGroup 内(请改为设置 InputGroup 的样式)或 horizontal-labels FormLayout 中无效。不接受根级 xstyle、className 和 style。
containerClassNamestring字段容器的类名,作用目标同 containerXstyle。
containerStyleCSSProperties字段容器的内联样式,作用目标同 containerXstyle。
controlXstyleStyleXStyles可视控件框的 StyleX 样式:绘制边框和背景并包含图标与尾部控件的元素,而非原生输入元素。
controlClassNamestring可视控件框的类名。
controlStyleCSSProperties可视控件框的内联样式。

使用建议

  • Always specify an accept prop to guide users toward valid file types.
  • Use maxSize and maxFiles to prevent oversized uploads.
  • Add a description to communicate constraints.
  • 避免:Don't use FileInput for directory uploads.
  • 避免:Don't use mode='input' unless space is constrained; dropzone mode provides a better experience.
  • 避免:Don't wrap a disabled FileInput in Tooltip to explain the disabled state; use the disabledMessage prop instead.

主题 API

{
  "targets": [
    {
      "className": "astryx-file-input",
      "visualProps": [
        "mode",
        "status"
      ]
    },
    {
      "className": "astryx-file-input-icon",
      "visualProps": [
        "mode"
      ]
    }
  ]
}

组件结构

  • Label: Text identifying the field.
  • Drop zone: Clickable area for file selection.
  • Placeholder: Hint text when no files selected.
  • File name display: Shows selected file names.
  • Clear button: Removes selected files.