ข้ามไปยังเนื้อหา

ค้นหาอัตโนมัติ

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}
/>

มีค่าที่เลือกไว้แล้ว

ส่งรายการเริ่มต้นเป็น 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}
/>

รายการมีโลโก้

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}
/>

ผลการค้นเป็นการ์ด

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}
/>

ช่องบังคับที่ยังไม่ได้เลือก

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}
/>

เปลี่ยนไอคอน

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}
/>

props ทั้งหมด

ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้

Combobox props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
labelstring—The visible label, already in the screen language. Every field needs one, unless the screen labels it another way.
hintstring—Help under the field, such as the format or a limit.
errorstring—What's wrong and how to fix it, shown under the field. It marks the field invalid.
requiredboolean—Marks the label with a red star, and the control as required.
hideLabelbooleanfalseKeeps 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.
labelledBystring—The id of a visible label the screen wrote. Use label instead, unless the label is elsewhere.
idstring—The input's id.
layoutOptionLayout
ค่าที่ใช้ได้:"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.
menuWidthMenuWidth
ค่าที่ใช้ได้:"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.
iconLucideIconSearchThe 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