รายการตัวเลือก
Select
A dropdown list on the ARIA listbox pattern. Arrows move, Enter or Space chooses, Esc closes, Home and End jump, and typing jumps to the first option that starts with what was typed. With `multiple`, choices show as chips and the list stays open while you pick. The list opens upward when there is no room below. With layout cards, the options show as a grid of cards, and all four arrows move through it.
ใช้เมื่อมีตัวเลือกไม่เกินประมาณ 15 รายการ ถ้ามากกว่านั้นหรือต้องค้นหา ใช้ Combobox ส่งตัวเลือกเป็นอาร์เรย์ ส่ง label hint และ error เป็นข้อความ และเก็บค่าที่เลือกไว้ใน state ของหน้าจอ
import { Select } from "@bsru/ui";ตัวอย่าง
เลือกรายการเดียว แบ่งกลุ่ม
ตัวเลือกที่มี group เดียวกันติดกันจะอยู่ใต้หัวข้อเดียว note แสดงเป็นบรรทัดที่สอง value เป็น null เมื่อยังไม่เลือก
รหัสงบประมาณ
แสดงงบคงเหลือใต้แต่ละรหัส
const [value, setValue] = useState<string | null>(null);
<Select
label="รหัสงบประมาณ"
hint="แสดงงบคงเหลือใต้แต่ละรหัส"
placeholder="เลือกรหัสงบประมาณ"
options={[
{
value: "a",
label: "BG-2570-ก-001",
note: "เงินรายได้ คงเหลือ 350,000.00",
group: "หน่วยงาน ก",
},
{
value: "b",
label: "BG-2570-ก-002",
note: "เงินงบประมาณแผ่นดิน คงเหลือ 1,200,000.00",
group: "หน่วยงาน ก",
},
{
value: "c",
label: "BG-2570-ข-001",
note: "เงินรายได้ คงเหลือ 480,000.00",
group: "หน่วยงาน ข",
},
{
value: "d",
label: "BG-2570-ข-002",
note: "เงินกองทุน คงเหลือ 95,000.00",
group: "หน่วยงาน ข",
},
]}
value={value}
onChange={setValue}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "รหัสงบประมาณ" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| hint | "แสดงงบคงเหลือใต้แต่ละรหัส" | string | Help under the field, such as the format or a limit. |
| placeholder* จำเป็น | "เลือกรหัสงบประมาณ" | string | Shown when nothing is chosen. |
| options* จำเป็น | รายการ 4 รายการ[
{
value: "a",
label: "BG-2570-ก-001",
note: "เงินรายได้ คงเหลือ 350,000.00",
group: "หน่วยงาน ก",
},
{
value: "b",
label: "BG-2570-ก-002",
note: "เงินงบประมาณแผ่นดิน คงเหลือ 1,200,000.00",
group: "หน่วยงาน ก",
},
{
value: "c",
label: "BG-2570-ข-001",
note: "เงินรายได้ คงเหลือ 480,000.00",
group: "หน่วยงาน ข",
},
{
value: "d",
label: "BG-2570-ข-002",
note: "เงินกองทุน คงเหลือ 95,000.00",
group: "หน่วยงาน ข",
},
] | readonly SelectOption<V>[] | — |
| value* จำเป็น | value | V | null | — |
| onChange* จำเป็น | setValue | (next: V) => void | — |
เลือกได้หลายรายการ
ส่ง multiple แล้ว value เป็นอาร์เรย์ รายการที่เลือกแสดงเป็นชิป
สถานะ
const [value, setValue] = useState<string[]>(["pending"]);
<Select
multiple
label="สถานะ"
placeholder="ทุกสถานะ"
options={[
{ value: "draft", label: "ร่าง" },
{ value: "pending", label: "รออนุมัติ" },
{ value: "approved", label: "อนุมัติแล้ว" },
{ value: "rejected", label: "ไม่อนุมัติ" },
]}
value={value}
onChange={setValue}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| multiple | true | false | — |
| label | "สถานะ" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| placeholder* จำเป็น | "ทุกสถานะ" | string | Shown when nothing is chosen. |
| options* จำเป็น | รายการ 4 รายการ[
{ value: "draft", label: "ร่าง" },
{ value: "pending", label: "รออนุมัติ" },
{ value: "approved", label: "อนุมัติแล้ว" },
{ value: "rejected", label: "ไม่อนุมัติ" },
] | readonly SelectOption<V>[] | — |
| value* จำเป็น | value | V | null | — |
| onChange* จำเป็น | setValue | (next: V) => void | — |
ตัวเลือกมีไอคอน
ช่องทางแจ้งเตือน
const [value, setValue] = useState<string | null>("mail");
<Select
label="ช่องทางแจ้งเตือน"
placeholder="เลือกช่องทาง"
options={[
{ value: "mail", label: "อีเมล", icon: Mail },
{ value: "sms", label: "ข้อความ SMS", icon: Smartphone },
{ value: "push", label: "แจ้งเตือนในแอป", icon: Bell },
]}
value={value}
onChange={setValue}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "ช่องทางแจ้งเตือน" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| placeholder* จำเป็น | "เลือกช่องทาง" | string | Shown when nothing is chosen. |
| options* จำเป็น | รายการ 3 รายการ[
{ value: "mail", label: "อีเมล", icon: Mail },
{ value: "sms", label: "ข้อความ SMS", icon: Smartphone },
{ value: "push", label: "แจ้งเตือนในแอป", icon: Bell },
] | readonly SelectOption<V>[] | — |
| value* จำเป็น | value | V | null | — |
| onChange* จำเป็น | setValue | (next: V) => void | — |
ตัวเลือกเป็นการ์ดมีรูป
layout cards แสดงตัวเลือกเป็นการ์ดเรียงเป็นตาราง มีรูปจาก image อยู่ด้านบน menuWidth lg ให้รายการกว้าง 42rem แม้ช่องจะแคบ และไม่กว้างเกินจอ ถ้าด้านขวาไม่พอ รายการจะชิดขอบขวาของช่องแทน ลูกศรทั้งสี่ทิศเลื่อนไปมาในตาราง
รูปแบบการจัดห้อง
const [value, setValue] = useState<string | null>("classroom");
<Select
label="รูปแบบการจัดห้อง"
placeholder="เลือกรูปแบบ"
options={[
{
value: "boardroom",
label: "ประชุมโต๊ะยาว",
note: "14 ที่นั่ง",
image: "/examples/rooms/boardroom.svg",
},
{
value: "classroom",
label: "ห้องเรียน",
note: "24 ที่นั่ง",
image: "/examples/rooms/classroom.svg",
},
{
value: "theatre",
label: "โรงละคร",
note: "60 ที่นั่ง",
image: "/examples/rooms/theatre.svg",
},
{
value: "ushape",
label: "ตัวยู",
note: "15 ที่นั่ง",
image: "/examples/rooms/ushape.svg",
},
{
value: "banquet",
label: "โต๊ะจัดเลี้ยง",
note: "24 ที่นั่ง",
image: "/examples/rooms/banquet.svg",
},
{
value: "workshop",
label: "กลุ่มปฏิบัติการ",
note: "18 ที่นั่ง",
image: "/examples/rooms/workshop.svg",
},
]}
layout="cards"
menuWidth="lg"
value={value}
onChange={setValue}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "รูปแบบการจัดห้อง" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| placeholder* จำเป็น | "เลือกรูปแบบ" | string | Shown when nothing is chosen. |
| options* จำเป็น | รายการ 6 รายการ[
{
value: "boardroom",
label: "ประชุมโต๊ะยาว",
note: "14 ที่นั่ง",
image: "/examples/rooms/boardroom.svg",
},
{
value: "classroom",
label: "ห้องเรียน",
note: "24 ที่นั่ง",
image: "/examples/rooms/classroom.svg",
},
{
value: "theatre",
label: "โรงละคร",
note: "60 ที่นั่ง",
image: "/examples/rooms/theatre.svg",
},
{
value: "ushape",
label: "ตัวยู",
note: "15 ที่นั่ง",
image: "/examples/rooms/ushape.svg",
},
{
value: "banquet",
label: "โต๊ะจัดเลี้ยง",
note: "24 ที่นั่ง",
image: "/examples/rooms/banquet.svg",
},
{
value: "workshop",
label: "กลุ่มปฏิบัติการ",
note: "18 ที่นั่ง",
image: "/examples/rooms/workshop.svg",
},
] | readonly SelectOption<V>[] | — |
| layout | "cards" | OptionLayout ค่าที่ใช้ได้: "list""cards" | How the options show: rows, or cards in a grid with each option's image on top. Cards suit a menuWidth wider than the field. |
| menuWidth | "lg" | MenuWidth ค่าที่ใช้ได้: "trigger""sm""md""lg""xl" | How wide the list is. trigger matches the field. The others may be wider than the field, but never wider than the screen. |
| value* จำเป็น | value | V | null | — |
| onChange* จำเป็น | setValue | (next: V) => void | — |
รายการมีรูปเล็ก
ในรายการแบบแถว image แสดงเป็นรูปสี่เหลี่ยมเล็กหน้าข้อความ แทนไอคอน menuWidth sm ให้รายการกว้าง 20rem
รูปแบบการจัดห้อง
const [value, setValue] = useState<string | null>(null);
<Select
label="รูปแบบการจัดห้อง"
placeholder="เลือกรูปแบบ"
options={[
{
value: "boardroom",
label: "ประชุมโต๊ะยาว",
note: "14 ที่นั่ง",
image: "/examples/rooms/boardroom.svg",
},
{
value: "classroom",
label: "ห้องเรียน",
note: "24 ที่นั่ง",
image: "/examples/rooms/classroom.svg",
},
{
value: "theatre",
label: "โรงละคร",
note: "60 ที่นั่ง",
image: "/examples/rooms/theatre.svg",
},
{
value: "ushape",
label: "ตัวยู",
note: "15 ที่นั่ง",
image: "/examples/rooms/ushape.svg",
},
{
value: "banquet",
label: "โต๊ะจัดเลี้ยง",
note: "24 ที่นั่ง",
image: "/examples/rooms/banquet.svg",
},
{
value: "workshop",
label: "กลุ่มปฏิบัติการ",
note: "18 ที่นั่ง",
image: "/examples/rooms/workshop.svg",
},
]}
menuWidth="sm"
value={value}
onChange={setValue}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "รูปแบบการจัดห้อง" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| placeholder* จำเป็น | "เลือกรูปแบบ" | string | Shown when nothing is chosen. |
| options* จำเป็น | รายการ 6 รายการ[
{
value: "boardroom",
label: "ประชุมโต๊ะยาว",
note: "14 ที่นั่ง",
image: "/examples/rooms/boardroom.svg",
},
{
value: "classroom",
label: "ห้องเรียน",
note: "24 ที่นั่ง",
image: "/examples/rooms/classroom.svg",
},
{
value: "theatre",
label: "โรงละคร",
note: "60 ที่นั่ง",
image: "/examples/rooms/theatre.svg",
},
{
value: "ushape",
label: "ตัวยู",
note: "15 ที่นั่ง",
image: "/examples/rooms/ushape.svg",
},
{
value: "banquet",
label: "โต๊ะจัดเลี้ยง",
note: "24 ที่นั่ง",
image: "/examples/rooms/banquet.svg",
},
{
value: "workshop",
label: "กลุ่มปฏิบัติการ",
note: "18 ที่นั่ง",
image: "/examples/rooms/workshop.svg",
},
] | readonly SelectOption<V>[] | — |
| menuWidth | "sm" | MenuWidth ค่าที่ใช้ได้: "trigger""sm""md""lg""xl" | How wide the list is. trigger matches the field. The others may be wider than the field, but never wider than the screen. |
| value* จำเป็น | value | V | null | — |
| onChange* จำเป็น | setValue | (next: V) => void | — |
ช่องบังคับที่ยังไม่ได้เลือก
required ใส่ดาวสีแดงที่ป้าย error แสดงหลังกดบันทึกโดยยังไม่ได้เลือก ตัวอย่างนี้ล้างข้อความเมื่อเลือกแล้ว
รหัสงบประมาณ
เลือกรหัสงบประมาณ
const [value, setValue] = useState<string | null>(null);
<Select
label="รหัสงบประมาณ"
required
placeholder="เลือกรหัสงบประมาณ"
options={[
{
value: "a",
label: "BG-2570-ก-001",
note: "เงินรายได้ คงเหลือ 350,000.00",
group: "หน่วยงาน ก",
},
{
value: "b",
label: "BG-2570-ก-002",
note: "เงินงบประมาณแผ่นดิน คงเหลือ 1,200,000.00",
group: "หน่วยงาน ก",
},
{
value: "c",
label: "BG-2570-ข-001",
note: "เงินรายได้ คงเหลือ 480,000.00",
group: "หน่วยงาน ข",
},
{
value: "d",
label: "BG-2570-ข-002",
note: "เงินกองทุน คงเหลือ 95,000.00",
group: "หน่วยงาน ข",
},
]}
value={value}
onChange={setValue}
error={value === null ? "เลือกรหัสงบประมาณ" : undefined}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "รหัสงบประมาณ" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| required | true | boolean | Marks the label with a red star, and the control as required. |
| placeholder* จำเป็น | "เลือกรหัสงบประมาณ" | string | Shown when nothing is chosen. |
| options* จำเป็น | รายการ 4 รายการ[
{
value: "a",
label: "BG-2570-ก-001",
note: "เงินรายได้ คงเหลือ 350,000.00",
group: "หน่วยงาน ก",
},
{
value: "b",
label: "BG-2570-ก-002",
note: "เงินงบประมาณแผ่นดิน คงเหลือ 1,200,000.00",
group: "หน่วยงาน ก",
},
{
value: "c",
label: "BG-2570-ข-001",
note: "เงินรายได้ คงเหลือ 480,000.00",
group: "หน่วยงาน ข",
},
{
value: "d",
label: "BG-2570-ข-002",
note: "เงินกองทุน คงเหลือ 95,000.00",
group: "หน่วยงาน ข",
},
] | readonly SelectOption<V>[] | — |
| value* จำเป็น | value | V | null | — |
| onChange* จำเป็น | setValue | (next: V) => void | — |
| error | value === null ? "เลือกรหัสงบประมาณ" : undefined | string | What's wrong and how to fix it, shown under the field. It marks the field invalid. |
props ทั้งหมด
ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| options* จำเป็น | readonly SelectOption<V>[] | — | — |
| placeholder* จำเป็น | string | — | Shown when nothing is chosen. |
| labelledBy | string | — | The id of a visible label the screen wrote. Use label instead, unless the label is elsewhere. |
| className | string | — | — |
| layout | OptionLayout ค่าที่ใช้ได้: "list""cards" | — | How the options show: rows, or cards in a grid with each option's image on top. Cards suit a menuWidth wider than the field. |
| menuWidth | MenuWidth ค่าที่ใช้ได้: "trigger""sm""md""lg""xl" | — | How wide the list is. trigger matches the field. The others may be wider than the field, but never wider than the screen. |
| hideLabel | boolean | — | Keeps the label for screen readers only, with the placeholder naming the field on screen, such as a filter in a DataTable's filter bar. Pass the label as usual. |
| label | string | — | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| 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. |
เมื่อ multiple: false
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| multiple | false | — | — |
| value* จำเป็น | V | null | — | — |
| onChange* จำเป็น | (next: V) => void | — | — |
เมื่อ multiple: true
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| multiple* จำเป็น | true | — | — |
| value* จำเป็น | readonly V[] | — | — |
| onChange* จำเป็น | (next: V[]) => void | — | — |
ชนิดข้อมูล
SelectOption
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| value* จำเป็น | V | — | — |
| label* จำเป็น | string | — | The text shown, already in the screen language. Typing jumps to an option by the start of this text. |
| note | string | — | A second line, such as the balance left on a budget code. |
| group | string | — | Options with the same group in a row sit under one heading. |
| icon | LucideIcon | — | — |
| image | string | — | A picture's address, such as a product photo or a room plan. It shows in place of the icon: a small square in a list, and across the top of a card. The label names the option, so the picture needs no text of its own. |
OptionLayout
How a dropdown shows its options: rows, or cards in a grid, each with its picture on top.
type OptionLayout = "list" | "cards";MenuWidth
How wide a dropdown's list is. trigger matches the field. The others are fixed widths that may be wider than the field: sm 20rem, md 28rem, lg 42rem and xl 56rem, never wider than the screen. A list wider than the room to the right of the field lines up with the field's right edge instead.
type MenuWidth = "trigger" | "sm" | "md" | "lg" | "xl";ข้อควรปฏิบัติ
- ส่ง label เสมอ ป้ายเป็นชื่อของปุ่มเลือกให้เอง ใช้ labelledBy เฉพาะเมื่อป้ายอยู่ที่อื่นบนหน้าจอ
- ส่ง label ของตัวเลือกในภาษาของหน้าจออยู่แล้ว
- image เป็นที่อยู่ของรูป รูปไม่ต้องมีข้อความแทนรูป เพราะ label เป็นชื่อของตัวเลือกอยู่แล้ว ใช้การ์ดเมื่อรูปช่วยให้เลือกได้ง่ายขึ้น เช่น ผังห้องหรือรุ่นอุปกรณ์ และให้ menuWidth กว้างพอสำหรับสองถึงห้าคอลัมน์
- แป้นพิมพ์: ลูกศรเลื่อน Enter หรือ Space เลือก Esc ปิด Home และ End ไปต้นและท้าย พิมพ์เพื่อกระโดดไปยังตัวเลือก ในแบบการ์ด ลูกศรขึ้นลงเลื่อนทีละแถว ซ้ายขวาเลื่อนทีละใบ
ซอร์สโค้ด: ui/src/select.tsx