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

ช่องค้นหา

SearchInput

A search field with a leading icon and a clear button that shows once something is typed. Pass suggestions to show a list under the field as people type, such as matching people or records from the core API. The screen chooses the suggestions and what a pick does. Arrows move through them, Enter picks, and Esc closes the list.

ใช้ค้นหาหรือกรองรายการด้วยข้อความอิสระ เช่น ค้นในตาราง ปุ่มล้างจะแสดงเมื่อมีข้อความ ถ้าต้องเลือกรายการเดียวจากรายการยาว เช่น ผู้ค้า ใช้ Combobox

import { SearchInput } from "@bsru/ui";

ตัวอย่าง

หน้าจอเก็บคำค้น

ส่ง value และ onValueChange เมื่อหน้าจอใช้คำค้นกรองรายการ หรือต้องล้างจากที่อื่น เช่น ปุ่มล้างตัวกรองทั้งหมด onValueChange ได้ "" เมื่อกดล้าง

const [query, setQuery] = useState("");

<SearchInput
  label="ค้นหาคำขอซื้อ"
  placeholder="พิมพ์เลขที่หรือชื่อรายการ"
  value={query}
  onValueChange={setQuery}
/>

มีรายการแนะนำ

suggestions แสดงรายการใต้ช่องขณะพิมพ์ หน้าจอเป็นผู้เลือกรายการที่ตรงกับคำค้น เช่น จาก API หลัก และกำหนดว่าเลือกแล้วทำอะไร เช่น เปิดระเบียนนั้น ตัวอย่างนี้ใส่ชื่อลงในช่อง

const [query, setQuery] = useState("");

<SearchInput
  label="ค้นหาบุคลากร"
  placeholder="พิมพ์ชื่อหรือหน่วยงาน"
  value={query}
  onValueChange={setQuery}
  suggestions={matches(query)}
  onSuggestionPick={(v) => setQuery(nameOf(v))}
  menuWidth="sm"
/>

รายการแนะนำเป็นการ์ด

layout cards แสดงรายการแนะนำเป็นการ์ดมีรูปด้านบน menuWidth lg ให้กว้างกว่าช่อง ลูกศรขึ้นลงเลื่อนทีละแถว

const [query, setQuery] = useState("");

<SearchInput
  label="ค้นหาห้อง"
  placeholder="พิมพ์เลขห้องหรือรูปแบบ"
  value={query}
  onValueChange={setQuery}
  suggestions={matches(query)}
  onSuggestionPick={(v) => setQuery(nameOf(v))}
  layout="cards"
  menuWidth="lg"
/>

มีคำค้นอยู่แล้ว

defaultValue ใส่คำค้นเริ่มต้นแล้วให้ช่องเก็บข้อความเอง ปุ่มล้างแสดงทันที

<SearchInput
  label="ค้นหา"
  defaultValue="ครุภัณฑ์"
  onValueChange={handleValueChange}
/>

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

icon เปลี่ยนไอคอนนำหน้า เช่น เมื่อช่องใช้กรองมากกว่าค้นหา

<SearchInput
  label="กรองหน่วยงาน"
  icon={Funnel}
  placeholder="พิมพ์ชื่อหน่วยงาน"
  onValueChange={handleValueChange}
/>

ในแถบเครื่องมือ

เมื่อไม่มีที่สำหรับป้ายที่มองเห็นได้ เช่น ในแถบเหนือตาราง ส่ง aria-label แทน label

<SearchInput
  aria-label="ค้นหาคำขอซื้อ"
  placeholder="ค้นหา"
  onValueChange={handleValueChange}
/>

props ทั้งหมด

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

SearchInput 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.
iconLucideIconSearchThe leading icon.
valuestring—The search text, when the screen keeps it, such as to clear it from a Clear all button.
defaultValuestring""The starting search text, when the input keeps it.
onValueChange(next: string) => void—Hears the text on each change, and "" when it is cleared.
suggestionsreadonly SelectOption[]—Suggestions to show under the field while it has focus, already matched to the text by the screen. Leave it out for a plain search field. An empty array shows no list.
onSuggestionPick(value: string) => void—Hears the value of the suggestion people pick, such as to open that record or fill in the text.
layoutOptionLayout
ค่าที่ใช้ได้:"list""cards"
"list"How the suggestions show: rows, or cards in a grid with each one's image on top.
menuWidthMenuWidth
ค่าที่ใช้ได้:"trigger""sm""md""lg""xl"
"trigger"How wide the suggestions are. trigger matches the field. The others may be wider than the field, but never wider than the screen.

รับ attribute ของ element HTML ต้นแบบได้ทั้งหมดด้วย เช่น id, name, required และ disabled

ชนิดข้อมูล

SelectOption

SelectOption fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
value* จำเป็นV——
label* จำเป็นstring—The text shown, already in the screen language. Typing jumps to an option by the start of this text.
notestring—A second line, such as the balance left on a budget code.
groupstring—Options with the same group in a row sit under one heading.
iconLucideIcon——
imagestring—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 หรือ aria-label ทุกครั้ง placeholder ไม่ใช่ชื่อของช่อง
  • ถ้าค้นผ่านเซิร์ฟเวอร์ ให้หน่วงเวลาเรียกหลังผู้ใช้หยุดพิมพ์
  • ปุ่มล้างมีชื่อสำหรับโปรแกรมอ่านหน้าจอ และคืนโฟกัสให้ช่องหลังกด
  • เมื่อมี suggestions ช่องจะทำงานแบบ combobox ลูกศรเลื่อน Enter เลือก Esc ครั้งแรกปิดรายการ ครั้งที่สองล้างข้อความ ถ้าต้องการให้เลือกได้รายการเดียวและเก็บรายการที่เลือกไว้ ใช้ Combobox

ซอร์สโค้ด: ui/src/inputs.tsx