สวิตช์
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}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| 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. |
| checked | checked | boolean | Whether it's on, when the screen keeps it. |
| onCheckedChange | setChecked | (next: boolean) => void | Hears each change. |
พร้อมคำแนะนำ
hint อธิบายผลของการตั้งค่าใต้สวิตช์
รายการที่ยกเลิกจะแสดงเป็นสีจาง
<Switch
label="แสดงรายการที่ยกเลิก"
hint="รายการที่ยกเลิกจะแสดงเป็นสีจาง"
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| 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. |
| hint | "รายการที่ยกเลิกจะแสดงเป็นสีจาง" | string | Help under the field, such as the format or a limit. |
| onCheckedChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: boolean) => void | Hears each change. |
ปิดใช้งาน
disabled ใช้เมื่อผู้ใช้เปลี่ยนไม่ได้ บอกเหตุผลใน hint
ผู้ดูแลระบบปิดไว้
<Switch label="ส่งข้อมูลให้ระบบภายนอก" disabled hint="ผู้ดูแลระบบปิดไว้" />| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| 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. |
| disabled | true | boolean | — |
| hint | "ผู้ดูแลระบบปิดไว้" | string | Help under the field, such as the format or a limit. |
| onCheckedChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: boolean) => void | Hears each change. |
props ทั้งหมด
ตัวอย่างด้านบนใช้ 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. |
| 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. |
| checked | boolean | — | Whether it's on, when the screen keeps it. |
| defaultChecked | boolean | false | Whether 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