ตัวเลือกแบบการ์ด
RadioCards
One choice from a few, shown as cards so each can carry a detail line, such as a leave type and the days left. Use it for two to six choices that should all be seen. For more, use Select.
ใช้เลือกหนึ่งตัวเลือกจาก 2 ถึง 6 ข้อ ที่แต่ละข้อมีรายละเอียดประกอบ เช่น ประเภทการลาและวันคงเหลือ ทุกตัวเลือกเห็นพร้อมกัน ถ้ามีมากกว่านั้น ใช้ Select ถ้าเป็นการสลับมุมมอง ใช้ Segmented
import { RadioCards } from "@bsru/ui";ตัวอย่าง
ประเภทการลา
ส่งตัวเลือกพร้อม detail และเก็บค่าที่เลือกใน state ของหน้าจอ บนมือถือการ์ดเรียงเป็นคอลัมน์เดียว
const [value, setValue] = useState<Leave | null>("vacation");
<RadioCards
label="ประเภทการลา"
options={[
{ value: "vacation", label: "ลาพักผ่อน", detail: "คงเหลือ 10 วัน" },
{ value: "sick", label: "ลาป่วย", detail: "คงเหลือ 30 วัน" },
{ value: "personal", label: "ลากิจ", detail: "คงเหลือ 6 วัน" },
]}
value={value}
onValueChange={setValue}
required
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label* จำเป็น | "ประเภทการลา" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. The question, shown as the group's legend. |
| options* จำเป็น | รายการ 3 รายการ[
{ value: "vacation", label: "ลาพักผ่อน", detail: "คงเหลือ 10 วัน" },
{ value: "sick", label: "ลาป่วย", detail: "คงเหลือ 30 วัน" },
{ value: "personal", label: "ลากิจ", detail: "คงเหลือ 6 วัน" },
] | readonly RadioCardOption<V>[] | The choices, in order. |
| value | value | V | null | The chosen value, or null for none, when the screen keeps it. |
| onValueChange | setValue | (next: V) => void | Hears the chosen value. |
| required | true | boolean | Marks the label with a red star, and the control as required. |
คอลัมน์เดียว ตัวเลือกที่ใช้ไม่ได้ และข้อผิดพลาด
columns เป็น 1 สำหรับพื้นที่แคบ disabled ปิดตัวเลือกที่ใช้ไม่ได้และบอกเหตุผลใน detail error แสดงใต้กลุ่ม
<RadioCards
label="วิธีชำระเงิน"
options={[
{ value: "qr", label: "QR Code", detail: "ชำระผ่านแอปธนาคาร" },
{
value: "transfer",
label: "โอนเงิน",
detail: "แนบหลักฐานภายใน 3 วัน",
},
{
value: "cash",
label: "เงินสด",
detail: "ปิดรับชั่วคราว",
disabled: true,
},
]}
columns={1}
error="เลือกวิธีชำระเงิน"
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label* จำเป็น | "วิธีชำระเงิน" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. The question, shown as the group's legend. |
| options* จำเป็น | รายการ 3 รายการ[
{ value: "qr", label: "QR Code", detail: "ชำระผ่านแอปธนาคาร" },
{
value: "transfer",
label: "โอนเงิน",
detail: "แนบหลักฐานภายใน 3 วัน",
},
{
value: "cash",
label: "เงินสด",
detail: "ปิดรับชั่วคราว",
disabled: true,
},
] | readonly RadioCardOption<V>[] | The choices, in order. |
| columns | 1 | 1 | 2 | 3 ค่าที่ใช้ได้: 123 | Cards per row on wide screens. Phones always show one column. |
| error | "เลือกวิธีชำระเงิน" | string | What's wrong and how to fix it, shown under the field. It marks the field invalid. |
| onValueChange | ไม่ได้ส่งในตัวอย่างนี้ | (next: V) => void | Hears the chosen value. |
props ทั้งหมด
ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| label* จำเป็น | string | — | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. The question, shown as the group's legend. |
| 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. |
| options* จำเป็น | readonly RadioCardOption<V>[] | — | The choices, in order. |
| value | V | null | — | The chosen value, or null for none, when the screen keeps it. |
| defaultValue | V | null | null | The value chosen at first, when the group keeps it. |
| onValueChange | (next: V) => void | — | Hears the chosen value. |
| name | string | — | The radio group's name for a form. One is made when it's left out. |
| columns | 1 | 2 | 3 | 3 | Cards per row on wide screens. Phones always show one column. |
ชนิดข้อมูล
RadioCardOption
One choice in RadioCards.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| value* จำเป็น | V | — | What the choice means to the screen, such as a leave type's code. |
| label* จำเป็น | string | — | The choice's name, already in the screen language. |
| detail | string | — | A second line, such as 10 days left. |
| disabled | boolean | — | Can't be chosen, such as a leave type with no days left. |
ข้อควรปฏิบัติ
- กลุ่มเป็น fieldset และ label เป็น legend ลูกศรเลื่อนระหว่างตัวเลือก
- ทั้งการ์ดกดได้ ตัวเลือกที่เลือกมีขอบสีเขียวหนา
ซอร์สโค้ด: ui/src/choice-inputs.tsx