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

สวิตช์

Switch

An on or off setting that takes effect at once, such as Email me when approved. The label sits on the left and the switch on the right. Use Checkbox instead for a choice that's saved with a form.

ใช้สำหรับการตั้งค่าที่เปิดหรือปิดแล้วมีผลทันที เช่น แจ้งอีเมลเมื่ออนุมัติ ป้ายอยู่ทางซ้ายและสวิตช์อยู่ทางขวา ถ้าตัวเลือกบันทึกพร้อมฟอร์มเมื่อกดบันทึก ใช้ Checkbox

import { Switch } from "@bsru/ui";

ตัวอย่าง

หน้าจอเก็บค่าเอง

ส่ง checked และรับค่าใหม่จาก onCheckedChange แล้วบันทึกการตั้งค่าทันที

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

<Switch
  label="แจ้งอีเมลเมื่ออนุมัติ"
  checked={checked}
  onCheckedChange={setChecked}
/>

พร้อมคำแนะนำ

hint อธิบายผลของการตั้งค่าใต้สวิตช์

รายการที่ยกเลิกจะแสดงเป็นสีจาง

<Switch
  label="แสดงรายการที่ยกเลิก"
  hint="รายการที่ยกเลิกจะแสดงเป็นสีจาง"
/>

ปิดใช้งาน

disabled ใช้เมื่อผู้ใช้เปลี่ยนไม่ได้ บอกเหตุผลใน hint

ผู้ดูแลระบบปิดไว้

<Switch label="ส่งข้อมูลให้ระบบภายนอก" disabled hint="ผู้ดูแลระบบปิดไว้" />

props ทั้งหมด

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

Switch props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
label* จำเป็นstring—The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. What the setting does, already in the screen language.
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.
checkedboolean—Whether it's on, when the screen keeps it.
defaultCheckedbooleanfalseWhether it starts on, when the switch keeps it.
onCheckedChange(next: boolean) => void—Hears each change.

รับ attribute ของ element HTML ต้นแบบได้ทั้งหมดด้วย เช่น id, name, required และ disabled

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

  • สวิตช์มี role switch โปรแกรมอ่านหน้าจออ่านว่าเปิดหรือปิด
  • ป้ายบอกสิ่งที่เปิด ไม่ใช่คำถาม เช่น แจ้งอีเมลเมื่ออนุมัติ

ซอร์สโค้ด: ui/src/choice-inputs.tsx