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

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

Segmented

A small set of choices in one bar, such as Day, Month and Year. `dark` is for the hero band in the accent colour.

ใช้สลับมุมมองของข้อมูลชุดเดียวกัน ที่มีตัวเลือก 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}
/>

มีจำนวนในแต่ละตัวเลือก

count แสดงจำนวนรายการของตัวเลือกต่อจากป้าย เช่น ตัวกรองสถานะบนแถบตัวกรองของ DataTable ที่บอกว่าแต่ละสถานะมีกี่รายการ

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

<Segmented
  label="สถานะ"
  options={[
    { value: "all", label: "ทั้งหมด", count: 35 },
    { value: "pending", label: "รออนุมัติ", count: 15 },
    { value: "approved", label: "อนุมัติแล้ว", count: 12 },
    { value: "rejected", label: "ไม่อนุมัติ", count: 8 },
  ]}
  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 the accent colour, for use on the hero 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.
countnumber—How many records the choice has, shown after the label, such as 35 after ทั้งหมด in a table's filter bar.

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

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

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