รายการตัวเลือก
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.
ใช้เมื่อมีตัวเลือกไม่เกินประมาณ 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 | — |
ช่องบังคับที่ยังไม่ได้เลือก
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 | — | — |
| 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 | — | — |
ข้อควรปฏิบัติ
- ส่ง label เสมอ ป้ายเป็นชื่อของปุ่มเลือกให้เอง ใช้ labelledBy เฉพาะเมื่อป้ายอยู่ที่อื่นบนหน้าจอ
- ส่ง label ของตัวเลือกในภาษาของหน้าจออยู่แล้ว
- แป้นพิมพ์: ลูกศรเลื่อน Enter หรือ Space เลือก Esc ปิด Home และ End ไปต้นและท้าย พิมพ์เพื่อกระโดดไปยังตัวเลือก
ซอร์สโค้ด: ui/src/select.tsx