ข้ามไปยังเนื้อหา

ไฟล์แนบ

AttachmentList

The files attached to a record, each with its size and a download button. `downloadLabel` comes before the name.

ใช้แสดงไฟล์ที่แนบกับรายการ พร้อมขนาดและปุ่มดาวน์โหลด ส่งชื่อไฟล์และขนาดเป็นไบต์ ส่วนประกอบแสดงขนาดเป็น KB หรือ MB เอง ถ้าผู้ใช้ต้องแนบไฟล์ใหม่ ใช้ FileDrop

import { AttachmentList } from "@bsru/ui";

ตัวอย่าง

ไฟล์ของคำขอ

href ชี้ไปที่ Route Handler สำหรับดาวน์โหลดของแอป ห้ามใส่ลิงก์ที่มี token หน้าเอกสารนี้ไม่มี route นั้น จึงปิดการกดลิงก์ไว้

  • ใบเสนอราคา-บริษัทตัวอย่าง.pdf245 KB
  • รายละเอียดคุณลักษณะเฉพาะ.pdf1.4 MB
<AttachmentList
  files={[
    {
      key: "quote",
      name: "ใบเสนอราคา-บริษัทตัวอย่าง.pdf",
      size: 250880,
      href: "/api/requests/PR-2570-0001/files/quote",
    },
    {
      key: "spec",
      name: "รายละเอียดคุณลักษณะเฉพาะ.pdf",
      size: 1468006,
      href: "/api/requests/PR-2570-0001/files/spec",
    },
  ]}
  downloadLabel="ดาวน์โหลด"
/>

ไฟล์จำลองในต้นแบบ

ไฟล์ที่ไม่มี href เรียก onDownload แทน ใช้ในต้นแบบหรือการทดสอบ ลองกดปุ่มดาวน์โหลด

  • ใบเสนอราคา-บริษัทตัวอย่าง.pdf245 KB
  • รายละเอียดคุณลักษณะเฉพาะ.pdf1.4 MB
<AttachmentList
  files={[
    {
      key: "quote",
      name: "ใบเสนอราคา-บริษัทตัวอย่าง.pdf",
      size: 250880,
    },
    { key: "spec", name: "รายละเอียดคุณลักษณะเฉพาะ.pdf", size: 1468006 },
  ]}
  downloadLabel="ดาวน์โหลด"
  onDownload={handleDownload}
/>

props ทั้งหมด

ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้

AttachmentList props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
files* จำเป็นreadonly Attachment[]—The files, in the order to show them.
downloadLabel* จำเป็นstring—Such as ดาวน์โหลด, read with the file name by screen readers.
onDownload(file: Attachment) => void—Called for a file without href, such as in a mock.

ชนิดข้อมูล

Attachment

One file attached to a record.

Attachment fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
key* จำเป็นstring—Unique among the files.
name* จำเป็นstring—The file name, with its extension.
size* จำเป็นnumber—Size in bytes.
hrefstring—The app's download Route Handler, never a link with a token in it.

ข้อควรปฏิบัติ

  • downloadLabel อ่านคู่กับชื่อไฟล์ เช่น ดาวน์โหลด ใบเสนอราคา.pdf ส่งในภาษาของหน้าจอ
  • ตรวจสิทธิ์ที่ Route Handler ทุกครั้งที่ดาวน์โหลด ไม่ใช่แค่ซ่อนปุ่ม
  • ชื่อไฟล์ยาวถูกตัดด้วย … ในแถวเดียว ขนาดไฟล์ใช้ fileSize() ถ้าต้องแสดงที่อื่น

ซอร์สโค้ด: ui/src/record.tsx