แนบไฟล์
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}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "ไฟล์แนบ" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| title* จำเป็น | "ลากไฟล์มาวางที่นี่ หรือเลือกไฟล์" | string | The main line in the drop zone, such as "Drop files here, or choose files". |
| hint | "PDF หรือรูปภาพ ไม่เกิน 10 MB ต่อไฟล์" | string | A second line in the drop zone, with the allowed types and sizes. |
| accept | ".pdf,image/*" | string | The file types the picker offers, as in the input's accept attribute, such as ".pdf,image/*". |
| value | files | readonly File[] | The files, when the screen keeps them. |
| onFiles | setFiles | (files: File[]) => void | Hears the whole list of files after each choice, drop or removal. |
ไฟล์เดียว
multiple={false} รับไฟล์เดียว เลือกใหม่จะแทนที่ไฟล์เดิม accept จำกัดชนิดไฟล์ในหน้าต่างเลือกไฟล์
<FileDrop
label="ใบเสนอราคา"
required
title="เลือกไฟล์ใบเสนอราคา"
hint="PDF ไฟล์เดียว ไม่เกิน 5 MB"
multiple={false}
accept="application/pdf"
onFiles={handleFiles}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "ใบเสนอราคา" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| required | true | boolean | Marks the label with a red star, and the control as required. |
| title* จำเป็น | "เลือกไฟล์ใบเสนอราคา" | string | The main line in the drop zone, such as "Drop files here, or choose files". |
| hint | "PDF ไฟล์เดียว ไม่เกิน 5 MB" | string | A second line in the drop zone, with the allowed types and sizes. |
| multiple | false | boolean | Lets people choose more than one file. |
| accept | "application/pdf" | string | The file types the picker offers, as in the input's accept attribute, such as ".pdf,image/*". |
| onFiles | handleFiles | (files: File[]) => void | Hears the whole list of files after each choice, drop or removal. |
ข้อผิดพลาด
หน้าจอตรวจชนิดและขนาดใน onFiles แล้วส่ง error ที่บอกว่าไฟล์ใดผิดและแก้อย่างไร
แบบแปลนอาคาร.pdf ใหญ่เกิน 10 MB ลดขนาดไฟล์แล้วแนบใหม่
<FileDrop
label="ไฟล์แนบ"
title="ลากไฟล์มาวางที่นี่ หรือเลือกไฟล์"
hint="PDF หรือรูปภาพ ไม่เกิน 10 MB ต่อไฟล์"
error="แบบแปลนอาคาร.pdf ใหญ่เกิน 10 MB ลดขนาดไฟล์แล้วแนบใหม่"
onFiles={handleFiles}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "ไฟล์แนบ" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| title* จำเป็น | "ลากไฟล์มาวางที่นี่ หรือเลือกไฟล์" | string | The main line in the drop zone, such as "Drop files here, or choose files". |
| hint | "PDF หรือรูปภาพ ไม่เกิน 10 MB ต่อไฟล์" | string | A second line in the drop zone, with the allowed types and sizes. |
| error | "แบบแปลนอาคาร.pdf ใหญ่เกิน 10 MB ลดขนาดไฟล์แล้วแนบใหม่" | string | What's wrong and how to fix it, shown under the field. It marks the field invalid. |
| onFiles | handleFiles | (files: File[]) => void | Hears the whole list of files after each choice, drop or removal. |
props ทั้งหมด
ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| required | boolean | — | Marks the label with a red star, and the control as required. |
| label | string | — | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| error | string | — | What's wrong and how to fix it, shown under the field. It marks the field invalid. |
| id | string | — | 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". |
| hint | string | — | A second line in the drop zone, with the allowed types and sizes. |
| multiple | boolean | true | Lets people choose more than one file. |
| accept | string | — | The file types the picker offers, as in the input's accept attribute, such as ".pdf,image/*". |
| value | readonly 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