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

ตัวเลือกแบบการ์ด

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

คอลัมน์เดียว ตัวเลือกที่ใช้ไม่ได้ และข้อผิดพลาด

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="เลือกวิธีชำระเงิน"
/>

props ทั้งหมด

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

RadioCards 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.
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.
options* จำเป็นreadonly RadioCardOption<V>[]—The choices, in order.
valueV | null—The chosen value, or null for none, when the screen keeps it.
defaultValueV | nullnullThe value chosen at first, when the group keeps it.
onValueChange(next: V) => void—Hears the chosen value.
namestring—The radio group's name for a form. One is made when it's left out.
columns1 | 2 | 33Cards per row on wide screens. Phones always show one column.

ชนิดข้อมูล

RadioCardOption

One choice in RadioCards.

RadioCardOption fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
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.
detailstring—A second line, such as 10 days left.
disabledboolean—Can't be chosen, such as a leave type with no days left.

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

  • กลุ่มเป็น fieldset และ label เป็น legend ลูกศรเลื่อนระหว่างตัวเลือก
  • ทั้งการ์ดกดได้ ตัวเลือกที่เลือกมีขอบสีเขียวหนา

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