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

ตัวกรองที่ใช้อยู่

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 แล้วแปลงเป็นชิป ลองลบทีละชิปหรือกดล้างทั้งหมด เมื่อไม่มีตัวกรองแถบนี้จะซ่อนเอง

รออนุมัติหน่วยงาน กบริษัท ตัวอย่าง จำกัด1 ต.ค. 2569 - 30 พ.ย. 256910,000.00 - 200,000.00
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([])}
/>

ในพื้นที่แคบ

ชิปขึ้นบรรทัดใหม่เมื่อไม่พอ เช่น บนมือถือหรือในแผงด้านข้าง ปุ่มล้างทั้งหมดอยู่ท้ายเสมอ

อนุมัติแล้วห้างหุ้นส่วนจำกัด จำลองการค้าBG-2570-ข-001 เงินรายได้
<FilterChips
  chips={[
    { key: "status-approved", label: "อนุมัติแล้ว", onRemove: removeStatus },
    { key: "vendor", label: "ห้างหุ้นส่วนจำกัด จำลองการค้า", onRemove: removeVendor },
    { key: "budget", label: "BG-2570-ข-001 เงินรายได้", onRemove: removeBudget },
  ]}
  onClearAll={clearAll}
/>

props ทั้งหมด

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

FilterChips 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.
classNamestring"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.

FilterChip fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
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