ui
ธีม รูปแบบ CarbonKanok ภาษา English
ภาพรวมติดตั้งการย้ายไป 1.0ธีมและ layerดีไซน์ Kanokปรับแต่งหน้าตาไอคอนฟอร์มและการตรวจค่าใช้กับ frameworkใช้กับ AIButtonText inputText areaNumber inputSearchSelectDropdownComboboxMulti-selectCheckboxRadio buttonToggleDate pickerTime pickerSliderFile uploaderModalSide panelTearsheetNotificationToastTooltipDefinition tooltipPopoverToggletipMenuData tableStructured listPaginationContained listTree viewTabsBreadcrumbLinkPagination navContent switcherAccordionProgress indicatorUI shellTagTileCardProduct cardCode snippetCopy buttonUser avatarTruncated textListLoadingInline loadingProgress barSkeletonBadge indicatorStatus indicatorGridHeadingLayerAction setPage headerBig numberEdit in placeแนะนำText inputText areaNumber inputSearchSelectDropdownComboboxMulti-selectCheckboxRadio buttonToggleDate pickerTime pickerSliderFile uploaderTabsPagination navContent switcherTooltipDefinition tooltipPopoverMenuAccordionProgress indicatorUI shellModalSide panelTearsheetNotificationToastData tableStructured listPaginationTree viewCode snippetCopy buttonTruncated textLoadingTagCardProduct cardEdit in place

Headless File uploader

FileUploader ให้พฤติกรรมของ file uploader โดยไม่มี class เลย: Trigger ที่เปิดหน้าต่างเลือกไฟล์ DropZone ที่รับไฟล์ที่ลากมาวาง (ไม่รับโฟลเดอร์) เงื่อนไขจำกัดประเภท ขนาด และจำนวน รายการไฟล์ที่เลือก โฟกัสที่ย้ายต่อเมื่อแถวหายไป และไฟล์ที่ผ่านเงื่อนไขที่ส่งไปกับฟอร์ม

แถว ไอคอน ข้อความ แถบความคืบหน้า และ style ทั้งหมดเป็นของคุณ ไม่มีอะไรอัปโหลดเอง คุณต้องส่งไฟล์และอัปเดตสถานะเอง ตัวที่มี style อยู่ที่ File uploader

ติดตั้ง

ส่วน headless มากับแพ็กเกจเลย (ไม่มี item ของ shadcn; item ของ React + Tailwind สร้างบนมัน):

npm install @purpurabog/ui import { FileUploader } from '@purpurabog/ui/headless';

วิธีใช้

import { useState } from 'react'; import { FileUploader } from '@purpurabog/ui/headless'; const button = 'h-10 w-fit bg-primary px-4 text-sm text-on-primary hover:bg-blue-hover focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus disabled:cursor-not-allowed disabled:bg-button-disabled disabled:text-on-color-disabled'; export default function Example() { const [change, setChange] = useState<FileUploader.FileChange>(); return ( <FileUploader.Root name="attachments" multiple onChange={setChange}> <div className="grid gap-4"> <FileUploader.Group aria-label="ไฟล์แนบ"> <FileUploader.DropZone> <FileUploader.Trigger className={button}>เพิ่มไฟล์</FileUploader.Trigger> <FileUploader.Label hidden>ไฟล์แนบ</FileUploader.Label> <FileUploader.Input hidden /> <FileUploader.FormValue /> </FileUploader.DropZone> </FileUploader.Group> <Files /> {change && ( <p className="text-xs text-ink-muted"> onChange: {change.action}, currentFiles: {change.currentFiles.length} </p> )} </div> </FileUploader.Root> ); } // แถวของไฟล์ ตัวอย่างแต่ละอันในหน้านี้เป็นโมดูลเต็ม ตัวอย่างที่มีรายการจึงมี <Files /> นี้ซ้ำ function Files() { const { files, remove } = FileUploader.useFileUploaderContext(); return ( <FileUploader.List className="grid w-80 gap-2"> {files.map((f) => ( <FileUploader.Item key={f.uuid} uuid={f.uuid} name={f.name} status={f.status} invalid={f.invalid} onDelete={remove} className="group bg-layer-1 text-sm text-ink data-invalid:outline-2 data-invalid:-outline-offset-2 data-invalid:outline-error" > <FileUploader.ItemRow className="flex min-h-12 items-center ps-4"> <span className="flex-1 truncate">{f.name}</span> <FileUploader.Remove aria-label={`ลบไฟล์ - ${f.name}`} className="hidden size-12 shrink-0 place-items-center hover:bg-layer-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus group-data-removable:grid" > × </FileUploader.Remove> </FileUploader.ItemRow> {f.errorSubject && <p role="alert" className="border-t border-hairline px-4 py-2 text-xs text-error-text">{f.errorSubject}</p>} </FileUploader.Item> ))} </FileUploader.List> ); }
  • แต่ละไฟล์เป็น FileItem: มี uuid, name, ตัว File และ status, invalid, errorSubject, errorBody, progress, retryable
  • Root เรียก onAdd ทันทีที่ผู้ใช้เลือกไฟล์ (รวมไฟล์ที่ไม่ผ่านเงื่อนไขด้วย) ก่อนรายการจะอัปเดต (เหมือน onAddFiles ของ Carbon) เรียก onChange พร้อม addedFiles, removedFiles, currentFiles และ action (add, remove หรือ clear) และเรียก onDelete เมื่อไฟล์ถูกลบ
  • ถ้าไม่ได้ตั้ง multiple การเลือกครั้งใหม่จะแทนที่รายการเดิม

