ชิป
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>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| pressed* จำเป็น | pressed | boolean | Whether the chip is on. The screen keeps this in its state. |
| onPressedChange* จำเป็น | setPressed | (next: boolean) => void | Called with the new value when the chip is pressed. |
กลุ่มตัวกรองจากอาร์เรย์
เก็บค่าที่เลือกเป็นอาร์เรย์ใน 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>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| filters | รายการ 3 รายการ[
{ value: "pending", label: "รออนุมัติ" },
{ value: "month", label: "เดือนนี้" },
{ value: "mine", label: "หน่วยงานของฉัน" },
] | array | — |
| selected | ["pending"] | array | — |
| onPressedChange* จำเป็น | (on) => setSelected(on ? [...selected, f.value] : selected.filter((v) => v !== f.value)) | (next: boolean) => void | Called with the new value when the chip is pressed. |
ชิปที่ลบได้
ส่ง label และ onRemove หน้าจอเป็นผู้นำค่าออกจากรายการของตัวเอง ปุ่มลบอ่านว่า "ลบ" ตามด้วยค่า เปลี่ยนคำได้ด้วย removeLabel
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>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| tags | ["ครุภัณฑ์", "เร่งด่วน", "ปีงบ 2570"] | array | — |
| onPressedChange* จำเป็น | ไม่ได้ส่งในตัวอย่างนี้ | (next: boolean) => void | Called with the new value when the chip is pressed. |
props ทั้งหมด
ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้
Chip
A choice or filter the user turns on and off.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| 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.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| 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. |
| removeLabel | string | — | Read before the label on the remove button. Defaults to "ลบ " or "Remove ". |
ข้อควรปฏิบัติ
- Chip เป็นปุ่มที่มี aria-pressed กด Enter หรือ Space เพื่อเปิดหรือปิด
- ปุ่มลบของ RemovableChip กดด้วย Tab แล้ว Enter ได้ ถ้าชิปหายไป ให้ย้ายโฟกัสไปยังชิปถัดไปหรือช่องที่เกี่ยวข้อง
- ข้อความในชิปส่งมาในภาษาของหน้าจออยู่แล้ว และสั้นพอให้อ่านได้ในบรรทัดเดียว
ซอร์สโค้ด: ui/src/status.tsx