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

ชิป

Chip

A choice or filter the user turns on and off.

Chip คือตัวเลือกหรือตัวกรองที่ผู้ใช้กดเปิดปิดได้ RemovableChip คือค่าที่ผู้ใช้เพิ่มเองและลบได้ ถ้าแค่แสดงสถานะและกดไม่ได้ ใช้ Badge ถ้าเลือกได้ทีละหนึ่งจากหลายตัว ใช้ Segmented ถ้าเป็นตัวกรองที่ใช้อยู่ในตาราง ใช้ FilterChips ถ้าผู้ใช้พิมพ์ค่าเพิ่มเอง ใช้ TagInput

import { Chip, RemovableChip } from "@bsru/ui";

ตัวอย่าง

ชิปเปิดปิด

ส่ง pressed จาก state ของหน้าจอ และรับค่าใหม่จาก onPressedChange ชิปที่เปิดอยู่มีพื้นสีเขียว

const [pressed, setPressed] = useState(true);

<Chip pressed={pressed} onPressedChange={setPressed}>รออนุมัติ</Chip>

กลุ่มตัวกรองจากอาร์เรย์

เก็บค่าที่เลือกเป็นอาร์เรย์ใน state แล้วสร้างชิปหนึ่งตัวต่อหนึ่งตัวเลือก ตั้งชื่อกลุ่มด้วย role group และ aria-label

const FILTERS = [
  { value: "pending", label: "รออนุมัติ" },
  { value: "month", label: "เดือนนี้" },
  { value: "mine", label: "หน่วยงานของฉัน" },
];
const [selected, setSelected] = useState(["pending"]);

<div role="group" aria-label="ตัวกรองด่วน" className="flex flex-wrap gap-2">
  {FILTERS.map((f) => (
    <Chip
      key={f.value}
      pressed={selected.includes(f.value)}
      onPressedChange={(on) =>
        setSelected(on ? [...selected, f.value] : selected.filter((v) => v !== f.value))
      }
    >
      {f.label}
    </Chip>
  ))}
</div>

ชิปที่ลบได้

ส่ง label และ onRemove หน้าจอเป็นผู้นำค่าออกจากรายการของตัวเอง ปุ่มลบอ่านว่า "ลบ" ตามด้วยค่า เปลี่ยนคำได้ด้วย removeLabel

ครุภัณฑ์เร่งด่วนปีงบ 2570
const [tags, setTags] = useState(["ครุภัณฑ์", "เร่งด่วน", "ปีงบ 2570"]);

<div className="flex flex-wrap gap-2">
  {tags.map((tag, i) => (
    <RemovableChip
      key={tag}
      label={tag}
      onRemove={() => setTags(tags.filter((_, j) => j !== i))}
    />
  ))}
</div>

props ทั้งหมด

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

Chip

A choice or filter the user turns on and off.

Chip props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
pressed* จำเป็นboolean—Whether the chip is on. The screen keeps this in its state.
onPressedChange* จำเป็น(next: boolean) => void—Called with the new value when the chip is pressed.
children* จำเป็นReactNode—The text, already in the screen language.

RemovableChip

A value the user added, with a button that removes it. `removeLabel` prefixes the label for screen readers.

RemovableChip props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
label* จำเป็นstring—The value shown on the chip.
onRemove* จำเป็น() => void—Called when the remove button is pressed. The screen takes the value out of its list.
removeLabelstring—Read before the label on the remove button. Defaults to "ลบ " or "Remove ".

ข้อควรปฏิบัติ

  • Chip เป็นปุ่มที่มี aria-pressed กด Enter หรือ Space เพื่อเปิดหรือปิด
  • ปุ่มลบของ RemovableChip กดด้วย Tab แล้ว Enter ได้ ถ้าชิปหายไป ให้ย้ายโฟกัสไปยังชิปถัดไปหรือช่องที่เกี่ยวข้อง
  • ข้อความในชิปส่งมาในภาษาของหน้าจออยู่แล้ว และสั้นพอให้อ่านได้ในบรรทัดเดียว

ซอร์สโค้ด: ui/src/status.tsx