โครงสร้าง

<FileUploader.Root> <FileUploader.Group> <FileUploader.Title /> <FileUploader.DropZone> <FileUploader.Trigger /> <FileUploader.Label /> <FileUploader.Input /> </FileUploader.DropZone> <FileUploader.FormValue /> </FileUploader.Group> <FileUploader.List> <FileUploader.Item> <FileUploader.ItemRow> <FileUploader.Retry /> <FileUploader.Remove /> <FileUploader.ItemStatus /> </FileUploader.ItemRow> </FileUploader.Item> </FileUploader.List> </FileUploader.Root>

ปุ่มหรือพื้นที่วางไฟล์

DropZone รับไฟล์ที่ลากมาวาง ส่วน Trigger เป็นปุ่มของคุณ หรือทั้งพื้นที่ก็ได้ openOnKeyDown เปิดหน้าต่างเลือกไฟล์ทันทีที่กด Enter / Space เหมือน drop container ของ Carbon

import { FileUploader } from '@purpurabog/ui/headless'; // ชื่อไฟล์ของการเลือกหรือการวางครั้งล่าสุด จาก context function Picked() { const { picked } = FileUploader.useFileUploaderContext(); return <p className="text-xs text-ink-muted">picked: {picked.join(', ')}</p>; } export default function Example() { return ( <FileUploader.Root multiple> <div className="grid gap-4"> <FileUploader.DropZone className="w-80 border border-dashed border-border-strong-1 data-drag-over:border-interactive data-drag-over:bg-layer-selected-1"> <FileUploader.Trigger openOnKeyDown className="w-full p-4 text-start text-sm text-link focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus"> ลากไฟล์มาวาง หรือคลิกเพื่อเลือก </FileUploader.Trigger> <FileUploader.Label hidden>แบบแปลน</FileUploader.Label> <FileUploader.Input hidden /> </FileUploader.DropZone> <Picked /> <Files /> </div> </FileUploader.Root> ); } // Files: แถวของไฟล์จาก "วิธีใช้" function Files() { const { files, remove } = FileUploader.useFileUploaderContext(); return ( <FileUploader.List className="grid w-80 gap-2"> {files.map((f) => ( <FileUploader.Item key={f.uuid} uuid={f.uuid} name={f.name} status={f.status} invalid={f.invalid} onDelete={remove} className="group bg-layer-1 text-sm text-ink data-invalid:outline-2 data-invalid:-outline-offset-2 data-invalid:outline-error" > <FileUploader.ItemRow className="flex min-h-12 items-center ps-4"> <span className="flex-1 truncate">{f.name}</span> <FileUploader.Remove aria-label={`ลบไฟล์ - ${f.name}`} className="hidden size-12 shrink-0 place-items-center hover:bg-layer-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus group-data-removable:grid" > × </FileUploader.Remove> </FileUploader.ItemRow> {f.errorSubject && <p role="alert" className="border-t border-hairline px-4 py-2 text-xs text-error-text">{f.errorSubject}</p>} </FileUploader.Item> ))} </FileUploader.List> ); }

prop render ของ Trigger ให้คุณ render ปุ่มของตัวเอง (เช่นปุ่มที่มี style) พร้อม props ที่มันจะใส่ให้ <button> ส่วน picked ใน context เก็บชื่อไฟล์ของการเลือกครั้งล่าสุด ถ้าต้องการให้ปุ่มแสดงชื่อไฟล์หรือ "3 ไฟล์"

