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

แนบไฟล์

FileDrop

A drop zone that is also a file picker. The chosen files are listed as chips, each with a remove button. Choosing or dropping more adds to the list. The screen checks types and sizes, and says what's wrong in error.

ใช้แนบไฟล์ในฟอร์ม ผู้ใช้ลากไฟล์มาวางหรือกดเพื่อเลือกไฟล์ก็ได้ ไฟล์ที่เลือกแสดงเป็นชิปใต้ช่อง แต่ละชิปมีปุ่มลบ และการเลือกเพิ่มจะต่อท้ายรายการเดิม สำหรับแสดงไฟล์ที่แนบไว้แล้วในหน้ารายละเอียด ใช้ AttachmentList

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

ตัวอย่าง

หลายไฟล์

เลือกได้หลายไฟล์เป็นค่าเริ่มต้น หน้าจอเก็บรายการไฟล์ด้วย value onFiles ได้รายการทั้งหมดหลังเลือก วาง หรือลบแต่ละไฟล์ title และ hint แสดงในช่องวาง

const [files, setFiles] = useState<File[]>([]);

<FileDrop
  label="ไฟล์แนบ"
  title="ลากไฟล์มาวางที่นี่ หรือเลือกไฟล์"
  hint="PDF หรือรูปภาพ ไม่เกิน 10 MB ต่อไฟล์"
  accept=".pdf,image/*"
  value={files}
  onFiles={setFiles}
/>

ไฟล์เดียว

multiple={false} รับไฟล์เดียว เลือกใหม่จะแทนที่ไฟล์เดิม accept จำกัดชนิดไฟล์ในหน้าต่างเลือกไฟล์

<FileDrop
  label="ใบเสนอราคา"
  required
  title="เลือกไฟล์ใบเสนอราคา"
  hint="PDF ไฟล์เดียว ไม่เกิน 5 MB"
  multiple={false}
  accept="application/pdf"
  onFiles={handleFiles}
/>

ข้อผิดพลาด

หน้าจอตรวจชนิดและขนาดใน onFiles แล้วส่ง error ที่บอกว่าไฟล์ใดผิดและแก้อย่างไร

แบบแปลนอาคาร.pdf ใหญ่เกิน 10 MB ลดขนาดไฟล์แล้วแนบใหม่

<FileDrop
  label="ไฟล์แนบ"
  title="ลากไฟล์มาวางที่นี่ หรือเลือกไฟล์"
  hint="PDF หรือรูปภาพ ไม่เกิน 10 MB ต่อไฟล์"
  error="แบบแปลนอาคาร.pdf ใหญ่เกิน 10 MB ลดขนาดไฟล์แล้วแนบใหม่"
  onFiles={handleFiles}
/>

props ทั้งหมด

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

FileDrop props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
requiredboolean—Marks the label with a red star, and the control as required.
labelstring—The visible label, already in the screen language. Every field needs one, unless the screen labels it another way.
errorstring—What's wrong and how to fix it, shown under the field. It marks the field invalid.
idstring—The file input's id. One is made when it's left out.
title* จำเป็นstring—The main line in the drop zone, such as "Drop files here, or choose files".
hintstring—A second line in the drop zone, with the allowed types and sizes.
multiplebooleantrueLets people choose more than one file.
acceptstring—The file types the picker offers, as in the input's accept attribute, such as ".pdf,image/*".
valuereadonly File[]—The files, when the screen keeps them.
onFiles(files: File[]) => void—Hears the whole list of files after each choice, drop or removal.

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

  • บอกชนิดและขนาดไฟล์ที่รับได้ใน hint เสมอ accept ไม่ได้กันไฟล์ที่ลากมาวาง ให้ตรวจชนิดและขนาดใน onFiles อีกครั้ง
  • ลบไฟล์ได้ทีละไฟล์ด้วยปุ่มบนชิป ปุ่มมีชื่อไฟล์สำหรับโปรแกรมอ่านหน้าจอ การเลือกหรือวางเพิ่มจะต่อท้ายรายการ
  • แป้นพิมพ์: Tab ไปที่ช่อง แล้ว Enter หรือ Space เปิดหน้าต่างเลือกไฟล์ Tab ต่อไปยังปุ่มลบของแต่ละไฟล์

ซอร์สโค้ด: ui/src/special-inputs.tsx