แถบจัดการหลายรายการ
BulkBar
A bar for actions on the chosen rows, with the count and Clear selection. Hidden when nothing is chosen.
ใช้เหนือตารางข้อมูลเมื่อผู้ใช้เลือกหลายแถวแล้วทำคำสั่งเดียวกันกับทุกแถว เช่น ส่งออก ส่งจำนวนที่เลือกและปุ่มคำสั่ง แถบซ่อนเองเมื่อไม่มีแถวที่เลือก คำสั่งสำหรับรายการเดียวให้อยู่ในแถวหรือในหน้ารายละเอียด ส่งปุ่มคำสั่งเป็น Button ขนาด sm ตอนนี้หน้าจอยังต้องเขียนตารางและช่องเลือกเอง
import { BulkBar } from "@bsru/ui";ตัวอย่าง
เลือกแถวในตาราง
หน้าจอเก็บเลขที่ของแถวที่เลือกไว้ใน Set แล้วส่ง count ลองเลือกหรือยกเลิก แถบจะซ่อนเมื่อไม่มีแถวที่เลือก
| เลขที่ | รายการ | วงเงิน (บาท) | |
|---|---|---|---|
| PR-2570-0003 | วัสดุสำนักงาน | 19,500.00 | |
| PR-2570-0004 | จ้างซ่อมเครื่องปรับอากาศ | 43,500.00 | |
| PR-2570-0005 | โต๊ะและเก้าอี้ห้องเรียน | 88,000.00 |
const [picked, setPicked] = useState(new Set(["PR-2570-0003", "PR-2570-0005"]));
<BulkBar count={picked.size} onClear={() => setPicked(new Set())}>
<Button label="ส่งออก" icon={Download} size="sm" onClick={exportPicked} />
</BulkBar>
// Today the screen still writes the table and its select boxes. A data-driven DataTable is coming next.
<table className="data-table">
<thead>
<tr>
<th className="w-10">
<Checkbox aria-label="เลือกทุกแถวในหน้านี้" checked={all} indeterminate={some && !all} onChange={pickAll} />
</th>
...
</tr>
</thead>
<tbody>
{rows.map((r) => (
<tr key={r.no} data-selected={picked.has(r.no)}>
<td>
<Checkbox aria-label={`เลือก ${r.no}`} checked={picked.has(r.no)} onChange={(e) => pick(r.no, e.target.checked)} />
</td>
...
</tr>
))}
</tbody>
</table>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| count* จำเป็น | 2 | number | How many rows are chosen, across all pages. The bar hides at 0. |
| picked | ["PR-2570-0003", "PR-2570-0005"] | array | — |
| rows | รายการ 3 รายการ[
{ no: "PR-2570-0003", item: "วัสดุสำนักงาน", amount: 19500 },
{
no: "PR-2570-0004",
item: "จ้างซ่อมเครื่องปรับอากาศ",
amount: 43500,
},
{ no: "PR-2570-0005", item: "โต๊ะและเก้าอี้ห้องเรียน", amount: 88000 },
] | array | — |
| onClear* จำเป็น | () => setPicked(new Set()) | () => void | Called by Clear selection. Empty the selection. |
หลายคำสั่ง
ส่ง Button ขนาด sm เป็น children เรียงตามลำดับที่ใช้บ่อย ยกเลิกการเลือกอยู่ท้ายเสมอ ตัวอย่างนี้ไม่เก็บ state จึงกดแล้วไม่เปลี่ยน
<BulkBar count={3} onClear={handleClear}>
<Component label="ส่งออก" icon={Download} size="sm" />
<Component label="อนุมัติ" tone="primary" icon={Check} size="sm" />
</BulkBar>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| count* จำเป็น | 3 | number | How many rows are chosen, across all pages. The bar hides at 0. |
| onClear* จำเป็น | handleClear | () => void | Called by Clear selection. Empty the selection. |
props ทั้งหมด
ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| count* จำเป็น | number | — | How many rows are chosen, across all pages. The bar hides at 0. |
| onClear* จำเป็น | () => void | — | Called by Clear selection. Empty the selection. |
| children* จำเป็น | ReactNode | — | The actions, such as Export. |
| className | string | "flex flex-wrap items-center gap-3 rounded-control soft-green bg-green-100 px-3 py-2" | Classes for the bar, such as a top margin. |
ข้อควรปฏิบัติ
- count คือจำนวนที่เลือกทั้งหมดทุกหน้า การเปลี่ยนหน้าหรือการเรียงไม่ล้างการเลือก
- ช่องเลือกทุกแถวที่หัวตารางเลือกเฉพาะแถวในหน้านี้ และแสดงสถานะผสมเมื่อเลือกบางแถว
- ทุกช่องเลือกต้องมี aria-label ที่บอกว่าเลือกแถวไหน เช่น เลือก PR-2570-0003
- คำสั่งที่ย้อนกลับไม่ได้ เช่น ลบ ต้องถามยืนยันใน Dialog ก่อน
ซอร์สโค้ด: ui/src/data-table.tsx