ค้นหาอัตโนมัติ
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.
ใช้กับรายการยาวที่ต้องค้นหา เช่น ผู้ค้าหรือรหัสงบประมาณ ค้นได้ทั้งรหัส ชื่อไทย และชื่ออังกฤษ ถ้ามีตัวเลือกไม่เกินประมาณ 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. |
ช่องบังคับที่ยังไม่ได้เลือก
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. |
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. |
| 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. |
ข้อควรปฏิบัติ
- ส่ง label เสมอ ป้ายผูกกับช่องค้นหาให้เอง ใช้ labelledBy เฉพาะเมื่อป้ายอยู่ที่อื่นบนหน้าจอ
- code ต้องไม่ซ้ำกันในรายการ ถ้าไม่มีชื่ออังกฤษ ปล่อย en ว่างได้ จะแสดงชื่อไทยแทน
- แสดงผลไม่เกิน 8 รายการ ถ้าไม่พบ จะบอกให้ลองค้นด้วยรหัสหรือชื่อบางส่วน
- แป้นพิมพ์: ลูกศรขึ้นลงเลื่อน Enter เลือก Esc ปิดรายการ Tab ปิดแล้วไปช่องถัดไป
ซอร์สโค้ด: ui/src/combobox.tsx