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

ช่องทำเครื่องหมาย

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)}
/>

มีคำอธิบาย

hint อธิบายผลของการเลือกไว้ใต้ช่อง defaultChecked ให้ช่องเก็บค่าเอง แล้วอ่านจากฟอร์มตอนส่งด้วย name

ส่งอีเมลเมื่อคำขอได้รับอนุมัติหรือถูกตีกลับ

<Checkbox
  label="แจ้งเตือนทางอีเมล"
  hint="ส่งอีเมลเมื่อคำขอได้รับอนุมัติหรือถูกตีกลับ"
  name="notifyByEmail"
  defaultChecked
/>

ข้อผิดพลาด

error บอกว่าผิดอะไรและแก้อย่างไร แสดงหลังกดส่งแล้วพบปัญหา ตัวอย่างนี้ล้างข้อความเมื่อติ๊กแล้ว

ติ๊กยืนยันก่อนส่งคำขอ

const [checked, setChecked] = useState(false);

<Checkbox
  label="ยืนยันว่าข้อมูลในคำขอถูกต้อง"
  checked={checked}
  onChange={(e) => setChecked(e.target.checked)}
  error={checked ? undefined : "ติ๊กยืนยันก่อนส่งคำขอ"}
/>

เลือกบางรายการ

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>

ใช้ไม่ได้

ข้อความข้างช่องจางลงให้เอง บอกเหตุผลใน hint

เปิดใช้เมื่อวงเงินเกิน 500,000.00 บาท

<Checkbox
  label="ส่งให้ผู้อำนวยการกองอนุมัติ"
  hint="เปิดใช้เมื่อวงเงินเกิน 500,000.00 บาท"
  disabled
/>

props ทั้งหมด

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

Checkbox props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
labelstring—The visible label, already in the screen language. Every field needs one, unless the screen labels it another way.
hintstring—Help under the field, such as the format or a limit.
errorstring—What's wrong and how to fix it, shown under the field. It marks the field invalid.
requiredboolean—Marks the label with a red star, and the control as required.
indeterminatebooleanfalseShows 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