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

ปุ่มเลือกแบบแบ่งส่วน

Segmented

A small set of choices in one bar, such as Day, Month and Year. `dark` is for purple bands.

ใช้สลับมุมมองของข้อมูลชุดเดียวกัน ที่มีตัวเลือก 2 ถึง 5 ข้อ และเห็นได้ทั้งหมดพร้อมกัน เช่น วัน เดือน ปี ถ้าตัวเลือกเปลี่ยนเนื้อหาทั้งส่วน ใช้ Tabs ถ้ามีตัวเลือกมากกว่านั้น ใช้ Select

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

ตัวอย่าง

สลับมุมมอง

ส่งตัวเลือกเป็นอาร์เรย์ และเก็บค่าที่เลือกไว้ใน state ของหน้าจอ label ตั้งชื่อกลุ่มให้โปรแกรมอ่านหน้าจอ

const [value, setValue] = useState("month");

<Segmented
  label="มุมมอง"
  options={[
    { value: "day", label: "วัน" },
    { value: "month", label: "เดือน" },
    { value: "year", label: "ปี" },
  ]}
  value={value}
  onChange={setValue}
/>

บนแถบสีม่วง

dark ใช้บนพื้นสีม่วง เช่น แถบหัวของแดชบอร์ด

const [value, setValue] = useState("2569");

<Segmented
  dark
  label="ปีงบประมาณ"
  options={[
    { value: "2568", label: "ปีงบ 2568" },
    { value: "2569", label: "ปีงบ 2569" },
    { value: "2570", label: "ปีงบ 2570" },
  ]}
  value={value}
  onChange={setValue}
/>

ตัวเลือกต่างภาษา

lang บอกภาษาของป้ายที่ต่างจากหน้า เพื่อให้โปรแกรมอ่านหน้าจอออกเสียงถูก

const [value, setValue] = useState("all");

<Segmented
  label="แสดงคำขอ"
  options={[
    { value: "all", label: "ทั้งหมด" },
    { value: "mine", label: "My requests", lang: "en" },
  ]}
  value={value}
  onChange={setValue}
/>

props ทั้งหมด

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

Segmented props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
value* จำเป็นV—The value of the chosen option.
options* จำเป็นreadonly SegmentedOption<V>[]—The choices, in order. Two to five work best.
onChange* จำเป็น(next: V) => void—Called with the value of the option pressed.
label* จำเป็นstring—Names the group for screen readers.
darkbooleanfalseWhite on purple, for use on a purple band.
classNamestring—Extra layout utilities, such as a margin.

ชนิดข้อมูล

SegmentedOption

One choice in a Segmented bar.

SegmentedOption fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
value* จำเป็นV—The value reported to onChange.
label* จำเป็นReactNode—The text on the button, already in the screen language.
langstring—The language of the label, when it differs from the page, such as en for EN.

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

  • ปุ่มที่เลือกอยู่มี aria-pressed เป็น true แต่ละปุ่มกดได้ด้วย Tab แล้ว Enter หรือ Space
  • label ต้องมีเสมอ และอยู่ในภาษาของหน้าจอ
  • ป้ายของตัวเลือกสั้น คำเดียวหรือสองคำ

ซอร์สโค้ด: ui/src/segmented.tsx