Skip to content

Upload 上传 ​

一套手绘风文件选择与拖放区域。组件只负责选择、筛选和文件列表管理,不自行发起网络请求。

上传提示选择完成后由 super-upload-change 提供新的 fileList;开启 auto-upload 时还会派发 super-upload-submit,真正的上传请求由消费者处理。

注册 ​

ts
import "yxzq-element/upload";

// SSR 或自定义注册时机
import { defineSuperUpload } from "yxzq-element/define";
defineSuperUpload();

基础用法 ​

html
<super-upload aria-label="上传附件"></super-upload>
<super-upload multiple accept="image/*" aria-label="上传图片"></super-upload>
<super-upload disabled aria-label="禁用的上传区域"></super-upload>
ts
const upload = document.querySelector("super-upload");
upload.addEventListener("super-upload-change", (event) => {
  upload.fileList = event.detail.fileList;
});

自定义触发内容 ​

默认 Slot 替换选择区域的提示内容。它只负责展示,点击和键盘触发仍由整个区域管理。

上传简历支持 PDF、DOCX,单个文件不超过 5 MB拖入设计素材也可以点击这里选择多个文件
html
<super-upload aria-label="上传简历">
  <upload-icon aria-hidden="true"></upload-icon>
  <strong>上传简历</strong>
  <span>支持 PDF、DOCX,单个文件不超过 5 MB</span>
</super-upload>

数量与大小限制 ​

max-count 以当前 fileList 为基准限制总数;max-size 以字节为单位。被拒绝的文件不会进入列表,并通过 super-upload-exceed 报告原因。

html
<super-upload
  multiple
  max-count="3"
  max-size="204800"
  auto-upload
  aria-label="上传最多三个文件"
></super-upload>
ts
upload.addEventListener("super-upload-exceed", (event) => {
  console.log(event.detail.reason); // "max-count" | "max-size"
  console.log(event.detail.files);
});

upload.addEventListener("super-upload-submit", async (event) => {
  const body = new FormData();
  event.detail.files.forEach((file) => body.append("files", file));
  await fetch("/api/upload", { method: "POST", body });
});

尺寸与校验状态 ​

大
中
小

文件列表与移除 ​

fileList 是 JavaScript 属性,不会从 HTML 属性反序列化。每项支持内置移除按钮,并会派发包含被移除文件和剩余列表的事件。只读 value 是派生视图,按 fileList 顺序返回文件名 string[];修改文件必须写入 fileList。

ts
const upload = document.querySelector("super-upload");
upload.fileList = [existingFile];

upload.addEventListener("super-upload-remove", (event) => {
  upload.fileList = event.detail.fileList;
});

键盘与无障碍 ​

整个选择区域是一个可聚焦的 role="button",内部原生文件输入使用 hidden。Enter 与 Space 打开原生文件选择器;Tab 可进入文件列表中的移除按钮。禁用时区域离开 Tab 序列,并设置 aria-disabled="true"。

操作行为
Enter / Space打开文件选择器
拖放文件在区域上松开后加入列表
Tab在区域与移除按钮间移动焦点

API ​

Attributes / Properties ​

名称类型默认值说明
acceptstring空字符串原生 accept 规则,支持扩展名、MIME 和 type/*
multiplebooleanfalse为 false 时每次选择替换当前文件;为 true 时允许选择多个文件并追加到列表
disabledbooleanfalse禁用选择、拖放与移除
max-countnumber不限制文件列表最大数量
max-sizenumber不限制单文件最大字节数
auto-uploadbooleanfalse选择成功后额外派发提交事件
fileListFile[][]当前文件列表,仅支持 JS property
valuestring[][]只读派生视图,按 fileList 顺序返回文件名;写入入口是 fileList
sizelarge | medium | smallmedium尺寸
validationnone | success | warning | error | infonone校验状态
prompt-textstring选择文件默认提示标题
hint-textstring或将文件拖到这里默认提示说明
remove-labelstring移除文件移除按钮的可访问名称前缀
aria-labelstring上传文件选择区域与文件列表的可访问名称

Events ​

名称Detail说明
super-upload-change{ fileList: File[] }文件成功加入列表后派发
super-upload-submit{ files: File[] }auto-upload 为真时,与本次加入的文件一起派发
super-upload-remove{ file: File, fileList: File[] }用户移除文件后派发
super-upload-exceed{ files: File[], reason: "max-count" | "max-size" }文件超出数量或大小时派发

所有事件均为 bubbles: true、composed: true。程序化修改 fileList 不会派发用户事件。

Slots ​

名称说明
默认 Slot选择区域的提示内容

CSS Parts ​

名称说明
wrapper外层容器
dropzone / trigger可选择、可拖放的触发区域
prompt默认提示内容容器
input隐藏的原生文件输入
file-list文件列表
file单个文件项
file-name文件名
file-size文件大小
remove-button移除按钮

CSS Custom Properties ​

css
super-upload {
  --super-upload-width: 360px;
  --super-upload-min-height: 152px;
  --super-upload-padding: var(--super-spacing-lg);
  --super-upload-color: var(--super-text-color-primary);
  --super-upload-background: var(--super-background-color);
  --super-upload-border-color: var(--super-border-color);
  --super-upload-hover-color: var(--super-color-primary);
  --super-upload-focus-color: var(--super-focus-color);
  --super-upload-shadow-color: var(--super-sketch-shadow);
  --super-upload-rotation: var(--super-sketch-rotation);
}

已知边界 ​

super-upload 不发起网络请求,也不是 form-associated Custom Element;fileList 不参与 FormData、原生表单重置或外部约束验证。上传进度、取消、重试和并发控制由消费者应用实现。拖放在不支持 DataTransfer 的测试或运行环境中会得到空文件列表。