ช่องทำเครื่องหมาย
Checkbox
A checkbox with its label beside it. It can also show the mixed state, such as a header box when only some rows are chosen. Without label, give it an aria-label, as in a table row.
ใช้ให้ผู้ใช้เปิดหรือปิดตัวเลือกที่เป็นอิสระต่อกัน หรือเลือกหลายแถวในตาราง ส่งข้อความเป็น label แล้วช่องจะวางข้อความไว้ข้าง ๆ ให้กดได้ทั้งแถว ถ้าการเปิดปิดมีผลทันที เช่น การตั้งค่า ใช้ Switch ถ้าเลือกได้ทีละหนึ่งจากหลายตัวเลือก ใช้ RadioCards, Segmented หรือ Select
import { Checkbox } from "@bsru/ui";ตัวอย่าง
ช่องเดียวพร้อมข้อความ
label เป็นข้อความข้างช่อง กดที่ข้อความได้ และเป็นชื่อของช่องสำหรับโปรแกรมอ่านหน้าจอ หน้าจอเก็บค่าไว้ใน state
const [checked, setChecked] = useState(true);
<Checkbox
label="แนบใบเสนอราคาแล้ว"
checked={checked}
onChange={(e) => setChecked(e.target.checked)}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "แนบใบเสนอราคาแล้ว" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| checked | checked | boolean | — |
| onChange | (e) => setChecked(e.target.checked) | function | — |
มีคำอธิบาย
hint อธิบายผลของการเลือกไว้ใต้ช่อง defaultChecked ให้ช่องเก็บค่าเอง แล้วอ่านจากฟอร์มตอนส่งด้วย name
ส่งอีเมลเมื่อคำขอได้รับอนุมัติหรือถูกตีกลับ
<Checkbox
label="แจ้งเตือนทางอีเมล"
hint="ส่งอีเมลเมื่อคำขอได้รับอนุมัติหรือถูกตีกลับ"
name="notifyByEmail"
defaultChecked
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "แจ้งเตือนทางอีเมล" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| hint | "ส่งอีเมลเมื่อคำขอได้รับอนุมัติหรือถูกตีกลับ" | string | Help under the field, such as the format or a limit. |
| name | "notifyByEmail" | string | — |
| defaultChecked | true | boolean | — |
ข้อผิดพลาด
error บอกว่าผิดอะไรและแก้อย่างไร แสดงหลังกดส่งแล้วพบปัญหา ตัวอย่างนี้ล้างข้อความเมื่อติ๊กแล้ว
ติ๊กยืนยันก่อนส่งคำขอ
const [checked, setChecked] = useState(false);
<Checkbox
label="ยืนยันว่าข้อมูลในคำขอถูกต้อง"
checked={checked}
onChange={(e) => setChecked(e.target.checked)}
error={checked ? undefined : "ติ๊กยืนยันก่อนส่งคำขอ"}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "ยืนยันว่าข้อมูลในคำขอถูกต้อง" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| checked | checked | boolean | — |
| onChange | (e) => setChecked(e.target.checked) | function | — |
| error | checked ? undefined : "ติ๊กยืนยันก่อนส่งคำขอ" | string | What's wrong and how to fix it, shown under the field. It marks the field invalid. |
เลือกบางรายการ
indeterminate แสดงสถานะผสม เช่น ช่องเลือกทั้งหมดเมื่อเลือกเพียงบางแถว คำนวณจากรายการที่เลือกใน state ของหน้าจอ
const [picked, setPicked] = useState(["PR-2570-0002"]);
const all = picked.length === rows.length;
<Checkbox
label="เลือกทั้งหมด"
checked={all}
indeterminate={picked.length > 0 && !all}
onChange={(e) => setPicked(e.target.checked ? rows : [])}
/>
<div className="pl-6">
{rows.map((no) => (
<Checkbox
key={no}
label={no}
checked={picked.includes(no)}
onChange={(e) => setPicked(e.target.checked ? [...picked, no] : picked.filter((p) => p !== no))}
/>
))}
</div>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| rows | ["PR-2570-0001", "PR-2570-0002", "PR-2570-0003"] | array | — |
| picked | ["PR-2570-0002"] | array | — |
ใช้ไม่ได้
ข้อความข้างช่องจางลงให้เอง บอกเหตุผลใน hint
เปิดใช้เมื่อวงเงินเกิน 500,000.00 บาท
<Checkbox
label="ส่งให้ผู้อำนวยการกองอนุมัติ"
hint="เปิดใช้เมื่อวงเงินเกิน 500,000.00 บาท"
disabled
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "ส่งให้ผู้อำนวยการกองอนุมัติ" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| hint | "เปิดใช้เมื่อวงเงินเกิน 500,000.00 บาท" | string | Help under the field, such as the format or a limit. |
| disabled | true | boolean | — |
props ทั้งหมด
ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| label | string | — | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| hint | string | — | Help under the field, such as the format or a limit. |
| error | string | — | What's wrong and how to fix it, shown under the field. It marks the field invalid. |
| required | boolean | — | Marks the label with a red star, and the control as required. |
| indeterminate | boolean | false | Shows the mixed state, when only some of the items it stands for are chosen. |
รับ attribute ของ element HTML ต้นแบบได้ทั้งหมดด้วย เช่น id, name, required และ disabled
ข้อควรปฏิบัติ
- ทุกช่องต้องมีชื่อ: ส่ง label หรือส่ง aria-label เมื่อไม่มีข้อความให้เห็น เช่น ช่องเลือกแถวในตาราง
- รับ attribute ของ input ได้ทุกตัว เช่น checked onChange name disabled
- แป้นพิมพ์: Space เปิดหรือปิด สถานะผสมเปลี่ยนเป็นเลือกเมื่อกด
ซอร์สโค้ด: ui/src/inputs.tsx