ตัวกรองที่ใช้อยู่
FilterChips
The filters in use, each a chip with a remove button, then Clear all. Hidden when no filter is in use.
ใช้ใต้ช่องค้นหาของตาราง เพื่อแสดงตัวกรองที่ใช้อยู่ให้เห็นเสมอ แต่ละชิปลบตัวกรองของตัวเองได้ และมีปุ่มล้างทั้งหมด ส่งรายการตัวกรองที่จัดรูปแบบข้อความแล้ว ถ้าผู้ใช้ต้องกดเลือกหรือยกเลิกตัวเลือกเอง ใช้ Chip หรือ Select แบบหลายรายการ
import { FilterChips } from "@bsru/ui";ตัวอย่าง
ตัวกรองที่ใช้อยู่
หน้าจอเก็บตัวกรองที่ใช้ไว้ใน state แล้วแปลงเป็นชิป ลองลบทีละชิปหรือกดล้างทั้งหมด เมื่อไม่มีตัวกรองแถบนี้จะซ่อนเอง
const [filters, setFilters] = useState([
{ key: "status-pending", label: "รออนุมัติ" },
{ key: "unit-a", label: "หน่วยงาน ก" },
{ key: "vendor", label: "บริษัท ตัวอย่าง จำกัด" },
{ key: "date", label: "1 ต.ค. 2569 - 30 พ.ย. 2569" },
{ key: "amount", label: "10,000.00 - 200,000.00" },
]);
<FilterChips
chips={filters.map((f) => ({
...f,
onRemove: () => setFilters(filters.filter((x) => x.key !== f.key)),
}))}
onClearAll={() => setFilters([])}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| chips* จำเป็น | รายการ 5 รายการ[
{ key: "status-pending", label: "รออนุมัติ" },
{ key: "unit-a", label: "หน่วยงาน ก" },
{ key: "vendor", label: "บริษัท ตัวอย่าง จำกัด" },
{ key: "date", label: "1 ต.ค. 2569 - 30 พ.ย. 2569" },
{ key: "amount", label: "10,000.00 - 200,000.00" },
] | readonly FilterChip[] | The filters in use, in the order they appear in the criteria. |
| onClearAll* จำเป็น | () => setFilters([]) | () => void | Called by Clear all. Remove every filter, but keep the search text. |
ในพื้นที่แคบ
ชิปขึ้นบรรทัดใหม่เมื่อไม่พอ เช่น บนมือถือหรือในแผงด้านข้าง ปุ่มล้างทั้งหมดอยู่ท้ายเสมอ
<FilterChips
chips={[
{ key: "status-approved", label: "อนุมัติแล้ว", onRemove: removeStatus },
{ key: "vendor", label: "ห้างหุ้นส่วนจำกัด จำลองการค้า", onRemove: removeVendor },
{ key: "budget", label: "BG-2570-ข-001 เงินรายได้", onRemove: removeBudget },
]}
onClearAll={clearAll}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| chips* จำเป็น | รายการ 3 รายการ[
{ key: "status-approved", label: "อนุมัติแล้ว", onRemove: () => {} },
{
key: "vendor",
label: "ห้างหุ้นส่วนจำกัด จำลองการค้า",
onRemove: () => {},
},
{
key: "budget",
label: "BG-2570-ข-001 เงินรายได้",
onRemove: () => {},
},
] | readonly FilterChip[] | The filters in use, in the order they appear in the criteria. |
| onClearAll* จำเป็น | handleClearAll | () => void | Called by Clear all. Remove every filter, but keep the search text. |
props ทั้งหมด
ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| chips* จำเป็น | readonly FilterChip[] | — | The filters in use, in the order they appear in the criteria. |
| onClearAll* จำเป็น | () => void | — | Called by Clear all. Remove every filter, but keep the search text. |
| className | string | "flex flex-wrap items-center gap-2" | Classes for the row of chips, such as a top margin. |
ชนิดข้อมูล
FilterChip
One filter in use, shown as a chip.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| key* จำเป็น | string | — | Unique among the chips, such as status-pending. |
| label* จำเป็น | string | — | What the filter is, already in the screen language and formatted, such as a date range. |
| onRemove* จำเป็น | () => void | — | Called when the chip's remove button is pressed. Remove the filter and go back to page 1. |
ข้อควรปฏิบัติ
- ชิปแสดงเฉพาะตัวกรองที่กดใช้แล้ว ไม่ใช่ค่าที่กำลังแก้ในแผงเงื่อนไข
- label ต้องอ่านเข้าใจได้โดยลำพัง เช่น ช่วงวันที่และช่วงจำนวนเงินที่จัดรูปแบบแล้ว จำนวนเงินใช้ money()
- เมื่อลบตัวกรอง ให้กลับไปหน้า 1 ล้างทั้งหมดลบตัวกรองทุกตัว แต่คงคำค้นหาไว้
- ปุ่มลบของแต่ละชิปมีชื่อ ลบเงื่อนไข ตามด้วย label ให้โปรแกรมอ่านหน้าจอ
ซอร์สโค้ด: ui/src/data-table.tsx