จำกัดไฟล์

accept, maxFileSize, maxFiles และ maxTotalSize ทำให้ไฟล์ที่ไม่ผ่านเป็น invalid ไฟล์นั้นยังอยู่ในรายการแต่ไม่ถูกส่งไปกับฟอร์ม describeRejection ให้ข้อความ error ตามเหตุผล: 'invalid-type', 'too-large', 'too-many' หรือ 'total-too-large' ถ้าไม่ใส่ ไฟล์ที่ไม่ผ่านจะไม่มีข้อความ error

import { FileUploader } from '@purpurabog/ui/headless'; const button = 'h-10 w-fit bg-primary px-4 text-sm text-on-primary hover:bg-blue-hover focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus disabled:cursor-not-allowed disabled:bg-button-disabled disabled:text-on-color-disabled'; const describeRejection = (rejection: FileUploader.Rejection) => ({ errorSubject: rejection === 'invalid-type' ? 'รับเฉพาะรูป JPG หรือ PNG' : `ไฟล์ไม่ผ่านเงื่อนไข (${rejection})`, }); export default function Example() { return ( <FileUploader.Root accept=".jpg,.png" maxFileSize={2_000_000} maxFiles={2} multiple describeRejection={describeRejection}> <div className="grid gap-4"> <FileUploader.Group aria-label="รูปถ่าย"> <FileUploader.DropZone> <FileUploader.Trigger className={button}>เพิ่มรูป</FileUploader.Trigger> <FileUploader.Label hidden>รูปถ่าย</FileUploader.Label> <FileUploader.Input hidden /> </FileUploader.DropZone> </FileUploader.Group> <p className="text-xs text-ink-muted">JPG หรือ PNG ไม่เกินไฟล์ละ 2 MB สูงสุด 2 รูป</p> <Files /> </div> </FileUploader.Root> ); } // Files: แถวของไฟล์จาก "วิธีใช้" function Files() { const { files, remove } = FileUploader.useFileUploaderContext(); return ( <FileUploader.List className="grid w-80 gap-2"> {files.map((f) => ( <FileUploader.Item key={f.uuid} uuid={f.uuid} name={f.name} status={f.status} invalid={f.invalid} onDelete={remove} className="group bg-layer-1 text-sm text-ink data-invalid:outline-2 data-invalid:-outline-offset-2 data-invalid:outline-error" > <FileUploader.ItemRow className="flex min-h-12 items-center ps-4"> <span className="flex-1 truncate">{f.name}</span> <FileUploader.Remove aria-label={`ลบไฟล์ - ${f.name}`} className="hidden size-12 shrink-0 place-items-center hover:bg-layer-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus group-data-removable:grid" > × </FileUploader.Remove> </FileUploader.ItemRow> {f.errorSubject && <p role="alert" className="border-t border-hairline px-4 py-2 text-xs text-error-text">{f.errorSubject}</p>} </FileUploader.Item> ))} </FileUploader.List> ); }

accept กรองไฟล์ในหน้าต่างเลือกไฟล์ด้วย และไฟล์ที่ลากมาวางก็ถูกตรวจเช่นกัน

อัปโหลดไฟล์

เก็บรายการเองด้วย files และแทนที่ด้วยรายการใหม่เมื่อการอัปโหลดแต่ละไฟล์คืบหน้า ส่ง progress และ retryable ต่อให้แต่ละ Item ตัวอย่างนี้ไม่ได้ส่งไฟล์ไปไหน ใช้ timer แทนการอัปโหลด และไฟล์ที่ใหญ่กว่า 1 MB จะล้มเหลวหนึ่งครั้งให้ลองอัปโหลดใหม่

