ค้นหาอัตโนมัติ
Combobox
A searchable field on the ARIA combobox pattern, for long lists such as vendors or budget codes. It shows at most 8 matches, highlights what matched, and says what to try when nothing does. Typing clears the choice, so onChange gets null until an item is picked again. With layout cards, matches show as a grid of cards, and the up and down arrows move by row.
ใช้กับรายการยาวที่ต้องค้นหา เช่น ผู้ค้าหรือรหัสงบประมาณ ค้นได้ทั้งรหัส ชื่อไทย และชื่ออังกฤษ ถ้ามีตัวเลือกไม่เกินประมาณ 15 รายการ ใช้ Select ส่งรายการเป็นอาร์เรย์ของ { code, th, en } ส่ง label hint และ error เป็นข้อความ และเก็บรายการที่เลือกไว้ใน state ของหน้าจอ
import { Combobox } from "@bsru/ui";ตัวอย่าง
ค้นหาผู้ค้า
value เป็นรายการที่เลือกทั้งก้อน หรือ null เมื่อยังไม่เลือก การพิมพ์จะล้างค่าที่เลือกไว้ onChange จึงได้ null จนกว่าจะเลือกใหม่
ลองพิมพ์ ทดสอบ หรือ VD-0003
const [vendor, setVendor] = useState<ComboItem | null>(null);
<Combobox
label="ผู้ค้า"
hint="ลองพิมพ์ ทดสอบ หรือ VD-0003"
placeholder="พิมพ์ชื่อผู้ค้าหรือรหัส"
items={[
{
code: "VD-0001",
th: "บริษัท ตัวอย่าง จำกัด",
en: "Sample Co., Ltd.",
},
{
code: "VD-0002",
th: "บริษัท ทดสอบครุภัณฑ์ จำกัด",
en: "Test Equipment Co., Ltd.",
},
{
code: "VD-0003",
th: "ห้างหุ้นส่วนจำกัด จำลองการค้า",
en: "Mock Trading Ltd. Partnership",
},
]}
value={vendor}
onChange={setVendor}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "ผู้ค้า" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| hint | "ลองพิมพ์ ทดสอบ หรือ VD-0003" | string | Help under the field, such as the format or a limit. |
| placeholder* จำเป็น | "พิมพ์ชื่อผู้ค้าหรือรหัส" | string | Hint text in the empty field, such as what people can search by. |
| items* จำเป็น | รายการ 3 รายการ[
{
code: "VD-0001",
th: "บริษัท ตัวอย่าง จำกัด",
en: "Sample Co., Ltd.",
},
{
code: "VD-0002",
th: "บริษัท ทดสอบครุภัณฑ์ จำกัด",
en: "Test Equipment Co., Ltd.",
},
{
code: "VD-0003",
th: "ห้างหุ้นส่วนจำกัด จำลองการค้า",
en: "Mock Trading Ltd. Partnership",
},
] | readonly T[] | Every item people can choose from. The component filters them as people type. |
| value* จำเป็น | vendor | T | null | The chosen item, or null when nothing is chosen. The screen keeps it in state. |
| onChange* จำเป็น | setVendor | (next: T | null) => void | Called with the picked item, or with null when people type or clear the field. |
มีค่าที่เลือกไว้แล้ว
ส่งรายการเริ่มต้นเป็น value ช่องแสดงชื่อในภาษาของหน้าจอ และปุ่มล้างใช้ได้ทันที
ลองพิมพ์ ครุภัณฑ์ หรือ ข-00
const [code, setCode] = useState<ComboItem | null>({
code: "BG-2570-ข-002",
th: "ทุนวิจัย เงินกองทุน",
en: "Research grant, fund",
});
<Combobox
label="รหัสงบประมาณ"
hint="ลองพิมพ์ ครุภัณฑ์ หรือ ข-00"
placeholder="พิมพ์รหัสหรือชื่อรายการ"
items={[
{
code: "BG-2570-ก-001",
th: "ค่าครุภัณฑ์ เงินรายได้",
en: "Equipment, revenue",
},
{
code: "BG-2570-ก-002",
th: "ค่าใช้สอย เงินงบประมาณแผ่นดิน",
en: "Operations, state budget",
},
{
code: "BG-2570-ก-003",
th: "ค่าวัสดุ เงินรายได้",
en: "Supplies, revenue",
},
{
code: "BG-2570-ข-001",
th: "ค่าครุภัณฑ์ เงินรายได้",
en: "Equipment, revenue",
},
{
code: "BG-2570-ข-002",
th: "ทุนวิจัย เงินกองทุน",
en: "Research grant, fund",
},
{
code: "BG-2570-ข-003",
th: "ค่าสาธารณูปโภค เงินงบประมาณแผ่นดิน",
en: "Utilities, state budget",
},
]}
value={code}
onChange={setCode}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "รหัสงบประมาณ" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| hint | "ลองพิมพ์ ครุภัณฑ์ หรือ ข-00" | string | Help under the field, such as the format or a limit. |
| placeholder* จำเป็น | "พิมพ์รหัสหรือชื่อรายการ" | string | Hint text in the empty field, such as what people can search by. |
| items* จำเป็น | รายการ 6 รายการ[
{
code: "BG-2570-ก-001",
th: "ค่าครุภัณฑ์ เงินรายได้",
en: "Equipment, revenue",
},
{
code: "BG-2570-ก-002",
th: "ค่าใช้สอย เงินงบประมาณแผ่นดิน",
en: "Operations, state budget",
},
{
code: "BG-2570-ก-003",
th: "ค่าวัสดุ เงินรายได้",
en: "Supplies, revenue",
},
{
code: "BG-2570-ข-001",
th: "ค่าครุภัณฑ์ เงินรายได้",
en: "Equipment, revenue",
},
{
code: "BG-2570-ข-002",
th: "ทุนวิจัย เงินกองทุน",
en: "Research grant, fund",
},
{
code: "BG-2570-ข-003",
th: "ค่าสาธารณูปโภค เงินงบประมาณแผ่นดิน",
en: "Utilities, state budget",
},
] | readonly T[] | Every item people can choose from. The component filters them as people type. |
| value* จำเป็น | code | T | null | The chosen item, or null when nothing is chosen. The screen keeps it in state. |
| onChange* จำเป็น | setCode | (next: T | null) => void | Called with the picked item, or with null when people type or clear the field. |
รายการมีโลโก้
image ของแต่ละรายการแสดงเป็นรูปเล็กหน้าชื่อ menuWidth md ให้รายการกว้าง 28rem แม้ช่องจะแคบ ชื่อยาวจึงไม่ต้องตัดบรรทัดมาก
const [vendor, setVendor] = useState<ComboItem | null>(null);
<Combobox
label="ผู้ค้า"
placeholder="พิมพ์ชื่อผู้ค้าหรือรหัส"
items={[
{
code: "VD-0001",
th: "บริษัท ตัวอย่าง จำกัด",
en: "Sample Co., Ltd.",
image: "/examples/vendors/vd-0001.svg",
},
{
code: "VD-0002",
th: "บริษัท ทดสอบครุภัณฑ์ จำกัด",
en: "Test Equipment Co., Ltd.",
image: "/examples/vendors/vd-0002.svg",
},
{
code: "VD-0003",
th: "ห้างหุ้นส่วนจำกัด จำลองการค้า",
en: "Mock Trading Ltd. Partnership",
image: "/examples/vendors/vd-0003.svg",
},
{
code: "VD-0004",
th: "บริษัท สมมุติวัสดุ จำกัด",
en: "Pretend Supplies Co., Ltd.",
image: "/examples/vendors/vd-0004.svg",
},
{
code: "VD-0005",
th: "บริษัท ทดลองบริการ จำกัด",
en: "Trial Services Co., Ltd.",
image: "/examples/vendors/vd-0005.svg",
},
{
code: "VD-0006",
th: "ร้าน ตัวอย่างการพิมพ์",
en: "Sample Printing Shop",
image: "/examples/vendors/vd-0006.svg",
},
]}
menuWidth="md"
value={vendor}
onChange={setVendor}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "ผู้ค้า" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| placeholder* จำเป็น | "พิมพ์ชื่อผู้ค้าหรือรหัส" | string | Hint text in the empty field, such as what people can search by. |
| items* จำเป็น | รายการ 6 รายการ[
{
code: "VD-0001",
th: "บริษัท ตัวอย่าง จำกัด",
en: "Sample Co., Ltd.",
image: "/examples/vendors/vd-0001.svg",
},
{
code: "VD-0002",
th: "บริษัท ทดสอบครุภัณฑ์ จำกัด",
en: "Test Equipment Co., Ltd.",
image: "/examples/vendors/vd-0002.svg",
},
{
code: "VD-0003",
th: "ห้างหุ้นส่วนจำกัด จำลองการค้า",
en: "Mock Trading Ltd. Partnership",
image: "/examples/vendors/vd-0003.svg",
},
{
code: "VD-0004",
th: "บริษัท สมมุติวัสดุ จำกัด",
en: "Pretend Supplies Co., Ltd.",
image: "/examples/vendors/vd-0004.svg",
},
{
code: "VD-0005",
th: "บริษัท ทดลองบริการ จำกัด",
en: "Trial Services Co., Ltd.",
image: "/examples/vendors/vd-0005.svg",
},
{
code: "VD-0006",
th: "ร้าน ตัวอย่างการพิมพ์",
en: "Sample Printing Shop",
image: "/examples/vendors/vd-0006.svg",
},
] | readonly T[] | Every item people can choose from. The component filters them as people type. |
| menuWidth | "md" | 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* จำเป็น | vendor | T | null | The chosen item, or null when nothing is chosen. The screen keeps it in state. |
| onChange* จำเป็น | setVendor | (next: T | null) => void | Called with the picked item, or with null when people type or clear the field. |
ผลการค้นเป็นการ์ด
layout cards แสดงผลการค้นเป็นการ์ดมีรูปด้านบน ลูกศรขึ้นลงเลื่อนทีละแถว ส่วนซ้ายขวายังใช้เลื่อนเคอร์เซอร์ในช่อง
const [vendor, setVendor] = useState<ComboItem | null>(null);
<Combobox
label="ผู้ค้า"
placeholder="พิมพ์ชื่อผู้ค้าหรือรหัส"
items={[
{
code: "VD-0001",
th: "บริษัท ตัวอย่าง จำกัด",
en: "Sample Co., Ltd.",
image: "/examples/vendors/vd-0001.svg",
},
{
code: "VD-0002",
th: "บริษัท ทดสอบครุภัณฑ์ จำกัด",
en: "Test Equipment Co., Ltd.",
image: "/examples/vendors/vd-0002.svg",
},
{
code: "VD-0003",
th: "ห้างหุ้นส่วนจำกัด จำลองการค้า",
en: "Mock Trading Ltd. Partnership",
image: "/examples/vendors/vd-0003.svg",
},
{
code: "VD-0004",
th: "บริษัท สมมุติวัสดุ จำกัด",
en: "Pretend Supplies Co., Ltd.",
image: "/examples/vendors/vd-0004.svg",
},
{
code: "VD-0005",
th: "บริษัท ทดลองบริการ จำกัด",
en: "Trial Services Co., Ltd.",
image: "/examples/vendors/vd-0005.svg",
},
{
code: "VD-0006",
th: "ร้าน ตัวอย่างการพิมพ์",
en: "Sample Printing Shop",
image: "/examples/vendors/vd-0006.svg",
},
]}
layout="cards"
menuWidth="lg"
value={vendor}
onChange={setVendor}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "ผู้ค้า" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| placeholder* จำเป็น | "พิมพ์ชื่อผู้ค้าหรือรหัส" | string | Hint text in the empty field, such as what people can search by. |
| items* จำเป็น | รายการ 6 รายการ[
{
code: "VD-0001",
th: "บริษัท ตัวอย่าง จำกัด",
en: "Sample Co., Ltd.",
image: "/examples/vendors/vd-0001.svg",
},
{
code: "VD-0002",
th: "บริษัท ทดสอบครุภัณฑ์ จำกัด",
en: "Test Equipment Co., Ltd.",
image: "/examples/vendors/vd-0002.svg",
},
{
code: "VD-0003",
th: "ห้างหุ้นส่วนจำกัด จำลองการค้า",
en: "Mock Trading Ltd. Partnership",
image: "/examples/vendors/vd-0003.svg",
},
{
code: "VD-0004",
th: "บริษัท สมมุติวัสดุ จำกัด",
en: "Pretend Supplies Co., Ltd.",
image: "/examples/vendors/vd-0004.svg",
},
{
code: "VD-0005",
th: "บริษัท ทดลองบริการ จำกัด",
en: "Trial Services Co., Ltd.",
image: "/examples/vendors/vd-0005.svg",
},
{
code: "VD-0006",
th: "ร้าน ตัวอย่างการพิมพ์",
en: "Sample Printing Shop",
image: "/examples/vendors/vd-0006.svg",
},
] | readonly T[] | Every item people can choose from. The component filters them as people type. |
| layout | "cards" | OptionLayout ค่าที่ใช้ได้: "list""cards" | How the matches show: rows, or cards in a grid with each item'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* จำเป็น | vendor | T | null | The chosen item, or null when nothing is chosen. The screen keeps it in state. |
| onChange* จำเป็น | setVendor | (next: T | null) => void | Called with the picked item, or with null when people type or clear the field. |
ช่องบังคับที่ยังไม่ได้เลือก
required ใส่ดาวสีแดงที่ป้าย error แสดงหลังกดบันทึกโดยยังไม่ได้เลือก ข้อความที่พิมพ์ไว้แต่ไม่ได้เลือกจากรายการไม่นับว่าเลือกแล้ว
เลือกผู้ค้าจากรายการ ถ้าไม่พบ ให้ขอเพิ่มผู้ค้าก่อน
const [vendor, setVendor] = useState<ComboItem | null>(null);
<Combobox
label="ผู้ค้า"
required
placeholder="พิมพ์ชื่อผู้ค้าหรือรหัส"
items={[
{
code: "VD-0001",
th: "บริษัท ตัวอย่าง จำกัด",
en: "Sample Co., Ltd.",
},
{
code: "VD-0002",
th: "บริษัท ทดสอบครุภัณฑ์ จำกัด",
en: "Test Equipment Co., Ltd.",
},
{
code: "VD-0003",
th: "ห้างหุ้นส่วนจำกัด จำลองการค้า",
en: "Mock Trading Ltd. Partnership",
},
]}
value={vendor}
onChange={setVendor}
error={vendor === 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 | Hint text in the empty field, such as what people can search by. |
| items* จำเป็น | รายการ 3 รายการ[
{
code: "VD-0001",
th: "บริษัท ตัวอย่าง จำกัด",
en: "Sample Co., Ltd.",
},
{
code: "VD-0002",
th: "บริษัท ทดสอบครุภัณฑ์ จำกัด",
en: "Test Equipment Co., Ltd.",
},
{
code: "VD-0003",
th: "ห้างหุ้นส่วนจำกัด จำลองการค้า",
en: "Mock Trading Ltd. Partnership",
},
] | readonly T[] | Every item people can choose from. The component filters them as people type. |
| value* จำเป็น | vendor | T | null | The chosen item, or null when nothing is chosen. The screen keeps it in state. |
| onChange* จำเป็น | setVendor | (next: T | null) => void | Called with the picked item, or with null when people type or clear the field. |
| error | vendor === null ? "เลือกผู้ค้าจากรายการ ถ้าไม่พบ ให้ขอเพิ่มผู้ค้าก่อน" : undefined | string | What's wrong and how to fix it, shown under the field. It marks the field invalid. |
เปลี่ยนไอคอน
icon เปลี่ยนไอคอนนำหน้าให้บอกว่าช่องหาอะไร เช่น BuildingComplex สำหรับหน่วยงาน หรือ UserRound สำหรับบุคคล ค่าเริ่มต้นคือแว่นขยาย ตัวอย่างนี้เป็นตัวกรองในแถบเหนือตาราง จึงใช้ hideLabel
const [unit, setUnit] = useState<ComboItem | null>(null);
<Combobox
label="อยู่ใต้หน่วยงาน"
hideLabel
icon={BuildingComplex}
placeholder="อยู่ใต้หน่วยงานใดก็ได้"
items={[
{
code: "U-100",
th: "สำนักงานอธิการบดี",
en: "Office of the President",
},
{ code: "U-110", th: "กองคลัง", en: "Finance Division" },
{ code: "U-111", th: "งานพัสดุ", en: "Procurement Section" },
{
code: "U-200",
th: "คณะตัวอย่างศาสตร์",
en: "Faculty of Sample Studies",
},
]}
value={unit}
onChange={setUnit}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| label | "อยู่ใต้หน่วยงาน" | string | The visible label, already in the screen language. Every field needs one, unless the screen labels it another way. |
| hideLabel | true | 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. |
| icon | BuildingComplex | LucideIcon | The leading icon, which says what the field finds, such as BuildingComplex for a unit or UserRound for a person. Search by default. |
| placeholder* จำเป็น | "อยู่ใต้หน่วยงานใดก็ได้" | string | Hint text in the empty field, such as what people can search by. |
| items* จำเป็น | รายการ 4 รายการ[
{
code: "U-100",
th: "สำนักงานอธิการบดี",
en: "Office of the President",
},
{ code: "U-110", th: "กองคลัง", en: "Finance Division" },
{ code: "U-111", th: "งานพัสดุ", en: "Procurement Section" },
{
code: "U-200",
th: "คณะตัวอย่างศาสตร์",
en: "Faculty of Sample Studies",
},
] | readonly T[] | Every item people can choose from. The component filters them as people type. |
| value* จำเป็น | unit | T | null | The chosen item, or null when nothing is chosen. The screen keeps it in state. |
| onChange* จำเป็น | setUnit | (next: T | null) => void | Called with the picked item, or with null when people type or clear the field. |
props ทั้งหมด
ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| 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. |
| hideLabel | boolean | false | 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. |
| items* จำเป็น | readonly T[] | — | Every item people can choose from. The component filters them as people type. |
| value* จำเป็น | T | null | — | The chosen item, or null when nothing is chosen. The screen keeps it in state. |
| onChange* จำเป็น | (next: T | null) => void | — | Called with the picked item, or with null when people type or clear the field. |
| placeholder* จำเป็น | string | — | Hint text in the empty field, such as what people can search by. |
| labelledBy | string | — | The id of a visible label the screen wrote. Use label instead, unless the label is elsewhere. |
| id | string | — | The input's id. |
| layout | OptionLayout ค่าที่ใช้ได้: "list""cards" | "list" | How the matches show: rows, or cards in a grid with each item's image on top. Cards suit a menuWidth wider than the field. |
| menuWidth | MenuWidth ค่าที่ใช้ได้: "trigger""sm""md""lg""xl" | "trigger" | How wide the list is. trigger matches the field. The others may be wider than the field, but never wider than the screen. |
| icon | LucideIcon | Search | The leading icon, which says what the field finds, such as BuildingComplex for a unit or UserRound for a person. Search by default. |
ชนิดข้อมูล
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 เฉพาะเมื่อป้ายอยู่ที่อื่นบนหน้าจอ
- code ต้องไม่ซ้ำกันในรายการ ถ้าไม่มีชื่ออังกฤษ ปล่อย en ว่างได้ จะแสดงชื่อไทยแทน
- image เป็นที่อยู่ของรูป เช่น โลโก้ ไม่ต้องมีข้อความแทนรูป เพราะชื่อแสดงอยู่แล้ว
- แสดงผลไม่เกิน 8 รายการ ถ้าไม่พบ จะบอกให้ลองค้นด้วยรหัสหรือชื่อบางส่วน
- แป้นพิมพ์: ลูกศรขึ้นลงเลื่อน Enter เลือก Esc ปิดรายการ Tab ปิดแล้วไปช่องถัดไป
ซอร์สโค้ด: ui/src/combobox.tsx