ปุ่มเลือกแบบแบ่งส่วน
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}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label* จำเป็น | "มุมมอง" | string | Names the group for screen readers. |
| options* จำเป็น | รายการ 3 รายการ[
{ value: "day", label: "วัน" },
{ value: "month", label: "เดือน" },
{ value: "year", label: "ปี" },
] | readonly SegmentedOption<V>[] | The choices, in order. Two to five work best. |
| value* จำเป็น | value | V | The value of the chosen option. |
| onChange* จำเป็น | setValue | (next: V) => void | Called with the value of the option pressed. |
บนแถบสีม่วง
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}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| dark | true | boolean | White on purple, for use on a purple band. |
| label* จำเป็น | "ปีงบประมาณ" | string | Names the group for screen readers. |
| options* จำเป็น | รายการ 3 รายการ[
{ value: "2568", label: "ปีงบ 2568" },
{ value: "2569", label: "ปีงบ 2569" },
{ value: "2570", label: "ปีงบ 2570" },
] | readonly SegmentedOption<V>[] | The choices, in order. Two to five work best. |
| value* จำเป็น | value | V | The value of the chosen option. |
| onChange* จำเป็น | setValue | (next: V) => void | Called with the value of the option pressed. |
ตัวเลือกต่างภาษา
lang บอกภาษาของป้ายที่ต่างจากหน้า เพื่อให้โปรแกรมอ่านหน้าจอออกเสียงถูก
const [value, setValue] = useState("all");
<Segmented
label="แสดงคำขอ"
options={[
{ value: "all", label: "ทั้งหมด" },
{ value: "mine", label: "My requests", lang: "en" },
]}
value={value}
onChange={setValue}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label* จำเป็น | "แสดงคำขอ" | string | Names the group for screen readers. |
| options* จำเป็น | รายการ 2 รายการ[
{ value: "all", label: "ทั้งหมด" },
{ value: "mine", label: "My requests", lang: "en" },
] | readonly SegmentedOption<V>[] | The choices, in order. Two to five work best. |
| value* จำเป็น | value | V | The value of the chosen option. |
| onChange* จำเป็น | setValue | (next: V) => void | Called with the value of the option pressed. |
props ทั้งหมด
ตัวอย่างด้านบนใช้ 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. |
| dark | boolean | false | White on purple, for use on a purple band. |
| className | string | — | Extra layout utilities, such as a margin. |
ชนิดข้อมูล
SegmentedOption
One choice in a Segmented bar.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| value* จำเป็น | V | — | The value reported to onChange. |
| label* จำเป็น | ReactNode | — | The text on the button, already in the screen language. |
| lang | string | — | 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