import { useEffect, useRef, useState } from 'react'; import { FileUploader } from '@purpurabog/ui/headless'; type FileItem = FileUploader.FileItem; const button = 'h-10 w-fit bg-primary px-4 text-sm text-on-primary hover:bg-blue-hover focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus disabled:cursor-not-allowed disabled:bg-button-disabled disabled:text-on-color-disabled'; const icon = 'size-12 shrink-0 place-items-center hover:bg-layer-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus'; // ไม่มีเซิร์ฟเวอร์จริง: การอัปโหลดจำลองขยับครั้งละ 10% และไฟล์ที่ใหญ่กว่า 1 MB ล้มเหลวที่ 60% ในครั้งแรก function tick(f: FileItem, retried: ReadonlySet<string>): FileItem { if (f.status !== 'uploading') return f; const progress = Math.min(100, (f.progress ?? 0) + 10); if (progress === 60 && f.file.size > 1_000_000 && !retried.has(f.uuid)) { return { ...f, status: 'edit', invalid: true, retryable: true, errorSubject: 'อัปโหลดไม่สำเร็จ', errorBody: 'ตรวจการเชื่อมต่อแล้วลองอีกครั้ง' }; } return progress === 100 ? { ...f, status: 'complete', progress } : { ...f, progress }; } function UploadList() { const { files, remove, retry } = FileUploader.useFileUploaderContext(); return ( <FileUploader.List className="grid w-80 gap-2"> {files.map((f) => ( <FileUploader.Item key={f.uuid} uuid={f.uuid} name={f.name} status={f.status} invalid={f.invalid} retryable={f.retryable} progress={f.progress} onDelete={remove} onRetry={retry} className="group bg-layer-1 text-sm text-ink data-invalid:outline-2 data-invalid:-outline-offset-2 data-invalid:outline-error" > <FileUploader.ItemRow className="flex min-h-12 items-center ps-4"> <span className="flex-1 truncate">{f.name}</span> {f.status === 'complete' && <span aria-hidden="true" className="grid size-12 place-items-center text-success">✓</span>} <FileUploader.Retry aria-label={`ลองอัปโหลดอีกครั้ง - ${f.name}`} className={`grid ${icon}`}>↻</FileUploader.Retry> <FileUploader.Remove aria-label={`ลบไฟล์ - ${f.name}`} className={`hidden group-data-removable:grid ${icon}`}>×</FileUploader.Remove> <FileUploader.ItemStatus className="sr-only">อัปโหลดแล้ว: {f.name}</FileUploader.ItemStatus> </FileUploader.ItemRow> {f.status === 'uploading' && ( <div role="progressbar" aria-label={`กำลังอัปโหลด ${f.name}`} aria-valuenow={f.progress} className="h-0.5 bg-border-subtle-1"> <div className="h-full bg-interactive" style={{ width: `${f.progress ?? 0}%` }} /> </div> )} {f.invalid && ( <div role="alert" className="border-t border-hairline px-4 py-2 text-xs"> <p className="text-error-text">{f.errorSubject}</p> <p className="text-ink-muted">{f.errorBody}</p> </div> )} </FileUploader.Item> ))} </FileUploader.List> ); } export default function Example() { const [files, setFiles] = useState<FileItem[]>([]); const [deleted, setDeleted] = useState(''); const retried = useRef(new Set<string>()); const update = (uuid: string, changes: Partial<FileItem>) => setFiles((list) => list.map((f) => (f.uuid === uuid ? { ...f, ...changes } : f))); const upload = (item: FileItem) => update(item.uuid, { status: 'uploading', progress: 0, invalid: false, retryable: false, errorSubject: undefined, errorBody: undefined }); // การอัปโหลดจำลองเดินต่อขณะที่มีไฟล์กำลังอัปโหลด ไฟล์ที่ถูกลบก็แค่หายจากรายการ const uploading = files.some((f) => f.status === 'uploading'); useEffect(() => { if (!uploading) return; const timer = setInterval(() => setFiles((list) => list.map((f) => tick(f, retried.current))), 300); return () => clearInterval(timer); }, [uploading]); return ( <FileUploader.Root multiple files={files} onChange={(change) => { setFiles(change.currentFiles); if (change.action === 'add') change.addedFiles.filter((f) => !f.invalid).forEach(upload); }} onRetry={(item) => { retried.current.add(item.uuid); upload(item); }} onDelete={({ deletedFile }) => setDeleted(`${deletedFile.name} (${deletedFile.status})`)} > <div className="grid gap-4"> <FileUploader.Group aria-label="ไฟล์แนบ"> <FileUploader.DropZone> <FileUploader.Trigger className={button}>เพิ่มไฟล์</FileUploader.Trigger> <FileUploader.Label hidden>ไฟล์แนบ</FileUploader.Label> <FileUploader.Input hidden /> </FileUploader.DropZone> </FileUploader.Group> <UploadList /> {deleted && <p className="text-xs text-ink-muted">onDelete: {deleted}</p>} </div> </FileUploader.Root> ); }
  • status ที่ Root กำหนดสถานะเริ่มต้นของไฟล์ใหม่ (ค่าเริ่มต้น edit)
  • item ที่ uploading และมี progress ลบได้ ซึ่งเท่ากับยกเลิกการอัปโหลด และ onDelete บอกให้คุณหยุดส่งไฟล์นั้น
  • onRetry ได้ยินเมื่อผู้ใช้ลองอัปโหลดที่ล้มเหลวอีกครั้ง (ไฟล์ที่ invalid และ retryable) ไฟล์ที่ไม่ผ่านเงื่อนไขลองใหม่ไม่ได้

สถานะของไฟล์

สถานะอยู่ใน data-status, data-invalid และ data-removable ของ Item และ Item ใช้เดี่ยว ๆ ได้ด้วย สำหรับไฟล์ที่อยู่บนเซิร์ฟเวอร์แล้ว มันเรียก onDelete และ onRetry พร้อม uuid (หรือ name ถ้าไม่ใส่ uuid) ส่วนการเอาออกจากหน้าเป็นหน้าที่ของคุณ

import { useEffect, useState } from 'react'; import { FileUploader } from '@purpurabog/ui/headless'; const icon = 'size-12 shrink-0 place-items-center hover:bg-layer-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus'; const name = 'รายงานตรวจสภาพ.pdf'; export default function Example() { // ไฟล์ที่อยู่บนเซิร์ฟเวอร์แล้ว: ล้มเหลว → กำลังอัปโหลด (ไม่มี progress จึงไม่มี ×) → เสร็จ const [stage, setStage] = useState<'failed' | 'uploading' | 'complete' | 'removed'>('failed'); useEffect(() => { if (stage !== 'uploading') return; const timer = setTimeout(() => setStage('complete'), 1500); return () => clearTimeout(timer); }, [stage]); if (stage === 'removed') { return ( <button type="button" onClick={() => setStage('failed')} className="text-sm text-link hover:underline focus-visible:outline-2 focus-visible:outline-focus"> ลบแล้ว ใส่คืน </button> ); } const failed = stage === 'failed'; return ( <div className="grid w-80 gap-2"> <FileUploader.Item name={name} status={failed ? 'edit' : stage} invalid={failed} retryable onDelete={() => setStage('removed')} onRetry={() => setStage('uploading')} className="group bg-layer-1 text-sm text-ink data-invalid:outline-2 data-invalid:-outline-offset-2 data-invalid:outline-error" > <FileUploader.ItemRow className="flex min-h-12 items-center ps-4"> <span className="flex-1 truncate">{name}</span> {failed && <span role="alert" className="pe-2 text-xs text-error-text">อัปโหลดไม่สำเร็จ</span>} {stage === 'complete' && <span aria-hidden="true" className="grid size-12 place-items-center text-success">✓</span>} <FileUploader.Retry aria-label={`ลองอัปโหลดอีกครั้ง - ${name}`} className={`grid ${icon}`}>↻</FileUploader.Retry> <FileUploader.Remove aria-label={`ลบไฟล์ - ${name}`} className={`hidden group-data-removable:grid ${icon}`}>×</FileUploader.Remove> <FileUploader.ItemStatus className="sr-only">อัปโหลดแล้ว: {name}</FileUploader.ItemStatus> </FileUploader.ItemRow> </FileUploader.Item> <p className="text-xs text-ink-muted">stage: {stage}</p> {stage === 'complete' && ( <button type="button" onClick={() => setStage('failed')} className="w-fit text-sm text-link hover:underline focus-visible:outline-2 focus-visible:outline-focus"> เริ่มใหม่ </button> )} </div> ); }
  • edit ลบได้ uploading ลบได้เฉพาะเมื่อมี progress ส่วน complete ลบไม่ได้ Remove ไม่ render อะไรในแถวที่ลบไม่ได้ และถูกปิดตาม item
  • Retry แสดงเฉพาะในแถวที่ invalid และ retryable
  • ItemStatus แสดง children เมื่อการอัปโหลดเปลี่ยนจาก uploading เป็น complete

ในฟอร์ม

import { useState } from 'react'; import { FileUploader } from '@purpurabog/ui/headless'; const button = 'h-10 w-fit bg-primary px-4 text-sm text-on-primary hover:bg-blue-hover focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus disabled:cursor-not-allowed disabled:bg-button-disabled disabled:text-on-color-disabled'; const onlyPdf = () => ({ errorSubject: 'รับเฉพาะไฟล์ PDF' }); export default function Example() { const [missing, setMissing] = useState(false); const [sent, setSent] = useState<string[]>(); return ( <form className="grid gap-4" onSubmit={(e) => { e.preventDefault(); setSent(new FormData(e.currentTarget).getAll('contract').map((v) => (typeof v === 'string' ? v : v.name))); }} onReset={() => { setSent(undefined); setMissing(false); }} > <FileUploader.Root name="contract" accept=".pdf" required requiredText="แนบสัญญาที่ลงนามแล้ว" describeRejection={onlyPdf} onChange={() => setMissing(false)}> <FileUploader.Group aria-label="สัญญาที่ลงนามแล้ว" className="grid gap-2"> <FileUploader.DropZone> <FileUploader.Trigger className={button}>เพิ่มไฟล์</FileUploader.Trigger> <FileUploader.Label hidden>สัญญาที่ลงนามแล้ว</FileUploader.Label> {/* เบราว์เซอร์ไม่แสดง bubble ให้แสดงข้อความของคุณเองเมื่อฟอร์มถูกกัน */} <FileUploader.Input hidden onInvalid={() => setMissing(true)} /> </FileUploader.DropZone> {missing && <p className="text-xs text-error-text">แนบสัญญาที่ลงนามแล้ว</p>} <FileUploader.FormValue /> </FileUploader.Group> <Files /> </FileUploader.Root> <div className="flex gap-px"> <button type="submit" className={button}>ส่ง</button> <button type="reset" className="h-10 bg-secondary px-4 text-sm text-on-primary hover:bg-secondary-hover focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus"> ล้าง </button> </div> {sent && <p className="text-xs text-ink-muted">ส่งแล้ว: {JSON.stringify(sent)}</p>} </form> ); } // Files: แถวของไฟล์จาก "วิธีใช้" function Files() { const { files, remove } = FileUploader.useFileUploaderContext(); return ( <FileUploader.List className="grid w-80 gap-2"> {files.map((f) => ( <FileUploader.Item key={f.uuid} uuid={f.uuid} name={f.name} status={f.status} invalid={f.invalid} onDelete={remove} className="group bg-layer-1 text-sm text-ink data-invalid:outline-2 data-invalid:-outline-offset-2 data-invalid:outline-error" > <FileUploader.ItemRow className="flex min-h-12 items-center ps-4"> <span className="flex-1 truncate">{f.name}</span> <FileUploader.Remove aria-label={`ลบไฟล์ - ${f.name}`} className="hidden size-12 shrink-0 place-items-center hover:bg-layer-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus group-data-removable:grid" > × </FileUploader.Remove> </FileUploader.ItemRow> {f.errorSubject && <p role="alert" className="border-t border-hairline px-4 py-2 text-xs text-error-text">{f.errorSubject}</p>} </FileUploader.Item> ))} </FileUploader.List> ); }
  • เมื่อมี name FormValue ส่งไฟล์ที่ผ่านเงื่อนไขทุกไฟล์ภายใต้ชื่อนั้น เหมือน <input type="file" multiple> ไฟล์ที่ไม่ผ่านไม่ถูกส่ง
  • required กันไม่ให้ส่งฟอร์มจนกว่าจะมีไฟล์ที่ผ่านเงื่อนไขอย่างน้อยหนึ่งไฟล์ โดยใช้ requiredText เป็นข้อความ (ถ้าไม่ใส่ จะใช้ข้อความจากคำแปลภาษาอังกฤษ "Add at least one file.") ไม่มี bubble ของเบราว์เซอร์ และโฟกัสไปที่ Trigger ตัวอย่างแสดงข้อความของตัวเองจาก onInvalid ของ Input
  • การ reset ฟอร์มล้างรายการแบบ uncontrolled disabled ปิด Trigger, DropZone, Input, FormValue และทุก Item ใน Root

ใส่ style

import { useState } from 'react'; import { FileUploader } from '@purpurabog/ui/headless'; function StyledFiles() { const { files, remove } = FileUploader.useFileUploaderContext(); return ( <FileUploader.List className="grid w-80 gap-2"> {files.map(({ uuid, name, status, invalid }) => ( <FileUploader.Item key={uuid} uuid={uuid} name={name} status={status} invalid={invalid} onDelete={remove} className="group flex min-h-12 items-center bg-layer-1 ps-4 text-sm text-ink data-invalid:outline-2 data-invalid:-outline-offset-2 data-invalid:outline-error"> <FileUploader.ItemRow className="flex flex-1 items-center"> <span className="flex-1 truncate">{name}</span> <FileUploader.Remove className="hidden size-12 place-items-center hover:bg-layer-hover-1 group-data-removable:grid" aria-label={`ลบไฟล์ - ${name}`}>×</FileUploader.Remove> </FileUploader.ItemRow> </FileUploader.Item> ))} </FileUploader.List> ); } export default function Example() { const [disabled, setDisabled] = useState(false); return ( <div className="grid gap-4"> <FileUploader.Root multiple accept=".pdf" disabled={disabled}> <FileUploader.DropZone className="w-80 border border-dashed border-border-strong-1 p-4 data-drag-over:border-interactive data-disabled:opacity-50"> <FileUploader.Trigger className="text-sm text-link focus-visible:outline-2 focus-visible:outline-focus disabled:cursor-not-allowed"> ลากไฟล์ PDF มาวาง หรือคลิก </FileUploader.Trigger> <FileUploader.Label hidden>เอกสาร</FileUploader.Label> <FileUploader.Input hidden /> </FileUploader.DropZone> <StyledFiles /> </FileUploader.Root> <label className="flex items-center gap-2 text-sm text-ink"> <input type="checkbox" checked={disabled} onChange={(e) => setDisabled(e.target.checked)} /> disabled </label> </div> ); } .drop-zone { border: 1px dashed #8d8d8d; } .drop-zone[data-drag-over] { border-color: #0f62fe; } .drop-zone[data-disabled] { opacity: 0.5; } [data-file-item][data-invalid] { outline: 2px solid #da1e28; } [data-file-item]:not([data-removable]) [data-file-remove] { display: none; }

ชิ้นส่วน

FileUploader.Root

ไม่ render element ใด ๆ แค่เก็บรายการ เงื่อนไขจำกัด และกฎของโฟกัส

Prop Type ค่าเริ่มต้น
accept string '' นามสกุลและ MIME type คั่นด้วยจุลภาค
multiple boolean false ถ้าไม่ใส่ การเลือกใหม่แทนที่รายการเดิม
maxFileSize number 0 ไบต์ ไฟล์ที่ใหญ่กว่าถูกเพิ่มเป็น invalid (0 = ไม่จำกัด)
maxFiles number 0 จำนวนไฟล์ที่ผ่านเงื่อนไขมากที่สุด ไฟล์ที่เกินเป็น invalid (0 = ไม่จำกัด)
maxTotalSize number 0 ขนาดรวมของไฟล์ที่ผ่านเงื่อนไขเป็นไบต์ (0 = ไม่จำกัด)
describeRejection (rejection: Rejection) => { errorSubject?: string; errorBody?: string } ข้อความ error ของไฟล์ที่ไม่ผ่าน
name string ชื่อที่ FormValue ใช้ส่ง
disabled boolean false
status 'edit' | 'uploading' | 'complete' 'edit' สถานะเริ่มต้นของไฟล์ใหม่
required boolean false ต้องมีไฟล์ที่ผ่านเงื่อนไขอย่างน้อยหนึ่งไฟล์ก่อนส่งฟอร์ม
requiredText string '' ข้อความ validity ขณะ required และยังไม่มีไฟล์ที่ผ่าน ถ้าว่างใช้ข้อความจากคำแปลภาษาอังกฤษ
files FileItem[] รายการแบบ controlled
defaultFiles FileItem[] []
onAdd (addedFiles: FileItem[]) => void ทันทีหลังเลือกหรือวางไฟล์
onChange (change: FileChange) => void ทุกครั้งที่รายการเปลี่ยน
onDelete ({ deletedFile, remainingFiles }) => void ไฟล์ถูกลบ
onRetry (retriedFile: FileItem) => void ผู้ใช้ลองอัปโหลดที่ล้มเหลวอีกครั้ง

FileUploader.Group

render <div role="group"> ตั้งชื่อด้วย aria-label หรือ aria-labelledby ที่ชี้ไปที่ titleId ใน context

Attribute เมื่อ
data-disabled disabled

FileUploader.Title

render <p> ที่มี id เป็น titleId ใน context

FileUploader.DropZone

render <div> ที่รับไฟล์ที่ลากมาวาง โฟลเดอร์ถูกข้าม

Attribute เมื่อ
data-drag-over มีไฟล์ลากอยู่เหนือมัน
data-disabled disabled

FileUploader.Trigger

render <button type="button"> ที่เปิดหน้าต่างเลือกไฟล์ ถูกปิดขณะ disabled และโฟกัสกลับมาที่มันเมื่อรายการใต้มันว่างลง

Prop Type ค่าเริ่มต้น
openOnKeyDown boolean false Enter / Space เปิดหน้าต่างตอนกดปุ่มลง
render (props) => ReactElement render ปุ่มของคุณเองด้วย props เหล่านี้
Attribute เมื่อ
data-drag-over มีไฟล์ลากอยู่เหนือพื้นที่วางไฟล์
data-disabled disabled

FileUploader.Label, FileUploader.Input

Label render <label> ของ Input ให้ซ่อนจากสายตา Input render <input type="file"> ที่ Trigger คลิก มี tabIndex={-1} ค่า multiple, accept และ disabled มาจาก Root ให้ซ่อนไว้

FileUploader.FormValue

เมื่อ Root มี name จะ render <input type="file" multiple> แบบซ่อนที่เก็บไฟล์ที่ผ่านเงื่อนไขทุกไฟล์ ถ้าไม่มี name จะไม่ render อะไร

FileUploader.List

render <div> ครอบ item โฟกัสย้ายภายในนี้เมื่อแถวหายไป

FileUploader.Item

render <div> ของไฟล์หนึ่งไฟล์

Prop Type ค่าเริ่มต้น
name string ชื่อไฟล์ (ต้องใส่)
uuid string name ใช้ระบุไฟล์ใน onDelete / onRetry
status 'edit' | 'uploading' | 'complete' 'edit'
invalid boolean false
retryable boolean false ในแถวที่ invalid: Retry แสดง
disabled boolean false เปิดด้วยเมื่อ Root ถูกปิด
progress number 0–100 ขณะอัปโหลด แล้ว Remove จะยกเลิกการอัปโหลด
onDelete (uuid: string) => void
onRetry (uuid: string) => void
Attribute เมื่อ
data-file-item เสมอ
data-status เสมอ: edit, uploading หรือ complete
data-invalid invalid
data-removable ลบไฟล์ได้: edit หรือ uploading ที่มี progress และไม่ disabled

FileUploader.ItemRow

render <div> ที่มี aria-disabled ขณะ item ถูกปิด โฟกัสจะมาอยู่ที่มันเมื่อปุ่มสุดท้ายของแถวหายไปพร้อมโฟกัส

FileUploader.Remove

render <button type="button"> ที่ลบไฟล์ (onDelete) หรือยกเลิกการอัปโหลด ถูกปิดตาม item ไม่ render อะไรในแถว complete หรือแถว uploading ที่ไม่มี progress

Attribute เมื่อ
data-file-remove เสมอ

FileUploader.Retry

render <button type="button"> ที่ลองอัปโหลดที่ล้มเหลวอีกครั้ง (onRetry) เฉพาะในแถวที่ invalid และ retryable ถูกปิดตาม item

Attribute เมื่อ
data-file-retry เสมอ

FileUploader.ItemStatus

render <span role="status"> ที่แสดง children เมื่ออัปโหลดเสร็จ

FileUploader.useFileUploaderContext() ใน Root ให้ files, picked, dragOver, titleId, remove, retry, browse และอื่น ๆ ส่วน FileUploader.useItemContext() ใน Item ให้ status, percent (ความคืบหน้าขณะอัปโหลด) และ finished

คีย์บอร์ดและ accessibility

ปุ่ม ผล
Enter หรือ Space ที่ Trigger เปิดหน้าต่างเลือกไฟล์
Enter หรือ Space ที่ Remove ลบไฟล์หรือยกเลิกการอัปโหลด โฟกัสย้ายไปที่ Remove ของแถวถัดไป ถ้าไม่มีไปแถวก่อนหน้า ถ้าไม่มีอีกไปที่ Trigger
Enter หรือ Space ที่ Retry เรียก onRetry เมื่อปุ่มหายไป โฟกัสอยู่ที่ Remove ของแถว ถ้าไม่มีอยู่ที่ตัวแถว

สิ่งที่ชิ้นส่วนจัดการให้:

  • role="group" และ aria-disabled ที่ Group และ file input ที่มีชื่อจาก Label และอยู่นอกลำดับ Tab
  • โฟกัสหลังแถวหรือปุ่ม retry หายไป จึงไม่ตกไปที่หน้า
  • required: validity ของฟอร์มโดยไม่มี bubble ของเบราว์เซอร์ และโฟกัสไปที่ Trigger เมื่อการส่งถูกกัน
  • live region แบบ polite ใน ItemStatus

สิ่งที่คุณต้องใส่เอง:

  • ชื่อของ Group และข้อความของ Label
  • aria-label ที่ Remove และ Retry แต่ละปุ่มพร้อมชื่อไฟล์: "ลบไฟล์ - contract.pdf", "ลองอัปโหลดอีกครั้ง - contract.pdf"
  • ข้อความ error (เป็น alert ด้วย role="alert") ข้อความของ ItemStatus และแถบความคืบหน้าถ้าต้องการแสดง